Introducing the 'lives left' concept in my game

please format code with </> button * homework policy * asking questions

I have created this game where a pedestrian (square) collides with the car(rectangle), if the pedestrian goes above the screen, the score moves up by 1, but if the the pedestrian hits the car the lives should decrease by 1, I am facing some issues regarding how to decrease the live and place the pedestrian in its original location, so after certain collisions it should display “Game Over”, which I have used. I would appreciate if someone can help me with this.
Thanks

float s1 = 3;
float x1 = 0;
float carX;
float carY;
float carWidth;
float playerX;
float playerY;
float playerLength;
boolean colliding= false;
int score;
int lives;

void setup(){
  size(1200,400); 
  background(#838982);
  lives = 3;
  init();
  
}

void draw() {
  background(#838982);
  
  textSize(20);
  text("Lives left: " + lives, width -200, height -10);
  text("Score is " + score, width - 100, height -10);
  drawVehicle();
  Player();
  collision();
  
  if(colliding == false){
  } else {
    background(0);
    fill(255,0,0);
    textSize(50);
    text("Game Over", 450, 200);
  }
}

void init() {
 carX = width/20;
  carY = height/10;
  carWidth = width/14;
  playerX = width/2;
  playerY = height /2 +150;
  playerLength = 20;
  score = 0; 
}

void drawVehicle() {
    rect(carX,carY,carWidth,carY); 
    carX = carX +s1;
    if(carX > width) {
    carX = x1;
  }
}

void Player() {
  square(playerX,playerY,playerLength);
  if(playerX < 0) {
    playerX = width;
  }
  if(playerY < 0) {
    playerY = height;
    score = score +1;
  }
}

void collision() {
  
  if (playerX > carX &&
    playerX < carX + carWidth && 
    playerY > carY && 
    playerY < carY + carWidth) {
      
      colliding = true;
      lives--;
      init();
  }
}

void keyPressed() {
  if (keyCode == UP) {
  playerY-= 20;
  } else if (keyCode == DOWN) {
    playerY+= 20;
  } else if (keyCode == RIGHT) {
    playerX+=20;
  } else if (keyCode == LEFT) {
    playerX-=20;
  }
}
  

You have to think about the logic of the program as a whole.

For example you have the variable colliding which indicates to show the game over screen. Good.

The name is wrong with your new version, it could be gameOverFlag.

In draw() you need to sort the code a bit so that the game runs only when the variable gameOverFlag is still false.

Then think about when and where we have to say
gameOverFlag=true;

Background

In draw() we have different states of the program.

  • For example a Welcome Screen, the Game, a Game Over Screen and a High Score.

draw() has to separate the states/screens and execute only one of them.
This is a powerful concept to distinguish between states.

This distinction is done in draw() by if( gameOverFlag==false)... for example. Or instead of a boolean you can have an int variable state that can be 0 for the Welcome Screen, 1 for the the Game, 2 for the Game Over Screen and 3 for the High Score. Then you can say

  • if(state==0) {…} else if(state==1) {…} else if… OR
  • switch(state) { case 0: … break; case 1: … break; }

Chrisir

I have successfully done the lives left concept but now facing the same issue like last time of not displaying the game over screen after 3 collisions, Could you please run the code and try to fix the error, I would highly appreciate it.
Thanks

float s1 = 3;
float x1 = 0;
float carX;
float carY;
float carWidth;
float playerX;
float playerY;
float playerLength;
boolean colliding= false;
int score;
int lives;
boolean gameOverFlag = false;

void setup(){
  size(1200,400); 
  background(#838982);
  lives = 3;
  init();
  
}

void draw() {
  
  background(#838982);
  textSize(20);
  text("Lives left: " + lives, width -200, height -10);
  text("Score is " + score, width - 100, height -10);
  drawVehicle();
  Player();
  
  if(gameOverFlag==false) {
    displayGameOver();
  }
  collision();
}

void displayGameOver() {
  if(colliding == false){
  } else {
    background(0);
    fill(255,0,0);
    textSize(50);
    text("Game Over", 450, 200);
  }
}

void init() {
 carX = width/20;
 carY = height/10;
 carWidth = width/14;
 playerX = width/2;
 playerY = height /2 +150;
 playerLength = 20;
 score = 0; 
}

void drawVehicle() {
    rect(carX,carY,carWidth,carY); 
    carX = carX +s1;
    if(carX > width) {
    carX = x1;
  }
}

void Player() {
  square(playerX,playerY,playerLength);
  if(playerX < 0) {
    playerX = width;
  }
  if(playerY < 0) {
    playerY = height;
    score = score +1;
  }
}

void collision() {
  
  if (playerX > carX &&
    playerX < carX + carWidth && 
    playerY > carY && 
    playerY < carY + carWidth) {
      gameOverFlag = true;
      colliding = true;
      lives--;
      init();
      if(lives<=0) {
        displayGameOver();
      } 
    }
}

void keyPressed() {
  if (keyCode == UP) {
  playerY-= 20;
  } else if (keyCode == DOWN) {
    playerY+= 20;
  } else if (keyCode == RIGHT) {
    playerX+=20;
  } else if (keyCode == LEFT) {
    playerX-=20;
  }
}
  

You need to read carefully what I wrote.

  • Instead of renaming the variable colliding you made a new one (gameOverFlag). Why??

  • Instead of setting gameOverFlag to true you call displayGameOver();
    WWHAYY??

  • Why did you implement a new function displayGameOver()?

You haven’t done this at all.

You can’t just run off and do something else than what I told you.

Debugging is a very rewarding thing. :wink:

1 Like

Thank you sooo much, I misunderstood the first time, but now implemented and working as I wanted. Once again Thanks :slight_smile:

1 Like

Well done!

Very proud.

Can you show the entire code please.

Here is the final result. Let me know how is it.

float s1 = 3;
float x1 = 0;
float carX;
float carY;
float carWidth;
float playerX;
float playerY;
float playerLength;
boolean gameOverFlag= false;
int score;
int lives;


void setup(){
  size(1200,400); 
  background(#838982);
  lives = 3;
  init();
  
}

void draw() {
  
  background(#838982);
  textSize(20);
  text("Lives left: " + lives, width -200, height -10);
  text("Score is " + score, width - 100, height -10);
  drawVehicle();
  Player();
  collision();
  
   if(gameOverFlag == false){
  } else {
    background(0);
    fill(255,0,0);
    textSize(50);
    text("Game Over", 450, 200);
  }
}


void init() {
 carX = width/20;
 carY = height/10;
 carWidth = width/14;
 playerX = width/2;
 playerY = height /2 +150;
 playerLength = 20;
 score = 0; 
}

void drawVehicle() {
    rect(carX,carY,carWidth,carY); 
    carX = carX +s1;
    if(carX > width) {
    carX = x1;
  }
}

void Player() {
  square(playerX,playerY,playerLength);
  if(playerX < 0) {
    playerX = width;
  }
  if(playerY < 0) {
    playerY = height;
    score = score +1;
  }
}

void collision() {
  
  if (playerX > carX &&
    playerX < carX + carWidth && 
    playerY > carY && 
    playerY < carY + carWidth) {
      lives--;
      init();
      if(lives<=0) {
         gameOverFlag = true;
      } 
    }
}

void keyPressed() {
  if (keyCode == UP) {
  playerY-= 20;
  } else if (keyCode == DOWN) {
    playerY+= 20;
  } else if (keyCode == RIGHT) {
    playerX+=20;
  } else if (keyCode == LEFT) {
    playerX-=20;
  }
}
  
1 Like