Hardware Reference
In-Depth Information
Listing 5-3 continued
if event.key == pygame.K_ESCAPE :
terminate()
if event.key == pygame.K_DOWN : ;
# expand / contract the box
updateBox(1,-2)
if event.key == pygame.K_UP :
updateBox(1,2)
if event.key == pygame.K_LEFT :
updateBox(0,-2)
if event.key == pygame.K_RIGHT :
updateBox(0,2)
if event.key == pygame.K_s :
rally = True
if event.key == pygame.K_SPACE :
pause = False
if event.key == pygame.K_PAGEDOWN :
if batY[1] < screenHeight - batInc :
batY[1] += batInc
if event.key == pygame.K_PAGEUP :
if batY[1] > batInc :
batY[1] -= batInc
if __name__ == '__main__':
main()
here are a lot more changes this time, but hopefully this code is recognisable in structure
from Listing 5-2. he various phases of the program are deined by the Boolean variables
rally and pause , which can be changed from the keyboard, in the checkForEvent()
function, along with the Y position of the bat. I chose to use the Page Up and Page Down
keys for the movement of the bat as they are on the far right of my keyboard.
he updateBox() function has two more limits added to it, that of the ball approaching the
bat from the left or right. he drawScreen() function now draws the two bats and only
draws the ball if the rally variable is true. It also draws a surface bitmap containing the
score. updateScore() is a new function that, as its name implies, changes the surface con-
taining the score and positions the score rectangle. his works just like you saw in Chapter 4,
“Here's the News”, with the teleprompter.
he moveBall() function has grown some. he list of if statements is now replaced by a
string of elif clauses based on the original if statement. his means that only one of the
sections of code will be executed in each pass. his is because the positional condition for a
Search WWH ::




Custom Search