Game Development Reference
In-Depth Information
The first is when a player has completely run out of lives, and the second is when the bottom row of bricks has reached
past the floor. Each of these situations should end the game immediately by executing the gameOver function.
A new level is determined by checking on the number of hits the puck has made with the paddle. It was decided
that five hits should make a new level when you declared the constant PADDLE_HITS_FOR_NEW_LEVEL . Now on every
fifth paddle hit, a new level of bricks is thrown into gameplay.
Ending the Game
You are now officially finished with the game loop functions. During this cycle, the game was constantly checking to
see if the game should be over, and if so, to call on gameOver . Listing 4-20 shows this as one of the final three functions
in the game, all of them handling the end-game scenario.
Listing 4-20. The gameOver Function
function gameOver() {
createjs.Ticker.setPaused(true);
gameRunning = false;
messageTxt.text = "press spacebar to play";
puck.visible = false;
paddle.visible = false;
stage.update();
messageInterval = setInterval(function () {
messageTxt.visible = messageTxt.visible ? false : true;
stage.update();
}, 1000);
}
The game should immediately be paused in the gameOver function to prevent the game loop from carrying on,
and gameRunning is set to false. Then, the paddle and puck are hidden and the message below is changed to instruct
the player how to restart the game. After this, the stage needs to be manually updated since the update/render cycle
has been paused. To draw attention to this new message, a simple interval is set up to blink these instructions on and
off, and is set to the messageInterval game variable. This is done so it can be referenced and stopped when the game
is restarted.
Resetting the Game
As simple as the game state logic is in this game, it's enough to determine if the game is in motion or if it's waiting to
be restarted. Because the gameRunning variable is now set to false, the hit of the spacebar will no longer simply pause
the game, but will run a function to completely reset all game values and start it over (see Listing 4-21).
Listing 4-21. Resetting the Game Variables and Starting the Game Over
function resetGame() {
clearInterval(messageInterval);
level = 0;
score = 0;
lives = 5;
paddleHits = 0;
puck.y = 160;
puck.velY = PUCK_SPEED;
puck.visible = true;
 
Search WWH ::




Custom Search