Tải bản đầy đủ (.pdf) (342 trang)

beginning game development with python and pygame

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (9.13 MB, 342 trang )

this print for content only—size & color not accurate spine = 0.802" 344 page count
Books for professionals By professionals
®
Beginning Game Development with Python and Pygame:
From Novice to Professional
Dear Reader,
Creating computer games is a fascinating combination of an art and a science, and
can make a rewarding career or hobby. Do you know you can write cutting-edge
games that run on Windows, Linux, and Mac OS X with open source technologies
and freely available tools?
In this book, I introduce you to Python—a powerful, dynamic language—
and explain how to use the Pygame library to quickly develop compelling
games. I also explain how to create three-dimensional graphics using OpenGL,
the technology behind many commercial games, such as the Quake series.
My aim for Beginning Game Development with Python and Pygame is
to make game development accessible to as wide an audience as possible.
Previous programming experience is useful but not essential, because the
first two chapters introduce the Python programming language—or serve as a
refresher course if you are already familiar with Python. Further chapters cover
how to use Pygame to set up a graphical display, draw to the screen, handle
events, and read input devices. I also talk you through essential topics such as
creating game characters with artificial intelligence and playing sound (includ-
ing music).
Four chapters are devoted to the topic of three-dimensional graphics in
games, and show you how to manipulate objects in a virtual world and render
impressive visuals with lighting and special effects. The math involved in pro-
gramming a game with three-dimensional graphics can be intimidating, but I
explain it in visual terms rather than the more traditional pure mathematical
approach you find in textbooks. I also share with you a few of the tips and tricks
I have picked up over the years in my career as a professional game developer.
I hope to be playing one of your Pygame creations in the future!


Will McGugan
US $39.99
Shelve in
Python
User level:
Beginner–Intermediate
The eXperT’s Voice
®
in open source
Beginning
Game
Development
with Python and Pygame
From Novice to Professional
cyan
MaGenTa
yelloW
Black
panTone 123 c
Will McGugan
THE APRESS ROADMAP
Foundations of Python
Network Programming
Beginning Python:
From Novice to Professional
The Definitive Guide to Django:
Web Development Done Right
Beginning Game Development
with Python and Pygame
www.apress.com

SOURCE CODE ONLINE
Companion eBook

See last page for details
on $10 eBook version
ISBN-13: 978-1-59059-872-6
ISBN-10: 1-59059-872-5
9 781590 598726
5 3 9 9 9
Create stunning video games using
popular open source technologies!
McGugan
Game
Companion
eBook Available
Beginning
Development
with
Python and Pygame
www.it-ebooks.info
www.it-ebooks.info
Beginning Game
Development with
Python and Pygame
From Novice to Professional
■■■
Will McGugan
8725.book Page i Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
Beginning Game Development with Python and Pygame: From Novice to Professional

Copyright © 2007 by Will McGugan
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-59059-872-6
ISBN-10 (pbk): 1-59059-872-5
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Jason Gilmore
Technical Reviewer: Richard Jones
Editorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick,
Jason Gilmore, Kevin Goff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper,
Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Kylie Johnston
Copy Editor: Liz Welch
Assistant Production Director: Kari Brooks-Copony
Production Editor: Kelly Winquist
Compositor: Pat Christenson
Proofreader: Erin Poe
Indexer: Becky Hornyak
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail , or
visit .
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600,
Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail , or visit http://
www.apress.com.

The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly
by the information contained in this work.
The source code for this book is available to readers at in the Source Code/
Download section.
8725.book Page ii Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
For Maria
8725.book Page iii Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
8725.book Page iv Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
v
Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvii
Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
■CHAPTER 1 Introducing Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
■CHAPTER 2 Exploring Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
■CHAPTER 3 Introducing Pygame. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
■CHAPTER 4 Creating Visuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
■CHAPTER 5 Making Things Move . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
■CHAPTER 6 Accepting User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
■CHAPTER 7 Take Me to Your Leader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
■CHAPTER 8 Moving into the Third Dimension. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
■CHAPTER 9 Exploring the Third Dimension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
■CHAPTER 10 Making Things Go Boom. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
■CHAPTER 11 Lights, Camera, Action! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235

