PDF Python, Pygame, and Raspberry Pi Game Development 2nd Edition Sloan Kelly download
PDF Python, Pygame, and Raspberry Pi Game Development 2nd Edition Sloan Kelly download
com
https://textbookfull.com/product/python-pygame-and-
raspberry-pi-game-development-2nd-edition-sloan-kelly/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/biota-grow-2c-gather-2c-cook-loucas/
textboxfull.com
https://textbookfull.com/product/beginning-robotics-with-raspberry-pi-
and-arduino-using-python-and-opencv-2nd-edition-jeff-cicolani/
textboxfull.com
https://textbookfull.com/product/beginning-robotics-with-raspberry-pi-
and-arduino-using-python-and-opencv-2nd-edition-jeff-cicolani-2/
textboxfull.com
https://textbookfull.com/product/raspberry-pi-for-python-programmers-
cookbook-second-edition-tim-cox/
textboxfull.com
Raspberry Pi 3 Cookbook for Python Programmers: Unleash
the potential of Raspberry Pi 3 with over 100 recipes Tim
Cox
https://textbookfull.com/product/raspberry-pi-3-cookbook-for-python-
programmers-unleash-the-potential-of-raspberry-pi-3-with-
over-100-recipes-tim-cox/
textboxfull.com
https://textbookfull.com/product/beginning-robotics-with-raspberry-pi-
and-arduino-using-python-and-opencv-jeff-cicolani/
textboxfull.com
https://textbookfull.com/product/hello-raspberry-pi-python-
programming-for-kids-and-other-beginners-1st-edition-ryan-heitz/
textboxfull.com
https://textbookfull.com/product/build-your-own-robot-using-python-
crickit-and-raspberry-pi-1st-edition-marwan-alsabbagh/
textboxfull.com
Sloan Kelly
Python, PyGame, and Raspberry Pi Game Development
Sloan Kelly
Niagara Falls, ON, Canada
v
Table of Contents
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
vi
Table of Contents
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
vii
Table of Contents
viii
Table of Contents
Testing��������������������������������������������������������������������������������������������������������������104
Iteration�������������������������������������������������������������������������������������������������������������105
Conclusion��������������������������������������������������������������������������������������������������������105
ix
Table of Contents
Returning Values�����������������������������������������������������������������������������������������������133
Returning Tuples������������������������������������������������������������������������������������������134
Accessing Global Variables�������������������������������������������������������������������������������135
Real-World Example of a Function��������������������������������������������������������������������136
Conclusion��������������������������������������������������������������������������������������������������������139
x
Table of Contents
xi
Table of Contents
xii
Table of Contents
xiii
Table of Contents
xiv
Table of Contents
Index�������������������������������������������������������������������������������������������������381
xv
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.
xvii
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!
xix
Acknowledgments
I would like to thank Divya Modi, Spandana Chatterjee, and the entire
team at Apress for giving me the opportunity to write the second edition
of this book. A special thank you to Divya for keeping me in the loop
throughout this process. I would also like to thank John Watson for the
feedback that he gave while reviewing the text.
Thank you also to Eben Upton who gave us the little machine we will
use to make games and explore electronics with and to Pete Shinners
for starting PyGame and the community for keeping it going. Finally, a
huge thank you to Guido van Rossum for designing the excellent Python
language that you, dear reader, are about to learn and enjoy.
xxi
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.
xxiii
Introduction
• User-defined functions
• File input/output
There are five projects that produce complete games, all the code and
resources for which are on the web site www.sloankelly.net/. The five
games are
• Bricks
• Snake
• Invaders
• Copycat
• Couch quiz
Throughout the book are lines of code that you can type in to tell the
computer to perform actions, or to add text to a file. In the book, these
lines will appear like this:
xxiv
Introduction
name='Sloan'
if (name=='Sloan'):
print ('Hello', name)
The line that starts with “print” has been indented using the “tab” key.
OS commands that are to be typed into a command window like
Terminal will be preceded with a “$” sign:
$ ls -al
xxv
CHAPTER 1
What Is a
Programming
Language?
A computer program is a list of statements that a computer must carry
out in order to complete a task, usually a repetitive task that would take
a human a long time to calculate. A computer language describes the
arrangement or syntax of those statements. There are various computer
languages out there, each suitable to one or more tasks.
Each language has its own unique syntax and set of commands, but
they all have constructs that perform roughly the same types of actions:
• Input
• Output
• Loops
for any other purpose in your program. We’ll dive deeper into them later in
this book, but examples of keywords in Python are
• for
• if
• pass
2
Chapter 1 What Is a Programming Language?
Conclusion
Computer programs are used to perform laborious tasks on a series of
data elements that are input by users. For games, that means updating the
player avatar location and maintaining the game world while displaying it
to the player.
It is not advisable to stick to one language but rather experience as
many languages as you can. This will enable you, the programmer, to
decide which language is best for a given situation. Your first language is
a great choice; Python is a very powerful language that can be used for a
variety of purposes and is perfect for the first-time programmer.
3
CHAPTER 2
What Is Python?
Python is a modern programming language that supports object-oriented,
functional, and imperative programming styles. It is ideal for the beginner
because of its readability and ease of use. The upside to all of this is that
you can write programs in less lines of code than an equivalent C/C++ or
Java program.
What on earth did I just say? Let’s break that last paragraph down and
make it a little more readable.
P
rogramming Styles
Python is suitable for programming in the following styles:
• Imperative
• Object-oriented
• Functional
Imperative programming was for the longest time the most common
way to write computer code. It describes step by step how to perform
changes to the data in a very linear manner.
For example, we have the following items:
• Tea bag
• Milk
• Cup
• Spoon
• Kettle
• Water
These are the things we use and manipulate in our ‘program’; this is
our data. We want to change this data to a different state. What state? Well,
we want a cup of milky tea. How do we do that? We prescribe a series of
operations that will transform this data into some other data like so:
• Serve
addTo(cup, tea_bag)
addTo(kettle, water)
boil(kettle)
while isBoiling(kettle):
watchTV()
addTo(cup, getWaterFrom(kettle))
addTo(cup, milk)
stir(cup)
serve(cup)
6
Chapter 2 What Is Python?
These are the prescribed steps (process) to change our initial data (our
input) and transform it into our output. See Figure 2-1.
O
bject-Oriented
Imperative programs separate the functionality (the algorithm) from the
data. Object-oriented languages keep the functionality with the data.
Objects contain the data and the instructions used to manipulate that data
in one place.
There is an advantage to this; algorithms stored with it process your
data. Let’s take a pencil as an example. It has certain attributes that
describe it:
• Color
• Hardness
• Nib size
• Length
• Write
• Erase
• Sharpen
7
Chapter 2 What Is Python?
These methods change the state of the object; remember that state
is determined by the data. For example, when you write using a pencil,
the nib length gets smaller and smaller. When you sharpen the pencil, its
overall length gets shorter, but the nib size is reset to its maximum.
F unctional
Functional programming is not new and was first developed in the
1930s. It has its roots in lambda calculus. Functional programming uses
mathematical functions to perform calculations. No data is changed in
these calculations; instead new values are calculated. This means that
functional programs have no state.
Functional programming tends to be used for recursion (calling the
same function from itself ) and iteration through items.
In Python, Fibonacci numbers can be calculated with the following
one line:
fib(5)
What Is Pygame?
Pygame was started by Pete Shinners as a wrapper around the Simple
DirectMedia Library (SDL). It has been maintained by the community
since 2000 and is released under the GNU Lesser General Public License.
Which means you are free to look at the source code if you so choose.
8
Chapter 2 What Is Python?
C
onclusion
Python is a modern, multiparadigm programming language. It can be used
for imperative, object-oriented, and functional programming.
In addition, Pygame is a framework that allows you to create fast-paced
action games in 2D.
So, now that we know what Python is capable of, it’s time we looked at
the language itself.
9
Exploring the Variety of Random
Documents with Different Content
The Battle of Manila, May 1, 1898.—The American Fleet.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must, at
no additional cost, fee or expense to the user, provide a copy, a
means of exporting a copy, or a means of obtaining a copy upon
request, of the work in its original “Plain Vanilla ASCII” or other
form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com