CYAN
MAGENTA
YELLOW
BLACK
PANTONE 123 C
Books for professionals by professionals ®
Beginning Game Development with Python and Pygame:
From Novice to Professional
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 (including 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 programming 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
Companion eBook
THE APRESS ROADMAP
See last page for details
on $10 eBook version
Beginning Python:
From Novice to Professional
Foundations of Python
Network Programming
Beginning Game Development
with Python and Pygame
The Definitive Guide to Django:
Web Development Done Right
www.apress.com
ISBN-13: 978-1-59059-872-6
ISBN-10: 1-59059-872-5
53999
US $39.99
Beginning
McGugan
SOURCE CODE ONLINE
Companion
eBook Available
Game Development Python and Pygame
with
Dear Reader,
The EXPERT’s VOIce ® in Open Source
Beginning
Game
Development
with
Python and Pygame
From Novice to Professional
Create stunning video games using
popular open source technologies!
Will McGugan
Shelve in
Python
User level:
Beginner–Intermediate
9 781590 598726
www.it-ebooks.info
this print for content only—size & color not accurate
spine = 0.802" 344 page count
www.it-ebooks.info
8725.book Page i Wednesday, September 26, 2007 8:08 PM
Beginning Game
Development with
Python and Pygame
From Novice to Professional
■■■
Will McGugan
www.it-ebooks.info
8725.book Page ii Wednesday, September 26, 2007 8:08 PM
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
[email protected], or
visit http://www.springeronline.com.
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
[email protected], 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 http://www.apress.com in the Source Code/
Download section.
www.it-ebooks.info
8725.book Page iii Wednesday, September 26, 2007 8:08 PM
For Maria
www.it-ebooks.info
8725.book Page iv Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
8725.book Page v Wednesday, September 26, 2007 8:08 PM
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
v
www.it-ebooks.info
8725.book Page vi Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
8725.book Page vii Wednesday, September 26, 2007 8:08 PM
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
19
Creating Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Understanding Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Understanding Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Defining Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introducing Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . .
Using Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Python in Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introducing import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Useful Modules for Games. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
■CHAPTER 3
Exploring Python. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
19
20
20
23
24
26
27
31
35
35
36
39
Introducing Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
41
History of Pygame. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Installing Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Using Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
vii
www.it-ebooks.info
8725.book Page viii Wednesday, September 26, 2007 8:08 PM
viii
■C O N T E N T S
Hello World Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Understanding Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Retrieving Events. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Mouse Motion Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Mouse Button Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Handling Keyboard Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Filtering Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Posting Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Opening a Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Full-Screen Displays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Resizable Pygame Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Windows with No Borders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Additional Display Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Font Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
When Pygame Goes Wrong . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Pygame in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
■CHAPTER 4
44
50
50
53
53
54
56
56
57
57
59
61
61
62
63
64
65
Creating Visuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
67
Using Pixel Power . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Representing Color in Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Scaling Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Blending Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Storing Images. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Surface Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Drawing with Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
pygame.draw.rect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
pygame.draw.polygon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
pygame.draw.circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
pygame.draw.ellipse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
pygame.draw.arc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
pygame.draw.line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
pgame.draw.lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
pygame.draw.aaline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
pygame.draw.aalines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
67
68
69
71
73
75
76
76
83
83
84
85
86
87
87
88
89
89
89
www.it-ebooks.info
8725.book Page ix Wednesday, September 26, 2007 8:08 PM
■C O N T E N T S
■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
111
Controlling the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Understanding Keyboard Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Detecting Key Presses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Directional Movement with Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Rotational Movement with Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Implementing Mouse Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Rotational Movement with the Mouse . . . . . . . . . . . . . . . . . . . . . . . .
Mouse Gameplay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Implementing Joystick Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Joystick Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Joystick Direction Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Joystick Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Seeing Joysticks in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
■CHAPTER 7
Accepting User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
111
112
112
115
118
120
121
124
124
125
128
133
133
137
Take Me to Your Leader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
139
Creating Artificial Intelligence for Games . . . . . . . . . . . . . . . . . . . . . . . . . . 139
What Is Intelligence? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
www.it-ebooks.info
ix
8725.book Page x Wednesday, September 26, 2007 8:08 PM
x
■C O N T E N T S
Exploring AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Implementing State Machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Game Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Building Worlds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Ant Entity Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Building the Brains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
■CHAPTER 10
165
165
167
169
170
171
172
172
175
179
Exploring the Third Dimension. . . . . . . . . . . . . . . . . . . . . . . . . . . .
181
What Is a Matrix? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using the Matrix Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Introducing OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Installing PyOpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Initializing OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
OpenGL Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Seeing OpenGL in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
■CHAPTER 9
Moving into the Third Dimension . . . . . . . . . . . . . . . . . . . . . . . . .
Creating the Illusion of Depth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Understanding 3D Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using 3D Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Time-Based Movement in 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Projecting 3D Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Parallel Projections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Perspective Projections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A 3D World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
■CHAPTER 8
140
141
143
144
147
148
163
181
183
196
196
197
197
203
210
Making Things Go Boom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
211
What Is Sound? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Storing Sound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sound Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Sound Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Stock Sound Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
211
212
213
214
216
www.it-ebooks.info
8725.book Page xi Wednesday, September 26, 2007 8:08 PM
■C O N T E N T S
Playing Sounds with Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sound Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sound Channels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Mixer Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Hearing the Mixer in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Playing Music with Pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Obtaining Music. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Playing Music . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Hearing Music in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
■CHAPTER 12
Lights, Camera, Action! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
235
Working with Textures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Uploading Textures with OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Texture Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Rendering Textures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Deleting Textures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Seeing Textures in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Mip Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Texture Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Storing Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
OBJ Format for 3D Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Seeing Models in Action. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
■CHAPTER 11
216
217
218
221
221
226
226
227
228
233
235
235
238
239
240
240
244
245
248
248
249
250
260
Setting the Scene with OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . .
263
Understanding Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Enabling Lighting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Setting Light Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Working with Materials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Tweaking Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Managing Lights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Understanding Blending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using Blending . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Seeing Blending in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Blending Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
263
264
264
266
266
267
267
267
271
275
www.it-ebooks.info
xi
8725.book Page xii Wednesday, September 26, 2007 8:08 PM
xii
■C O N T E N T S
Understanding Fog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Fog Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Seeing Fog in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Rendering the Backdrop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Skyboxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Where to Go for Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
■APPENDIX A
Game Object Reference
275
275
276
277
278
282
283
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Importing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Contributing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
gameobjects.color.Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
gameobjects.matrix44.Matrix44 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
gameobjects.vector2.Vector2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
gameobjects.vector3.Vector3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Class Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
www.it-ebooks.info
285
285
285
286
286
286
287
287
287
287
288
289
290
290
290
290
291
291
291
291
292
292
8725.book Page xiii Wednesday, September 26, 2007 8:08 PM
■C O N T E N T S
■APPENDIX B
Packaging Your Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
293
Creating Windows Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Using py2exe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Building the Installer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Packages for Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Creating Packages for the Mac . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
293
294
294
296
296
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
www.it-ebooks.info
xiii
8725.book Page xiv Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
8725.book Page xv Wednesday, September 26, 2007 8:08 PM
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 written a script to automate source code backup as well as several popular
desktop applications and a web site in Python. Will is currently working 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.
xv
www.it-ebooks.info
8725.book Page xvi Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info
8725.book Page xvii Wednesday, September 19, 2007 4:23 PM
About the Technical Reviewer
■
RICHARD JONES organizes the biannual Python Programming Game
Challenge (PyWeek http://www.pyweek.org/) challenge and develops
OpenGL applications in Python for a living.
xvii
www.it-ebooks.info
8725.book Page xviii Wednesday, September 26, 2007 8:08 PM
www.it-ebooks.info