■CHAPTER 12 Setting the Scene with OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
■APPENDIX A Game Object Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
■APPENDIX B Packaging Your Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
8725.book Page v Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
8725.book Page vi Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
vii
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xv
About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
■CHAPTER 1 Introducing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Your First Look at Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Lists and Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Python in Practice. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
■CHAPTER 2 Exploring Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Creating Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Working with Logic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Understanding Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Understanding Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Defining Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Introducing Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . 26
Using Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

Python in Practice. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Using the Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Introducing import. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Useful Modules for Games. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
■CHAPTER 3 Introducing Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
History of Pygame. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Installing Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Using Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
8725.book Page vii Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
viii
■CONTENTS
Hello World Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Understanding Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Retrieving Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Handling Mouse Motion Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Handling Mouse Button Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Handling Keyboard Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Filtering Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Posting Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Opening a Display. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Full-Screen Displays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Resizable Pygame Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Windows with No Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Additional Display Flags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Using the Font Module. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
When Pygame Goes Wrong. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Pygame in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

■CHAPTER 4 Creating Visuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Using Pixel Power. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Working with Color. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Representing Color in Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Scaling Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Blending Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Using Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Storing Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Working with Surface Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Drawing with Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
pygame.draw.rect. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
pygame.draw.polygon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
pygame.draw.circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
pygame.draw.ellipse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
pygame.draw.arc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
pygame.draw.line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
pgame.draw.lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
pygame.draw.aaline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
pygame.draw.aalines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
8725.book Page viii Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
■CONTENTS
ix
■CHAPTER 5 Making Things Move . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Understanding Frame Rate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Moving in a Straight Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
It’s About Time. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Diagonal Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Exploring Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

Creating Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Storing Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Vector Magnitude . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Unit Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Vector Addition. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Vector Subtraction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Vector Negation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Vector Multiplication and Division . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Game Objects Vector Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Using Vectors to Create Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Diagonal Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
■CHAPTER 6 Accepting User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Controlling the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Understanding Keyboard Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Detecting Key Presses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Directional Movement with Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Rotational Movement with Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Implementing Mouse Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Rotational Movement with the Mouse. . . . . . . . . . . . . . . . . . . . . . . . 121
Mouse Gameplay. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Implementing Joystick Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Joystick Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Joystick Direction Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Joystick Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Seeing Joysticks in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
■CHAPTER 7 Take Me to Your Leader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Creating Artificial Intelligence for Games . . . . . . . . . . . . . . . . . . . . . . . . . . 139
What Is Intelligence? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

8725.book Page ix Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
x
■CONTENTS
Exploring AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Implementing State Machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Game Entities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Building Worlds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Ant Entity Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Building the Brains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
■CHAPTER 8 Moving into the Third Dimension . . . . . . . . . . . . . . . . . . . . . . . . . 165
Creating the Illusion of Depth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Understanding 3D Space. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Using 3D Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Time-Based Movement in 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Projecting 3D Points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Parallel Projections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Perspective Projections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
A 3D World. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
■CHAPTER 9 Exploring the Third Dimension. . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
What Is a Matrix? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Using the Matrix Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Introducing OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Installing PyOpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Initializing OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
OpenGL Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Seeing OpenGL in Action. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210

■CHAPTER 10 Making Things Go Boom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
What Is Sound?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Storing Sound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Sound Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Creating Sound Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Stock Sound Effects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
8725.book Page x Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
■CONTENTS
xi
Playing Sounds with Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Sound Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Sound Channels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Mixer Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Hearing the Mixer in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Playing Music with Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Obtaining Music. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
Playing Music. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Hearing Music in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
■CHAPTER 11 Lights, Camera, Action! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Working with Textures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Uploading Textures with OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Texture Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Rendering Textures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Deleting Textures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Seeing Textures in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Mip Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Texture Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Working with Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248

Storing Models. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
OBJ Format for 3D Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Seeing Models in Action. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
■CHAPTER 12 Setting the Scene with OpenGL. . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Understanding Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
Enabling Lighting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Setting Light Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Working with Materials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Tweaking Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Managing Lights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Understanding Blending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Using Blending. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Seeing Blending in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Blending Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
8725.book Page xi Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
xii
■CONTENTS
Understanding Fog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Fog Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Seeing Fog in Action. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Rendering the Backdrop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Skyboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278
Where to Go for Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
■APPENDIX A Game Object Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Importing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Contributing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
gameobjects.color.Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

