Making functions

Hi all!, I have to make this game similar to crossy road for a project. My pedestrain is meant to have a certain number of lives (livesLeft) before the game ends. If the pedestrain runs out of lives, the game is meant to show “Game over” and restart. I am having trouble making a function for that which i can use. Can someone help please? Here is my coding for that part:

< void collisonDetectionreset () {

if (dist(pedX, pedY, posX, posY) < posY -pedY) {

if(livesLeft<=MAX_LIVES) {
score = 0;
pedY = height-40; }

if (livesLeft==0) {
  showGameOverScreen= true; 


There’s no code between the curly brackets following your first if statement, so even if that condition is true, nothing different happens.

You check that livesLeft <= MAX_LIVES seems silly. Shouldn’t the number of lives you have always been less than or equal to the maximum number of lives possible?

While it can sort of make sense to track that the game is over, you could just check that the number of lives left is 0 directly, and draw a game over screen in draw() if it is.

That said, this code is very incomplete. We certainly can’t run it as it is now. Post the whole thing, and use the code format button </> too!

1 Like

here you need lives--;

Initially lives could be 3

Then check if (lives<=3)

1 Like