# Pong-Ball Game Bug

**URL:** https://discourse.processing.org/t/pong-ball-game-bug/38794
**Category:** Coding Questions
**Created:** [September 16, 2022, 1:12am UTC](https://discourse.processing.org/t/pong-ball-game-bug/38794 "2022-09-16T01:12:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![isabela](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/isabela/32/19188_2.png) [@isabela](https://discourse.processing.org/u/isabela)
#### Post date: [September 16, 2022, 1:12am UTC](https://discourse.processing.org/t/pong-ball-game-bug/38794/1 "2022-09-16T01:12:50Z")

</div>

I am trying to code a pong game. Even when following tutorials, the same bug appears: if you loose on the first try, the ball will never bounce on the rectangle. But if you win, the ball will bounce even when the rectangle is not there… can someone help? Thanks!  
(the notes are in Portuguese, my first language, sorry)

boolean gameStart = false;

float xVACA, yVACA, moveXVACA, moveYVACA, diam; //variáveis vaca  
float xPlat, yPlat; //variáveis plataforma, localização  
float larguraPlat, alturaPlat; // variáveis plataforma, tamanho

int pontos; //pontuação

PShape vaca; // carrega imagem da bola 1

PImage fundo;

void setup () {  
size (500, 600);  
background(0);

pontos = 0;

vaca = loadShape (“vaca.svg”);  
fundo = loadImage (“fundo.jpg”);

//sorteia valor aleatório para a coordenada x da bola entre 0 e largura da janela  
xVACA = random(width);

//sorteia valor aleatório para a coordenada y da bola entre 0 e 20 px antes da raquete  
yVACA = random(0,height-90);

//sorteia algum valor entre -10 e 10 para incrementar a posição y da bola, definindo a “velocidade” de x  
moveXVACA = 5;

//sorteia algum valor entre -10 e 10 para incrementar a posição y da bola, definindo a “velocidade” de y  
moveYVACA = 5;

diam = 80; //diâmetro da bola

larguraPlat=200;  
alturaPlat=50;

xPlat = width/2; //onde começa coordenada x da raquete inferior  
yPlat = height-(alturaPlat/2 + 20); //onde começa coordenada y da raquete inferior, com uma distância do final da tela

rectMode (CENTER); //muda localização dos parâmetros da função rect()para centro do retângulo

}

void draw () {  
background(fundo);

fill (0);  
textSize (20);  
text (pontos, width/2, 30);

shapeMode (CENTER); // inicia o desenho da bola pelo centro  
//sobe a imagem da primeira bola  
shape (vaca, xVACA, yVACA, diam,diam);

//desenha retângulo que acompanha movimento do mouse  
noStroke ();  
fill (253, 185, 200);  
rect(mouseX, yPlat, larguraPlat, alturaPlat);

if (gameStart) {

//incrementa as posições x e y da bolinha  
xVACA = xVACA + moveXVACA;  
yVACA = yVACA + moveYVACA;

//faz com que a bola “quique” na raquete  
if (xVACA \>= (xPlat - larguraPlat/2) && xVACA \<= (xPlat + larguraPlat/2) &&  
( (yVACA+diam/2) \>= (yPlat - alturaPlat/2) || (yVACA) \>= (yPlat - alturaPlat/2) || (yVACA+diam/4) \>= (yPlat - alturaPlat/2))) {  
moveYVACA = -moveYVACA;  
moveXVACA = -moveXVACA;  
xVACA = xVACA + moveXVACA;  
yVACA = yVACA + moveYVACA;  
pontos = ++pontos; }

```
//faz com que a bola "quique" nas paredes

```

else if (yVACA \< 0) {  
moveYVACA = -moveYVACA;  
}

if (xVACA \< 0 || xVACA \> width) {  
moveXVACA = -moveXVACA;  
}

if (yVACA+diam/2 \> height) {  
gameStart = false;  
xVACA = width/2;  
yVACA = height/2;  
moveXVACA = 5;  
moveYVACA = 5;  
pontos = 0;  
}  
}

noCursor ();

}

void mousePressed() {  
gameStart = true;  
}

---

<div class="post-metadata">

### Author: ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)
#### Post date: [September 16, 2022, 7:14am UTC](https://discourse.processing.org/t/pong-ball-game-bug/38794/2 "2022-09-16T07:14:56Z")

</div>

You are never actually setting your xPlat to the right x-coordinate.  
You should add something like

```auto
xPlat=mouseX;

```

before the if(gameStart)

Once you fixed that bug you will notice that the ball is bouncing… weirdly you can fix that however by removing the line:

```auto
moveXVACA = -moveXVACA;

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [September 16, 2022, 10:27am UTC](https://discourse.processing.org/t/pong-ball-game-bug/38794/3 "2022-09-16T10:27:48Z")

</div>

this version runs

```auto

boolean gameStart = false;

float xVACA=222, yVACA, 
  moveXVACA, moveYVACA, diam; //variáveis vaca
float xPlat, yPlat; //variáveis plataforma, localização
float larguraPlat, alturaPlat; // variáveis plataforma, tamanho

int pontos; //pontuação

PShape vaca; // carrega imagem da bola 1

PImage fundo;

void setup () {
  size (500, 600);
  background(0);

  pontos = 0;

  // vaca = loadShape ("vaca.svg");
  // fundo = loadImage ("fundo.jpg");

  //sorteia valor aleatório para a coordenada x da bola entre 0 e largura da janela
  xVACA = random(width);

  //sorteia valor aleatório para a coordenada y da bola entre 0 e 20 px antes da raquete
  yVACA = random(0, height-90);

  //sorteia algum valor entre -10 e 10 para incrementar a posição y da bola, definindo a “velocidade” de x
  moveXVACA = random(1, 5);

  //sorteia algum valor entre -10 e 10 para incrementar a posição y da bola, definindo a “velocidade” de y
  moveYVACA = random(1, 5);

  diam = 80; //diâmetro da bola

  larguraPlat=200;
  alturaPlat=50;

  xPlat = width/2; //onde começa coordenada x da raquete inferior
  yPlat = height-(alturaPlat/2 + 20); //onde começa coordenada y da raquete inferior, com uma distância do final da tela

  rectMode (CENTER); //muda localização dos parâmetros da função rect()para centro do retângulo

  noCursor ();
}

void draw () {
  background(0); // fundo);

  fill (0);
  textSize (20);
  text (pontos, width/2, 30);

  shapeMode (CENTER); // inicia o desenho da bola pelo centro
  //sobe a imagem da primeira bola
  fill(222); 
  text ("vaca", xVACA, yVACA); // was shape 

  //desenha retângulo que acompanha movimento do mouse
  noStroke ();
  fill (253, 185, 200);
  rect(mouseX, yPlat, larguraPlat, alturaPlat);

  xPlat=mouseX;

  if (gameStart) {

    //incrementa as posições x e y da bolinha
    xVACA = xVACA + moveXVACA;
    yVACA = yVACA + moveYVACA;

    //faz com que a bola “quique” na raquete
    if (xVACA >= (xPlat - larguraPlat/2) &&
      xVACA <= (xPlat + larguraPlat/2) &&
      ( (yVACA+diam/2) >= (yPlat - alturaPlat/2) || (yVACA) >= (yPlat - alturaPlat/2) || (yVACA+diam/4) >= (yPlat - alturaPlat/2))) {
      moveYVACA = -moveYVACA;
      // moveXVACA = -moveXVACA;
      xVACA = xVACA + moveXVACA;
      yVACA = yVACA + moveYVACA;
      pontos = pontos+1;
    }

    //faz com que a bola "quique" nas paredes
    else if (yVACA < 0) {
      moveYVACA = -moveYVACA;
    }

    if (xVACA < 0) {
      moveXVACA = abs(moveXVACA);
    }  

    if (xVACA > width) {
      moveXVACA = - abs(moveXVACA);
    }

    if (yVACA+diam/2 > height) {
      gameStart = false;
      xVACA = width/2;
      yVACA = height/2;
      moveXVACA = 5;
      moveYVACA = 5;
      pontos = 0;
    }
  } else {
    fill(222);
    text("Press mouse to start", 
      30, 30);
  }
}

void mousePressed() {
  gameStart = true;
}
//

```
