-->
Navigation
Python, Pygame, and Raspberry Pi Game Development, Second Edition

Python, Pygame, and Raspberry Pi Game Development, Second Edition

Now pay Easier and Secure using Paypal
Price:

Read more

Sloan Kelly


e-books shop
e-books shop
Purchase Now !
Just with Paypal



Book Details
 Price
 4.00
 Pages
 395 p
 File Size 
 5,991 KB
 File Type
 PDF format
 ISBN-13
 978-1-4842-4532-3 (pbk)
 978-1-4842-4533-0(electronic) 
 Copyright©   
 2019 by Sloan Kelly

About the Author
Sloan Kelly has worked in the games industry
for nearly 12 years. He has worked on a
number of AAA and indie titles and currently
works for an educational game company. He
lives in Ontario, Canada, with his wife and
children. Sloan is on Twitter @codehoose and
makes YouTube videos in his spare time.

About the Technical Reviewer
John Watson is a game developer, artist, guitar
player, husband, and father. Among John’s
many software-powered side projects, he’s
building a Raspberry Pi–powered device that
generates interactive music in live modern
dance performances. He’s also developing a
retro-inspired 2D twin-stick arcade shooter
called Gravity Ace. You can follow his progress
on Twitter @yafd or at gravityace.com. Stop by
and say hi!

Introduction
This book is intended for anyone who wants to learn how to program
games. It is ideally suited to students who want to learn Python and
PyGame on their Raspberry Pi. While not necessary, this book has been
oriented toward the Raspberry Pi computer.
The Python programming language is ideally suited to beginners and
experts alike. The skills you will learn in this book are easily transferable to
other computer languages too.
If you are unfamiliar with the Raspberry Pi, there are several good
eBook guides on getting started including mine called A Slice of Raspberry
Pi, available from all good eBook retailers.
This book assumes that you are familiar with the Raspberry Pi
computer and that you have the Raspberry Pi Foundation’s recommended
Raspbian operating system installed. Raspbian is a distribution of the
Debian Linux operating system built specifically for the Raspberry Pi. This
distribution contains all the Software Development Kits (SDKs) including
one for Python that includes PyGame. If you don’t have Raspbian installed,
you will have to ensure that you have Python and PyGame installed on
your system.
Don’t have a Raspberry Pi? Not to worry, you can still learn Python and
PyGame. The code in this book will work on other OSs with Python and
PyGame installed; Python is a platform-independent language.
You can obtain more information and download versions of Python from
www.python.org. PyGame can be obtained from www.pygame.org/.
Sprites from Ari Feldman’s SpriteLib have been used for the projects
contained in this book.


