Game Development Reference
In-Depth Information
ļ?®
Keeping track of the score; this is basically just the number of stains eaten
so far times 10.
ļ?®
Increasing the speed of Mr. Nom after every ten stains he's eaten. That will
make the game a little more challenging.
ļ?®
Keeping track of whether Mr. Nom is still alive. We'll use this to determine
whether the game is over later on.
ļ?®
Creating a new stain after Mr. Nom eats the current one (a subtle but
important and surprisingly complex task).
There are only two items on this task list that we haven't discussed yet: updating the world in a
time-based manner and placing a new stain.
Time-Based Movement of Mr. Nom
In Chapter 3, we talked about time-based movement. This basically means that we define
velocities of all of our game objects, measure the time that has passed since the last update
(a.k.a. the delta time), and advance the objects by multiplying their velocity by the delta time. In
the example given in Chapter 3, we used floating-point values to achieve this. Mr. Nom's parts
have integer positions, though, so we need to figure out how to advance the objects in this
scenario.
Let's first define the velocity of Mr. Nom. The world of Mr. Nom has time, and we measure it in
seconds. Initially, Mr. Nom should advance by one cell every 0.5 seconds. All we need to do is
keep track of how much time has passed since we last advanced Mr. Nom. If that accumulated
time goes over our 0.5-second threshold, we call the
Snake.advance()
method and reset our
time accumulator. Where do we get those delta times from? Remember the
Screen.update()
method. It gets the frame delta time. We just pass that on to the
update()
method of our
World
class, which will do the accumulation. To make the game more challenging, we will decrease
that threshold by 0.05 seconds each time Mr. Nom eats another ten stains. We have to make
sure, of course, that we don't reach a threshold of 0, or else Mr. Nom would travel at infinite
speedāsomething Einstein wouldn't take kindly to.
Placing Stains
The second issue we have to solve is how to place a new stain when Mr. Nom has eaten the
current one. It should appear in a random cell of the world. So we could just instantiate a new
Stain
with a random position, right? Sadly, it's not that easy.
Imagine Mr. Nom taking up a considerable number of cells. There is a reasonable probability that
the stain would be placed in a cell that's already occupied by Mr. Nom, and it will increase the
bigger Mr. Nom gets. Thus, we have to find a cell that is currently not occupied by Mr. Nom. Easy
again, right? Just iterate over all cells, and use the first one that is not occupied by Mr. Nom.
Again, that's a little suboptimal. If we started our search at the same position, the stain wouldn't
be placed randomly. Instead, we'll start at a random position in the world, scan all cells until we
reach the end of the world, and then scan all cells above the start position, if we haven't found a
free cell yet.