Beginning Game Programming with Pygame Zero: Coding Interactive Games on Raspberry Pi Using Python 1st Edition Stewart Watkiss download
Beginning Game Programming with Pygame Zero: Coding Interactive Games on Raspberry Pi Using Python 1st Edition Stewart Watkiss download
https://ebookmeta.com/product/beginning-game-programming-with-
pygame-zero-coding-interactive-games-on-raspberry-pi-using-
python-1st-edition-stewart-watkiss/
https://ebookmeta.com/product/python-pygame-and-raspberry-pi-
game-development-sloan-kelly/
https://ebookmeta.com/product/python-pygame-and-raspberry-pi-
game-development-1st-edition-sloan-kelly/
https://ebookmeta.com/product/beginning-robotics-with-raspberry-
pi-and-arduino-using-python-and-opencv-pdfdrive-com-1st-edition-
jeff-cicolani/
https://ebookmeta.com/product/the-lioness-in-bloom-modern-thai-
fiction-about-women-susan-fulop-kepner/
The City of Zirdai 1st Edition Maria V Snyder Snyder
Maria V
https://ebookmeta.com/product/the-city-of-zirdai-1st-edition-
maria-v-snyder-snyder-maria-v/
https://ebookmeta.com/product/patient-reported-outcomes-an-
overview-1st-edition-annabel-nixon-diane-wild-muehlhausen-willie/
https://ebookmeta.com/product/dangerous-fun-the-social-lives-of-
big-wave-surfers-1st-edition-ugo-corte/
https://ebookmeta.com/product/small-steps-to-big-reading-
converting-non-readers-into-readers-1st-edition-hozefa-a-
bhinderwala/
https://ebookmeta.com/product/transnational-advocacy-networks-
and-human-rights-law-emergence-and-framing-of-gender-identity-
and-sexual-orientation-1st-edition-giulia-dondoli/
The Role of the Supreme Court in American Government
and Politics 1835 1864 Charles Grove Haines
https://ebookmeta.com/product/the-role-of-the-supreme-court-in-
american-government-and-politics-1835-1864-charles-grove-haines/
Contents
1. Cover
2. Front Matter
3. 1. Creating Computer Games
4. 2. Getting Started with Python
5. 3. Pygame Zero
6. 4. Game Design
7. 5. Graphic Design
8. 6. Colors
9. 7. Tank Game Zero
10. 8. Sound
11. 9. Object-Oriented Programming
12. 10. Artificial Intelligence
13. 11. Improvements and Debugging
14. Back Matter
Landmarks
1. Cover
2. Table of Contents
3. Body Matter
Stewart Watkiss
Apress standard
© Stewart Watkiss 2020
This work is subject to copyright. All rights are reserved by the Publisher,
whether the whole or part of the material is concerned, specifically the rights
of translation, reprinting, reuse of illustrations, recitation, broadcasting,
reproduction on microfilms or in any other physical way, and transmission or
information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than
use a trademark symbol with every occurrence of a trademarked name, logo,
or image we use the names, logos, and images only in an editorial fashion and
to the benefit of the trademark owner, with no intention of infringement of the
trademark. The use in this publication of trade names, trademarks, service
marks, and similar terms, even if they are not identified as such, is not to be
taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and
accurate at the date of publication, neither the authors nor the editors nor the
publisher can accept any legal responsibility for any errors or omissions that
may be made. The publisher makes no warranty, express or implied, with
respect to the material contained herein.
The book starts with a simple text-based game to cover the basics
of programming in Python. It then quickly moves on to creating
simple graphical games in Pygame Zero. The book introduces
object-oriented programming to make it easier to make more
complex games. It also explains how you can create your own
graphics and sounds.
Throughout the book, you will get to apply the new techniques in
a variety of 2D games. As well as some new games, there are some
variations on class games including a space shooter game and
battleships.
The games are designed to run on the Raspberry Pi, although they
can be used on other platforms that support Python 3 with
Pygame Zero .
The games you make will be playable and hopefully fun to play.
They are only the beginning. If all you ever do is copy the code
from this book, then you are only going to learn so much, but by
adapting and improving these games, they can become more
enjoyable as well as helping you learn more than you ever will
from just typing out code that's written down for you. For each of
the games, there is a list of suggestions for you to develop the
games further.
All the code and resource files used in the book are available
from the page to accompany the book at
https://www.apress.com/gb/book/9781484256497 .
Acknowledgments
My family has been very supportive in my maker activities and
while writing this book. Thank you to my wife Sarah for her
support and to my children Oliver and Amelia who have been a
source of inspiration and help while writing the book. Oliver has
been particularly helpful in testing the games and giving me
feedback, and my daughter’s knowledge of music was a great help
while writing about making sounds.
I’d also like to thank the team behind the Raspberry Pi including
the Raspberry Pi Foundation and the community that has grown
around it. I’ve also been inspired by the work of Nicholas
Tollervey who created the Mu editor that is used throughout the
book and Daniel Pope who created Pygame Zero, without which
the book wouldn’t have been possible.
I’m also grateful to all the support from the team at Apress, to
Jessica Vakili for her support in putting the book together, and to
Sai Yamanoor for the technical review. There are also many other
people who helped to contribute through reviews and getting the
book production ready.
Table of Contents
Chapter 1:Creating Computer Games
Inspiration Rather Than Imitation
Playing Games
Create the Resources
Development Cycle
Making Programming Enjoyable
Python and Pygame Zero
Compiled vs.Interpreted
Choosing a Programming Environment
Summary
Chapter 2:Getting Started with Python
Using the Mu Editor
Python Programming
Variables
Strings and Format
Lists
Dictionaries
Tuples
Conditional Statements (if, elif, else)
Simple Quiz Game
Loops – While, For
While Loop
For Loop
Forever Loop – while True
Changing Loop Flow – break and continue
Functions
Variable Scope
Refactoring the Code
Further Improvements
Summary
Chapter 3:Pygame Zero
Pygame Zero Development
Compass Game
Required Files
Running Mu in Pygame Zero Mode
Adding a Background Image
Adding an Actor
Moving the Sprite Around the Screen
Making the Movements More Realistic
Keeping Game State
Detecting Collisions
Change in Direction
Keeping Score
Adding a Countdown Timer
Final Code for Compass Game Version 0.1
Summary
Chapter 4:Game Design
What Makes a Game Enjoyable?
Challenging but Achievable
Choices and Consequences
Rewards and Progress
Likeable Characters
Storyline/Historical Relevance
Educational
Takes an Appropriate Level of Time to Play
Inclusivity
Age Appropriate
Improving Compass Game
Updated Timer
Adding Obstacles
Adding a High Score
Try and Except
Summary
Chapter 5:Graphic Design
Creating a Theme
File Formats
Bitmap Images
Vector Images
Useful Tools
LibreOffice Draw
Inkscape
GIMP
Blender
Create Using Code
Other Sources
Summary
Chapter 6:Colors
Color Mixing
Bouncing Ball
Background Color Selector
Handling Mouse Events
Creating the Color Selector
Summary
Chapter 7:Tank Game Zero
Vector Image of Tank
Creating a Dynamic Landscape
Calculating the Trajectory
Detecting a Collision
Complete Game Code
Improving the Game
Summary
Chapter 8:Sound
Recording Sound Effects
Creating Artificial Sound Effects
Recording Audio on the Raspberry Pi
Connecting a USB Microphone
Using arecord
Audacity
Recording Sounds with Audacity
Creating Music with Sonic Pi
Downloading Free Sounds and Music
Adding Sound Effects in Pygame Zero
Playing Music in Pygame Zero
Piano Game Created with Tones
Summary
Chapter 9:Object-Oriented Programming
What Is Object-Oriented Programming?
OOP Classes and Objects
Creating a Class, Attributes, and Methods
Creating an Instance of a Class (Object)
Accessing Attributes of an Object
Terminology
Encapsulation and Data Abstraction
Inheritance
Design for Object-Oriented Programming
Matching Pairs Memory Game
Creating the Classes
Program File
Summary
Chapter 10:Artificial Intelligence
Memory Game with AI
A Good Memory
Battleships
Summary
Chapter 11:Improvements and Debugging
Additional Techniques
More About Pygame Zero
More About Pygame
Adding Fonts
Scrolling Screen
Reading from a CSV config file
Joysticks and Gamepads
Creating Arcade Games for Picade
RetroPie
Debugging
Error Messages
Check for Variable Names
Print Statements
IDE Debugging Tools
Rubber Duck Debugging
Performance
Space Shooter Game
Summary
Where Next?
Appendix A:Quick Reference
Pygame Zero
Useful Keywords
Actor (Sprite)
Background Image or Color
Sound Effects
Mouse Events
Keyboard Events
Displaying Text
Python 3
Lists
Dictionaries
Conditional Statements (if, elif, else)
Loops
Python 3 Modules
Random
Math
Time
DateTime
Appendix B:More Information
Python
Pygame Zero
Pygame
Index
About the Author and About the Technical
Reviewer
About the Author
Stewart Watkiss