Table of Contents
About the Author ..............................................................................xvii
About the Technical Reviewer ...........................................................xix
Acknowledgments .............................................................................xxi
Introduction .....................................................................................xxiii
Chapter 1: What Is a Programming Language? ....................................1
What Does a Computer Program Do? ...................................................................2
Conclusion ............................................................................................................3
Chapter 2: What Is Python? ..................................................................5
Programming Styles ..............................................................................................5
Object-Oriented .....................................................................................................7
Functional .............................................................................................................8
What Is Pygame? ..................................................................................................8
Conclusion ............................................................................................................9
Chapter 3: Introducing Python ............................................................11
The Terminal Window ..........................................................................................11
Running the Python Interpreter ...........................................................................12
Python Is Interpreted ...........................................................................................13
Python As a Calculator ........................................................................................13
Keywords ............................................................................................................16
Printing ...............................................................................................................17
String Formatting ..........................................................................................19
Variables .............................................................................................................22
Naming Variables ...........................................................................................22
Python As a Calculator, Part II .............................................................................23
Arithmetic Operators .....................................................................................26
Data Types .....................................................................................................27
Numeric Types ...............................................................................................27
String Formatting Again ................................................................................29
Conclusion ..........................................................................................................31
Chapter 4: Breaking Free from the Interpreter ...................................33
What Is IDLE? ......................................................................................................33
Starting IDLE .......................................................................................................33
Starting a New File .............................................................................................34
Hello, World! ........................................................................................................36
Running from the Command Line ..................................................................36
Running from Inside IDLE ..............................................................................38
Conclusion ..........................................................................................................39
Chapter 5: Making Decisions ..............................................................41
A Note About Blocks ............................................................................................44
Testing for Equality .............................................................................................45
Using Boolean Logic ............................................................................................50
And ................................................................................................................50
Or ...................................................................................................................51
Not .................................................................................................................52
Nesting Ifs ...........................................................................................................52
A Note on Switch ................................................................................................54
Conclusion ..........................................................................................................54
Chapter 6: Making the Raspberry Pi Repeat Itself .............................55
The for Loop ........................................................................................................55
The range() Function .....................................................................................57
While Loops .........................................................................................................58
Counting ........................................................................................................58
Sentinel .........................................................................................................60
Conditional .....................................................................................................62
Conclusion ..........................................................................................................62
Chapter 7: Containers .........................................................................63
Container Nomenclature .....................................................................................63
Tuples ..................................................................................................................64
Removing Elements from a Tuple ..................................................................64
Changing Element Values ..............................................................................65
Tuples in Printing ...........................................................................................66
Deconstructing Tuples ...................................................................................67
Lists ....................................................................................................................67
List Creation ..................................................................................................68
Adding Values to the List ...............................................................................68
Removing Values from a List .........................................................................69
Doctor’s Waiting Room Program ....................................................................70
Dictionaries .........................................................................................................73
Iterating Through Dictionaries .......................................................................74
Adding New Items to Dictionaries .................................................................74
Removing Entries from a Dictionary ..............................................................74
Conclusion ..........................................................................................................75
Chapter 8: Putting It Together: Tic-Tac-Toe ........................................77
The Rules ............................................................................................................77
Program Layout ...................................................................................................79
Variables .............................................................................................................79
The Game ............................................................................................................80
Save and Run ......................................................................................................85
Conclusion ..........................................................................................................85
Chapter 9: Basic Introduction to PyGame ...........................................87
Importing the PyGame Framework .....................................................................87
Initializing PyGame ..............................................................................................88
The Main Loop ....................................................................................................89
Images and Surfaces ..........................................................................................92
Creating Images ..................................................................................................93
Loading Images ...................................................................................................93
Drawing Images ..................................................................................................94
Screen Coordinates and Resolution ....................................................................94
Sprite Sheets ......................................................................................................95
Full Listing ..........................................................................................................97
Conclusion ..........................................................................................................97
Chapter 10: Designing Your Game ......................................................99
Initial Concept .....................................................................................................99
Prototyping ..................................................................................................100
Functional Specification ...................................................................................101
Weapon Firing .............................................................................................101
Program Design ................................................................................................101
Coding ...............................................................................................................102
Testing ..............................................................................................................104
Iteration .............................................................................................................105
Conclusion ........................................................................................................105
Chapter 11: Game Project: Bricks ....................................................107
The Main Framework ........................................................................................108
Images ..............................................................................................................109
Moving the Bat ..................................................................................................110
Bat Initialization ...........................................................................................111
Drawing the Bat ...........................................................................................112
Moving the Bat ............................................................................................112
Moving the Ball .................................................................................................114
Ball Initialization ..........................................................................................114
Ball Movement .............................................................................................115
Bat and Ball Collision ...................................................................................118
Serving the Ball ...........................................................................................120
Brick Wall ..........................................................................................................121
Brick and Ball Collision ................................................................................122
Out of Bounds ..............................................................................................124
Conclusion ........................................................................................................125
Chapter 12: User-Defined Functions .................................................127
What Is a Function? ..........................................................................................127
Format of a Function .........................................................................................127
Functions as a Menial Task/Mnemonic Device .................................................128
Sending Parameters .........................................................................................129
Default Argument Values ...................................................................................131
Named Parameters ......................................................................................132
Returning Values ...............................................................................................133
Returning Tuples ..........................................................................................134
Accessing Global Variables ...............................................................................135
Real-World Example of a Function ....................................................................136
Conclusion ........................................................................................................139
Chapter 13: File Input and Output .....................................................141
Reading a File from Disk ...................................................................................141
Writing Data to a File ........................................................................................143
Reading and Writing Containers to a File ..........................................................144
Writing Your Own Serializer .........................................................................145
Writing Your Own Deserializer .....................................................................147
JSON .................................................................................................................148
JSON Serialization .......................................................................................148
JSON Deserializer ........................................................................................149
Handling Errors .................................................................................................150
Conclusion ........................................................................................................151
Chapter 14: Introducing Object-Oriented Programming ...................153
Classes and Objects ..........................................................................................154
Encapsulation ...................................................................................................154
Abstraction ........................................................................................................155
Inheritance ........................................................................................................155
Polymorphism ...................................................................................................155
Why Should You Use OOP? ................................................................................156
Data Hiding ..................................................................................................156
Reusable ......................................................................................................156
Easier to Code and Test Separately .............................................................156
The Ball Class ...................................................................................................157
Creating an Instance of the Class .....................................................................160
The Ball update( ) Method ............................................................................161
Constructors ................................................................................................162
SOLID ................................................................................................................164
Single Responsibility ...................................................................................165
Open-Closed Principle .................................................................................165
Liskov Substitution ......................................................................................166
Interface Segregation ..................................................................................166
Dependency Inversion .................................................................................167
Conclusion ........................................................................................................170
Chapter 15: Inheritance, Composition, and Aggregation ..................171
Inheritance ........................................................................................................172
Base and Child Classes .....................................................................................173
Programming to the Interface .....................................................................175
A Note About Constructors and Base Classes .............................................175
Composition ......................................................................................................177
Aggregation .......................................................................................................179
Conclusion ........................................................................................................180
Chapter 16: Game Project: Snake .....................................................181
Functions ..........................................................................................................182
Snake Framework .............................................................................................183
Images ..............................................................................................................190
Loading the Images .....................................................................................191
The Game Map ..................................................................................................192
Drawing the ‘Game Over’ Screen ......................................................................193
Drawing the Game ............................................................................................195
Drawing the Walls .............................................................................................196
Drawing the Player Data ...................................................................................198
Drawing the Snake ............................................................................................199
Updating the Game ...........................................................................................202
The updateGame( ) Method ..........................................................................203
Snake Movement .........................................................................................205
Touching a Berry ..........................................................................................206
Collision Detection ............................................................................................208
Helper Functions .........................................................................................208
Conclusion ........................................................................................................212
Chapter 17: Model View Controller ...................................................213
Model ................................................................................................................214
View ..................................................................................................................214
Controller ..........................................................................................................214
Why Use MVC? ..................................................................................................215
The Classes .................................................................................................216
Folder ..........................................................................................................217
The Robot Model .........................................................................................217
The Robot View ............................................................................................219
The Radar View ............................................................................................221
The Robot Controller ....................................................................................222
The Robot Generator ....................................................................................225
Ensuring Constant Speed ..................................................................................227
The Main Robot Program ..................................................................................228
Conclusion ........................................................................................................230
Chapter 18: Audio .............................................................................233
Playing a Sound ................................................................................................234
Playing, Pausing, and Changing Volume ...........................................................235
Conclusion ........................................................................................................240
Chapter 19: Finite State Machines ...................................................241
Game State .......................................................................................................241
Menu System ....................................................................................................241
Non-player Artificial Intelligence .......................................................................242
A Finite State Machine Example .......................................................................243
Finite State Machine Manager ....................................................................244
Conclusion ........................................................................................................249
Chapter 20: Game Project: Invaders .................................................251
The Classes .......................................................................................................253
The Finite State Machine ..................................................................................254
MVC and ‘Invaders’ ...........................................................................................255
The Framework .................................................................................................255
Bitmap Font .................................................................................................259
Interstitial Screens ......................................................................................263
The Main Menu ............................................................................................264
Player and Bullets .............................................................................................267
The Bullet Classes .......................................................................................267
The Player Classes ......................................................................................270
Testing Player ..............................................................................................273
The Alien Swarm Classes ..................................................................................275
Collision Detection ............................................................................................282
Explosions ...................................................................................................282
Collision Controller ......................................................................................285
The Main Program .............................................................................................288
The Main Game State ........................................................................................289
Running the Game ............................................................................................292
Conclusion ........................................................................................................293
Chapter 21: Simple Electronics with the GPIO Pins ..........................295
Voltage, Current, and Resistance ......................................................................296
What You Will Need ...........................................................................................298
Breadboard ..................................................................................................298
Breakout Board ............................................................................................299
Jumper Wires ..............................................................................................300
LEDs ............................................................................................................302
Resistors ......................................................................................................304
Switches ......................................................................................................307
Building a Circuit ...............................................................................................308
Connecting the Breakout Board to the Raspberry Pi ...................................308
Providing Power and Ground .......................................................................311
Adding the LED ............................................................................................312
Completing the Circuit .................................................................................313
Testing the Circuit ........................................................................................315
Pin Meanings ....................................................................................................316
The gpiozero Library .........................................................................................317
The Circuit ...................................................................................................318
The Python Program ....................................................................................319
Getting Button Input ....................................................................................320
Reading Button Input in Python ...................................................................321
Conclusion ........................................................................................................322
Chapter 22: Game Project: Memory ..................................................323
Arranging the Breadboard .................................................................................324
Placing the LEDs ..........................................................................................324
Testing the Circuit .............................................................................................326
Placing the Tact Switches ............................................................................327
Testing the Button Circuit ............................................................................328
The Memory Game ............................................................................................330
The ButtonLED and ButtonLEDCollection Classes .......................................331
The Main Program .......................................................................................334
Full Listing buttonled.py ....................................................................................336
Full Listing memorygame.py .............................................................................338
Conclusion ........................................................................................................339
Chapter 23: Game Project: Quiz ........................................................341
The Electronics .................................................................................................341
Testing the Buttons ......................................................................................342
The Finite State Machine ..................................................................................345
Making the Game ..............................................................................................347
The Questions ..............................................................................................348
UI Helper Classes .........................................................................................354
The Game Runner and Base State Class .....................................................359
Player Input .................................................................................................361
The State Classes ........................................................................................362
Playing the Game ..............................................................................................376
Conclusion ........................................................................................................377
Chapter 24: Conclusion .....................................................................379
Index .................................................................................................381


Bookscreen
e-books shop

Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Spandana Chatterjee
Development Editor: James Markham
Coordinating Editor: Divya Modi
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)

0