Hardware Reference
In-Depth Information
Listing 6-2
continued
537, 600]
powerPills = [(0,4), (9,4), (0,8), (9,8) ]
# places in the grid where pills are not placed
pillExclude = [(1,1), (3,1), (6,1), (8,1), (1,3), (4,3),;
(5,3), (8,3), (4,5), (5,5), (4,7), (5,7), (4,9), (5,9)]
pillShift = [(1,8),(1,9),(2,8),(2,9),(7,8),(7,9),(8,8),;
(8,9) ]
for X in range(0,10):
if X > 5 :
pillShiftOffset = -20
else:
pillShiftOffset = 20
for Y in range(0,11):
if not((X,Y) in pillExclude):
if(X,Y) in pillShift:
offset = screenOffset + pillShiftOffset
else:
offset = screenOffset
pygame.draw.rect(pillsLayer,(255,255,0), ;
(pillX[X]- offset,pillY[Y], 10,10),0)
if (X,Y) in powerPills:
pygame.draw.rect(pillsLayer,(0,255,255), ;
(pillX[X]- offset-4,pillY[Y]-4,18,18),0)
pygame.draw.rect(pillsLayer,(255,255,0), ;
(pillX[X]- offset,pillY[Y], 10,10),0)
his function sets up the pills layer and is called every time this layer needs to be refreshed,
such as at the start of the game and when all the pills have been eaten. Whereas Listing 6-1
is a simple brute-force set of draw instructions, Listing 6-2 is a bit more subtle. he pills are
mainly on a regular grid, so this lends itself to being programmed in an algorithmic way.
However, not all the pills in the grid are drawn because some would be over walls or inside
boxes. Similarly, some pills need to be dawn slightly of the grid to it into the walls. his
brings into play a new Python function - that of testing if any item in one list is contained in
another list. he coordinates of the grid of pills is deined by the
pillX
and
pillY
lists, the
coordinates pairs of the pills not to plot are in the
pillExclude
list and similarly the coor-
dinates pairs of the ofset pills are in the
pillShift
list. Two nested
for
loops generate the
sequence of coordinates, and the line
if not((X,Y) in pillExclude)