Acknowledgments |
|
xix | |
Introduction |
|
xxi | |
Who Is This Book For? |
|
xxii | |
Python Version(s) and Installation |
|
xxii | |
How Will I Explain OOP? |
|
xxiii | |
What's in the Book |
|
xxiv | |
Development Environments |
|
xxvi | |
Widgets and Example Games |
|
xxvi | |
|
PART I INTRODUCING OBJECT-ORIENTED PROGRAMMING |
|
|
|
1 Procedural Python Examples |
|
|
3 | (18) |
|
Higher or Lower Card Game |
|
|
4 | (1) |
|
|
4 | (1) |
|
|
4 | (3) |
|
|
7 | (1) |
|
|
7 | (1) |
|
Analysis of Required Operations and Data |
|
|
7 | (1) |
|
Implementation 1 Single Account Without Functions |
|
|
8 | (2) |
|
Implementation 2 Single Account with Functions |
|
|
10 | (2) |
|
Implementation 3 Two Accounts |
|
|
12 | (1) |
|
Implementation 4 Multiple Accounts Using Lists |
|
|
13 | (3) |
|
Implementation 5 List of Account Dictionaries |
|
|
16 | (2) |
|
Common Problems with Procedural Implementation |
|
|
18 | (1) |
|
ObjectOriented Solution --- First Look at a Class |
|
|
19 | (1) |
|
|
20 | (1) |
|
2 Modeling Physical Objects with Object-Oriented Programming |
|
|
21 | (26) |
|
Building Software Models of Physical Objects |
|
|
22 | (1) |
|
State and Behavior: Light Switch Example |
|
|
22 | (1) |
|
Introduction to Classes and Objects |
|
|
23 | (2) |
|
Classes, Objects, and Instantiation |
|
|
25 | (1) |
|
Writing a Class in Python |
|
|
26 | (1) |
|
Scope and Instance Variables |
|
|
27 | (1) |
|
Differences Between Functions and Methods |
|
|
28 | (1) |
|
Creating an Object from a Class |
|
|
28 | (2) |
|
Calling Methods of an Object |
|
|
30 | (1) |
|
Creating Multiple Instances from the Same Class |
|
|
31 | (1) |
|
Python Data Types Are Implemented as Classes |
|
|
32 | (1) |
|
|
33 | (1) |
|
Building a Slightly More Complicated Class |
|
|
33 | (2) |
|
Representing a More Complicated Physical Object as a Class |
|
|
35 | (5) |
|
Passing Arguments to a Method |
|
|
40 | (1) |
|
|
41 | (2) |
|
Initialization Parameters |
|
|
43 | (2) |
|
|
45 | (1) |
|
|
45 | (1) |
|
|
46 | (1) |
|
3 Mental Models Of Objects And The Meaning Of "Self" |
|
|
47 | (10) |
|
Revisiting the DimmerSwitch Class |
|
|
48 | (1) |
|
High-Level Mental Model #1 |
|
|
49 | (1) |
|
|
49 | (3) |
|
What Is the Meaning of "self"? |
|
|
52 | (3) |
|
|
55 | (2) |
|
4 Managing Multiple Objects |
|
|
57 | (32) |
|
|
58 | (2) |
|
|
60 | (1) |
|
|
61 | (1) |
|
Creating Multiple Accounts |
|
|
62 | (2) |
|
Multiple Account Objects in a List |
|
|
64 | (2) |
|
Multiple Objects with Unique Identifiers |
|
|
66 | (2) |
|
Building an Interactive Menu |
|
|
68 | (2) |
|
Creating an Object Manager Object |
|
|
70 | (2) |
|
Building the Object Manager Object |
|
|
72 | (2) |
|
Main Code That Creates an Object Manager Object |
|
|
74 | (2) |
|
Better Error Handling with Exceptions |
|
|
76 | (1) |
|
|
76 | (1) |
|
The raise Statement and Custom Exceptions |
|
|
77 | (1) |
|
Using Exceptions in Our Bank Program |
|
|
78 | (1) |
|
Account Class with Exceptions |
|
|
78 | (1) |
|
|
79 | (2) |
|
Main Code That Handles Exceptions |
|
|
81 | (2) |
|
Calling the Same Method on a List of Objects |
|
|
83 | (1) |
|
Interface vs. Implementation |
|
|
84 | (1) |
|
|
85 | (4) |
|
PART II GRAPHICAL USER INTERFACES WITH PYGAME |
|
|
|
|
89 | (32) |
|
|
90 | (1) |
|
|
91 | (1) |
|
The Window Coordinate System |
|
|
91 | (3) |
|
|
94 | (1) |
|
|
95 | (1) |
|
|
96 | (1) |
|
Bringing Up a Blank Window |
|
|
97 | (3) |
|
|
100 | (2) |
|
|
102 | (3) |
|
|
105 | (4) |
|
Creating a Location-Based Animation |
|
|
109 | (2) |
|
|
111 | (3) |
|
|
114 | (1) |
|
|
114 | (1) |
|
|
115 | (1) |
|
|
116 | (2) |
|
Reference for Primitive Shapes |
|
|
118 | (2) |
|
|
120 | (1) |
|
|
121 | (22) |
|
Building the Screensaver Ball with OOP Pygame |
|
|
121 | (1) |
|
|
122 | (2) |
|
|
124 | (1) |
|
Creating Many Ball Objects |
|
|
125 | (1) |
|
Creating Many, Many Ball Objects |
|
|
126 | (1) |
|
Building a Reusable Object-Oriented Button |
|
|
127 | (1) |
|
|
128 | (2) |
|
Main Code Using a SimpleButton |
|
|
130 | (1) |
|
Creating a Program with Multiple Buttons |
|
|
131 | (2) |
|
Building a Reusable Object-Oriented Text Display |
|
|
133 | (1) |
|
|
133 | (1) |
|
Creating a SimpleText Class |
|
|
133 | (2) |
|
Demo Ball with SimpleText and SimpleButton |
|
|
135 | (2) |
|
Interface vs. Implementation |
|
|
137 | (1) |
|
|
137 | (1) |
|
|
138 | (1) |
|
Using a Callback with SimpleButton |
|
|
139 | (2) |
|
|
141 | (2) |
|
|
143 | (20) |
|
Passing Arguments into a Function or Method |
|
|
144 | (1) |
|
Positional and Keyword Parameters |
|
|
145 | (1) |
|
Additional Notes on Keyword Parameters |
|
|
146 | (1) |
|
Using None as a Default Value |
|
|
146 | (2) |
|
Choosing Keywords and Default Values |
|
|
148 | (1) |
|
Default Values in GUI Widgets |
|
|
148 | (1) |
|
|
148 | (1) |
|
|
149 | (1) |
|
|
150 | (1) |
|
|
151 | (1) |
|
Adding Buttons, Checkboxes, and Radio Buttons |
|
|
152 | (2) |
|
|
154 | (3) |
|
|
157 | (1) |
|
Pygwidgets Example Program |
|
|
157 | (1) |
|
The Importance of a Consistent API |
|
|
158 | (1) |
|
|
158 | (5) |
|
PART III ENCAPSULATION, POLYMORPHISM, AND INHERITANCE |
|
|
|
|
163 | (20) |
|
Encapsulation with Functions |
|
|
164 | (1) |
|
Encapsulation with Objects |
|
|
164 | (1) |
|
|
165 | (1) |
|
Interpretations of Encapsulation |
|
|
165 | (1) |
|
Direct Access and Why You Should Avoid It |
|
|
166 | (4) |
|
Strict Interpretation with Getters and Setters |
|
|
170 | (2) |
|
|
172 | (1) |
|
Making Instance Variables More Private |
|
|
172 | (1) |
|
|
172 | (1) |
|
|
173 | (1) |
|
|
174 | (3) |
|
Encapsulation in pygwidgets Classes |
|
|
177 | (1) |
|
A Story from the Real World |
|
|
178 | (1) |
|
|
179 | (3) |
|
|
182 | (1) |
|
|
183 | (28) |
|
Sending Messages to Real-World Objects |
|
|
184 | (1) |
|
A Classic Example of Polymorphism in Programming |
|
|
184 | (1) |
|
Example Using Pygame Shapes |
|
|
185 | (1) |
|
|
186 | (1) |
|
The Circle and Triangle Shape Classes |
|
|
187 | (3) |
|
The Main Program Creating Shapes |
|
|
190 | (2) |
|
|
192 | (1) |
|
Pygwidgets Exhibits Polymorphism |
|
|
192 | (1) |
|
Polymorphism for Operators |
|
|
193 | (1) |
|
|
194 | (1) |
|
Comparison Operator Magic Methods |
|
|
195 | (1) |
|
A Rectangle Class with Magic Methods |
|
|
196 | (2) |
|
Main Program Using Magic Methods |
|
|
198 | (2) |
|
Math Operator Magic Methods |
|
|
200 | (1) |
|
|
201 | (2) |
|
Creating a String Representation of Values in an Object |
|
|
203 | (2) |
|
A Fraction Class with Magic Methods |
|
|
205 | (3) |
|
|
208 | (3) |
|
|
211 | (30) |
|
Inheritance in Object-Oriented Programming |
|
|
212 | (1) |
|
|
213 | (1) |
|
Employee and Manager Example |
|
|
214 | (1) |
|
|
214 | (1) |
|
|
215 | (2) |
|
|
217 | (1) |
|
The Client's View of a Subclass |
|
|
218 | (1) |
|
Real-World Examples of Inheritance |
|
|
219 | (1) |
|
|
219 | (3) |
|
|
222 | (2) |
|
|
224 | (3) |
|
Multiple Classes Inheriting from the Same Base Class |
|
|
227 | (4) |
|
Abstract Classes and Methods |
|
|
231 | (3) |
|
How pygwidgets Uses Inheritance |
|
|
234 | (2) |
|
|
236 | (2) |
|
The Difficulty of Programming with Inheritance |
|
|
238 | (1) |
|
|
239 | (2) |
|
11 Managing Memory Used By Objects |
|
|
241 | (26) |
|
|
242 | (1) |
|
|
242 | (6) |
|
|
248 | (1) |
|
|
248 | (1) |
|
|
249 | (1) |
|
Class Variables for Counting |
|
|
250 | (1) |
|
Putting It All Together: Balloon Sample Program |
|
|
251 | (2) |
|
|
253 | (1) |
|
|
254 | (2) |
|
|
256 | (2) |
|
Balloon Class and Objects |
|
|
258 | (3) |
|
|
261 | (2) |
|
|
263 | (4) |
|
PART IV USING OOP IN GAME DEVELOPMENT |
|
|
|
|
267 | (14) |
|
|
268 | (2) |
|
|
270 | (2) |
|
|
272 | (1) |
|
|
272 | (2) |
|
|
274 | (2) |
|
|
276 | (2) |
|
|
278 | (1) |
|
|
278 | (1) |
|
Games with Unusual Card Decks |
|
|
279 | (1) |
|
|
279 | (2) |
|
|
281 | (14) |
|
Timer Demonstration Program |
|
|
282 | (1) |
|
Three Approaches for Implementing Timers |
|
|
283 | (1) |
|
|
283 | (1) |
|
|
284 | (1) |
|
Building a Timer by Calculating Elapsed Time |
|
|
285 | (2) |
|
|
287 | (1) |
|
|
287 | (3) |
|
|
290 | (1) |
|
|
291 | (2) |
|
|
293 | (1) |
|
|
294 | (1) |
|
|
295 | (16) |
|
Building Animation Classes |
|
|
296 | (1) |
|
|
296 | (4) |
|
Simple Sprite Sheet Animation Class |
|
|
300 | (4) |
|
|
304 | (1) |
|
Animation Classes in pygwidgets |
|
|
304 | (1) |
|
|
305 | (1) |
|
Sprite Sheet Animation Class |
|
|
306 | (1) |
|
Common Base Class: PygAnimation |
|
|
307 | (1) |
|
Example Animation Program |
|
|
308 | (2) |
|
|
310 | (1) |
|
|
311 | (30) |
|
The State Machine Approach |
|
|
312 | (2) |
|
A pygame Example with a State Machine |
|
|
314 | (5) |
|
A Scene Manager for Managing Many Scenes |
|
|
319 | (1) |
|
A Demo Program Using a Scene Manager |
|
|
320 | (2) |
|
|
322 | (1) |
|
|
323 | (3) |
|
|
326 | (2) |
|
Rock, Paper, Scissors Using Scenes |
|
|
328 | (4) |
|
Communication Between Scenes |
|
|
332 | (1) |
|
Requesting Information from a Target Scene |
|
|
333 | (1) |
|
Sending Information to a Target Scene |
|
|
333 | (1) |
|
Sending Information to All Scenes |
|
|
334 | (1) |
|
Testing Communications Among Scenes |
|
|
334 | (1) |
|
Implementation of the Scene Manager |
|
|
334 | (2) |
|
|
336 | (1) |
|
|
337 | (1) |
|
Communication Between Scenes |
|
|
338 | (2) |
|
|
340 | (1) |
|
|
341 | (26) |
|
|
342 | (1) |
|
|
342 | (3) |
|
|
345 | (2) |
|
Building a Full Game: Dodger |
|
|
347 | (1) |
|
|
347 | (1) |
|
|
348 | (18) |
|
|
366 | (1) |
|
|
366 | (1) |
|
17 Design Patterns And Wrap-Up |
|
|
367 | (10) |
|
|
367 | (1) |
|
|
368 | (1) |
|
Statistical Display Example |
|
|
368 | (5) |
|
Advantages of the MVC Pattern |
|
|
373 | (1) |
|
|
374 | (3) |
Index |
|
377 | |