Invent Your Own Computer Games with Python 2nd Edition - Pdf 12

Invent Your Own
Computer Games with Python

2nd Edition


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 13

ISBN 978-0-9821060-1-3

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

Dinosaur Comics reproduced with permission. Thanks Ryan!
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.

Table of ContentsSource Code Listing

hello.py 21
guess.py 30
jokes.py
51
dragon.py 58
buggy.py 83
coinFlips.py 87
hangman.py 103
tictactoe.py 150
truefalsefizz.py 172

11
Storing Values in Variables
12
Using More Than One Variable
15
Summary
16
3 Strings, and Your First Program 18
Strings
18
String Concatenation
19
Writing Programs in IDLE's File Editor
20
Hello World!
20
How the "Hello World" Program Works
23
Summary
26
4 Guess the Number 28
The "Guess the Number" Game
28
Sample Run of "Guess the Number"
29
Guess the Number's Source Code
29
The import Statement
31
The random.randint() Function

Sample Run of Jokes
50
Joke's Source Code
51
Escape Characters
52
Quotes and Double Quotes
53
The end Keyword Argument
54
Summary
55
6 Dragon Realm 56
Introducing Functions
56
Sample Run of Dragon Realm
57
Dragon Realm's Source Code
57
def Statements
60
Boolean Operators
61
Return Values
65
Variable Scope
65
Parameters
68
Where to Put Function Definitions

89
Sample Run of "Hangman"
89
ASCII Art
91
Designing a Program with a Flowchart
92
Creating the Flow Chart
93
Summary: The Importance of Planning Out the Game
100
9 Hangman 102
Hangman's Source Code
103
Multi-line Strings
107
Constant Variables
108
Lists
108
Changing the Values of List Items with Index Assignment
110
List Concatenation
110
The in Operator
111
Removing Items from Lists with del Statements
112
Lists of Lists
113

149
Source Code of Tic Tac Toe
150
Designing the Program
154
Game AI
156
List References
162
Short-Circuit Evaluation
170
The None Value
175
Summary: Creating Game-Playing Artificial Intelligences
182
11 Bagels 183
Sample Run
184
Bagel's Source Code
184
Designing the Program
186
The random.shuffle() Function
188
Augmented Assignment Operators
190
The sort() List Method
192
The join() String Method
192

239
The Caesar Cipher
240
ASCII, and Using Numbers for Letters
241
The chr() and ord() Functions
242
Sample Run of Caesar Cipher
243
Caesar Cipher's Source Code
244
The isalpha() String Method
247
The isupper() and islower() String Methods
248
Brute Force
251
Summary: Reviewing Our Caesar Cipher Program
253
15 Reversi 256
How to Play Reversi
255
Sample Run
257
Reversi's Source Code
260
The bool() Function
276
Summary: Reviewing the Reversi Game
290

Fonts, and the pygame.font.SysFont() Function
315
Attributes
316
Constructor Functions and the type() function.
317
The pygame.PixelArray Data Type
321
Events and the Game Loop
322
Animation
324
The Animation Program's Source Code
324
Some Small Modifications
335
Summary: Pygame Programming
335
18 Collision Detection and Input 337
The Collision Detection Program's Source Code
337
The Collision Detection Function
341
The pygame.time.Clock Object and tick() Method
344
The Keyboard Input Program's Source Code
348
The colliderect() Method
356
Summary: Collision Detection and Pygame Input

403
Appendix C
Running Python Programs without Python Installed
404
Appendix D
Common Error Messages in Python
407
Glossary 411
About the Author 421Topics Covered In This Chapter:
 Downloading and installing the Python interpreter.
 Usi
ng IDLE's interactive shell to run instructions.
 How to use this book.
 The book's website at
Hello! This is a book that will teach you how to program by showing you how to create
computer games. Once you learn how the games in this book work, you'll be able to create
your own games. All you'll need is a computer, some software called the Python
Interpreter, and this book. The software you'll need is free and you can download it from
the Internet.
When I was a kid, I found a book like this that taught me how to write my first programs
and games. It was fun and easy. Now as an adult, I still have fun programming computers,
and I get paid for it. But even if you don't become a computer programmer when you grow
up, programming is a useful and fun skill to have.
Computers are very useful machines. The good news is that learning to program a
computer is easy. If you can read this book, you can program a computer. A computer
program is just a bunch of instructions run by a computer, just like a storybook is just a
whole bunch of sentences read by the reader.

