Invent Your Own
Computer Games
with Python
3
rd
Edition
By Al Sweigart
ii http://inventwithpython.com
Post questions to http://invpy.com/forum
Copyright © 2008-2015 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.
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:
http://creativecommons.org/licenses/by-nc-sa/3.0/us/legalcode
The source code in this book is released under a BSD 2-Clause license, located here:
http://opensource.org/licenses/BSD-2-Clause
Book Version 3.0.1, ISBN 978-1503212305
Attribution: Treasure chest icon by Victor Escorsin, Sonar icon by Pantelis Gkavos
If you’ve downloaded this book from a torrent, it’s probably out of date. Go
to http://inventwithpython.com to download the latest version instead.
For Caro, with more love
than I ever knew I had.
iv http://inventwithpython.com
Post questions to http://invpy.com/forum
A Note to Parents and Fellow Programmers
Thank you for reading this book. My motivation for writing it came from a gap I saw in today’s
literature for kids interested in learning to program. I started programming in the BASIC
programming 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 used by programmers professionally.
The current crop of programming books fall into two categories. First, books that didn’t 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’ve 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.
What’s New in the 3
rd
Edition?
The third edition features no new content since the second edition. However, the third edition has
been streamlined to cover the same content with 20% fewer pages. Explanations have been
expanded where needed and ambiguities clarified.
Chapter 9 was split into chapters 9 and 9½ to keep the chapter numbering the same.
The source code has intentionally been kept the same as the second edition to prevent confusion.
If you’ve already read the second edition, there’s no reason to read this book. However, if you are
new to programming, or introducing a friend to programming, this third edition will make the
process easier, smoother, and more fun.
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 most newbie coders don’t
need. This book will teach you how to program your own computer games. You’ll learn a useful
skill and have fun games to show for it! This book is for:
Complete beginners who want to teach themselves computer programming, even if they
have no previous experience programming.
Kids and teenagers who want to learn programming by creating games.
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.