Invent Your Own Computer Games with Python

[Pages:473]i

Invent Your Own Computer Games with Python, 2nd Edition

By Al Sweigart

ii

Copyright ? 2008-2012 by Albert Sweigart Some 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 States License. You are free:

To Share -- to copy, distribute, display, and perform the work

To Remix -- to make derivative works Under the following conditions:

Attribution -- You must attribute the work in the manner specified by the author or licensor (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 distribute the resulting work only under the same or similar license to this one.

This summary is located here:

Your fair use and other rights are in no way affected by the above. There is a human-readable summary of the Legal Code (the full license), located here:

Book Version 31

If you've downloaded this book from a torrent, it's probably out of date. Go to to download the latest version instead.

ISBN 978-0-9821060-1-3

2.1 Edition

iii

iv

For Caro, with more love than I ever knew I had.

v

A Note to Parents and Fellow Programmers

Thank your for reading this book. My motivation for writing this book comes from a gap I saw in today's literature for kids interested in learning to program. I started programming when I was 9 years old in the BASIC language with a book similar to this one. During the course of writing 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 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-down languages to make programming "easy" (to the point that it is no longer programming). Or second, they taught programming like a mathematics textbook: all principles and concepts with little application given to the reader. This book takes a different approach: show the source code for 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 to make copies and distribute this book (or excerpts) with my full permission, as long as attribution to me is left intact and it is used for noncommercial purposes. (See the copyright page.) I want to make 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 Sweigart

al@

The full text of this book is available in HTML or PDF format at:



vi

Who is this book for?

Programming isn't hard. But it is hard to find learning materials that teach you to do interesting things with programming. Other computer books go over many topics that most newbie coders don't need. This book will teach you how to program your own computer games. You will learn a useful skill and have fun games to show for it! This book is for:

Complete beginners who wants to teach themselves computer programming, even if they have no previous experience programming.

Kids and teenagers who want to learn computer programming by creating games. Kids as young 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 professional

programming language.

vii

TABLE OF CONTENTS

Installing Python .............................................................................................................................. 1 Downloading and Installing Python............................................................................................. 2 Windows Instructions .................................................................................................................. 3 Mac OS X Instructions................................................................................................................. 4 Ubuntu and Linux Instructions .................................................................................................... 4 Starting Python............................................................................................................................. 4 How to Use This Book................................................................................................................. 5 The Featured Programs ................................................................................................................ 5 Line Numbers and Spaces............................................................................................................ 6 Text Wrapping in This Book ................................................................................................... 6 Tracing the Program Online......................................................................................................... 7 Checking Your Code Online........................................................................................................ 7 Summary ...................................................................................................................................... 7

The Interactive Shell ........................................................................................................................ 8 Some Simple Math Stuff.............................................................................................................. 8 Integers and Floating Point Numbers........................................................................................... 9 Expressions ................................................................................................................................ 10 Evaluating Expressions .............................................................................................................. 11 Expressions Inside Other Expressions ....................................................................................... 12 Storing Values in Variables ....................................................................................................... 12 Using More Than One Variable................................................................................................. 15 Overwriting Variables................................................................................................................ 16 Summary .................................................................................................................................... 17

Strings ............................................................................................................................................ 18 Strings ........................................................................................................................................ 18 String Concatenation.................................................................................................................. 19

viii

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

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download