ACKNOWLEDGMENTS This book would not have been possible without the exceptional work of the No Starch Press team Thanks to my publisher Bill Pollock thanks to my editors Laurel Chun Jan Cash and Tyler Ortman for their incredible help throughout the process thanks to my technical editor Ari Lacenski for her thorough review and thanks to Josh Ellingson for yet another great cover
INTRODUCTION When I first played video games as a kid I was hooked But I didn’t just want to play video games I wanted to make them I found a book like this one that taught me how to write my first programs and games It was fun and easy The first games I made were like the ones in this book They weren’t as fancy as the Nintendo games my parents bought for me but they were games I had made myself Now as an adult I still have fun programming and I get paid for it But even if you don’t want to become a computer programmer programming is a useful and fun skill to have It trains your brain to think logically make plans and reconsider your ideas whenever you find mistakes in your code
Many programming books for beginners fall into two categories The first category includes books that don’t teach programming so much as game creation software or languages that simplify so much that what is taught is no longer programming The other category consists of books that teach programming like a mathematics textbook all principles and concepts with few real-life applications for the reader This book takes a different approach and teaches you how to program by making video games I show the source code for the games right up front and explain programming principles from the examples This approach was the key for me when I was learning to program The more I learned how other people’s programs worked the more ideas I had for my own programs All you’ll need is a computer some free software called the Python interpreter and this book Once you learn how to create the games in this book you’ll be able to develop games on your own Computers are incredible machines and learning to program them isn’t as hard as people think A computer program is a bunch of instructions that the computer can understand just like a storybook is a bunch of sentences sentences that the reader can understand To instruct a computer you write a program in a language the computer understands This book will teach you a programming language called Python There are many other program ming languages you can learn like BASIC Java JavaScript PHP and C When I was a kid I learned BASIC but newer programming languages like Python are even easier to learn Python is also used by professional programmers programmers in their work and when programming for fun Plus it’s totally free to install and use you’ll just need an internet connection to download it Because video games are nothing but computer programs they are also made up of instructions The games you’ll create from this book seem simple compared to the games for Xbox PlayStation or Nintendo These games don’t have fancy graphics because they’re meant to teach you coding basics They’re purposely simple so you can focus on learning to program Games don’t have to be complicated to be fun Who Is This Book For Programming isn’t hard but it is hard to find materials that teach you to do interesting things with programming Other computer books go over many topics most new coders don’t need This book will teach you how to program your own games you’ll learn a useful skill and have fun games to show for it This book is for (cid:120)(cid:3) Complete beginners who want to teach themselves programming even if they have no previous experience (cid:120)(cid:3) Kids and teenagers who want to learn programming by creating games (cid:120)(cid:3) Adults and teachers who wish to teach others programming (cid:120)(cid:3) Anyone young or old who wants to learn how to program by learning a professional programming language xxii Introduction
About This Book In most of the chapters in this book a single new game project is introduced introduced and explained A few of the chapters cover additional useful topics like debugging New programming concepts are explained as games make use of them and the chapters are meant to be read in order Here’s a brief rundown of what you’ll find in each chapter Chapter 1 The Interactive Shell explains how Python’s interactive shell can be used to experiment with code one line at a time Chapter 2 Writing Programs covers how to write complete programs in Python’s file editor In Chapter 3 Guess the Number you’ll program the first game in the book Guess the Number which asks the player to guess a secret number and then provides hints as to whether the guess is too high or too low In Chapter 4 A Joke-Telling Program you’ll write a simple program that tells the user several jokes In Chapter 5 Dragon Realm you’ll program a guessing game in which the player must choose between two caves one has a friendly dragon and the other has a hungry dragon Chapter 6 Using the Debugger covers how to use the debugger to fix problems in your code Chapter 7 Designing Hangman with Flowcharts explains how flowcharts flowcharts can be used to plan longer programs such as the Hangman game In Chapter 8 Writing the Hangman Code you’ll write the Hangman game following the flowchart from Chapter 7 Chapter 9 Extending Hangman extends the Hangman game with new features by making use of Python’s dictionary data type In Chapter 10 Tic-Tac-Toe you’ll learn how to write a human-versus computer Tic-Tac-Toe game that uses artificial intelligence In Chapter 11 The Bagels Deduction Game you’ll learn how to make a deduction game called Bagels in which the player must guess secret numbers based on clues Chapter 12 The Cartesian Coordinate System explains the Cartesian coordinate system which you’ll use in later games In Chapter 13 Sonar Treasure Hunt you’ll learn how to write a treasure hunting game in which the player searches the ocean for lost treasure chests In Chapter 14 Caesar Cipher you’ll create a simple encryption program program that lets you write and decode secret messages In Chapter 15 The Reversegam Game you’ll program an advanced human-versus-computer Reversi-type game that has a nearly unbeatable artificial intelligence opponent Introduction xxiii