Help to reset/restart game

edited October 2017 in Questions about Code

Hey there, i'm VERY new to processing and been working on this little game, its nothing big or impressive, but a fun project for a newbie.

However im having issues with my restart/reset function. When i restart the game, the ball behaviors weird and crashes towards the ground (= gameover) in a milisecond. I would like to reset it like when i play the game the first time after hitting "Run"

Hope someone can help me with this flaw, i know the code itself is messy as heck already!

int gameScreen = 0;
int ballX, ballY;
int ballSize = 20;
int ballColor = color(200, 0, 0);
float gravity = 1;
float ballSpeedVert = 0;
float ballSpeedHorizon = 10;
float airfriction = 0.0001;
float friction = 0.1;
color racketColor = color(0);
float racketWidth = 100;
float racketHeight = 10;
int racketBounceRate = 20;
int score = 0;
PImage bg;

/********* SETUP BLOCK *********/

void setup() {
  size(600, 600);
  bg = loadImage("600x600.jpg");
  ballX=width/4;
  ballY=height/5;
}


/********* DRAW BLOCK *********/

void draw() {
  if (gameScreen == 0) {
    initScreen();
  } else if (gameScreen == 1) {
    gameScreen();
  } else if (gameScreen == 2) {
    gameOverScreen();
  }
}


/********* SCREEN CONTENTS *********/

void initScreen() {
  background(0);
  textAlign(CENTER);
  text("Click to start", height/2, width/2);
}

void gameScreen() {
  background(bg);
  stroke(226, 204, 0);
  text("score " + score, 20, 20);
  drawBall();
  applyGravity();
  keepInScreen();
  drawRacket();
  watchRacketBounce();
  applyHorizontalSpeed();
}

void gameOverScreen() {
  background(0);
  textAlign(CENTER);
  fill(255);
  textSize(30);
  text("Game Over, your score " + score, height/2, width/2 - 20);
  textSize(15);
  text("Press any key to Restart", height/2, width/2 + 10);
}

void restart() {
  gameScreen = 0;
  score = 0;
}


void drawBall() {
  fill(ballColor);
  ellipse(ballX, ballY, ballSize, ballSize);
}

void applyGravity() {
  ballSpeedVert += gravity;
  ballY += ballSpeedVert;
  ballSpeedVert -= (ballSpeedVert * airfriction);
}

void applyHorizontalSpeed() {
  ballX += ballSpeedHorizon;
  ballSpeedHorizon -= (ballSpeedHorizon * airfriction);
}

void makeBounceLeft(int surface) {
  ballX = surface+(ballSize/2);
  ballSpeedHorizon*=-1;
  ballSpeedHorizon -= (ballSpeedHorizon * friction);
}

void makeBounceRight(int surface) {
  ballX = surface-(ballSize/2);
  ballSpeedHorizon*=-1;
  ballSpeedHorizon -= (ballSpeedHorizon * friction);
}

void makeBounceBottom(int surface) {
  ballY = surface-(ballSize/2);
  ballSpeedVert*=-1;
  ballSpeedVert -= (ballSpeedVert * friction);
  score = score +1;
}

void makeBounceTop(int surface) {
  ballY = surface+(ballSize/2);
  ballSpeedVert*=-1;
  ballSpeedVert -= (ballSpeedVert * friction);
}


void keepInScreen() {
  // if the ball hits floor
  if (ballY+(ballSize/2) > height) { 
    gameScreen=2;
    gameOverScreen();
  }
  // If we hit the top
  if (ballY-(ballSize/2) < 0) {
    makeBounceTop(0);
  }
  if (ballX-(ballSize/2) < 0) {
    makeBounceLeft(0);
  }
  if (ballX+(ballSize/2) > width) {
    makeBounceRight(width);
  }
}
void drawRacket() {
  fill(racketColor);
  rectMode(CENTER);
  rect(mouseX, mouseY, racketWidth, racketHeight);
}
void watchRacketBounce() {
  float overhead = mouseY - pmouseY;
  if ((ballX+(ballSize/2) > mouseX-(racketWidth/2)) && (ballX-(ballSize/2) < mouseX+(racketWidth/2))) {
    if (dist(ballX, ballY, ballX, mouseY)<=(ballSize/2)+abs(overhead)) {
      if ((ballX+(ballSize/2) > mouseX-(racketWidth/2)) && (ballX-(ballSize/2) < mouseX+(racketWidth/2))) {
        if (dist(ballX, ballY, ballX, mouseY)<=(ballSize/2)+abs(overhead)) {
          makeBounceBottom(mouseY);
          // racket moving up
          if (overhead<0) {
            ballY+=overhead;
            ballSpeedVert+=overhead;
            ballSpeedHorizon = (ballX - mouseX)/5;
          }
        }
      }
    }
  }
}


/********* INPUTS *********/
void mousePressed() {
  if (gameScreen==0) {
    startGame();
  }
}

void keyPressed() {
  if (gameScreen==2) {
    setup();
    restart();
  }
}



/********* OTHER FUNCTIONS *********/

void startGame() {
  gameScreen=1;
}
Tagged:

