You are on page 1of 12

c00(FM)_4178.

qxd 10/2/03 11:22 AM Page iii

GAME PROGRAMMING
WITH PYTHON

SEAN RILEY

CHARLES RIVER MEDIA, INC.


Hingham, Massachusetts
c00(FM)_4178.qxd 10/2/03 11:22 AM Page v

Contents

Preface xvii

1 Overview 1
What This Book Is About 1
What Is Python? 1
What Is Game Development? 2
Can Python Be Used for Games? 3
The Contents of This Book 4
Prerequisites 4

SECTION I Introduction 5
2 Game Architecture 7
2.1 Architecture Diagram 8
2.2 Physical Simulations 9
2.3 Data Drivers 10
2.4 Graphics Engines 11
2D Engines 11
3D Engines 12
Graphics Engine Subcomponents 12
Competing APIs 12

v
c00(FM)_4178.qxd 10/2/03 11:22 AM Page vi

vi Contents

2.5 Audio Systems 13


2.6 Game Simulations 14
2.7 User Interfaces 15
2.8 Network Layers 17
Summary 18

3 Python Game Architectures 19


3.1 Performance Characteristics of Python 19
3.2 Pure Python 20
3.3 Python Programming 21
3.4 Python Scripting 22
3.5 Python Data 22
3.6 Other Languages 23
The Lua Programming Language 23
The Tcl Programming Language 23
The Java Programming Language 24
Summary 24

SECTION II Game Infrastructure 25


4 Python Game Framework 27
4.1 Infrastructure Technology 27
4.2 Initialization 28
4.3 The Main Module 29
4.4 The Main Loop 31
4.5 Cleanup 32
4.6 Running the Example Code 34
Summary 34

5 Simulation Concepts 35
5.1 The Physical Simulation 36
5.2 Components of the Physical Simulation 36
5.3 Implementing Simulations with Python 37
5.4 Reactive Simulation 37
c00(FM)_4178.qxd 10/2/03 11:22 AM Page vii

Contents vii

5.5 Controlled Simulation 39


5.6 Parallel Simulation 40
5.7 Simulation Conventions 41
5.8 Variable Rate Updating 43
5.9 The Base Simulation Class 44
5.10 The Simulation World 47
5.11 Updating the Simulation 49
Summary 52

6 Data-Driven Simulations 55
6.1 Benefits of Data-Driven Systems 56
6.2 Data Sources 57
Text Files 57
XML 57
Relational Databases 58
Scripting Languages 58
6.3 Implementing a Data Repository 58
Summary 67

7 Collision Detection 69
7.1 Classifying Objects for Collision 70
7.2 Partitioning the World for Collision Detection 72
7.3 Methods of Collision Checking 74
Per Polygon 74
Per Pixel 75
Bounding Boxes 75
Bounding Spheres 75
Tiles 76
7.4 Coordinate Spaces 77
Coordinate Spaces for Collision Detection 77
Coordinate Spaces and Bounding Boxes 78
7.5 Axially Aligned Bounding Boxes 79
7.6 Bounding Spheres 82
c00(FM)_4178.qxd 10/2/03 11:22 AM Page viii

viii Contents

7.7 Dealing with Moving Objects 84


7.8 Implementing a Collision Grid in Python 87
7.9 Making a Module 94
Summary 95

SECTION III Game Technologies 97


8 Graphics 99
8.1 Art Pipeline 100
8.2 Using Python for Graphics Programming 101
Minimize the Number of Elements Exposed in the Interface 102
Minimize the Amount of Python Code Executed at Runtime 102
Keep Graphics-Specific Data in the Graphics Engine 102
Hide Implementation Details 102
8.3 High-Level Graphics Interface 103
Module Interfaces versus Object Interfaces 103
The Graphics Engine Interface 103
8.4 Python Graphics Engine 107
The Module Interface 107
The Graphics Object Interface 116
Integrating with the Simulation 123
Example Application 127
8.5 Visualizing the Collision System 129
Summary 131

9 Audio 133
9.1 Audio Concepts 134
Amplitude 134
Frequency 134
Channels 134
Sample Rate 134
Pulse Code Modulation 135
9.2 Playing Sound Effects 135
9.3 Ambient Sounds 138
c00(FM)_4178.qxd 10/2/03 11:22 AM Page ix

Contents ix

9.4 Playing Music 141


Summary 142

10 Input 143
10.1 Message Handling 144
10.2 Keyboard Input 146
10.3 Mouse Input 148
10.4 Joystick Input 149
Summary 150

11 Unit Testing 151


11.1 Introduction to Unit Testing 152
11.2 Setup for Testing 152
11.3 Testing the DataManager 154
11.4 Testing the Simulation 155
Summary 159

SECTION IV Game Programming 161


12 Game Simulations 163
12.1 Players and Avatars 164
The PongBallGame Class 165
The PongPlayer Class 167
The Simulation Objects 169
12.2 Game Modes 172
Mode Classes 172
The Breakout Example 174
12.3 The Entity Class 185
12.4 Object Identification 185
12.5 Game Events 188
Summary 195

13 Game Levels 197


13.1 Game Levels 197
c00(FM)_4178.qxd 10/2/03 11:22 AM Page x

