Muutke küpsiste eelistusi

Object-Oriented Python: Master OOP by Building Games and GUIs [Pehme köide]

  • Formaat: Paperback / softback, 416 pages, kõrgus x laius: 234x177 mm
  • Ilmumisaeg: 25-Jan-2022
  • Kirjastus: No Starch Press,US
  • ISBN-10: 1718502060
  • ISBN-13: 9781718502062
Teised raamatud teemal:
  • Formaat: Paperback / softback, 416 pages, kõrgus x laius: 234x177 mm
  • Ilmumisaeg: 25-Jan-2022
  • Kirjastus: No Starch Press,US
  • ISBN-10: 1718502060
  • ISBN-13: 9781718502062
Teised raamatud teemal:
"This guide to mastering object-oriented programming with Python covers the basics of building classes and creating objects through clear examples using the pygame package. Also explores key concepts like encapsulation, polymorphism, and inheritance as well as best practices for coding with objects"--

Covering two fully functional Python code packages that will speed up development of graphical user interface (GUI programs in Python, this thorough guide shows how to master object-oriented programming from the ground up. Original.

Power up your Python with object-oriented programming and learn how to write powerful, efficient, and re-usable code.

Object-Oriented Python is an intuitive and thorough guide to mastering object-oriented programming from the ground up. You’ll cover the basics of building classes and creating objects, and put theory into practice using the pygame package with clear examples that help visualize the object-oriented style. You’ll explore the key concepts of object-oriented programming — encapsulation, polymorphism, and inheritance — and learn not just how to code with objects, but the absolute best practices for doing so. Finally, you’ll bring it all together by building a complex video game, complete with full animations and sounds. The book covers two fully functional Python code packages that will speed up development of graphical user interface (GUI)  programs in Python.

Arvustused

"If only I could have learned programming with this book! Any sufficiently advanced Python code will work with classes and Irv Kalb has provided a useful, fun introduction to Object-Oriented Programming (OOP). The projects start simple and build upon each other, always with a specific outcome in mind. For example, the first example is a basic card game written in procedural Python. A dozen chapters later, you'll be including card graphics and keeping track of the state of the deck with OOP concepts like encapsulation, polymorphism, and inheritanceall without reading a dry textbook. Object-Oriented Python is a fun way for new coders to level up their skills." Adam DuVander, EveryDeveloper

"The projects for the most part are real world appropriate, and easily understandable for multiple levels of expertise of the readers . . . Anyone who is interested in learning Python Object Oriented Programming would benefit by having this book in their library. Kudos to both Mr. Kalb and No Starch Press for this book!" Full Circle Magazine

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)
Representing the Data
4(1)
Implementation
4(3)
Reusable Code
7(1)
Bank Account Simulations
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)
Summary
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)
Definition of an Object
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)
Multiple Instances
41(2)
Initialization Parameters
43(2)
Classes in Use
45(1)
OOP as a Solution
45(1)
Summary
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)
A Deeper Mental Model #2
49(3)
What Is the Meaning of "self"?
52(3)
Summary
55(2)
4 Managing Multiple Objects
57(32)
Bank Account Class
58(2)
Importing Class Code
60(1)
Creating Some Test Code
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)
Try and Except
76(1)
The raise Statement and Custom Exceptions
77(1)
Using Exceptions in Our Bank Program
78(1)
Account Class with Exceptions
78(1)
Optimized Bank Class
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)
Summary
85(4)
PART II GRAPHICAL USER INTERFACES WITH PYGAME
5 Introduction to Pygame
89(32)
Installing Pygame
90(1)
Window Details
91(1)
The Window Coordinate System
91(3)
Pixel Colors
94(1)
Event-Driven Programs
95(1)
Using Pygame
96(1)
Bringing Up a Blank Window
97(3)
Drawing an Image
100(2)
Detecting a Mouse Click
102(3)
Handling the Keyboard
105(4)
Creating a Location-Based Animation
109(2)
Using Pygame rects
111(3)
Playing Sounds
114(1)
Playing Sound Effects
114(1)
Playing Background Music
115(1)
Drawing Shapes
116(2)
Reference for Primitive Shapes
118(2)
Summary
120(1)
6 Object-Oriented Pygame
121(22)
Building the Screensaver Ball with OOP Pygame
121(1)
Creating a Ball Class
122(2)
Using the Ball Class
124(1)
Creating Many Ball Objects
125(1)
Creating Many, Many Ball Objects
126(1)
Building a Reusable Object-Oriented Button
127(1)
Building a Button Class
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)
Steps to Display Text
133(1)
Creating a SimpleText Class
133(2)
Demo Ball with SimpleText and SimpleButton
135(2)
Interface vs. Implementation
137(1)
Callbacks
137(1)
Creating a Callback
138(1)
Using a Callback with SimpleButton
139(2)
Summary
141(2)
7 Pygame Gui Widgets
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)
The pygwidgets Package
148(1)
Setting Up
149(1)
Overall Design Approach
150(1)
Adding an Image
151(1)
Adding Buttons, Checkboxes, and Radio Buttons
152(2)
Text Output and Input
154(3)
Other pygwidgets Classes
157(1)
Pygwidgets Example Program
157(1)
The Importance of a Consistent API
158(1)
Summary
158(5)
PART III ENCAPSULATION, POLYMORPHISM, AND INHERITANCE
8 Encapsulation
163(20)
Encapsulation with Functions
164(1)
Encapsulation with Objects
164(1)
Objects Own Their Data
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)
Safe Direct Access
172(1)
Making Instance Variables More Private
172(1)
Implicitly Private
172(1)
More Explicitly Private
173(1)
Decorators and @property
174(3)
Encapsulation in pygwidgets Classes
177(1)
A Story from the Real World
178(1)
Abstraction
179(3)
Summary
182(1)
9 Polymorphism
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)
The Square Shape Class
186(1)
The Circle and Triangle Shape Classes
187(3)
The Main Program Creating Shapes
190(2)
Extending a Pattern
192(1)
Pygwidgets Exhibits Polymorphism
192(1)
Polymorphism for Operators
193(1)
Magic Methods
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)
Vector Example
201(2)
Creating a String Representation of Values in an Object
203(2)
A Fraction Class with Magic Methods
205(3)
Summary
208(3)
10 Inheritance
211(30)
Inheritance in Object-Oriented Programming
212(1)
Implementing Inheritance
213(1)
Employee and Manager Example
214(1)
Base Class: Employee
214(1)
Subclass: Manager
215(2)
Test Code
217(1)
The Client's View of a Subclass
218(1)
Real-World Examples of Inheritance
219(1)
InputNumber
219(3)
DisplayMoney
222(2)
Example Usage
224(3)
Multiple Classes Inheriting from the Same Base Class
227(4)
Abstract Classes and Methods
231(3)
How pygwidgets Uses Inheritance
234(2)
Class Hierarchy
236(2)
The Difficulty of Programming with Inheritance
238(1)
Summary
239(2)
11 Managing Memory Used By Objects
241(26)
Object Lifetime
242(1)
Reference Count
242(6)
Garbage Collection
248(1)
Class Variables
248(1)
Class Variable Constants
249(1)
Class Variables for Counting
250(1)
Putting It All Together: Balloon Sample Program
251(2)
Module of Constants
253(1)
Main Program Code
254(2)
Balloon Manager
256(2)
Balloon Class and Objects
258(3)
Managing Memory: Slots
261(2)
Summary
263(4)
PART IV USING OOP IN GAME DEVELOPMENT
12 Card Games
267(14)
The Card Class
268(2)
The Deck Class
270(2)
The Higher or Lower Game
272(1)
Main Program
272(2)
Game Object
274(2)
Testing with ___name___
276(2)
Other Card Games
278(1)
Blackjack Deck
278(1)
Games with Unusual Card Decks
279(1)
Summary
279(2)
13 Timers
281(14)
Timer Demonstration Program
282(1)
Three Approaches for Implementing Timers
283(1)
Counting Frames
283(1)
Timer Event
284(1)
Building a Timer by Calculating Elapsed Time
285(2)
Installing pyghelpers
287(1)
The Timer Class
287(3)
Displaying Time
290(1)
CountUpTimer
291(2)
CountDownTimer
293(1)
Summary
294(1)
14 Animation
295(16)
Building Animation Classes
296(1)
SimpleAnimation Class
296(4)
Simple Sprite Sheet Animation Class
300(4)
Merging Two Classes
304(1)
Animation Classes in pygwidgets
304(1)
Animation Class
305(1)
Sprite Sheet Animation Class
306(1)
Common Base Class: PygAnimation
307(1)
Example Animation Program
308(2)
Summary
310(1)
15 Scenes
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)
The Main Program
322(1)
Building the Scenes
323(3)
A Typical Scene
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)
run() Method
336(1)
Main Methods
337(1)
Communication Between Scenes
338(2)
Summary
340(1)
16 Full Game: Dodger
341(26)
Modal Dialogs
342(1)
Yes/No and Alert Dialogs
342(3)
Answer Dialogs
345(2)
Building a Full Game: Dodger
347(1)
Game Overview
347(1)
Implementation
348(18)
Extensions to the Game
366(1)
Summary
366(1)
17 Design Patterns And Wrap-Up
367(10)
Model View Controller
367(1)
File Display Example
368(1)
Statistical Display Example
368(5)
Advantages of the MVC Pattern
373(1)
Wrap-Up
374(3)
Index 377
Irv Kalb is an adjunct professor at UCSC-Extension and University of Silicon Valley (formerly Cogswell College), where he teaches introductory and object-oriented programming courses in Python. Kalb has a bachelor's and master's degree in Computer Science, decades of experience developing software while focusing on educational software, and has been teaching for over 10 years. He is the author of Learn to Program with Python 3: A Step-by-Step Guide to Programming (Apress).