Hardware Reference
In-Depth Information
Before you can write a pygame program, you need to install the pygame library. If you're
using the recommended Debian distribution, this is as simple as typing the following at the
console or terminal:
sudo apt-get install python-pygame
For other distributions, the pygame source iles can be downloaded from the oicial 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 ile)
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 *
he irst 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()
Search WWH ::




Custom Search