Hardware Reference
In-Depth Information
he irst 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 deine 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 instruc-
tion and remember the three colour values for red, green and blue. To deine the colours for
this example program, type the following lines:
redColour = pygame.Color(255, 0, 0)
blackColour = pygame.Color(0, 0, 0)
whiteColour = pygame.Color(255, 255, 255)
greyColour = pygame.Color(150, 150, 150)
he next few lines initialise some of the game's variables so they're ready for use. his is an
important step, because if these variables are left blank when the game begins, Python won't
know what to do. Don't worry about what each variable does for now—just type in the fol-
lowing lines:
snakePosition = [100,100]
snakeSegments = [[100,100],[80,100],[60,100]]
raspberryPosition = [300,300]
raspberrySpawned = 1
direction = 'right'
changeDirection = direction
Notice that three of the variables— snakePosition , snakeSegments and raspberry-
Position —are set to a list of comma-separated values. his causes Python to create the
variables as lists —a number of diferent values stored in a single variable name. Later, you'll
see how you can access individual values stored in a list.
Next, you need to deine a new function—a fragment of Python code which can be called
upon later in the program. Functions are useful for avoiding code repetition and making the
program easier to understand. If you have a particular set of instructions that are needed at
Search WWH ::




Custom Search