Invent Your Own Computer Games With Python

2y ago
46 Views
5 Downloads
4.84 MB
473 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Shaun Edmunds
Transcription

iInvent Your OwnComputer Gameswith Python, 2ndEditionBy Al Sweigart

iiCopyright 2008-2012 by Albert SweigartSome Rights Reserved. "Invent Your Own Computer Games with Python" ("Invent with Python")is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United StatesLicense.You are free:To Share — to copy, distribute, display, and perform the workTo Remix — to make derivative worksUnder the following conditions:Attribution — You must attribute the work in the manner specified by the author orlicensor (but not in any way that suggests that they endorse you or your use of the work).(Visibly include the title and author's name in any excerpts of this work.)Noncommercial — You may not use this work for commercial purposes.Share Alike — If you alter, transform, or build upon this work, you may distributethe resulting work only under the same or similar license to this one.This summary is located here: s/Your fair use and other rights are in no way affected by the above. There is a human-readablesummary of the Legal Code (the full license), located 3.0/us/legalcodeBook Version 31If you've downloaded this book from a torrent, it’s probably out of date. Goto http://inventwithpython.com to download the latest version instead.ISBN 978-0-9821060-1-32.1 Edition

iii

ivFor Caro, with more lovethan I ever knew I had.

vA Note to Parents andFellow ProgrammersThank your for reading this book. My motivation for writing this book comes from a gap I saw intoday's literature for kids interested in learning to program. I started programming when I was 9years old in the BASIC language with a book similar to this one. During the course of writingthis, I've realized how a modern language like Python has made programming far easier andversatile for a new generation of programmers. Python has a gentle learning curve while stillbeing a serious language that is used by programmers professionally.The current crop of programming books for kids that I've seen fell into two categories. First,books that did not teach programming so much as "game creation software" or a dumbed-downlanguages to make programming "easy" (to the point that it is no longer programming). Orsecond, they taught programming like a mathematics textbook: all principles and concepts withlittle application given to the reader. This book takes a different approach: show the source codefor games right up front and explain programming principles from the examples.I have also made this book available under the Creative Commons license, which allows you tomake copies and distribute this book (or excerpts) with my full permission, as long as attributionto me is left intact and it is used for noncommercial purposes. (See the copyright page.) I want tomake this book a gift to a world that has given me so much.Thank you again for reading this book, and feel free to email me any questions or comments.Al Sweigartal@inventwithpython.comThe full text of this book is available in HTML or PDF format at:http://inventwithpython.com

viWho is this book for?Programming isn't hard. But it is hard to find learning materials that teach you to do interestingthings with programming. Other computer books go over many topics that most newbie codersdon't need. This book will teach you how to program your own computer games. You will learn auseful skill and have fun games to show for it! This book is for: Complete beginners who wants to teach themselves computer programming, even if theyhave no previous experience programming.Kids and teenagers who want to learn computer programming by creating games. Kids asyoung as 9 or 10 years old should be able to follow along.Adults and teachers who wish to teach others programming.Anyone, young or old, who wants to learn how to program by learning a professionalprogramming language.

viiTABLE OF CONTENTSInstalling Python . 1Downloading and Installing Python. 2Windows Instructions . 3Mac OS X Instructions. 4Ubuntu and Linux Instructions . 4Starting Python. 4How to Use This Book. 5The Featured Programs . 5Line Numbers and Spaces . 6Text Wrapping in This Book . 6Tracing the Program Online . 7Checking Your Code Online . 7Summary . 7The Interactive Shell . 8Some Simple Math Stuff . 8Integers and Floating Point Numbers. 9Expressions . 10Evaluating Expressions . 11Expressions Inside Other Expressions . 12Storing Values in Variables . 12Using More Than One Variable . 15Overwriting Variables . 16Summary . 17Strings . 18Strings . 18String Concatenation. 19

viiiWriting Programs in IDLE's File Editor . 20Hello World! . 20hello.py . 21Saving Your Program . 22Opening The Programs You've Saved . 23How the “Hello World” Program Works . 24Comments . 24Functions . 25The print() function . 25The input() function . 25Ending the Program . 26Variable Names . 27Summary . 27Guess the Number . 29The “Guess the Number” Game. 29Sample Run of “Guess the Number” . 30Guess the Number's Source Code . 30The import statement . 32The random.randint() function . 33Calling Functions that are Inside Modules . 35Passing Arguments to Functions . 35Welcoming the Player . 36Loops . 37Blocks . 37The Boolean Data Type . 38Comparison Operators . 38Conditions . 39Experiment with Booleans, Comparison Operators, and Conditions. 39Looping with while statements . 41

ixThe Player Guesses . 43Converting Strings to Integers with the int() function . 43Incrementing Variables . 45if statements . 45Is the Player's Guess Too Low? . 45Is the Player's Guess Too High? . 47Leaving Loops Early with the break statement. 47Check if the Player Won . 48Check if the Player Lost . 48Summary: What Exactly is Programming? . 49A Web Page for Program Tracing. 50Jokes. 52Making the Most of print() . 52Sample Run of Jokes . 52Joke's Source Code . 53How the Code Works . 53Escape Characters . 54Some Other Escape Characters . 54Quotes and Double Quotes . 55The end Keyword Argument . 56Summary . 56Dragon Realm . 58Introducing Functions . 58How to Play “Dragon Realm” . 59Sample Run of Dragon Realm . 59Dragon Realm's Source Code . 59How the Code Works . 61Defining the displayIntro() Function . 61def Statements . 62

xDefining the chooseCave() Function. 62Boolean Operators . 63Evaluating an Expression That Contains Boolean Operators . 63Experimenting with the and and or Operators . 64Experimenting with the not Operator . 65Truth Tables . 66Getting the Player's Input . 66Return Values. 67Variable Scope . 67Global Scope and Local Scope . 68Defining the checkCave() Function . 69Parameters . 70Where to Put Function Definitions . 71Displaying the Game Results . 72Deciding Which Cave has the Friendly Dragon . 72The Colon : . 74Where the Program Really Begins . 74Calling the Functions in Our Program . 74Asking the Player to Play Again . 75Designing the Program . 76Summary . 77Using the Debugger . 78Bugs! . 78The Debugger. 80Starting the Debugger . 80Stepping . 82Click the Step button twice to run the two import lines. . 82Click the Step button three more times to execute the three def statements. . 82Click the Step button again to define the playAgain variable. . 82

xiThe Go and Quit Buttons . 83Stepping Into, Over, and Out . 83Find the Bug. 85Break Points . 88Example of Using Break Points . 89Summary . 91Flow Charts .

this, I've realized how a modern language like Python has made programming far easier and versatile for a new generation of programmers. Python has a gentle learning curve while still being a serious language that is used by programmers professionally. The current crop of programming books

Related Documents:

The Games organised at Olympia led to the development of the Panhellenic Games. These included: - The Games at Olympia (Olympic Games): every four years - The Games at Delphi (Pythian Games), 582 B.C.: every four years (third year of each Olympiad) - The Games at the Isthmus of Corinth (Isthmian Games), from 580 B.C.:

Section 3: Playground Markings Games 16 Section 4: Skipping, Hula Hoop & Elastics 25 Section 5: Catching games 32 Section 6: Relay games 41 Section 7: Ball games 48 Section 8: Fun games 59 Section 9: Frisbee games 66 Section 10: Parachute games 70 Section 11: Clapping and rhyming games 74 Useful websites 79

Olympic Winter Games medals Olympic Winter Games posters Olympic Summer Games posters Olympic Summer Games mascots Olympic Winter Games mascots The sports pictograms of the Olympic Summer Games The sports pictograms of the Olympic Winter Games The IOC, the Olympic Movement and the Olympic Games The Olympic programme evolution Torches and torch .

Regional Games and Multi-Sport Games (such as Pan American Games, African Games, European Games, Commonwealth Games, Mediterranean Games, Francophone Games, Youth Olympic Games) International Tournaments organised by the IJF (Grand Prix, Grand Slam, Masters) or under its auspices (continental open and cups),

Olympic Summer Games posters Olympic Summer Games mascots Olympic Winter Games mascots The IOC, the Olympic Movement and the Olympic Games The Olympic programme evolution The Olympic stadiums of the Summer Games The sports pictograms of the Olympic Summer Games The sports pictograms of the Olympic Winter Games .

Zero-Sum Games vs. Non-Zero-Sum Games 29 Static vs. Dynamic Games; Repeated Games 30 Cooperative vs. Non-Cooperative Games 30 Other Key Game Theory Concepts 31 Threats and Rewards (Promises) 31 Credibility 31 Sample Game with Threats 31 The Threat as a Strategy 32 Games of Chance: Uncertainty and Risk 32 Chapter 3 Modeling Games with Computer .

touchtest invent.com touch, test, invent Get More Done in Less Time The 2450 takes a giant step for test and measurement into today's world of state of the art smart technology. Unlike conventional instruments that have dedicated pushbutton technology and small, obscure, limited-character displays, the 2450 features an advanced,

touchtest invent.com touch, test, invent Get More Done in Less Time The 2450 takes a giant step for test and measurement into today's world of state of the art smart technology. Unlike conventional instruments that have dedicated pushbutton technology and small, obscure, limited-character displays, the 2450 features an advanced,