Professional Documents
Culture Documents
Textbook Python For Kids A Playful Introduction To Programming 1St Edition Briggs Ebook All Chapter PDF
Textbook Python For Kids A Playful Introduction To Programming 1St Edition Briggs Ebook All Chapter PDF
https://textbookfull.com/product/javascript-for-kids-a-playful-
introduction-to-programming-1st-edition-nick-morgan/
https://textbookfull.com/product/impractical-python-projects-
playful-programming-activities-to-make-you-smarter-1st-edition-
lee-vaughan/
https://textbookfull.com/product/impractical-python-projects-
playful-programming-activities-to-make-you-smarter-1st-edition-
lee-vaughan-2/
https://textbookfull.com/product/programming-for-computations-
python-a-gentle-introduction-to-numerical-simulations-with-
python-3-6-svein-linge/
Teach Your Kids to Code A Parent Friendly Guide to
Python Programming 1st Edition Bryson Payne
https://textbookfull.com/product/teach-your-kids-to-code-a-
parent-friendly-guide-to-python-programming-1st-edition-bryson-
payne/
https://textbookfull.com/product/bite-size-python-an-
introduction-to-python-programming-1st-edition-april-speight/
https://textbookfull.com/product/hello-raspberry-pi-python-
programming-for-kids-and-other-beginners-1st-edition-ryan-heitz/
https://textbookfull.com/product/introduction-to-scientific-
programming-with-python-joakim-sundnes/
https://textbookfull.com/product/a-concise-introduction-to-
programming-in-python-second-edition-mark-j-johnson/
playPython_fullcvr_9.pdf 1 9/25/2013 5:46:14 PM
Fo r k i d s ag e d 1 0 + ( a n d th e i r pa r e nts)
PYTHON
Re
r Ea
AlL p
Prro
oggr
raam
mmm ii n
ngg ..
Re
r Ea
AlL e
EaAs
S y.
Y.
P
PY
FO R K I D S
I llu str ati o n s by M i r an Li povaca
YT
TH O N FO R K I D S
Python is a powerful, expressive program- As you strike out on your programming
ming language that’s easy to learn and fun to adventure, you’ll learn how to:
use! But books about learning to program in Use fundamental data structures like lists,
Python can be kind of dull, gray, and boring, tuples, and maps
and that’s no fun for anyone.
Organize and reuse your code with func-
A P l ay f u l I n t r o d u c t i o n to P r o g r a m m i n g
Python for Kids brings Python to life and
tions and modules
brings you (and your parents) into the world of
programming. The ever-patient Jason R. Briggs Use control structures like loops and
will guide you through the basics as you experi- conditional statements Jason R. Briggs
C ment with unique (and often hilarious) example Draw shapes and patterns with Python’s
M
programs that feature ravenous monsters, secret turtle module
Y
agents, thieving ravens, and more. New terms Create games, animations, and other
CM
are defined; code is colored, dissected, and graphical wonders with tkinter
explained; and quirky, full-color illustrations
MY
Why should serious adults have all the fun?
keep things on the lighter side.
CY Python for Kids is your ticket into the amaz-
Chapters end with programming puzzles
ing world of computer programming.
designed to stretch your brain and strengthen
CMY
you’ll have programmed two complete games: Jason R. Briggs has been a programmer since
a clone of the famous Pong and “Mr. Stick Man the age of eight, when he first learned BASIC on
Races for the Exit”— a platform game with a Radio Shack TRS-80. He has written software
jumps, animation, and much more. professionally as a developer and systems archi-
tect and served as Contributing Editor for Java
Python runs on Windows, Developer’s Journal. His articles have appeared
OS X, Linux, OLPC Laptops,
in JavaWorld, ONJava, and ONLamp. Python
and even Raspberry pi!
for Kids is his first book.
Briggs
$34.95 ($36.95 CDN)
PROGRAMMING LANGUAGES/PYTHON
SHELVE IN:
w w w.nostarch.com
T H E F I N E ST I N
G E E K E N T E RTA I N M E N T ™
Praise for
Python for Kids
B y Ja s o n R . B r i g g s
San Francisco
PYTHON FOR KIDS. Copyright © 2013 by Jason R. Briggs.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any
means, electronic or mechanical, including photocopying, recording, or by any information storage
or retrieval system, without the prior written permission of the copyright owner and the publisher.
Printed in Korea
Fourth printing
16 15 14 13 456789
ISBN-10: 1-59327-407-6
ISBN-13: 978-1-59327-407-8
For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 415.863.9900; fax: 415.863.9950; info@nostarch.com; http://www.nostarch.com/
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc.
Other product and company names mentioned herein may be the trademarks of their respective
owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we
are using the names only in an editorial fashion and to the benefit of the trademark owner, with
no intention of infringement of the trademark.
The information in this book is distributed on an “As Is” basis, without warranty. While every
precaution has been taken in the preparation of this work, neither the author nor No Starch Press,
Inc. shall have any liability to any person or entity with respect to any loss or damage caused or
alleged to be caused directly or indirectly by the information contained in it.
Acknowledgments xvii
Introduction xix
Why Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx
How to Learn to Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx
Who Should Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
What’s in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii
The Companion Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Have Fun! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
2
Calculations and Variables 15
Calculating with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Python Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
The Order of Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Variables Are Like Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3
Strings, Lists, Tuples, and Maps 25
Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Creating Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Handling Problems with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Embedding Values in Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Multiplying Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Lists Are More Powerful Than Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Adding Items to a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Removing Items from a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
List Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Python Maps Won’t Help You Find Your Way . . . . . . . . . . . . . . . . . . . . . . . 39
What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Programming Puzzles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
#1: Favorites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
#2: Counting Combatants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
#3: Greetings! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4
Drawing with Turtles 43
Using Python’s turtle Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Creating a Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Moving the Turtle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Programming Puzzles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
#1: A Rectangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
#2: A Triangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
#3: A Box Without Corners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
5
Asking Questions with if and else 53
if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
A Block Is a Group of Programming Statements . . . . . . . . . . . . . . . 54
Conditions Help Us Compare Things . . . . . . . . . . . . . . . . . . . . . . . . 57
if-then-else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
if and elif Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Combining Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Variables with No Value—None . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
The Difference Between Strings and Numbers . . . . . . . . . . . . . . . . . . . . . . 62
What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
viii Contents in Detail
Programming Puzzles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
#1: Are You Rich? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
#2: Twinkies! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
#3: Just the Right Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
#4: I Can Fight Those Ninjas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
6
Going Loopy 67
Using for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
While We’re Talking About Looping... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Programming Puzzles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
#1: The Hello Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
#2: Even Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
#3: My Five Favorite Ingredients . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
#4: Your Weight on the Moon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
7
Recycling Your Code with Functions
and Modules 81
Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Parts of a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Variables and Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
Using Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Programming Puzzles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
#1: Basic Moon Weight Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
#2: Moon Weight Function and Years . . . . . . . . . . . . . . . . . . . . . . . . 90
#3: Moon Weight Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
8
How to Use Classes and Objects 93
Breaking Things into Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Children and Parents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Adding Objects to Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Defining Functions of Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Adding Class Characteristics as F unctions . . . . . . . . . . . . . . . . . . . 97
Why Use Classes and Objects? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Objects and Classes in Pictures . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Other Useful Features of Objects and Classes . . . . . . . . . . . . . . . . . . . . . . 102
Inherited Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Functions Calling Other Functions . . . . . . . . . . . . . . . . . . . . . . . . . 104
Contents in Detail ix
Initializing an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Programming Puzzles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
#1: The Giraffe Shuffle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
#2: Turtle Pitchfork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
9
Python’s Built-in Functions 109
Using Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
The abs Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
The bool Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
The dir Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
The eval Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
The exec Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
The float Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
The int Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
The len Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
The max and min Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
The range Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
The sum Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Working with Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Creating a Test File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Opening a File in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Writing to Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Programming Puzzles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
#1: Mystery Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
#2: A Hidden Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
#3: Copying a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
10
Useful Python Modules 129
Making Copies with the copy Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Keeping Track of Keywords with the keyword Module . . . . . . . . . . . . . . . 133
Getting Random Numbers with the random Module . . . . . . . . . . . . . . . . 133
Using randint to Pick a R andom Number . . . . . . . . . . . . . . . . . . . 134
Using choice to Pick a Random Item from a List . . . . . . . . . . . . . . 135
Using shuffle to Shuffle a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Controlling the Shell with the sys Module . . . . . . . . . . . . . . . . . . . . . . . . . 136
Exiting the Shell with the exit function . . . . . . . . . . . . . . . . . . . . . 136
Reading with the stdin Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Writing with the stdout Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Which Version of Python Am I Using? . . . . . . . . . . . . . . . . . . . . . . 138
x Contents in Detail
Doing Time with the time Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Converting a Date with asctime . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Getting the Date and Time with l ocaltime . . . . . . . . . . . . . . . . . . . 140
Taking Some Time Off with sleep . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Using the pickle Module to Save Information . . . . . . . . . . . . . . . . . . . . . . 142
What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Programming Puzzles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
#1: Copied Cars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
#2: Pickled Favorites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
11
More Turtle Graphics 145
Starting with the Basic Square . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Drawing Stars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Drawing a Car . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Coloring Things In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
A Function to Draw a Filled Circle . . . . . . . . . . . . . . . . . . . . . . . . . 153
Creating Pure Black and White . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
A Square-Drawing Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Drawing Filled Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Drawing Filled Stars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Programming Puzzles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
#1: Drawing an Octagon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
#2: Drawing a Filled Octagon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
#3: Another Star-Drawing Function . . . . . . . . . . . . . . . . . . . . . . . . 161
12
Using tkinter for Better Graphics 163
Creating a Clickable Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Using Named Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Creating a Canvas for Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Drawing Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Drawing Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Drawing a Lot of Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Setting the Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Drawing Arcs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Drawing Polygons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Displaying Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Displaying Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Creating Basic Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Making an Object React to Something . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
More Ways to Use the Identifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Contents in Detail xi
What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Programming Puzzles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
#1: Fill the Screen with Triangles . . . . . . . . . . . . . . . . . . . . . . . . . . 190
#2: The Moving Triangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
#3: The Moving Photo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
14
Finishing Your First Game: Bounce! 205
Adding the Paddle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Making the Paddle Move . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Finding Out When the Ball Hits the Paddle . . . . . . . . . . . . . . . . . 209
Adding an Element of Chance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Programming Puzzles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
#1: Delay the Game Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
#2: A Proper “Game Over” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
#3: Accelerate the Ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
#4: Record the Player’s Score . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
xii Contents in Detail
Creating the Game Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Preparing a Transparent Image . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Drawing Mr. Stick Man . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Drawing the Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Drawing the Door . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Drawing the Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Transparency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
16
Developing the Mr. Stick Man Game 233
Creating the Game Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Setting the Window Title and C reating the Canvas . . . . . . . . . . . 234
Finishing the __init__ Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Creating the mainloop Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Creating the Coords Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Checking for Collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Sprites Colliding Horizontally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Sprites Colliding Vertically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Putting It All Together: Our Final Collision-Detection Code . . . . 242
Creating the Sprite Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Adding the Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Adding a Platform Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Adding a Bunch of P latforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Programming Puzzles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
#1: Checkerboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
#2: Two-Image Checkerboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
#3: Bookshelf and Lamp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
17
Creating Mr. Stick Man 251
Initializing the Stick Figure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Loading the Stick F igure Images . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Setting Up Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Binding to Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Turning the Stick Figure Left and Right . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Making the Stick Figure Jump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
What We Have So Far . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Contents in Detail xiii
18
Completing the Mr. Stick Man Game 259
Animating the Stick Figure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Creating the Animate Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Getting the Stick Figure’s Position . . . . . . . . . . . . . . . . . . . . . . . . . 263
Making the Stick Figure Move . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Testing Our Stick Figure Sprite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
The Door! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Creating the DoorSprite Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Detecting the Door . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Adding the Door Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
The Final Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Programming Puzzles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
#1: “You Win!” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
#2: Animating the Door . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
#3: Moving Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Afterword
Where to Go from Here 285
Games and Graphics Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
PyGame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Programming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
C/C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
Objective-C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Final Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Appendix
Python Keywords 293
Glossary 307
Index 313
xiv Contents in Detail
About the Author
Jason R. Briggs has been a programmer since the age of eight,
when he first learned BASIC on a Radio Shack TRS-80. He
has written software professionally as a developer and systems
architect and served as Contributing Editor for Java Developer’s
Journal. His articles have appeared in JavaWorld, ONJava, and
ONLamp. Python for Kids is his first book.
Jason can be reached at http://jasonrbriggs.com/ or by email
at mail@jasonrbriggs.com.
Why Python?
Python is an easy-to-learn programming language that has some
really useful features for a beginning programmer. The code is quite
easy to read when compared to other programming languages, and
it has an interactive shell into which you can enter your programs
and see them run. In addition to its simple language structure and
an interactive shell with which to experiment, Python has some
features that greatly augment the learning process and allow you
to put together simple animations for creating your own games.
One is the turtle module, inspired by Turtle graphics (used by the
Logo programming language back in the 1960s) and designed for
educational use. Another is the tkinter module, an interface for the
Tk GUI toolkit, which provides a simple way to create programs
with slightly more advanced graphics and animation.
xx Introduction
When you find something frustrating or too challenging, here
are some things that I find helpful:
1. Break a problem down into smaller pieces. Try to understand
what a small piece of code is doing, or think about only a small
part of a difficult idea (focus on a small piece of code rather
than trying to understand the whole thing at once).
2. If that still doesn’t help, sometimes it’s best to just leave it
alone for a while. Sleep on it, and come back to it another day.
This is a good way to solve many problems, and it can be par-
ticularly helpful for computer programmers.
Introduction xxi
would use in other applications), and an editor window where you
can save your code for later use, which means IDLE works as both
an interactive environment for experimentation and something
a bit like a text editor. The examples will work just as well with
the standard console and a regular text editor, but IDLE’s syntax
highlighting and slightly more user-friendly environment can aid
understanding, so the very first chapter shows you how to set it up.
xxii Introduction
In the Afterword, we wrap up with a look at PyGame and
some other popular programming languages.
Finally, in the Appendix, you’ll learn about Python’s key-
words in detail, and in the Glossary, you’ll find definitions of the
programming terms used throughout this book.
Have Fun!
Remember as you work your way through this book that pro-
gramming can be fun. Don’t think of this as work. Think of
programming as a way to create some fun games or applications
that you can share with your friends or others.
Learning to program is a wonderful mental exercise and the
results can be very rewarding. But most of all, whatever you do,
have fun!
Introduction xxiii
Part I
Learning to
Program
1
Not All Snakes Slither
Degassing Units
Since mustard gas has a greatly delayed action it was found that
if men who had been exposed to it could be given a thorough bath
with soap and water within a half hour or even a full hour, the
mustard gas burns would be prevented or very greatly reduced in
severity. Accordingly degassing units were developed consisting
essentially of a 5 ton truck with a 1200 gallon water tank, fitted with
an instantaneous heater and piping to connect it to portable shower
baths. Another truck was kept loaded with extra suits of
underclothing and uniforms. These degassing units were to be
provided at the rate of two per division. Then, in the event of a
mustard gas attack anywhere in the division, one of these units
would be rushed to that vicinity and the men brought out of the line
and given a bath and change of clothing as soon as possible. At the
same time they were given a drink of bicarbonate of soda water and
their eyes, ears, mouth and nasal passages washed with the same.
Alarm Signals
Numerous, indeed, were the devices invented at one time or
another with which to sound gas alarms. The English early devised
the Strombos horn, a sort of trumpet operated by compressed air
contained in cylinders carried for that purpose. Its note is penetrating
and can be heard, under good conditions, for three or four miles.
When cloud gas attacks, which occurred only at intervals of two to
four months, were the only gas attacks to be feared, it was easy
enough to provide for alarm signals by methods as cumbersome and
as technically delicate as the Strombos horn.
With the advent of shell gas in general, and mustard gas in
particular, the number of gas attacks increased enormously. This
made it not only impossible, but inadvisable also, to furnish sufficient
Strombos horns for all gas alarms, as gas shell attacks are
comparatively local. In such cases, if the Strombos horn is used to
give warning, it causes troops who are long distances out of the area
attacked to take precautions against gas with consequent
interference with their work or fighting.
To meet these local conditions metal shell cases were first hung
up and the alarm sounded on them. Later steel triangles were used
in the same way. At a still later date the large policeman’s rattle, well
known in Europe, was adopted and following that the Klaxon horn.
As the warfare of movement developed the portability of alarm
apparatus became of prime importance. For those reasons the
Klaxon horn and the police rattle were having a race for popularity
when the Armistice was signed.
A recent gas alarm invention that gives promise is a small siren-
like whistle fired into the air like a bomb. It is fitted with a parachute
which keeps it from falling too rapidly when the bomb explodes and
sets it free. Its tone is said to be very penetrating and to be quite
effective over an ample area. Since future gas alarm signals must be
efficient and must be portable, the lighter and more compact they
can be made the better; hence the desirability of parachute whistles
or similar small handy alarms.
Summing Up
In summing up then, it is noted that there are several important
things in defense against gas. First, the mask which protects the
eyes and the lungs. Second, the training that teaches the man how
to utilize to best advantage the means of protection at his disposal,
whether he be alone or among others. Third, protective clothing that
protects hands and feet and the skin in general. Fourth, a knowledge
of gases and their tactical use that will enable commanders,
whenever possible, to move men out of gas infected areas. Fifth,
training in the offensive use of gas, as well as in defensive methods,
to teach the man that gas has no uncanny power and that it is simply
one element of war that must be reckoned with, thus preventing
stampedes when there is really no danger.
While these are the salient points in defense against gas, above
them and beyond them lies the vigorous offensive use of gas. This
involves not only the research, development and manufacture of
necessary gases in peace time, but also the necessary training to
enable our nation to hurl upon the enemy on the field of battle
chemical warfare materials in quantities he cannot hope to attain.
CHAPTER XXV
PEACE TIME USES OF GAS
“Peace hath her victories no less renowned than war.” Thus runs
the old proverb. In ancient times war profited by peace far more than
peace profited by war if indeed the latter ever actually occurred. The
implements developed for the chase in peace became the weapons
of war. This was true of David’s sling-shot, of the spear and of the
bow. Even powder itself was probably intended and used for scores
of years for celebrations and other peaceful events.
The World War reversed this story, especially in its later phases.
The greater part of the war was fought with implements and
machines prepared in peace either for war or for peaceful purposes.
Such implements were the aeroplane, submarine, truck, automobile
and gasoline motors in general. The first gas attack, which was
simply an adaptation of the peacetime use of the chemical chlorine,
inaugurated the change. Gas was so new and instantly recognized
as so powerful that the best brains in research among all the first
class powers were put to work to develop other gases and other
means of projecting them upon the enemy. The result was that in the
short space of three and one-half years a number of substances
were discovered, or experimented with anew, that are aiding today
and will continue to aid in the future in the peaceful life of every
nation.
Chlorine is even more valuable than ever as a disinfectant and
water purifier. It is the greatest bleaching material in the world, and
has innumerable other uses in the laboratory. Chloropicrin, cyanogen
chloride and cyanogen bromide are found to be very well adapted to
the killing of weevil and other similar insect destroyers of grain.
Hydrocyanic acid gas is the greatest destroyer today of insect pests
that otherwise would ruin the beautiful orange and lemon groves of
California and the South.
Fig. 120.