Answers

  • edited October 2017 Answer ✓

    Your variables are not getting reinitialized. I would initialize them (assign a value) within restart() and call restart() in setup(), then there isn't a need to call setup() again in keyPressed().

  • setup() is executed automatically when the sketch starts and you should never call it directly. @Bird has stated the preferred way to do this.

  • Aight my current code looks like this

    int gameScreen = 0;
    int ballX, ballY;
    int ballSize = 20;
    int ballColor = color(200, 0, 0);
    float gravity = 1;
    float ballSpeedVert = 0;
    float ballSpeedHorizon = 10;
    float airfriction = 0.0001;
    float friction = 0.1;
    color racketColor = color(0);
    float racketWidth = 100;
    float racketHeight = 10;
    int racketBounceRate = 20;
    int score = 0;
    PImage bg;
    
    /********* SETUP BLOCK *********/
    
    void setup() {
      size(600, 600);
      bg = loadImage("600x600.jpg");
      ballX=width/4;
      ballY=height/5;
    }
    
    
    /********* DRAW BLOCK *********/
    
    void draw() {
      if (gameScreen == 0) {
        initScreen();
      } else if (gameScreen == 1) {
        gameScreen();
      } else if (gameScreen == 2) {
        gameOverScreen();
      }
    }
    
    
    /********* SCREEN CONTENTS *********/
    
    void initScreen() {
      background(0);
      textAlign(CENTER);
      text("Click to start", height/2, width/2);
    }
    
    void gameScreen() {
      background(bg);
      stroke(226, 204, 0);
      text("score " + score, 20, 20);
      drawBall();
      applyGravity();
      keepInScreen();
      drawRacket();
      watchRacketBounce();
      applyHorizontalSpeed();
    }
    
    void gameOverScreen() {
      background(0);
      textAlign(CENTER);
      fill(255);
      textSize(30);
      text("Game Over, your score " + score, height/2, width/2 - 20);
      textSize(15);
      text("Click to Restart", height/2, width/2 + 10);
    }
    
    void restart() {
      gameScreen = 0;
      score = 0;
      setup();
      gravity = 1;
      ballSpeedVert = 0;
      ballSpeedHorizon = 10;
    }
    
    
    void drawBall() {
      fill(ballColor);
      ellipse(ballX, ballY, ballSize, ballSize);
    }
    
    void applyGravity() {
      ballSpeedVert += gravity;
      ballY += ballSpeedVert;
      ballSpeedVert -= (ballSpeedVert * airfriction);
    }
    
    void applyHorizontalSpeed() {
      ballX += ballSpeedHorizon;
      ballSpeedHorizon -= (ballSpeedHorizon * airfriction);
    }
    
    void makeBounceLeft(int surface) {
      ballX = surface+(ballSize/2);
      ballSpeedHorizon*=-1;
      ballSpeedHorizon -= (ballSpeedHorizon * friction);
    }
    
    void makeBounceRight(int surface) {
      ballX = surface-(ballSize/2);
      ballSpeedHorizon*=-1;
      ballSpeedHorizon -= (ballSpeedHorizon * friction);
    }
    
    void makeBounceBottom(int surface) {
      ballY = surface-(ballSize/2);
      ballSpeedVert*=-1;
      ballSpeedVert -= (ballSpeedVert * friction);
      score = score +1;
    }
    
    void makeBounceTop(int surface) {
      ballY = surface+(ballSize/2);
      ballSpeedVert*=-1;
      ballSpeedVert -= (ballSpeedVert * friction);
    }
    
    
    void keepInScreen() {
      // if the ball hits floor
      if (ballY+(ballSize/2) > height) { 
        gameScreen=2;
        gameOverScreen();
      }
      // If we hit the top
      if (ballY-(ballSize/2) < 0) {
        makeBounceTop(0);
      }
      if (ballX-(ballSize/2) < 0) {
        makeBounceLeft(0);
      }
      if (ballX+(ballSize/2) > width) {
        makeBounceRight(width);
      }
    }
    void drawRacket() {
      fill(racketColor);
      rectMode(CENTER);
      rect(mouseX, mouseY, racketWidth, racketHeight);
    }
    void watchRacketBounce() {
      float overhead = mouseY - pmouseY;
      if ((ballX+(ballSize/2) > mouseX-(racketWidth/2)) && (ballX-(ballSize/2) < mouseX+(racketWidth/2))) {
        if (dist(ballX, ballY, ballX, mouseY)<=(ballSize/2)+abs(overhead)) {
          if ((ballX+(ballSize/2) > mouseX-(racketWidth/2)) && (ballX-(ballSize/2) < mouseX+(racketWidth/2))) {
            if (dist(ballX, ballY, ballX, mouseY)<=(ballSize/2)+abs(overhead)) {
              makeBounceBottom(mouseY);
              // racket moving up
              if (overhead<0) {
                ballY+=overhead;
                ballSpeedVert+=overhead;
                ballSpeedHorizon = (ballX - mouseX)/5;
              }
            }
          }
        }
      }
    }
    
    
    /********* INPUTS *********/
    void mousePressed() {
      if (gameScreen==0) {
        startGame();
      }
      if (gameScreen==2) {
        restart();
      }
    }
    
    
    
    /********* OTHER FUNCTIONS *********/
    
    void startGame() {
      gameScreen=1;
    }
    

    And its working perfectly! Thanks for the help and insight to the both of you, much appreciated.

  • Wrong you are still calling setup() from line 73. Try this

    void setup() {
      size(600, 600);
      bg = loadImage("600x600.jpg");
      ballX=width/4;
      ballY=height/5;
      restart();
    }
    

    and

    void restart() {
      gameScreen = 0;
      score = 0;
      gravity = 1;
      ballSpeedVert = 0;
      ballSpeedHorizon = 10;
    }
    
Sign In or Register to comment.