Game Development Reference
In-Depth Information
livesTxt.text = "lives: " + lives;
scoreTxt.text = "score: " + score;
}
The render function assigns the puck and paddle their new positions using the temporary values that were
injected into them during the update process. The messaging is also updated by assigning their text values using the
score and lives game variables that may or may not have been changed during the update cycle.
Evaluating the Game
A few functions are left to run at the end of the game loop to evaluate the game. Listing 4-19 shows the code used to
determine a few key scenarios in the game, which could result in a loss of a life, a new level, or the end of the game
completely.
Listing 4-19. Evaluating Game Progress
function evalPuck() {
if (puck.y > paddle.y) {
puck.isAlive = false;
}
if (puck.y > canvas.height + 200) {
puck.y = bricks[0].y + bricks[0].height + 40;
puck.x = canvas.width / 2;
puck.velX *= -1;
puck.isAlive = true;
combo = 0;
lives--;
}
}
function evalGame() {
if (lives < 0 || bricks[0].y > board.y) {
gameOver();
}
if (paddleHits == PADDLE_HITS_FOR_NEW_LEVEL) {
newLevel();
}
}
The current position of the puck is first examined in evalPuck . If it's below the top of the paddle, you know it has
no chance of being hit in the next iterations of the game loop so its isAlive property is set to false. You also want to
see if it has traveled enough distance below the stage so you can reset its position and put it back in play. This gives
the player a bit of time to prepare for the puck being thrown back into the game. The position of the puck when reset
is determined by the bottom row of bricks by accessing the first index in the bricks array. The first brick in this array
will always be the lowest or one of the lowest positioned bricks on the screen. The puck's x position is always set near
the center of the stage to give the player a chance to get into position as the rows become lower and increasingly more
difficult to reach after a puck is reset.
During this puck reset, a few more properties need to be updated. The puck's x velocity is reversed, and its
isAlive property is set back to true. The combo game variable is reset to zero, and the number of lives is decreased
by one.
The game's progress is determined by whether a new level should be added, or if the game should end
completely. The evalGame will check for both. First, it checks if the game should be over by checking two scenarios.
 
Search WWH ::




Custom Search