Game Development Reference
In-Depth Information
The first line in this shape contains zero balloons. The reason is that the value of y is still 0 at that
point, which means the inner for instruction is executed zero times.
Restarting the Game
When the player has lost all their lives, the game is over. How do you deal with this? In the case of
the Painter game, you would like to show a Game Over screen. The player can press the left mouse
button, which will then restart the game. In order to add this to the game, you load an extra sprite
when the game is started that represents the Game Over screen:
sprites.gameover = loadSprite("spr_gameover_click.png");
Now you can use an if instruction in each of the game-loop methods to determine what you should
do. If the game is over, you don't want the cannon and the ball to handle input anymore; you simply
want to listen if the player presses the mouse button. If that happens, you reset the game. So, the
handleInput method in the PainterGameWorld class now contains the following instructions:
if (this.lives > 0) {
this.ball.handleInput(delta);
this.cannon.handleInput(delta);
}
else {
if (Mouse.leftPressed)
this.reset();
}
You add a reset method to the PainterGameWorld class so you can reset the game to its initial state.
This means resetting all the game objects. You also need to reset the number of lives to five. Here is
the full reset method in PainterGameWorld :
PainterGameWorld.prototype.reset = function () {
this.lives = 5;
this.cannon.reset();
this.ball.reset();
this.can1.reset();
this.can2.reset();
this.can3.reset();
};
For the update method, you only need to update the game objects if the game isn't over. Therefore,
you first check with an if instruction whether you need to update the game objects. If not (in other
words: the number of lives has reached zero), you return from the method:
if (this.lives <= 0)
return;
this.ball.update(delta);
this.cannon.update(delta);
this.can1.update(delta);
this.can2.update(delta);
this.can3.update(delta);
 
Search WWH ::




Custom Search