Game Development Reference
In-Depth Information
function runGame() {
update();
render();
evalPuck();
evalGame();
}
As you can see, the runGame function is called on every tick of the game. This function executes the update/
render cycle. It also runs a few functions that determine end level and/or end game scenarios.
Updating Game Elements in the Game Loop
The update cycle will evaluate the next position of the puck and paddle, and how they will affect the game in regards of
collisions and bounds. Let's take a quick look at update before analyzing each of the functions it calls (see Listing 4-13).
Listing 4-13. The Update Cycle
function update() {
updatePaddle();
updatePuck();
checkPaddle();
checkBricks();
}
The update function calls a series of functions used to evaluate and update display objects in motion. A series of
functions is also called to check if a brick is about to be busted, or if the puck is about to hit the paddle. You want to do
this before ultimately updating the stage. Let's go over each of these functions in detail, starting with updatePaddle
(see Listing 4-14).
Listing 4-14. Updating the Paddle
function updatePaddle() {
var nextX = paddle.x;
if (leftKeyDown) {
nextX = paddle.x - PADDLE_SPEED;
if (nextX < leftWall) {
nextX = leftWall;
}
}
else if (rightKeyDown) {
nextX = paddle.x + PADDLE_SPEED;
if (nextX > rightWall - paddle.width) {
nextX = rightWall - paddle.width;
}
}
paddle.nextX = nextX;
}
This should look familiar to you from Chapter 3. Depending on which arrow key is currently pressed down,
the next position of the paddle is assigned to it, and determines where it should be drawn during the next render
 
Search WWH ::




Custom Search