Top Banner
PYTHON FOR KIDS PYTHON FOR KIDS A Playful Introduction to Programming Jason R. Briggs
9

FOR KIDSFOR KIDS - No Starch Press · Beginning Your first Game: Bounce! 193 Whack the Bouncing Ball ... Finding Out When the Ball Hits the Paddle ... Java ...

May 13, 2018

Download

Documents

trinhthuy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: FOR KIDSFOR KIDS - No Starch Press · Beginning Your first Game: Bounce! 193 Whack the Bouncing Ball ... Finding Out When the Ball Hits the Paddle ... Java ...

PYTHON FOR KIDSPYTHON FOR KIDS

A Playful Introduction to Programming

J a s o n R . B r i g g s

Page 2: FOR KIDSFOR KIDS - No Starch Press · Beginning Your first Game: Bounce! 193 Whack the Bouncing Ball ... Finding Out When the Ball Hits the Paddle ... Java ...

Contents in DetaiL

about the author and technical reviewers xv

acknowledgments xvii

introduction xixWhy Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxHow to Learn to Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxWho Should Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiWhat’s in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiiThe Companion Website. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiiiHave Fun! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii

Part i: Learning to Program

1 not all snakes slither 3A Few Words About Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4Installing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Installing Python on Windows 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5Installing Python on Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Installing Python on Ubuntu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Once You’ve Installed Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Saving Your Python Programs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

2 Calculations and Variables 15Calculating with Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Python Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17The Order of Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

Variables Are Like Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Using Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Python for Kids©2012, Jason R. Briggs

Page 3: FOR KIDSFOR KIDS - No Starch Press · Beginning Your first Game: Bounce! 193 Whack the Bouncing Ball ... Finding Out When the Ball Hits the Paddle ... Java ...

viii Contents in Detail

3 strings, Lists, tuples, and Maps 25Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

Creating Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26Handling Problems with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27Embedding Values in Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30Multiplying Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

Lists Are More Powerful Than Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32Adding Items to a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Removing Items from a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35List Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38Python Maps Won’t Help You Find Your Way . . . . . . . . . . . . . . . . . . . . . . . 39What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41Programming Puzzles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

#1: Favorites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41#2: Counting Combatants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42#3: Greetings! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

4 Drawing with turtles 43Using Python’s turtle Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

Creating a Canvas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Moving the Turtle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51Programming Puzzles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

#1: A Rectangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51#2: A Triangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51#3: A Box Without Corners. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

5 asking Questions with if and else 53if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

A Block Is a Group of Programming Statements . . . . . . . . . . . . . . . 54Conditions Help Us Compare Things. . . . . . . . . . . . . . . . . . . . . . . . . 57

if-then-else Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58if and elif Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Combining Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Variables with No Value—None . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61The Difference Between Strings and Numbers. . . . . . . . . . . . . . . . . . . . . . . 62What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

Python for Kids©2012, Jason R. Briggs

Page 4: FOR KIDSFOR KIDS - No Starch Press · Beginning Your first Game: Bounce! 193 Whack the Bouncing Ball ... Finding Out When the Ball Hits the Paddle ... Java ...

Contents in Detail ix

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 67Using for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68While We’re Talking About Looping... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78Programming 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 81Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

Parts of a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83Variables and Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

Using Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89Programming 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 93Breaking Things into Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

Children and Parents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95Adding Objects to Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96Defining Functions of Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97Adding Class Characteristics as Functions . . . . . . . . . . . . . . . . . . . . 97Why Use Classes and Objects? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99Objects and Classes in Pictures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

Other Useful Features of Objects and Classes . . . . . . . . . . . . . . . . . . . . . . 102Inherited Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103Functions Calling Other Functions . . . . . . . . . . . . . . . . . . . . . . . . . 104

Python for Kids©2012, Jason R. Briggs

Page 5: FOR KIDSFOR KIDS - No Starch Press · Beginning Your first Game: Bounce! 193 Whack the Bouncing Ball ... Finding Out When the Ball Hits the Paddle ... Java ...

x Contents in Detail

Initializing an Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107Programming Puzzles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

#1: The Giraffe Shuffle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107#2: Turtle Pitchfork. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108

9 Python’s Built-in functions 109Using Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

The abs Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110The bool Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111The dir Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113The eval Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114The exec Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116The float Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116The int Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117The len Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118The max and min Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119The range Function. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121The sum Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

Working with Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123Creating a Test File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123Opening a File in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Writing to Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127Programming Puzzles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

#1: Mystery Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127#2: A Hidden Message. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128#3: Copying a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128

10 Useful Python Modules 129Making Copies with the copy Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130Keeping Track of Keywords with the keyword Module . . . . . . . . . . . . . . . 133Getting Random Numbers with the random Module . . . . . . . . . . . . . . . . . 133

Using randint to Pick a Random Number . . . . . . . . . . . . . . . . . . . . 134Using choice to Pick a Random Item from a List . . . . . . . . . . . . . . 135Using shuffle to Shuffle a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

Controlling the Shell with the sys Module . . . . . . . . . . . . . . . . . . . . . . . . . 136Exiting the Shell with the exit function. . . . . . . . . . . . . . . . . . . . . . 136Reading with the stdin Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137Writing with the stdout Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138Which Version of Python Am I Using?. . . . . . . . . . . . . . . . . . . . . . . 138

Python for Kids©2012, Jason R. Briggs

