Game Development Reference
In-Depth Information
public void update( float deltaTime) {
if (gameOver)
return ;
tickTime += deltaTime;
while (tickTime > tick ) {
tickTime - = tick ;
snake.advance();
if (snake.checkBitten()) {
gameOver = true ;
return ;
}
SnakePart head = snake.parts.get(0);
if (head.x == stain.x && head.y == stain.y) {
score += SCORE_INCREMENT ;
snake.eat();
if (snake.parts.size() == WORLD_WIDTH * WORLD_HEIGHT ) {
gameOver = true ;
return ;
} else {
placeStain();
}
if (score % 100 == 0 && tick - TICK_DECREMENT > 0) {
tick - = TICK_DECREMENT ;
}
}
}
}
}
The update() method is responsible for updating the World and all the objects in it, based on the
delta time we pass to it. This method will call each frame in the game screen so that the World
is updated constantly. We start off by checking whether the game is over. If that's the case, then
we don't need to update anything. Next, we add the delta time to our accumulator. The while
loop will use up as many ticks that have been accumulated (for example, when tickTime is 1.2
and one tick should take 0.5 seconds, we can update the world twice, leaving 0.2 seconds in the
accumulator). This is called a fixed-time-step simulation .
In each iteration, we first subtract the tick interval from the accumulator. Next, we tell Mr. Nom to
advance. We check if he has bitten himself, and set the game-over flag if that's the case. Finally,
we check whether Mr. Nom's head is in the same cell as the stain. If that's the case, we increment
the score and tell Mr. Nom to grow. Next, we check if Mr. Nom is composed of as many parts as
there are cells in the world. If that's the case, the game is over and we return from the function.
Otherwise, we place a new stain with the placeStain() method. The last thing we do is check
whether Mr. Nom has just eaten ten more stains. If that's the case, and our threshold is above zero,
we decrease it by 0.05 seconds. The tick will be shorter and thus make Mr. Nom move faster.
This completes our set of model classes. The last thing we need to implement is the game
screen!
 
Search WWH ::




Custom Search