Hardware Reference
In-Depth Information
he next section of code checks the value of the
ppCount
variable, which times the power
play. he way it works is that the variable is incremented every step and, depending on the
total, sets or clears the power play lag called
powerPlay
. his is set initially to be 90, so
there is no power play at the start. However, this allows you to use a simple mechanism for
starting a period of power play; you simply have to set the
ppCount
variable to zero, and it
will start. he code also deines a period near the end of the power play when the ghosts will
lash briely. After that section of code the
drawScreen
function is called, and the positions
of Pie Man and the ghosts are plotted. he following section of the code sees if there is a kill,
or ghost/Pie Man collision, checks if Pie Man has eaten a pill and then updates Pie Man's
position by calling the
makeStep
function.
The Ghost Chase
Next comes the movement of the ghosts. First of all, the ghost speed is altered depending on if
there is a power play in force. After all, there is little point in being able to kill a ghost if it can out-
run you, or at least match your speed. In normal play the ghosts can move as fast as you can,
which means errors you make in steering Pie Man will accumulate, so the ghosts will eventually
catch you. If you want the ghosts to be able to move faster than Pie Man, don't increase the speed
of the ghosts here; instead reduce the step size of Pie Man using the
pieIncrement
variable.
After that, and providing the
ghostRelease
time has been exceeded, the moves for the ghosts
are calculated by calling the
getNewDir
function and trying to move in that direction. If this
move is blocked, an attempt is made to move in the previous direction. If this is blocked, a series
of random directions are tried until one is found that is not blocked. It is this algorithm that gives
the ghosts their movement, and it is interesting to see the emergent behaviour that this gener-
ates. Note that the
gNumber
variable is used to release the ghosts one at a time at set intervals.
he next section of code deals mainly with housekeeping. he
step
variable is toggled to dis-
play alternating lid-open and lid-closed images. he
ppCount
variable is incremented to time
the power play period, and the
gStep
variable is changed up and down to deine the ghost
sprite to use. And that is it, until the game ends by reducing the number of lives left to zero.
The Game Ends
he inal part of the
main
function deals with what happens at the end of a game. First of, it
checks to see if a new high score has been reached, and if it has, it lashes the new score with
a number of changing colour backgrounds. he time between the lashes is controlled by the
time.sleep
statement. hen after another delay the whole thing starts again.
his is a long piece of code, so the odds are stacked against you for typing it in right the irst
time. However, if you have worked through the previous chapters, your debugging skills
should be quite good by now. he main things to watch out for are getting the indentation
correct and getting the case of the characters right; an uppercase
X,
for example, can look
very close to a lowercase
x
in some fonts.