x Contents

13.2 Level Data 198


13.3 Level Data in Python 199
13.4 Game Modes for Levels 202
The Pre-Level Mode 205
The Post-Level Mode 207
13.5 Managing Resources with Levels 209
Summary 211

14 User Interfaces 213


14.1 Game User Interfaces 214
UI Drawing 214
Coordinate Systems 214
User Interface Optimizations 217
14.2 PyUI Introduction 217
14.3 PyUI Concepts 218
The Desktop 218
Widgets 218
Panels 219
Windows 219
Frames 219
Dialogs 219
Layout Managers 219
Themes 220
Renderers 220
Fonts 221
Events 221
14.4 Using PyUI 221
CanvasPanel Class 222
CanvasFrame Class 224
SaveDialog Class 227
14.5 Interacting with the Simulation 230
14.6 Drawing Text 235
Bitmap Fonts 235
c00(FM)_4178.qxd 10/2/03 11:22 AM Page xi

Contents xi

Dynamic Fonts 236


Summary 237

15 Artificial Intelligence 239


15.1 Basic State Machines 240
15.2 State Machine Enhancements 244
Transition Validation 244
State Inputs 245
Parallel State Machines 246
15.3 Pathfinding Concepts 253
World Representations for Pathfinding 253
15.4 The A* Algorithm 256
15.5 Implementing A* in Python 258
15.6 Visualizing the Pathfinder 264
Summary 268

16 Procedurally Generated Game Content 269


16.1 Predictable Random Numbers 270
Random Number Distributions 271
Predictable Random Numbers 272
Using the random Module 272
16.2 Terrain Generation 274
Height Maps 274
Mid-Point Displacement 275
16.3 Terrain Generation in Python 279
16.4 Name Generation 287
Summary 293

SECTION V Multiplayer Games 295


17 Network Concepts 297
17.1 Network Identification 298
Internet Names 298
IP Addresses 299
c00(FM)_4178.qxd 10/2/03 11:22 AM Page xii

xii Contents

Name Translation 300


Port Numbers 301
Subnets 302
17.2 TCP/IP 303
17.3 UDP/IP 304
17.4 Blocking and Nonblocking I/O 305
17.5 Bandwidth 305
17.6 Latency 306
17.7 Byte Ordering 307
Summary 309

18 Network Layers 311


18.1 Socket Basics 313
18.2 Sending and Receiving Data 315
18.3 TCP/IP Server Models 317
Connection Management 317
Simple Iterative Server 318
Concurrent Forking Server 321
Concurrent Threaded Server 324
Concurrent Asynchronous Server (Reactor Pattern) 326
18.4 Introduction to Twisted 328
The Twisted Reactor 329
Twisted Protocols 330
18.5 Game Protocol Design 330
Static and Dynamic Protocols 330
Streams and Packing 331
Network Packets 333
Summary 339

19 Clients and Servers 341


19.1 Multiplayer Game Architectures 342
Peer-to-Peer Architecture 342
c00(FM)_4178.qxd 10/2/03 11:22 AM Page xiii

Contents xiii

Client/Server Architecture 344


Massively Multiplayer 345
19.2 Code Organization 347
19.3 A Game Server 348
19.4 A Game Client 356
The LobbyFrame class 364
Summary 366

20 Multiplayer Game Example 367


20.1 Code Organization 368
20.2 The Tic-Tac-Toe Game 369
20.3 The Game Server 370
The TicTacGame Class 372
The TicTacServerProtocol Class 376
The TicTacServer Class 377
Running the Server 377
20.4 The Tic-Tac-Toe Game Client 378
The Client Protocol 378
The Client Application 380
The TicTacWindow Class 381
Running the Client 385
Summary 388

SECTION VI Advanced Topics 389


21 Using the Python C API 391
21.1 The Abstract Object Layer 392
The Object Protocol 393
The Number Protocol 394
The Sequence Protocol 394
The Mapping Protocol 395
Other Protocols 395
c00(FM)_4178.qxd 10/2/03 11:22 AM Page xiv

xiv Contents

21.2 Reference Counting 395


Heap vs. Stack Memory 396
Reference Counting Macros 397
21.3 Exception Handling 398
21.4 Data Conversion 399
Extracting Parameter Values 399
Building Return Values 401
21.5 Concrete Object Layer 402
Fundamental Objects 403
Numeric Objects 404
Sequence Objects 404
Mapping Objects 405
Other Objects 405
Summary 406

22 Extending Python 407


22.1 Writing Extension Modules 408
Using Extension Modules 409
Creating an Extension Module 410
Extension Module Example 413
22.2 Writing Extension Types 422
Type Structures 423
Instance Creation and Deletion 426
Member Function of Extension Types 428
22.3 Compiling Extension Code 432
22.4 Automating Interface Generation 435
SWIG 437
Boost.Python 439
Psyco 440
Summary 442
c00(FM)_4178.qxd 10/2/03 11:22 AM Page xv

Contents xv

23 Embedding Python 443


23.1 Embedding the Interpreter 444
Python Context 446
Executing Python Code 448
23.2 Communicating with the Host Application 449
23.3 Example Application 451
23.3 Project Setup 454
Summary 455

You might also like