
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
185
For other distributions, the pygame source files can be downloaded from the official pygame
website at
http://www.pygame.org/download.shtml
. Instructions for installation
are provided on the same page.
Starting a pygame program is the same as starting any other Python project. Open a new blank
document in either IDLE or a text editor, and add the following shebang line to the top:
#!/usr/bin/env python
Next you need to tell Python that this program uses the pygame modules. To do this, you use
an
import
instruction, which tells Python to load an external module (another Python file)
and make it accessible from the current program. Type the following two lines to import the
necessary modules into your new project:
import pygame, sys, time, random
from pygame.locals import *
The first line imports the main
pygame
module along with the Python modules
sys
,
time
and
random
, which will also be used in this program. Typically, a module must then be called
by typing its name followed by a full stop and the name of the instruction from within the
module, but the second line in the preceding code tells Python to load all the instructions
from the
pygame.locals
module as though they’re native instructions. As a result, you
will need to do less typing when using these instructions. Other module names—such as
pygame.clock
, which is separate to
pygame.locals
—will still need to be typed in full.
Enter the next two lines to set up pygame so it’s ready to use in the example program:
pygame.init()
fpsClock = pygame.time.Clock()
The first line tells pygame to initialise itself, and the second line sets up a new variable called
fpsClock
, which will be used to control the speed of the game. Next, set up a new pygame
display surface—the canvas onto which in-game objects will be drawn—with the following
two lines:
playSurface = pygame.display.set_mode((640, 480))
pygame.display.set_caption(‘Raspberry Snake’)
Next, you should define some colours for the program to use. Although this step isn’t strictly
necessary, it again saves on typing: if you want to set a particular object to be red, you
can simply use the
redColour
variable rather than having to call the
pygame.Color
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...