Pong Game Doesn't Restart

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).

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;} 

Is this the second ball?

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.

    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:

    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

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

Chrisir

2 Likes

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.

1 Like

Please show your entire code

1 Like

the second what…? The 2nd ball?

Then you need to fix it…

1 Like

Sorry I couldn’t be of more help.

My apologies.

1 Like