Hardware Reference
In-Depth Information
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
Search WWH ::




Custom Search