
C H A P T E R 1 2
A N I N T R O D U C T I O N T O P Y T H O N
191
Python a place on the playing field to position the new raspberry. It’s important that the
location of the raspberry is set randomly: this prevents the player from learning where the
raspberry will appear next. Finally, the
raspberrySpawned
variable is set back to
1
, to
make sure that there will only be a single raspberry on the playing surface at any given time.
Now you have the code required to make the snake move and grow, and cause raspberries to
be eaten and created—a process known in gaming as
respawning
. However, nothing is being
drawn to the screen. Type the following lines:
playSurface.fill(blackColour)
for position in snakeSegments:
pygame.draw.rect(playSurface,whiteColour,Rect
↵
(position[0], position[1], 20, 20))
↵
pygame.draw.rect(playSurface,redColour,Rect
↵
(raspberryPosition[0], raspberryPosition[1], 20, 20))
pygame.display.flip()
These lines tell pygame to fill in the background of the playing surface in black, draw the
snake’s head and body segments in white, and finally, draw a raspberry in red. The last line,
pygame.display.flip()
, tells pygame to update the screen—without this instruction,
items will be invisible to the player. Every time you finish drawing objects onto the screen,
remember to use
pygame.display.flip()
so the user can see the changes.
Currently, it’s impossible for the snake to die. A game where the player can never die would
rapidly get boring, so enter the following lines to set up some scenarios for the snake’s death:
if snakePosition[0] > 620 or snakePosition[0] < 0:
gameOver()
if snakePosition[1] > 460 or snakePosition[1] < 0:
gameOver()
The first
if
statement checks to see if the snake has gone off the playing surface vertically,
while the second
if
statement checks if the snake has gone off the playing surface horizon-
tally. In either case, it’s bad news for the snake: the
gameOver
function, defined earlier in
the program, is called to print a message to the screen and quit the game. The snake should
also die if its head hits any portion of its body, so add the following lines:
for snakeBody in snakeSegments[1:]:
if snakePosition[0] == snakeBody[0] and
↵
snakePosition[1] == snakeBody[1]:
gameOver()
Summary of Contents for A
Page 1: ......
Page 2: ......
Page 3: ...Raspberry Pi User Guide 2nd Edition...
Page 4: ......
Page 5: ...Raspberry Pi User Guide 2nd Edition Eben Upton and Gareth Halfacree...
Page 10: ......
Page 26: ...R A S P B E R R Y P I U S E R G U I D E S E C O N D E D I T I O N 10...
Page 28: ......
Page 29: ...Chapter 1 Meet the Raspberry Pi...
Page 37: ...Chapter 2 Getting Started with the Raspberry Pi...
Page 56: ......
Page 57: ...Chapter 3 Linux System Administration...
Page 79: ...Chapter 4 Troubleshooting...
Page 89: ...Chapter 5 Network Configuration...
Page 109: ...Chapter 6 The Raspberry Pi Software Configuration Tool...
Page 122: ......
Page 123: ...Chapter 7 Advanced Raspberry Pi Configuration...
Page 140: ......
Page 141: ...Chapter 8 The Pi as a Home Theatre PC...
Page 151: ...Chapter 9 The Pi as a Productivity Machine...
Page 160: ......
Page 161: ...Chapter 10 The Pi as a Web Server...
Page 172: ......
Page 173: ...Chapter 11 An Introduction to Scratch...
Page 189: ...Chapter 12 An Introduction to Python...
Page 216: ......
Page 218: ......
Page 219: ...Chapter 13 Learning to Hack Hardware...
Page 234: ......
Page 235: ...Chapter 14 The GPIO Port...
Page 249: ...Chapter 15 The Raspberry Pi Camera Module...
Page 265: ...Chapter 16 Add on Boards...
Page 280: ......
Page 281: ...Appendix A Python Recipes...
Page 287: ...Appendix B Raspberry Pi Camera Module Quick Reference...
Page 293: ...Appendix C HDMI Display Modes...