# Pong Game Doesn't Restart

**URL:** https://discourse.processing.org/t/pong-game-doesnt-restart/38890
**Category:** Coding Questions
**Tags:** homework
**Created:** [September 20, 2022, 9:36pm UTC](https://discourse.processing.org/t/pong-game-doesnt-restart/38890 "2022-09-20T21:36:10Z")
**Posts on this page:** 7
**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 20, 2022, 9:36pm UTC](https://discourse.processing.org/t/pong-game-doesnt-restart/38890/1 "2022-09-20T21:36:10Z")

</div>

Hi! I am working on a Pong Game for school and wanted to add another ball when you get 5 points. The problem is, when you loose with the second ball, the game dosen’t restart. Can you help me? (the notes in the code are in Portuguese, sorry).

```auto
boolean gameStart = false;
boolean porcos = false;
boolean localizar = false;

float diam; // variável de tamanho dos animais/bolas
float xVACA, yVACA, moveXVACA, moveYVACA; //variáveis vaca
float xPORCO, yPORCO, moveXPORCO, moveYPORCO; // variáveis porco

float xPlat, yPlat; //variáveis plataforma, localização
float larguraPlat, alturaPlat; // variáveis plataforma, tamanho

int pontos; //pontuação

//variáveis de imagens dos animais/bolas
PShape vaca; 
PShape porco;

PFont fonte;

PImage fundo;

void setup () {
  fundo = loadImage ("fundo.jpg");
  size (400,600);
  
  pontos=0;
  fonte = createFont ("fonte.ttf",40);
  
  vaca = loadShape ("vaca.svg");
  porco = loadShape ("porco.svg");
  
  xVACA = width/2;
  yVACA = height/2;
  
  //xPORCO = random (width);
  //yPORCO = random (0,40);
  
  moveXVACA = 3;
  moveYVACA = 3;
  moveXPORCO = 5;
  moveYPORCO = 5;
  
  diam = 80;
  
  larguraPlat=150;
  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
  
} // fim setup

void localizar () {
  xPORCO = random (width);
  yPORCO = random (0,40);
} // fim localizar

void draw (){
  
  fill (255);
  
  background (fundo);

  textFont (fonte);
  text (pontos, width/2-5, 56);
  
  rectMode (CENTER);
  fill (0,255,50);
  noStroke ();
  rect (mouseX, yPlat, larguraPlat, alturaPlat);
  xPlat = mouseX;
    
  shapeMode (CENTER);
  shape (vaca, xVACA, yVACA, diam, diam);
  
  if (gameStart) {
    xVACA = xVACA+ moveXVACA;
    yVACA = yVACA + moveYVACA;
    
    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; }
    
    else if (yVACA < 0) {
    moveYVACA = -moveYVACA; }
  
  
    if (xVACA < 0 || xVACA > width) {
    moveXVACA = -moveXVACA; }
    
    if (gameStart && pontos >=5) {porcos = true;}
    
    if (porcos) {
      
      shapeMode (CENTER);
      shape (porco, xPORCO, yPORCO, diam, diam);
      
      xPORCO = xPORCO + moveXPORCO;
      yPORCO = yPORCO + moveYPORCO;
      
      if (xPORCO >= (xPlat - larguraPlat/2) && xPORCO <= (xPlat + larguraPlat/2) && 
      ( (yPORCO+diam/2) >= (yPlat - alturaPlat/2) || (yPORCO) >= (yPlat - alturaPlat/2) || (yPORCO+diam/4) >= (yPlat - alturaPlat/2))) {
      moveYPORCO = -moveYPORCO;
      moveXPORCO = -moveXPORCO;
      xPORCO = xPORCO + moveXPORCO;
      yPORCO = yPORCO + moveYPORCO;
      pontos = ++pontos; }
    
      else if (yPORCO < 0) {
      moveYPORCO = -moveYPORCO; }
     
     if (xPORCO < 0 || xPORCO > width) {
     moveXPORCO = -moveXPORCO; }
     
    } // fim porcos
    }//fim gameStart
    
    if (yVACA+diam/2 > height || yPORCO+diam/2 > height) {
    gameStart =false;
    pontos = 0;
    porcos = false;
    localizar = false;
    }
 

} //fim draw
 void mouseClicked () {
     gameStart = true;} 

```

---

<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 21, 2022, 3:50am UTC](https://discourse.processing.org/t/pong-game-doesnt-restart/38890/2 "2022-09-21T03:50:34Z")

</div>

> [@isabela](#):
>
> `xPORCO, yPORCO`

Is this the second ball?

---

<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 21, 2022, 7:26am UTC](https://discourse.processing.org/t/pong-game-doesnt-restart/38890/3 "2022-09-21T07:26:01Z")

</div>

You cannot know which ball is outside first.

Therefore you need to change the logic of the program.

**Step 1**

Consider to increase a counter when either of the balls goes outside.

```auto
    if (yVACA+diam/2 > height || yPORCO+diam/2 > height) {
         counter++; 
    }

```

this can go badly when both balls go outside at the same time (VERY rare). Cure:

```auto
    if (yVACA+diam/2 > height) {
         counter++; 
    }
    if (yPORCO+diam/2 > height) {
         counter++; 
    }

```

for many balls consider an `array`.

**Step 2**

Have a new if clause where you just check the counter. ONLY here make the code restart the game

```auto
if(counter >= 2) {
    gameStart =false;
    pontos = 0;
    porcos = false;
    localizar = false;
}

```

Chrisir

---

<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 22, 2022, 12:53pm UTC](https://discourse.processing.org/t/pong-game-doesnt-restart/38890/4 "2022-09-22T12:53:04Z")

</div>

> [@Chrisir](#):
>
> array

Thanks for the help!  
Unfortunately, even with this code, the game doesn’t restart. I think is because the second is not outside the window when the game starts, but is drawn when you make 5 points.

---

<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 22, 2022, 5:39pm UTC](https://discourse.processing.org/t/pong-game-doesnt-restart/38890/5 "2022-09-22T17:39:20Z")

</div>

Please show your entire code

---

<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 22, 2022, 8:09pm UTC](https://discourse.processing.org/t/pong-game-doesnt-restart/38890/6 "2022-09-22T20:09:20Z")

</div>

> [@isabela](#):
>
> I think is because the second is not outside the window when the game starts

the second what…? The 2nd ball?

Then you need to fix it…

---

<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: [January 2, 2024, 10:49pm UTC](https://discourse.processing.org/t/pong-game-doesnt-restart/38890/7 "2024-01-02T22:49:17Z")

</div>

Sorry I couldn’t be of more help.

My apologies.