Constructor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286
Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
gameobjects.matrix44.Matrix44 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Constructor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289
gameobjects.vector2.Vector2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Constructor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
gameobjects.vector3.Vector3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Constructor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
8725.book Page xii Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
■CONTENTS
xiii
■APPENDIX B Packaging Your Game. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Creating Windows Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Using py2exe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Building the Installer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294
Creating Packages for Linux. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Creating Packages for the Mac . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297

8725.book Page xiii Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
8725.book Page xiv Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
xv
About the Author
■WILL McGUGAN is a Scottish software developer who lives and works in
North West England. Will has worked on a number of game projects,
from self-published shareware games to triple A titles, most recently on
MotorStorm, one of the first games released for Sony’s PlayStation 3.
He has been an enthusiastic user of Python for many years, having writ-
ten a script to automate source code backup as well as several popular
desktop applications and a web site in Python. Will is currently work-
ing from home as a contractor and developing a Web 2.0 site with the
TurboGears framework in his spare time. When not programming, Will enjoys photography,
cycling, and juggling—although not at the same time. For more information on Will’s current
projects and various musings, visit his web site at www.willmcgugan.com.
8725.book Page xv Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
8725.book Page xvi Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
xvii
About the Technical Reviewer
■RICHARD JONES organizes the biannual Python Programming Game
Challenge (PyWeek challenge and develops
OpenGL applications in Python for a living.
8725.book Page xvii Wednesday, September 19, 2007 4:23 PM
www.it-ebooks.info
8725.book Page xviii Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info

xix
Acknowledgments
I thank the Apress team for giving me the wonderful opportunity to write a book—something
that has been my dream for many years. Many thanks to Jason Gilmore, whose enthusiastic
response to my proposal and guidance as my editor was much appreciated. I also thank Kylie
Johnston for her diligence and hard work. Liz Welch’s eagle eye kept this book free from spell-
ing and grammar mistakes. Richard Jones did an excellent job as my technical reviewer and
kept this book technically correct—the best kind of correct.
I can’t go without thanking my parents, Bill and Audrey, and my sisters, Ruth and Jen, for
their unwavering support and encouragement throughout the months of writing. Thanks,
Mum and Dad, for buying me the Spectrum 48K computer that put me on this path and for
always being there for me.
If I have omitted any names, it is because there are too many people to thank. My claim to
be a self-taught engineer is a little dubious because I have learned much from the talented peo-
ple I have had the good fortune to work with over the years.
8725.book Page xix Wednesday, September 26, 2007 8:08 PM
e8376d3cc8796c3240d60804b9c401fb
www.it-ebooks.info
8725.book Page xx Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
xxi
Introduction
I have accumulated a large collection of game development books over the years, virtually all of
which are an inch or two thicker than this book—even though they cover similar subjects and
techniques. The disparity is not because my writing is terse or I use a smaller font—it is because
traditional game development tools tend to require a large amount of technical knowledge that
the reader must first absorb before building even the simplest of games. Even seasoned game
developers find the technical requirements of starting a game to be enough of a barrier that they
are less likely to work on game ideas that are unproven or potentially not commercial-worthy.
Game development may have become easier over the years, with simplified programming inter-

faces and more programmers wishing to share their knowledge, but writing a game is still a
significant undertaking.
When I discovered Python, it lowered a number of barriers to writing software, because
I could work faster and accomplish more with less effort, and when combined with Pygame I
could experiment with game ideas and build a complete game from scratch in record time. The
beauty of Pygame is that it makes the various tasks in creating a game (setting up a display,
drawing to the screen, playing sound, etc.) only as complicated as they need to be—and it turns
out that’s not particularly complicated at all! Many one-liners in Pygame would take dozens of
lines in C++, the traditional tool of game developers.
Although Python and Pygame are superb tools for rapid game development, there is little
in the way of books or web tutorials for Python game programmers, who often have no choice
but to mentally translate from another language to Python when researching a new topic in
game development. This book was conceived to fill that gap and allow the beginner game pro-
grammer to get up to speed with Python and learn the fundamentals of game programming
without having to first learn C++, C#, Java, or another language first. It was also my opportunity
to explain 3D game programming in a way that is accessible to nonmathematicians—some-
thing that is not easy to find in other books.
In short, this is the book I would have wanted to have when I started out in game development!
Who This Book Is For
This book is for anyone who has thought about creating a computer game, or wants to learn
about the technology behind game development. Although Python is the tool of choice for this
book, many of the techniques covered are equally applicable to other languages.
8725.book Page xxi Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
xxii
■INTRODUCTION
How This Book Is Structured
Beginning Game Development with Python and Pygame is divided into 12 chapters, each of
which builds on the previous chapter—with a few notable exceptions. I’ve structured it so that
you can get results quickly and see something on screen, which you may appreciate if you are

as impatient as I am. Virtually all the listings are self-contained, and hopefully entertaining, lit-
tle projects that run independently. Since experimentation is the best way to learn, you are
encouraged to play with the sample code and modify it to produce different effects. You can
also use any of the code in your own projects—with my blessing!
The first two chapters introduce the Python language in a fairly conversational manner.
If you read them with a Python prompt in front of you, you should find you can quickly pick up
the language. These two chapters don’t make a complete language tutorial, but will cover
enough for you to be able to understand the Python code in the book and write some of your
own. Occasionally, new syntaxes and language features are introduced in the rest of the book,
but I explain them where they are first used. If you are proficient in Python, you can skip
straight to Chapter 3.
Chapter 3 is your first introduction to Pygame and covers its history and capabilities. It also
explains the basics of setting up a graphical display and handling events, skills that are essential
for any game. You will become intimately familiar with the code introduced in this chapter, as
it is used in all the sample code for the rest of the book.
Chapter 4 dives straight into creating visuals and the various ways in which you can draw
to the screen with Pygame. Chapter 5 explores the techniques that game programmers use to
make those images move. You should find the discussion on time-based movement to be par-
ticularly valuable, as it is essential for any kind of animation in a game.
Chapter 6 tells you all you need to know to interface your game with virtually any gaming
device. The sample code in this chapter will have you moving a character around with the key-
board, mouse, and joystick.
Chapter 7 is a little unusual in that it is more self-contained than the others and doesn’t
depend as much on previous chapters. It covers the subject of artificial intelligence and
includes a fully working simulation of an ant’s nest, but the techniques I explain in this chapter
can be used to add seemingly intelligent characters to any game.
Chapters 8 and 9 are a gentle introduction to working with three-dimensional graphics in
Pygame, which is an essential topic since most games have 3D elements these days—even if
they are not full 3D games. I explain the math in visual terms that make it easier to grasp, and
you should find that it is not as an intimidating a subject as it first appears.

Chapter 10 takes a break from 3D graphics to discuss how to use Pygame to add sound
effects and music, and even includes a fully working jukebox application.
The final two chapters build on Chapters 8 and 9 to advance your knowledge of 3D graph-
ics, and explain how to take advantage of the dedicated game hardware on your graphics card.
By the end of Chapter 11 you will have enough knowledge to render and manipulate a three-
dimensional object on the screen. Chapter 12 explores several techniques you can use to create
even more impressive 3D visuals and generate special effects.
In addition to the 12 chapters, there are two appendixes: Appendix A is a reference to the
Game Objects library that is used throughout this book, and Appendix B explains how you can
package your game and send it to others.
8725.book Page xxii Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
■INTRODUCTION
xxiii
Prerequisites
To run the code in this book, you will need at least version 2.4 of Python and version 1.7.1 of
Pygame, which you can download from www.python.org and www.pygame.org, respectively. If
you want to run the 3D sample code, you will also need PyOpenGL, which you can download
from pyopengl.sourceforge.net. All are free software, and this book contains instructions on
how to install them and get started.
Downloading the Code
The source code for this book is available to readers at www.apress.com in the Source Code
section of this book’s home page. Please feel free to visit the book’s home page on the Apress
web site and download all the code there. You can also check for errata and find related titles
from Apress.
Contacting the Author
I am happy to respond to any questions regarding this book’s content and source code. Feel
free to e-mail me at , or alternatively post a comment on my blog:
www.willmcgugan.com.
I hope you find this book informative and that you enjoy reading it! If it inspires you to

write a game, I would be more than happy to be one of your play-testers.
8725.book Page xxiii Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info

×