Teach Your Kids to Code A Parent-Friendly Guide to Python Programming (Bryson Payne) (Z-Library)

Author: Bryson Payne

商业

Teach Your Kids to Code is a parent's and teacher's guide to teaching kids basic programming and problem solving using Python, the powerful language used in college courses and by tech companies like Google and IBM. Step-by-step explanations will have kids learning computational thinking right away, while visual and game-oriented examples hold their attention. Friendly introductions to fundamental programming concepts such as variables, loops, and functions will help even the youngest programmers build the skills they need to make their own cool games and applications. Whether you've been coding for years or have never programmed anything at all, Teach Your Kids to Code will help you show your young programmer how to: Explore geometry by drawing colorful shapes with Turtle graphics Write programs to encode and decode messages, play Rock-Paper-Scissors, and calculate how tall someone is in Ping-Pong balls Create fun, playable games like War, Yahtzee, and Pong Add interactivity, animation, and sound to their apps Teach Your Kids to Code is the perfect companion to any introductory programming class or after-school meet-up, or simply your educational efforts at home. Spend some fun, productive afternoons at the computer with your kids—you can all learn something!

📄 File Format: PDF
💾 File Size: 14.7 MB
5
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
Teach Your Kids to Code is a parent’s and teacher’s guide to teaching kids basic programming and problem solving using Python, the powerful language used in college courses and by tech companies like Google and IBM. Step-by-step explanations will have kids learning computational thinking right away, while visual and game-oriented examples hold their attention. Friendly introductions to fundamental programming concepts such as variables, loops, and functions will help even the youngest programmers build the skills they need to make their own cool games and applications. SHELVE IN : PROGRAM M ING LANGUAGES/PYTHON www.nostarch.com TH E F I N EST I N G E E K E NTE RTA I N M E NT™ For kids aged 9+ (and their parents) $29.95 ($34.95 CDN) Programming so easy a Parent can do it! p Whether you’ve been coding for years or have never programmed anything at all, Teach Your Kids to Code will help you show your young programmer how to:  Explore geometry by drawing colorful shapes with Turtle graphics  Teach Your Kids to Code is the perfect com- panion to any introductory programming class or after-school meet-up, or simply your educational efforts at home. Spend some fun, productive afternoons at the computer with  Add interactivity, animation, and sound to their apps your kids—you can all learn something! ABOUT THE AUTHOR Dr. Bryson Payne has taught computer science at the University of North Georgia for more than 15 years. He has also taught middle school math and programming, and continues to work with K–12 schools to promote technology education. Write programs to encode and decode messages, play Rock-Paper-Scissors, and calculate how tall someone is in Ping-Pong balls  Create fun, playable games like War, Yahtzee, and Pong P A Y N E T e a c h Y o u r K id s t o C o d e T e a c h Y o u r K id s t o C o d e A Parent-friendly Guide to Python Programming B r y s o n P a y n e Teach Your Kids To Codet
📄 Page 2
Advance Praise for Teach Your Kids to Code “The text is clear, the graphics are engaging, and the apps are awesome. This is the programming guide for parents and kids to enjoy together.” —Aaron Walker, Cybersecurity Expert, NASA “The energy and excitement Bryson brings to teaching is captured perfectly in Teach Your Kids to Code, with colorful, captivating games and graphics that help develop real-world skills.” —Bindy Auvermann, Executive Director, Next Generation Youth Development, Inc. “Provides the building blocks of a great future in the rapidly changing world of technology.” —JoAnne Taylor, former Vice President, Global Telecommunications, IBM “The concepts in Teach Your Kids to Code can help any young person enhance their college prospects and expand their career opportunities, and Dr. Payne presents these skills through fun, challenging games and apps.” —Dr. Raj Sunderraman, Department Chair of Computer Science, Georgia State University “Every child on the planet should have this book, and so should every parent.” —James E. Daniel, Jr., Founder, App Studios, LLC “An innovative, motivating guide . . . Builds skills that can last a lifetime.” —Dr. Steven Burrell, Vice President for Information Technology & CIO, Georgia Southern University “The kind of book I wish I’d had as a kid.” —Scott Hand, Software Engineer, CareerBuilder
📄 Page 3
“Dr. Bryson Payne is a computer scientist and professor of the highest caliber, and with Teach Your Kids to Code, he brings the power of computers within easy reach for readers young and old.” —Dr. Antonio Sanz Montemayor, Informatics Professor, Universidad Rey Juan Carlos, Spain “A brilliant combination of engaging, imaginative apps and valuable, lifelong skills.” —Ted Cunningham, author of The Power of Home “Teach Your Kids to Code and the logical thinking it introduces will help build the next generation of technology leaders.” —N. Dean Meyer, author and executive coach “This book can jump-start your child’s success in a high-tech world.” —Ken Coleman, leadership author and former radio host of The Ken Coleman Show “Dr. Payne set us on the path that led us to our dream jobs! With Teach Your Kids to Code, he’s providing parents and teachers everywhere the chance to do the same for the next generation of creative problem-solvers.” —Shah and Susan Rahman, Riot Games “Bryson helps people improve their lives with technology. His book does the same.” —Ash Mady, Technical Manager, RedHat, Inc. “Enjoyable and accessible to parents and children alike.” —Steve McLeod, Deputy CIO, University of North Georgia “Dr. Payne used robots, games, and fun programs to motivate me in college, and Teach Your Kids to Code extends that same passion for coding cool apps beyond the walls of the campus.” —Bobby Brown, Lead Developer, GetUWired
📄 Page 4
Teach Your Kids to Code
📄 Page 5
(This page has no text content)
📄 Page 6
Teach Your Kids to Code A Parent-Friendly Guide to Python Programming By Bryson Payne San Francisco
📄 Page 7
TeAch Your Kids To code. Copyright © 2015 by Bryson Payne. 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. 19 18 17 16 15 1 2 3 4 5 6 7 8 9 ISBN-10: 1-59327-614-1 ISBN-13: 978-1-59327-614-0 Publisher: William Pollock Production Editor: Riley Hoffman Cover Illustration: Josh Ellingson Illustrator: Miran Lipovac� a Developmental Editors: Tyler Ortman and Leslie Shen Technical Reviewers: Michelle Friend and Ari Lacenski Copyeditor: Rachel Monaghan Compositor: Riley Hoffman Proofreader: Paula L. Fleming Indexer: BIM Indexing & Proofreading Services 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; info@nostarch.com www.nostarch.com Library of Congress Cataloging-in-Publication Data Payne, Bryson. Teach your kids to code : a parent-friendly guide to Python programming / by Bryson Payne. -- 1st edition. pages cm Includes index. Summary: "A guide to teaching basic programming skills for parents and teachers, with step-by-step explanations, visual examples, and exercises. Covers programming concepts including loops, lists, functions, and variables, and how to build games and applications"-- Provided by publisher. ISBN 978-1-59327-614-0 -- ISBN 1-59327-614-1 1. Python (Computer program language)--Study and teaching (Elementary) 2. Computer programming--Study and teaching (Elementary) 3. Python (Computer program language)--Study and teaching (Middle school) 4. Computer programming--Study and teaching (Middle school) I. Title. QA76.73.P98P39 2015 005.13'3--dc23 2015006794 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 inten- tion of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precau- tion 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.
📄 Page 8
To Alex and Max, my two favorite coders
📄 Page 9
About the Author Dr. Bryson Payne is a tenured professor of computer science at the University of North Georgia, where he has taught aspiring coders for more than 15 years. His students have built successful careers at Blizzard Entertainment, Riot Games, Equifax, CareerBuilder, and more. He was the first department head of computer science at UNG, and he holds a PhD in computer science from Georgia State University. In addition, he works extensively with K–12 schools to promote technology education. Dr. Payne has been programming for more than 30 years. The first program he sold was to RUN magazine (Commodore 64) for their “Magic” column in 1985, for $10. Dr. Payne lives north of Atlanta, Georgia, with his wife, Bev, and two sons, Alex and Max. About the Illustrator Miran Lipovac� a is the author of Learn You a Haskell for Great Good!. He enjoys boxing, playing bass guitar, and, of course, drawing. He has a fascination with dancing skeletons and the number 71, and when he walks through automatic doors he pre- tends that he’s actually opening them with his mind. About the Technical Reviewer Ari Lacenski is a developer of Android applications and Python software. She lives in San Francisco. She writes about Android programming at http://gradlewhy.ghost.io/, mentors with Women Who Code, and plays songs about space pirates on guitar.
📄 Page 10
(This page has no text content)
📄 Page 11
Brief Contents Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Introduction: What Is Coding and Why Is It Good for Your Kids? . . . . . . xxi Chapter 1: Python Basics: Get to Know Your Environment . . . . . . . . . . . . . 1 Chapter 2: Turtle Graphics: Drawing with Python . . . . . . . . . . . . . . . . . . . 11 Chapter 3: Numbers and Variables: Python Does the Math . . . . . . . . . . . . 31 Chapter 4: Loops Are Fun (You Can Say That Again) . . . . . . . . . . . . . . . . . 53 Chapter 5: Conditions (What If?) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Chapter 6: Random Fun and Games: Go Ahead, Take a Chance! . . . . . . . 105 Chapter 7: Functions: There’s a Name for That . . . . . . . . . . . . . . . . . . . . . 141 Chapter 8: Timers and Animation: What Would Disney Do? . . . . . . . . . . . 175 Chapter 9: User Interaction: Get into the Game . . . . . . . . . . . . . . . . . . . . . 207 Chapter 10: Game Programming: Coding for Fun . . . . . . . . . . . . . . . . . . . 231 Appendix A: Python Setup for Windows, Mac, and Linux . . . . . . . . . . . . . 263 Appendix B: Pygame Setup for Windows, Mac, and Linux . . . . . . . . . . . . 279 Appendix C: Building Your Own Modules . . . . . . . . . . . . . . . . . . . . . . . . . 289 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
📄 Page 12
(This page has no text content)
📄 Page 13
Contents in Detail acknowledgments xix introduction What is Coding and Why is it Good for Your Kids? xxi Why Should Kids Learn to Code? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii Coding Is Fun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii Coding Is a Valuable Job Skill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii Where Can Kids Learn to Code? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii How to Use This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiv Explore! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxiv Do It Together! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Coding = Solving Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvi 1 Python Basics: Get to Know Your environment 1 Getting Started with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1 . Download Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2 . Install Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 3 . Test Python with a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Writing Programs in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Running Programs in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Programming Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 #1: Mad Libs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 #2: More Mad Libs! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2 turtle Graphics: Drawing with Python 11 Our First Turtle Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 What Happens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Turtle on a Roll . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Turtle Roundup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Adding a Touch of Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 A Four-Color Spiral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Changing Background Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 One Variable to Rule Them All . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
📄 Page 14
xiv Contents in Detail What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Programming Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 #1: Changing the Number of Sides . . . . . . . . . . . . . . . . . . . . . . . . . . 27 #2: How Many Sides? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 #3: Rubber-Band Ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3 numbers and Variables: Python Does the Math 31 Variables: Where We Keep Our Stuff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Numbers and Math in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Python Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Python Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Doing Math in the Python Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Syntax Errors: What Did You Say? . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Variables in the Python Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Programming with Operators: A Pizza Calculator . . . . . . . . . . . . . . 39 Strings: The Real Characters in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Improving Our Color Spiral with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Lists: Keeping It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Python Does Your Homework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Programming Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 #1: Circular Spirals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 #2: Custom Name Spirals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 4 loops are Fun (You Can say that again) 53 Building Your Own for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Using a for Loop to Make a Rosette with Four Circles . . . . . . . . . . . 56 Modifying Our for Loop to Make a Rosette with Six Circles . . . . . . 58 Improving Our Rosette Program with User Input . . . . . . . . . . . . . . . . . . . . 59 Game Loops and while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 The Family Spiral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Putting It All Together: Spiral Goes Viral . . . . . . . . . . . . . . . . . . . . . . . . . . 68 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Programming Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 #1: Spiral Rosettes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 #2: A Spiral of Family Spirals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
📄 Page 15
Contents in Detail xv 5 Conditions (What if?) 77 if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Meet the Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 You’re Not Old Enough! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 else Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Polygons or Rosettes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Even or Odd? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 elif Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Complex Conditions: if, and, or, not . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Secret Messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Messin’ with Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 The Value of Character(s) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Our Encoder/Decoder Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Programming Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 #1: Colorful Rosettes and Spirals . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 #2: User-Defined Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 6 Random Fun and Games: Go ahead, take a Chance! 105 A Guessing Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Colorful Random Spirals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Pick a Color, Any Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Getting Coordinated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 How Big Is Our Canvas? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Rock-Paper-Scissors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Pick a Card, Any Card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Stacking the Deck . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Dealing Cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Counting Cards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Keeping It Going . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Roll the Dice: Creating a Yahtzee-Style Game . . . . . . . . . . . . . . . . . . . . . . 126 Setting Up the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Sorting the Dice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Testing the Dice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Kaleidoscope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
📄 Page 16
xvi Contents in Detail What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Programming Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 #1: Random Sides and Thickness . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 #2: Realistic Mirrored Spirals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 #3: War . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 7 Functions: there’s a name for that 141 Putting Things Together with Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Defining random_spiral() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Calling random_spiral() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Parameters: Feeding Your Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Smileys at Random Locations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Return: It’s What You Give Back That Counts . . . . . . . . . . . . . . . . . . . . . . 153 Returning a Value from a Function . . . . . . . . . . . . . . . . . . . . . . . . . 153 Using Return Values in a Program . . . . . . . . . . . . . . . . . . . . . . . . . 154 A Touch of Interaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Handling Events: TurtleDraw . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 Listening for Keyboard Events: ArrowDraw . . . . . . . . . . . . . . . . . . 160 Handling Events with Parameters: ClickSpiral . . . . . . . . . . . . . . . 163 Taking It One Step Further: ClickandSmile . . . . . . . . . . . . . . . . . . 166 ClickKaleidoscope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 The draw_kaleido() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 The draw_spiral() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Programming Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 #1: Mirrored Smileys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 #2: More Ping-Pong Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 #3: A Better Drawing Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 8 timers and animation: What Would Disney Do? 175 Getting All GUI with Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Drawing a Dot with Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 What’s New in Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 The Parts of a Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Timing It Just Right: Move and Bounce . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Moving a Smiley . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Animating a Smiley with the Clock Class . . . . . . . . . . . . . . . . . . . . 188 Bouncing a Smiley Off a Wall . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Bouncing a Smiley Off Four Walls . . . . . . . . . . . . . . . . . . . . . . . . . . 197
📄 Page 17
Contents in Detail xvii What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Programming Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 #1: A Color-Changing Dot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 #2: 100 Random Dots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 #3: Raining Dots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 9 User interaction: Get into the Game 207 Adding Interaction: Click and Drag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Clicking for Dots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Dragging to Paint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Advanced Interaction: Smiley Explosion . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Smiley Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 Setting Up Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Updating Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Bigger and Smaller Smileys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 SmileyPop, Version 1 .0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 Detecting Collisions and Removing Sprites . . . . . . . . . . . . . . . . . . . 224 Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Programming Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 #1: Randomly Colored Dots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 #2: Painting in Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 #3: Throwing Smileys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 10 Game Programming: Coding for Fun 231 Building a Game Skeleton: Smiley Pong, Version 1 .0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Drawing a Board and Game Pieces . . . . . . . . . . . . . . . . . . . . . . . . . 234 Keeping Score . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 Showing the Score . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Adding Difficulty and Ending the Game: Smiley Pong, Version 2 .0 . . . . . 245 Game Over . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Play Again . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Faster and Faster . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Adding More Features: SmileyPop v2 .0 . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Adding Sound with Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Tracking and Displaying Player Progress . . . . . . . . . . . . . . . . . . . . 254 Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
📄 Page 18
xviii Contents in Detail What You Learned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 Programming Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 #1: Sound Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 #2: Hits and Misses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 #3: Clear the Bubbles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 a Python setup for Windows, Mac, and linux 263 Python for Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 Download the Installer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 Run the Installer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Try Out Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Python for Mac . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Download the Installer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Run the Installer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Try Out Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Python for Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 B Pygame setup for Windows, Mac, and linux 279 Pygame for Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 Pygame for Mac . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 Python 2 .7 and Pygame 1 .9 .2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 Pygame for Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Pygame for Python 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 C Building Your own Modules 289 Building the colorspiral Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 Using the colorspiral Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 Reusing the colorspiral Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 Additional Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 Glossary 295 index 301
📄 Page 19
Acknowledgments This book would not have been possible without the exceptional support of the No Starch Press team. Thanks especially to Bill Pollock for believing in the project; to Tyler Ortman for champion- ing and editing; and to Leslie Shen, Riley Hoffman, Lee Axelrod, Mackenzie Dolginow, Serena Yang, and Laurel Chun for their indefatigable editing, reviewing, marketing, and production prowess and for the countless ways they helped me improve this book from my original manuscript. And thanks to Rachel Monaghan and Paula Fleming for their help copyediting and proofreading. Thanks to Michelle Friend and Ari Lacenski for their thought- ful and thorough technical review, and to Conor Seng for being the first to read the book and try out the programs—at 10 years old. Thanks to Miran Lipovac� a for his amazing illustrations—they bring the kind of life to the text that I could only have dreamed of. Thanks to my father-in-law, Norman Petty, a retired IBM’er, who began teaching himself Python using an early draft of the book. Special thanks to my wife and best friend, Bev, for her con- stant support, and to my amazing sons, Alex and Max, for helping test every program and suggesting improvements. This book and my entire life are infinitely better because of the three of you. Finally, thanks to my mom, Esta, who encouraged me to love learning and solving puzzles.
📄 Page 20
(This page has no text content)
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List