Home Page Icon
Home Page
Table of Contents for
Adventures in Python®
Close
Adventures in Python®
by Craig Richardson
Adventures in Python
Cover
Title Page
About the Author
Introduction
What Is Programming?
What Is Python and What Can You Do With It?
Who Should Read This Book?
What You Will Learn
What You Will Need for the Projects
How This Book Is Organised
Setting Up Python
Creating Your First Program
The Companion Website
Conventions
Reaching Out
Adventure 1: Diving into Python
What Is Programming?
Opening IDLE
Returning to Your First Python Program
It Isn’t Working—Grrr!
Using a File Editor
Asking Questions with Variables
Making the Program Make Decisions: Conditionals
Repeating Code with Loops
Praise Generator
A Bigger Adventure: Spaceship Control Console
Adventure 2: Drawing with Turtle Graphics
Getting Started with Turtle
Using Variables to Change Angles and Lengths
Using Addition to Draw a Spiral
Saving Some Space with Loops
A Shape with 360 Sides: Drawing a Circle
Creating Functions to Reuse Your Code
Shape Presets
Adding Randomly Generated Pictures
Adventure 3: Windows, Buttons, and Other GUI Stuff
Creating Buttons
Creating Text Boxes
Building a Random Sentence Generator
Programming a Guessing Game
Adventure 4: More GUI Elements with Tkinter
Creating Sliders
How Colours Work on Computers and as Hexadecimal Values
Changing the Canvas Colour
Making the Colour Picker
Adding a Text Box
Creating a Click Speed Game
Adventure 5: Drawing Shapes with PyGame
Installing PyGame
My First PyGame
Creating Rectangles
Creating Ellipses
Saving Your Images
Adventure 6: Adding Keyboard Input with PyGame
Using Keyboard Input
Other Keys You Can Use
Creating the Game
Adventure 7: Creative Ways to Use a Mouse with PyGame
Getting the Mouse Position
Making a Mesh
Creating Mouse Trails
Adventure 8: Using Images with PyGame
Loading an Image
Adding a Moustache to a Photograph
Making Sprites
Adventure 9: Using Sounds and Music with PyGame
Playing Sounds
Using Music with Python
Adding Sounds and Music to a Game
Adventure 10: Your Really Big Adventure
Writing the Program for the Game
Debugging the Game
Summary
Appendix A: Installing and Downloading the Proper Files
Installing PyGame
Downloading the Files for Adventures 8, 9 and 10
Glossary
End User License Agreement
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Prev
Previous Chapter
Title Page
Next
Next Chapter
Introduction
Adventures in Python®
Contents
Cover
Title Page
About the Author
Introduction
What Is Programming?
What Is Python and What Can You Do With It?
Who Should Read This Book?
What You Will Learn
What You Will Need for the Projects
How This Book Is Organised
Setting Up Python
Creating Your First Program
The Companion Website
Conventions
Reaching Out
Adventure 1: Diving into Python
What Is Programming?
Opening IDLE
Returning to Your First Python Program
It Isn’t Working—Grrr!
Using a File Editor
Asking Questions with Variables
Making the Program Make Decisions: Conditionals
Repeating Code with Loops
Praise Generator
A Bigger Adventure: Spaceship Control Console
Adventure 2: Drawing with Turtle Graphics
Getting Started with Turtle
Using Variables to Change Angles and Lengths
Using Addition to Draw a Spiral
Saving Some Space with Loops
A Shape with 360 Sides: Drawing a Circle
Creating Functions to Reuse Your Code
Shape Presets
Adding Randomly Generated Pictures
Adventure 3: Windows, Buttons, and Other GUI Stuff
Creating Buttons
Creating Text Boxes
Building a Random Sentence Generator
Programming a Guessing Game
Adventure 4: More GUI Elements with Tkinter
Creating Sliders
How Colours Work on Computers and as Hexadecimal Values
Changing the Canvas Colour
Making the Colour Picker
Adding a Text Box
Creating a Click Speed Game
Adventure 5: Drawing Shapes with PyGame
Installing PyGame
My First PyGame
Creating Rectangles
Creating Ellipses
Saving Your Images
Adventure 6: Adding Keyboard Input with PyGame
Using Keyboard Input
Other Keys You Can Use
Creating the Game
Adventure 7: Creative Ways to Use a Mouse with PyGame
Getting the Mouse Position
Making a Mesh
Creating Mouse Trails
Adventure 8: Using Images with PyGame
Loading an Image
Adding a Moustache to a Photograph
Making Sprites
Adventure 9: Using Sounds and Music with PyGame
Playing Sounds
Using Music with Python
Adding Sounds and Music to a Game
Adventure 10: Your Really Big Adventure
Writing the Program for the Game
Debugging the Game
Summary
Appendix A: Installing and Downloading the Proper Files
Installing PyGame
Downloading the Files for Adventures 8, 9 and 10
Glossary
End User License Agreement
List of Tables
Chapter 4
Table 4-1 A List of Python Keywords That Cannot Be Used for the Names of Variables, Functions and Arguments
Table 4-2 A Selection of Colours and Their Hexadecimal Values
Chapter 6
Table 6-1 Keyboard Control Codes in
PyGame
List of Illustrations
Introduction
FIGURE I-1 The code of a program that generates a circular progress bar. You will learn how to create programs like this one in your adventures.
FIGURE I-2 Downloading Python for Windows 8
FIGURE I-3 Python has been installed on Windows 8.
FIGURE I-4 IDLE installed and Python opened on Windows 8
FIGURE I-5 Downloading Python for Windows 7
FIGURE I-6 Python has been installed on Windows 7.
FIGURE I-7 IDLE installed and Python opened on Windows 7
FIGURE I-8 Downloading Python for Mac OS X
FIGURE I-9 Installing Python on a Mac
FIGURE I-10 Python has been installed on the Mac
FIGURE I-11 IDLE installed and Python opened on Mac
FIGURE I-12 The installation process in Linux
FIGURE I-13 Installing IDLE on Linux
FIGURE I-14 IDLE installed and Python opened on Linux
FIGURE I-15 After you press the Enter key, the text inside the speech marks appears on the next line of the Python shell
FIGURE I-16 The phrase appears on every line of the shell
Chapter 1
FIGURE 1-1 IDLE is ready for you to start programming!
FIGURE 1-2 The command prompt in a Python shell
FIGURE 1-3 Your first Python program running in the Python shell
FIGURE 1-4 An error message in the Python shell
FIGURE 1-5 The IDLE file editor window
FIGURE 1-6 The completed program and output in IDLE
FIGURE 1-7 The command prompt waiting for you to input a name
FIGURE 1-8 The finished program
FIGURE 1-9 The finished program running alongside the Python code
FIGURE 1-10 An
if
statement nested inside another
if
statement
FIGURE 1-11 Replying “yes” to the first question will cause the program to ask a second question.
FIGURE 1-12 The finished vending machine code and the Python shell waiting for you to make a choice
FIGURE 1-13 This program repeats until the user types
exit
.
FIGURE 1-14 The loop makes the code repeat infinitely.
FIGURE 1-15 The program displays a compliment when you enter a, b or c.
FIGURE 1-16 The finished program
Chapter 2
FIGURE 2-1 An example of a drawing made with Turtle Graphics.
FIGURE 2-2 The turtle after moving 100 steps
FIGURE 2-3 The turtle drew a rectangle.
FIGURE 2-4 A zigzag drawn with the turtle
FIGURE 2-5 The updated code with a new value for the
angle
variable
FIGURE 2-6 A spiral
FIGURE 2-7 An even better spiral, made with a
while
loop
FIGURE 2-8 A circle drawn with the turtle
FIGURE 2-9 A seven-sided shape drawn with the
shapes.py
program
FIGURE 2-10 The turtle has drawn a shape with a fill colour of blue.
FIGURE 2-11 The program has drawn a couple of shapes.
FIGURE 2-12 Shapes created using the functions and the example code
FIGURE 2-13 A randomly generated picture
Chapter 3
FIGURE 3-1 A simple GUI made in
Tkinter
FIGURE 3-2 The window with a button and the output from the click shown in the Python shell.
FIGURE 3-3 When the button is clicked, the text on the button changes.
FIGURE 3-4 The button after it has been clicked seven times
FIGURE 3-5 The text box after the button has been pressed
FIGURE 3-6 The text box, reversed
FIGURE 3-7 The password field
FIGURE 3-8 The finished random sentence generator
FIGURE 3-9 The finished guessing game
Chapter 4
FIGURE 4-1 A GUI with a slider
FIGURE 4-2 A
Tkinter
slider
FIGURE 4-3 A
Tkinter
canvas
FIGURE 4-4 The colour picker GUI
FIGURE 4-5 The colour picker with the text box
FIGURE 4-6 The click speed game
Chapter 5
FIGURE 5-1 Your first
PyGame
program
FIGURE 5-2 A
PyGame
rectangle
FIGURE 5-3 A gradient of rectangles
FIGURE 5-4 The colour grid
FIGURE 5-5 A single frame of the wobbling circle animation
FIGURE 5-6 A saved image from the colour grid program
Chapter 6
FIGURE 6-1 “Hello” is printed to the Python shell every time the w key is pressed.
FIGURE 6-2 The finished game
FIGURE 6-3 The small red dot will move upwards when the w key is pressed.
FIGURE 6-4 The small red dot will now move in different directions when you press the w, a, s and d keys.
FIGURE 6-5 The player is now able to move off the edge of the screen and reappear on the other side.
FIGURE 6-6 A blue ball is now generated at a random position in the game.
FIGURE 6-7 When the player collides with the ball there is a small explosion.
FIGURE 6-8 The goal
FIGURE 6-9 The points are displayed in the top-left corner of the screen.
FIGURE 6-10 The program closes after 60 seconds, and the points are displayed in the Python shell.
Chapter 7
FIGURE 7-1 Looking at the Python shell, you can see where the mouse button has been clicked.
FIGURE 7-2 When the button in the middle of the screen is pressed, the background will change to black.
FIGURE 7-3 The more you click the target, the more erratically it will move.
FIGURE 7-4 Every time you click on the window there is a new explosion.
FIGURE 7-5 Clicking anywhere on the window adds a new point to the mesh.
FIGURE 7-6 The trails follow the mouse all over the window.
FIGURE 7-7 Lines with and without anti-aliasing. Notice the extra pixels on the line with anti-aliasing.
Chapter 8
FIGURE 8-1 The image loads into the window. You can see the black background due to transparency around the image of the cat.
FIGURE 8-2 The cat is now wearing a hat. The image has a transparent background so that you can see the cat’s face.
FIGURE 8-3 The image of the cat appears randomly all over the window.
FIGURE 8-4 The cat now has a moustache.
FIGURE 8-5 You can make the sprite walk down the window by using the s key.
FIGURE 8-6 The sprite can now walk in all four directions.
FIGURE 8-7 Pressing the space bar will now teleport the sprite to a random location.
Chapter 9
FIGURE 9-1 The completed code. Sadly, I can’t take a screen print of the noise it makes!
FIGURE 9-2 Running this program will play a loop of sounds. Every fourth sound will be different from the other three.
FIGURE 9-3 Pressing the a and s keys will now play sounds.
FIGURE 9-4 The code to play music in
PyGame
FIGURE 9-5 When the music plays, the volume goes up and down in a wave.
FIGURE 9-6 The code for a superb basic music player, complete with control button and volume slider, which plays a single song on a loop.
FIGURE 9-7 The sprite program now features sounds and music.
Chapter 10
FIGURE 10-1 In the finished game, two players compete to collect coins.
FIGURE 10-2 All the player sprites used in the game
FIGURE 10-3 The game’s background
FIGURE 10-4 The players collect coins and gain a point for every coin they collect.
FIGURE 10-5 The finished game
Appendix
FIGURE A-1 Installing
PyGame
on Windows 8
FIGURE A-2
PyGame
ready to be used on Windows 8
FIGURE A-3 Installing
PyGame
on Windows 7
FIGURE A-4 Python packages for Mac
FIGURE A-5 Installing
PyGame
on Mac
FIGURE A-6
PyGame
ready to be used on Mac
FIGURE A-7 Installing
PyGame
on Linux
Guide
Cover
Table of Contents
Begin Reading
Pages
iii
iv
v
vi
vii
viii
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
34
35
36
37
38
39
40
41
43
44
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
105
106
107
108
109
110
111
112
113
114
116
117
118
119
120
121
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
220
221
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
243
244
245
246
247
248
249
250
251
252
253
254
255
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset