Hardware Reference
In-Depth Information
Listing 6-4 continued
if pill: #you have a pill is it a power pill?
ppill = False
if fpx != 0:
for pix in range(fpx,fpx+12,2):
if pillsLayer.get_at((pix, p[1]+24)) == ppColour:
ppill = True
if fpy !=0:
for piy in range(fpy,fpy+12,2):
if pillsLayer.get_at((pix-screenOffset, piy));
== ppColour:
ppill = True
if ppill:
#print” we have a power pill”
ppSound.play()
updateScore(40)
ppCount = 0 # start off power play
return pill
here are two functions here. he second, pillEat , deals with the pill detection in the manner
already discussed. It returns a Boolean variable showing if it has detected a pill and also clears a
global variable ( ppCount ) if a power pill has been found. he inding of the power pill also trig-
gers the power pill sound as well as giving the score a bonus. he irst function, eat , takes in a
position list and calls the pillEat function as part of an if statement. When a pill is detected,
the sound is played, and the score is updated as you might expect, but also the pill has to be
removed from the display. his is done by simply drawing a rectangle covering the whole of the
sprite in the pills layer of the colour that you have set to represent transparency. his wipes it
from view. here is a count kept of how many pills have been consumed, and when this reaches
the maximum number, 96, the ghosts are sent back to their box, and the pills are redrawn again
on the pill layer. he line that resets the ghosts contains something you will have not seen before:
ghostPos = copy.deepcopy(ghostInitalPos) # return ghosts to base
his is what is known as a deep copy. When copying lists just putting
ghostPos = ghostInitalPos
will make the two lists the same object; that is, if you change one, the other will also be changed.
When the deep copy is used every individual element in the list is transferred to each individual
element in the other list. Also, when drawing a new lot of pills the global variable ghostRelease
is set, which controls when the ghosts can come out of their box, but more on this later when
you look at the main function.
Search WWH ::




Custom Search