Page 6: FOR KIDSFOR KIDS - No Starch Press · Beginning Your first Game: Bounce! 193 Whack the Bouncing Ball ... Finding Out When the Ball Hits the Paddle ... Java ...

Contents in Detail xi

Doing Time with the time Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138Converting a Date with asctime. . . . . . . . . . . . . . . . . . . . . . . . . . . . 140Getting the Date and Time with localtime . . . . . . . . . . . . . . . . . . . 140Taking Some Time Off with sleep . . . . . . . . . . . . . . . . . . . . . . . . . . 141

Using the pickle Module to Save Information . . . . . . . . . . . . . . . . . . . . . . 142What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144Programming Puzzles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

#1: Copied Cars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144#2: Pickled Favorites. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

11 More turtle Graphics 145Starting with the Basic Square . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146Drawing Stars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147Drawing a Car. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151Coloring Things In . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152

A Function to Draw a Filled Circle . . . . . . . . . . . . . . . . . . . . . . . . . 153Creating Pure Black and White . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

A Square-Drawing Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155Drawing Filled Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157Drawing Filled Stars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160Programming 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 163Creating a Clickable Button. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165Using Named Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167Creating a Canvas for Drawing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167Drawing Lines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168Drawing Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

Drawing a Lot of Rectangles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172Setting the Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174

Drawing Arcs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177Drawing Polygons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179Displaying Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180Displaying Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181Creating Basic Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183Making an Object React to Something . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186More Ways to Use the Identifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188

Python for Kids©2012, Jason R. Briggs

Page 7: FOR KIDSFOR KIDS - No Starch Press · Beginning Your first Game: Bounce! 193 Whack the Bouncing Ball ... Finding Out When the Ball Hits the Paddle ... Java ...

xii Contents in Detail

What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190Programming Puzzles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

#1: Fill the Screen with Triangles . . . . . . . . . . . . . . . . . . . . . . . . . . 190#2: The Moving Triangle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190#3: The Moving Photo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190

Part ii: Bounce!

13 Beginning Your first Game: Bounce! 193Whack the Bouncing Ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194Creating the Game Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194Creating the Ball Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196Adding Some Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

Making the Ball Move. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198Making the Ball Bounce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200Changing the Ball’s Starting Direction . . . . . . . . . . . . . . . . . . . . . . 202

What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204

14 finishing Your first Game: Bounce! 205Adding the Paddle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206

Making the Paddle Move . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207Finding Out When the Ball Hits the Paddle . . . . . . . . . . . . . . . . . . 209

Adding an Element of Chance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216Programming 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

Part iii: Mr. stick Man races for the exit

15 Creating Graphics for the Mr. stick Man Game 221Mr. Stick Man Game Plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222Getting GIMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222

Python for Kids©2012, Jason R. Briggs

Page 8: FOR KIDSFOR KIDS - No Starch Press · Beginning Your first Game: Bounce! 193 Whack the Bouncing Ball ... Finding Out When the Ball Hits the Paddle ... Java ...

Contents in Detail xiii

Creating the Game Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224Preparing a Transparent Image. . . . . . . . . . . . . . . . . . . . . . . . . . . . 224Drawing Mr. Stick Man . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225Drawing the Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227Drawing the Door . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228Drawing the Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229Transparency. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231

16 Developing the Mr. stick Man Game 233Creating the Game Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234

Setting the Window Title and Creating the Canvas . . . . . . . . . . . . 234Finishing the __init__ Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235Creating the mainloop Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 236

Creating the Coords Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238Checking for Collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

Sprites Colliding Horizontally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239Sprites Colliding Vertically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241Putting It All Together: Our Final Collision-Detection Code. . . . . 242

Creating the Sprite Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244Adding the Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

Adding a Platform Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246Adding a Bunch of Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247

What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249Programming Puzzles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249

#1: Checkerboard. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249#2: Two-Image Checkerboard. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250#3: Bookshelf and Lamp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250

17 Creating Mr. stick Man 251Initializing the Stick Figure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252

Loading the Stick Figure Images . . . . . . . . . . . . . . . . . . . . . . . . . . . 252Setting Up Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253Binding to Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255

Turning the Stick Figure Left and Right. . . . . . . . . . . . . . . . . . . . . . . . . . . 255Making the Stick Figure Jump . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256What We Have So Far . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258

Python for Kids©2012, Jason R. Briggs

Page 9: FOR KIDSFOR KIDS - No Starch Press · Beginning Your first Game: Bounce! 193 Whack the Bouncing Ball ... Finding Out When the Ball Hits the Paddle ... Java ...

xiv Contents in Detail

18 Completing the Mr. stick Man Game 259Animating the Stick Figure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260

Creating the animate Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260Getting the Stick Figure’s Position . . . . . . . . . . . . . . . . . . . . . . . . . 263Making the Stick Figure Move . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265

Testing Our Stick Figure Sprite. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273The Door! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273

Creating the DoorSprite Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274Detecting the Door . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275Adding the Door Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275

The Final Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282Programming Puzzles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

#1: “You Win!” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283#2: Animating the Door. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283#3: Moving Platforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

afterword Where to Go from Here 285Games and Graphics Programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286

PyGame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286Programming Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288

Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288C/C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288C# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289Objective-C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290Perl. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290Ruby . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291

Final Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291

appendix Python Keywords 293

Glossary 307

index 313

Python for Kids©2012, Jason R. Briggs