How do you stop a game?


#1

We’ve added levels, including win and lose screens. However, how do you tell it to stop keeping score, losing lives, looping the characters, etc?


#2

Without seeing the code, I can’t say exactly but here is my general idea.
A lot of this you probably have done in a different context as the game you describe sounds pretty complicated.

You need to create a variable that tracks whether the game should be playing or not.
Inside the draw function, create an if else statement that uses the variable above as the condition.
I guess other ways to do it would be to change the frameRate to zero (I’ve never tried this.)

var playing = true ;

if (playing)
{
// all of the code for the game when it is playing. Including an if statement that will change the variable playing to false. 
}
else
{
//the code for when the game stops. 
}

Let us know how it goes. It'd also be fun to add a link to the game.

#3

I never got it to work with true and false, but it did work with numbers. I started with declaring start to have a value of 5, then changed it to 3 with a click and the game starts. Then to end, I had all the sprites go invisible. Not elegant, but it worked.


#4

Different strategies exist for ending a game.

  1. Number of levels reached: every time to go to a different level counts as 1 level. Once they have reached 12 levels the game could end.
  2. Specific level reached. When a specific level is reached, indicate that the player is a star and end the game.
  3. Maximum Number of lives of characters has been reached. Everyone starts with a certain number of lives. Every time the character loses, the character dies. A new character is retrieved from the live people pool. When there are no more lives left in the pool, the game ends.
  4. Timer. When the maximum time for a game has been reached, end the game.

#5

But the question I wanted to ask was, how to stop all the sprites and switch to just an end screen.


#6

Hi Janet,

The nature of Game Lab and similar systems is that the draw loop always continues to run, so if the game is over, you’ll just be drawing different things to the screen. Your solution of making the sprites invisible (or destroying them) sounds great, as does John’s of using a conditional to run a completely different block of code depending on the state of the game.

I think what Janine may have been suggesting were different conditions you could use for the true/false boolean in John’s method of using different blocks of code.

I tried out your game, and it’s a lot of fun!

Elizabeth