Buy Ebook Python, Pygame, and Raspberry Pi Game Development 2nd Edition Sloan Kelly Cheap Price
Buy Ebook Python, Pygame, and Raspberry Pi Game Development 2nd Edition Sloan Kelly Cheap Price
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
Change “how their pride brought Kawun and Koonbhkurun to such
destruction” to Rawun and Koombhkurun.
“you destroyed the great demon, Kawun: and whenever devils” to
Rawun.
[Chapter II]
“On the hearing this, the Raja Pureechit said to Shookdeo” delete
the first the.
[Chapter III]
“joining its hands, came before him Vishnù said to it” add period
after him.
[Chapter VI]
“the lucky moment, and exact, ray of light, and having
considered” delete the comma after exact.
“on their foreheads, and given them pawn, (betle leaf,) dismissed”
to betel.
“the lines of fate cannot he effaced by one. For this reason” to be.
[Chapter X]
“went and saved the milk; Whilst Krishnù, having broken the
vessels” to whilst.
[Chapter XII]
“of the trees falling, exactly as they had occured” to occurred.
[Chapter XIII]
“Brother! this is some very frighful cave, do not let us go inside” to
frightful.
[Chapter XIV]
“Brothers! this is a nice place, why need We go farther?” to we.
[Chapter XVI]
“Shree Shoodeo said,—O great king! when Shree Krishnù” to
Shookdeo.
“of a kudum tree, placing, his head on the thigh of a companion.”
Delete the second comma.
[Chapter XXIV]
(where the wives were cooking, and said them, “Shree Krishnù
has) to said to them.
[Chapter XXXI]
(began to say,—“Tell me friend! where has Mohun gone) add
comma after me.
[Chapter XXXVII]
(touch other persons.” Krihsnù replied, “I will summon all the) to
Krishnù.
“and the cowherdessess and cowherds, to celebrate with songs”
to cowherdesses.
[Chapter XLVI]
“We will accomplish, whatever you may order us to perform.”
Delete the comma.
“First of all send an invitation to all their brethen” to brethren.
“there is a demon, named Sunkasoor, in the form of a shell” to
Sunkhasoor.
[Chapter XLVII]
“I will give an account now how Shree Kishnù Chund brought” to
Krishnù.
(whether they still continued to love him Nund added, “Give me)
add period after him.
“although other bees buz over it during the whole night” to buzz.
[Chapter L]
“O lord of the earth! when Shree Khrishnù Jee heard these words”
to Krishnù.
[Chapter LI]
“the descendants of Judoo with you, proctect the fort.” to protect.
[Chapter LII]
(be released from it.” Shree Krishnù Jee; said, “Listen,) delete the
semicolon.
[Chapter LIII]
“There is a saying, that it most fitting to contract marriages” add
is after it.
“Raja Rheekmuk was at first very sad, when he heard these” to
Bheekmuk.
“Rookum has given thee to Sissoopal; and now Rookmunee, you
have become a queen.” add comma after now.
“pillars formed of sprouting plaintain were fixed in the earth” to
plantain.
[Chapter LIV]
“Shree Kishnù Chund, having thus inspired the brahmin” to
Krishnù.
“to receive them: and having, met the bridegroom with great
honour” delete the comma.
“for the reception of the bridegroon and his train, entertained” to
bridegroom.
[Chapter LV]
(performing the ceremony “Arta,” showerd down flowers) to
showered.
“where Rajas Oorgursen, Soorsen, Basoodeo and all the grandees”
to Oogursen.
[Chapter LVII]
“conferring happiness and wealth; Worship it continually,” to
worship.
“and suffering great afflction, they began to weep and lament” to
affliction.
(explained all his anxieties to his wife His wife said, “O husband!)
add period after the first wife.
[Chapter LVIII]
“went thence to Akroor, joining his hands bowing his head,” add
comma after hands.
“and came to Shree Krishnù Chund, the extirpater of grief” to
extirpator.
“and there was abundance All the distress and want that had
been” add period after abundance.
[Chapter LX]
“Tell me what answer I am to take back from you to this requst”
to request.
“he ate up all the sweatmeats and other confections” to
sweetmeats.
[Chapter LXI]
“abandoning your worship, medidation, prayers, penance and
guidance” to meditation.
[Chapter LXIII]
(at hearing Banasoor, thus address him, and began to say himself,
“I granted) delete the first comma and add to after say.
“Shree Krishuù, will descend upon the earth in the family of
Judoo” to Krishnù.
(Chitrekha replied, “How shall he escape me?” Chitreka proceeded
to) to Chitrekha.
[Chapter LXIV]
“Banasoor worshipping calling to mind, and meditating upon Shree
Muhadeo Jee” add comma after worshipping.
[Chapter LXV]
“as to reckon the number of cows Raja Narug gave away” to
Nurug.
[Chapter LXVI]
“all the cowherds’ chidren came running out, when they saw the
flag” to children.
“all the materials, necessary for the festival were immediately
produced.” Delete the comma.
[Chapter LXIX]
“and all the Kuoruvùs rose haughtily and went to their homes” to
Kouruvùs.
[Chapter LXXII]
“came Shree Krishnù Chund Jee; Wherever they encamped, a
handsome” to wherever.
[Chapter LXXIII]
(I have still one desire.” Krishnù enquired, “What it is?”) to is it.
“the world has been deceived by your delusive power you behave
towards us as a mortal” add exclamation mark after power.
“the money you have given me to whom shall I go now, and ask
for more?” add comma after me.
“my business is to keep watch in a cemetry, and to receive a tax”
to cemetery.
“and died from excess of the hunger het hen endured, and
afterwards” to he then.
[Chapter LXXV]
“Do not use your amrs against him, stand here and look on” to
arms.
[Chapter LXXVI]
“clearing away what food was left, and the treachers, formed of
leaves” to trenchers.
“on the contrary, their character was enchanced” to enhanced.
[Chapter LXXVII]
“when Shree Krishnù Chund and Bulram Jee were at hustinapoor”
to Hustinapoor.
“he constantly rose up and eat a handful of sand; and performed
difficult penance” to ate.
[Chapter LXXVIII]
“this man is without good qualites, avaricious and proud” to
qualities.
[Chapter LXXIX]
“O great king! when Sounuk and all the saints, and sages having
received Bulram Jee’s order” delete the comma after saints.
[Chapter LXXX]
“and he had nothing to eat, One day Soodama’s wife being”
change the comma to a period.
[Chapter LXXXII]
“and Renooka came stood near her husband’s body, she rolled
upon the ground” to came and stood.
“I have had no controul in these matters, the condition or” to
control.
[Chapter LXXXIII]
“O great king! listen, whilst I gave an account of the
conversations” to give.
[Chapter LXXXV]
(Krishnù enquired, “What it was?” Dewukee Jee replied, “Son!
grief) to What is that.
“when the inhabitatns heard this, there was joy throughout the
city” to inhabitants.
[Chapter LXXXVI]
“explained to Buldeo all the secreets connected with the carrying
off Soobhudra” to secrets.
“having beheld the sincere faith of of those two worshippers”
delete one of.
[Chapter LXXXVII]
“for this reason, that one diety is both without, and with passions”
to deity.
“when fourteen worlds became the semblence of water” to
semblance.
“and that on what sidesoever we look, there you are presented” to
side soever.
[Chapter LXXXIX]
“prepared to be burnt upon it with his bow and arows having
then” to arrows.
“upon the head of the king of the serpent face was an
ornamented throne” to race.
“flowers reaching to his feet, and a pearl of necklace” to necklace
of pearls.
[Chapter XC]
“holy and virtuous men sung, the praises of Huri during the eight
watches” delete the comma.
“surpassed the other, how shall I attempt to describe them.”
change period to question mark.
“Whatever benefit is derived from, religious penance, sacrifice,
gifts, fasting, pilgrimage” delete comma after from.
[End of text]
*** END OF THE PROJECT GUTENBERG EBOOK THE PREM SAGUR
***
Updated editions will replace the previous one—the old editions will
be renamed.
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.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
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