
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
187
As with loops, the code for a function should be indented. Every line after the
def
instruc-
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—
sys.exit()
—you can stop indenting.
The
gameOver
function uses a selection of pygame’s commands to perform a simple task:
write the words
Game Over
to the screen in a large font, pause for 5 seconds, and then quit
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
instruction.
With the beginning of the program complete, it’s time to start the main section. This takes
place in an infinite loop—a
while
loop that never exits. This is so that the game can con-
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
True
is true. Because that’s always
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
gameOver
function.
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
while
loop begins, should be indented four spaces—
but it’s a loop of its own, using a
for
instruction to check for pygame events like key presses. As 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
if
instruction to check whether the user has pressed a key. As a
result, the next line—
pygame.quit()
—is indented an additional four spaces for a total of 12
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
if
loop tells Python to check to see if a particular evaluation is true. The first check,
if
event.type == QUIT
, tells Python to execute the indented code below if pygame reports
Содержание A
Страница 1: ......
Страница 2: ......
Страница 3: ...Raspberry Pi User Guide 2nd Edition...
Страница 4: ......
Страница 5: ...Raspberry Pi User Guide 2nd Edition Eben Upton and Gareth Halfacree...
Страница 10: ......
Страница 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...
Страница 28: ......
Страница 29: ...Chapter 1 Meet the Raspberry Pi...
Страница 37: ...Chapter 2 Getting Started with the Raspberry Pi...
Страница 56: ......
Страница 57: ...Chapter 3 Linux System Administration...
Страница 79: ...Chapter 4 Troubleshooting...
Страница 89: ...Chapter 5 Network Configuration...
Страница 109: ...Chapter 6 The Raspberry Pi Software Configuration Tool...
Страница 122: ......
Страница 123: ...Chapter 7 Advanced Raspberry Pi Configuration...
Страница 140: ......
Страница 141: ...Chapter 8 The Pi as a Home Theatre PC...
Страница 151: ...Chapter 9 The Pi as a Productivity Machine...
Страница 159: ...C H A P T E R 9 T H E P I A S A P R O D U C T I V I T Y M A C H I N E 143 Figure 9 4 Exporting a file from The Gimp...
Страница 160: ......
Страница 161: ...Chapter 10 The Pi as a Web Server...
Страница 171: ...Part III Programming with the Raspberry Pi Chapter 11 An Introduction to Scratch Chapter 12 An Introduction to Python...
Страница 172: ......
Страница 173: ...Chapter 11 An Introduction to Scratch...
Страница 189: ...Chapter 12 An Introduction to Python...
Страница 216: ......
Страница 218: ......
Страница 219: ...Chapter 13 Learning to Hack Hardware...
Страница 234: ......
Страница 235: ...Chapter 14 The GPIO Port...
Страница 249: ...Chapter 15 The Raspberry Pi Camera Module...
Страница 265: ...Chapter 16 Add on Boards...
Страница 279: ...Part V Appendixes Appendix A Python Recipes Appendix B Camera Module Quick Reference Appendix C HDMI Display Modes...
Страница 280: ......
Страница 281: ...Appendix A Python Recipes...
Страница 287: ...Appendix B Raspberry Pi Camera Module Quick Reference...
Страница 293: ...Appendix C HDMI Display Modes...