Raspberry Pi A User Manual page 203

Hide thumbs Also See for Raspberry Pi A:
Table of Contents

Advertisement

C H A P T E R 1 2
As with loops, the code for a function should be indented. Every line after the
tion should have four spaces at the start—if you're using IDLE, these spaces will be inserted
automatically, but if you're using a text editor, you will need to insert the spaces yourself.
After the final line of the function—
The
function uses a selection of pygame's commands to perform a simple task:
gameOver
write the words
Game Over
both pygame and Python itself. It may seem strange to set up the instructions for quitting
the game before the game has even begun, but functions should always be defined before
they are called. Python won't execute these instructions until it is told to do so using the
newly created
gameOver
With the beginning of the program complete, it's time to start the main section. This takes
place in an infinite loop—a
tinue to run until the player dies by hitting a wall or eating his or her own tail. Begin the main
loop with the following line:
while True:
Without anything to evaluate, Python will check to see if
the case, the loop will continue to run forever—or, at least until you tell Python to quit out
of the loop by calling the
Continue the program with the following lines, paying attention to the indentation levels:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
The first line, which comes right after the
but it's a loop of its own, using a
result, the line under that needs to be indented an additional four spaces for a total of eight—but
that line, too, is a loop, using an
result, the next line—
pygame.quit()
spaces. This logical progression of indentation tells Python where each loop begins and ends,
which is important: if the wrong number of spaces is used, the program won't work correctly. This
is why using a development environment like IDLE, which attempts to automatically indent code
where required, can be easier than using a plain text editor to create Python programs.
An
loop tells Python to check to see if a particular evaluation is true. The first check,
if
event.type == QUIT
A N I N T R O D U C T I O N T O P Y T H O N
sys.exit()
to the screen in a large font, pause for 5 seconds, and then quit
instruction.
loop that never exits. This is so that the game can con-
while
function.
gameOver
loop begins, should be indented four spaces—
while
instruction to check for pygame events like key presses. As a
for
instruction to check whether the user has pressed a key. As a
if
—is indented an additional four spaces for a total of 12
, tells Python to execute the indented code below if pygame reports
—you can stop indenting.
is true. Because that's always
True
instruc-
def
if
187

Hide quick links:

Advertisement

Table of Contents
loading

Related Products for Raspberry Pi Raspberry Pi A

This manual is also suitable for:

Raspberry pi b

Table of Contents