Downloading and Installing Python
Before we can begin programming you'll need to install the Python software; specifically
the Python interpreter. (You may need to ask an adult for help here.) The interpreter is a
program that understands the instructions that you'll write in the Python language. Without
the interpreter, your computer won't understand these instructions and your programs won't
work. (We'll just refer to "the Python interpreter" as "Python" from now on.)
Because we'll be writing our games in the Python language, we need to download Python
first, from the official website of the Python programming language,

I'm going to give you instructions for installing Python on Microsoft Windows, not
because that's my favorite operating system but because chances are that's the operating
system that your computer is running. You might want the help of someone else to
download and install the Python software.
When you get to python.org, you should see a list of links on the left (About, News,
Documentation, Download,
and so on.) Click on the
Download
link to go to the
download
2
page, then look for the file called
Python 3.1 Windows Installer
(Windows binary does not include source) and click on its link to download Python for Windows.

Figure
1-1: Click the Windows installer link to download Python for Windows from
Double-click on the python-3.1.msi file that you've just downloaded to start the Python

Figure 1-2: The IDLE program's interactive shell on Windows.
IDLE stands for Interactive DeveLopment Environment. The development environment
is software that makes it easy to write Python programs. We will be using IDLE to type in
our programs and run them.
The window that appears when you first run IDLE is called the interactive shell. A shell
is a program that lets you type instructions into the computer. The Python shell lets you
type Python instructions, and the shell sends these instructions to software called the
Python interpreter to perform. We can type Python instructions into the shell and, because
the shell is interactive, the computer will read our instructions and respond in some way.
(Ideally in a way that we expect but that will depend on whether we write the correct
instructions.)
How to Use This Book
There are a few things you should understand about this book before you get started.
"Invent with Python" is different from other programming books because it focuses on the
complete source code for different games. Instead of teaching you programming concepts
and leaving it up to you to figure out how to make fun games with those concepts, this book
shows you fun games and then
explains how they are put together.

4
The Featured
Programs

Most chapters begin with a sample run of the featured program. This sample run shows
you what t
he program's output looks like, with what the user types in shown as bold print.
This will give you an idea of what the complete game will look like when you have entered
the code and run it.
Some chapters also show the complete source code of the game, but remember: you don't
have to enter every line of code right now. Instead, you can read the chapter first to

Tex
t Wrapping in This Book
Some lines of code are too long to fit on one line on the page, and the text of the code
will
wrap around to the next line. When you type these lines into the file editor, enter the
code all on one line without pressing Enter.
You can tell when a new line starts by looking at the line numbers on the left side of the
code. For example, the code below has only two lines of code, even though the first line
wraps around:
1. print('This is the first line! xxxxxxxxxxxxxxx
xxxxxxxxxxxx')
2. print('This is the second line! ')
T
racing the Program Online
You can visit to see a trace through each of the
program
s in this book. Tracing a program means to step through the code one line at a time,
in the same way that a computer would execute it. The traces web page has notes and
helpful reminders at each step of the trace to explain what the program is doing, so it can
help you better understand why these programs work the way they do.
Checking Your Code Online
Some of the games in this book are a little long. Although it is very helpful to learn
Python by typing out the source code for these games, you may accidentally make typos
that cause your game programs to crash. It may not be obvious where the typo is.
You can copy and paste the text of your source code to the online diff tool on the book's
website. The diff tool will show any differences between the source code in the book and
the source code you've typed. This is an easy way of finding any typos in your program.
Copying and pasting text is a very useful computer skill, especially for computer
programming. There is a video tutorial on copying and pasting at this book's website at


make learning to program much easier. This is because most programming is built on only
a few simple concepts combined together to make advanced programs.
Let's start by learning how to use Python's interactive shell.
Some Simple Math Stuff
To open IDLE on Windows, click on Start > Programs > Python 3.1 > IDLE (Python
GUI). With IDLE open, let's do some simple math with Python. The interactive shell can
work just like a calculator. Type 2+2 into the shell and press the Enter key on your
keyboard. (On some keyboards, this is the RETURN key.) As you can see in Figure 2-1,
the computer should
respond with the number 4; the sum of 2+2.

8

Figure
2-1: Type 2+2 into the shell.
As you can see, we can use the Python shell just like a calculator. This isn't a program by
itself because we are just learning the basics right now. The + sign tells the computer to add
the numbers 2 and 2. To subtract numbers use the - sign, and to multiply numbers use an
asterisk (*), like so:
When used in this way, +, -, *, and / are called operators because they tell the
computer to perform the specified operation on the numbers surrounding them.
Integers and Floating Point Numbers
In programming (and also in mathematics), whole numbers like 4, 0, and 99 are called
integ
ers. Numbers with fractions or decimal points (like 3.5 and 42.1 and 5.0) are not
integers. In Python, the number 5 is an integer, but if we wrote it as 5.0 it would not be an
integer. Numbers with a decimal point are called floating point numbers. In
mathematics, 5.0 is still considered an integer and the same as the number 5, but in
computer programming the computer considers any number with a decimal point as not an
integer.

expressions. Computers can solve
millions of these problems in seconds.
Expressions are made up of values (the
numbers) connected by operators (the
math signs). Let's learn exactly what
values and operators are.
As you can see with the last
expression in the above example, you
can put any amount of spaces in
between the integers and these operators. (But be sure to always start at the very beginning
of the line, with no spaces in front.)
Numbers are a type of value. Integers are a type of number. But, even though integers are
numbers, not all numbers are integers. (For example, fractions and numbers with decimal
points like 2.5 are numbers that are not integers.)

Figure 2
-
3: An expression is
a made up of values and operators.
10
This is
like how a cat is a type of pet, but not all pets are cats. Someone could have a
pet
dog or a pet lizard. An expression is ma
de up of values (such as integers like 8 and 6)
connected by an operator (such as the * multiplication sign). A single value by itself is also
considered an expression.
In the next chapter, we will learn about working with text in expressions. Python isn't
limited to just numbers. It's more than just a fancy calculator!
Evaluating Expressions


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status