Coding Games in Python 1st Edition Carol Vorderman all chapter instant download
Coding Games in Python 1st Edition Carol Vorderman all chapter instant download
com
https://textbookfull.com/product/coding-games-in-python-1st-
edition-carol-vorderman/
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/how-to-be-good-at-maths-carol-
vorderman/
textboxfull.com
https://textbookfull.com/product/coding-for-kids-python-learn-to-code-
with-50-awesome-games-and-activities-2019th-edition-adrienne-b-tacke/
textboxfull.com
https://textbookfull.com/product/help-your-kids-with-music-a-unique-
step-by-step-visual-guide-carol-vorderman/
textboxfull.com
Help Your Kids with Maths A Unique Step by Step Visual
Guide Carol Vorderman
https://textbookfull.com/product/help-your-kids-with-maths-a-unique-
step-by-step-visual-guide-carol-vorderman/
textboxfull.com
https://textbookfull.com/product/help-your-kids-with-english-a-unique-
step-by-step-visual-guide-carol-vorderman/
textboxfull.com
https://textbookfull.com/product/the-python-book-the-ultimate-guide-
to-coding-with-python-imagine-publishing/
textboxfull.com
https://textbookfull.com/product/competitive-programming-in-
python-128-algorithms-to-develop-your-coding-skills-1st-edition-
christoph-durr/
textboxfull.com
codinG
Games
PYTHON
R
IN
codinG
Games
PYTHON
R
IN
DK UK
Project editor Ben Ffrancon Davies
Senior art editor Sunita Gahir
Consultant editor Craig Steele
Jacket design development manager Sophia MTT
Jacket editor Claire Gell
Producer, pre-production Gillian Reid
Senior Producer Alex Bell
US editors Jill Hamilton, Kayla Dugger
Managing editor Lisa Gillespie
Managing art editor Owen Peyton Jones
Publisher Andrew Macintyre
Associate publishing director Liz Wheeler
Art director Karen Self
Design director Phil Ormerod
Publishing director Jonathan Metcalf
DK INDIA
Senior editor Bharti Bedi
Project art editor Sanjay Chauhan
Editor Tina Jindal
Assistant art editors Rabia Ahmad,
Simar Dhamija, Sonakshi Singh
Jacket designer Juhi Sheth
Jackets editorial coordinator Priyanka Sharma
Managing jackets editor Saloni Singh
DTP designer Sachin Gupta
Senior DTP designer Harish Aggarwal
Senior managing editor Rohan Sinha
Managing art editor Sudakshina Basu
Pre-production manager Balwant Singh
First American Edition, 2018
Published in the United States by DK Publishing
345 Hudson Street, New York, New York 10014
A WORLD OF IDEAS:
SEE ALL THERE IS TO KNOW
www.dk.com
CAROL VORDERMAN MBE is one of Britain’s best-loved TV presenters and is
renowned for her mathematical skills. She has hosted numerous TV shows on
science and technology, from Tomorrow’s World to How 2, and was co-host of
Channel 4’s Countdown for 26 years. A Cambridge University engineering
graduate, she has a passion for communicating science and technology and
is particularly interested in coding.
12 What is Python?
14 Gaming in Python
16 Installing Python
18 Installing Pygame Zero
20 Using IDLE
22 Your first program
28 Creating variables
32 Making decisions
36 Playing with loops
40 Functions
44 Fixing bugs
What is the
capital of France? 7
London Paris
Berlin Tokyo
6 2
Computer programmers are the unsung heroes of the modern world. From smartphones
to laptops, traffic systems to bank cards, their hard work touches almost every aspect
of our lives. Behind each technological advance is a team of creative coders.
Over the past 30 years, computer games have become one of the most exciting and
popular areas of the entertainment industry to work in. Becoming a game programmer
takes creative flair to help create the story, graphics, music, and characters you need
for your games, and the technical know-how to bring them to life. Who knows? This
book may be the very first step on your journey from gamer to game maker.
Learning to code isn’t just for people who want to be professional programmers,
though. Coding skills are useful in lots of different jobs that may seem to have nothing
to do with computers at first. Programming expertise is essential to subject areas as
diverse as science, business, art, and music.
This book uses a programming language called Python®, a fairly simple text-based
language, and is perfect for beginners, or as a step up from Scratch™. However,
unlike Scratch, it was not created especially to teach coding. Python is as popular with
budding coders as it is with professionals, and is one of the most widely used
professional programming languages in the world. It pops up in banking, medicine,
animation, and even space exploration.
The best way to learn any new language is to get immersed in it, and programming
languages are no different. Building your own computer games is a fun way to combine
theory and practice. If you’re a brand-new coder, start off with the basics at the
beginning of this book before moving on to the more complex games as the book
progresses. By following the step-by-step guides, you’ll find out how professional
coders think when they’re building a computer game. Follow those steps carefully and
you’ll have your own games up and running in no time. Then, if you really want to push
yourself, you can try tweaking the code to make your games unique.
Most importantly, have fun! Once you’ve completed the games, you can show them off
to your friends and family—they’ll be amazed by what you’ve managed to make. This
book is packed with games to suit every audience, and we hope you enjoy building and
playing them as much as we enjoyed creating them for you.
1+1=2
◁ Diverse applications
Python is used to build
△ Easy to understand systems and tools for lots of
Unlike some other programming languages, Python interesting tasks in different
doesn’t use complicated symbols. You type the industries, such as banking,
code using a mixture of English words, characters, and healthcare, the space industry,
numbers, so it’s easy to read and write—just like a book. education, and many others.
print("Hello World!")
when clicked This block displays
the message in a Type your
say Hello World! speech bubble. message here.
Hello World!
The message appears
on the screen like this.
Hello World!
△ Print in Python
△ Print in Scratch In Python, the “print” command displays
In Scratch, the “say” block is used to a message on the screen.
display a message on the screen. This command
is executed if
the condition
after if is True. if a == 2:
if a = 2 then This message is
print("Hello!") displayed if the
This message is else: answer is False.
say Hello!
displayed if the
answer is True. print("Goodbye!")
else
This command
say Goodbye!
is executed if the
This is the end of the condition after
“if-then-else” block. if is False.
Goodbye!
Hello! ? Goodbye!
Gaming in Python
Video games are computer programs that
contain a bunch of instructions. Python can
be used to build lots of different types of
games. With Python, there’s something for
every gamer!
Types of games
There are lots of different categories, or
genres, of computer games. These range
from simple one-button games to more ▷ One button
complex strategy ones. Which genre would With Python, you can build fun,
you like to create first? action-packed games that only
need one button to be played.
These games are so addictive,
you’ll want to play them over
and over again.
◁ Puzzles
Puzzles are a great
way to exercise your
brain or test someone’s
general knowledge.
They come in all shapes
and sizes, from jigsaw △ Platform
puzzles to word and Platform games, such as racing games, create
number games. the illusion of speed by making the background
scroll past the player’s viewpoint. The gameplay
generally involves moving around obstacles or
jumping over them.
◁ Strategy
A strategy game is all
△ Multiplayer about decisions. You
Some games you play by yourself, but others let you need to plan ahead
compete against other players. You can use Python to and make the right
build multiplayer games and challenge your friends. choices to win.
GAMING IN PYTHON 15
Python modules EXPERT TIPS
Python has bundles of code called “modules” Downloading modules
that help you complete common coding
tasks. You can use these modules by Python comes with several built-in modules
for developing games, such as Pyglet. But
importing them into your programs.
some other modules, like Pygame, have to
Here are some Python modules that you be downloaded separately.
might find useful.
▽ Pygame
Pygame is designed for writing games in Python.
With this module, you can easily add and control
game characters, update scores and timers, use ◁ Pygame Zero
special animations and graphics, and use gamepads
Pygame Zero is a great module for
and joysticks with your games. It is not a built-in
beginner game programmers. It’s a
Python module, so it needs to be installed separately.
simplified version of Pygame, which makes
it even easier to get started by hiding some
of Pygame’s more complicated features. It
comes with several tools that are useful for
beginners, but it’s also powerful enough
to build some impressive games.
▷ Random
This module can pick a random
number or shuffle a list into a
random order. It is great for
▷ Math adding an element of chance
to a game. Use it when you
Math is a standard Python
want to simulate rolling dice
module that can be used to
or when choosing a random
perform simple calculations
enemy for the player to face.
in games. However, you might
need to use other modules for
trickier calculations.
◁ Tkinter
This tool is used to build
◁ Time simple graphics in games
This module provides tools to to create Graphical
work with time and dates in User Interfaces (GUIs,
a program. For example, you pronounced “goo-eys”)
might need to calculate how that let users interact
many seconds have passed with, and control,
since a game started. Python programs.
16 G E T T I N G S TA R T E D
The games in this book use Python 3. It’s When you install Python 3, you will
free, and you can download it from the also get another free program called
Python website. Follow the instructions IDLE (short for Integrated Development
Environment). Designed for
that match your computer. Never install beginners, IDLE includes a basic
any program unless you have the computer text editor that allows you to write
owner’s permission. and edit Python code.
Z Z Z
Installing Python on Windows
First you need to find out if your computer uses the
32-bit or 64-bit version of Windows. Go to the Start
menu, then Computer, Properties, and choose
System if the option appears.
1 Download Python
Go to www.python.org and click on Downloads. Click 2 Install Python
Open the installer file, then click Custom
on the latest version of Python for Windows. It should Installation, then Next until you get to Advanced
start with the number 3. Select executable installer Options. Leave the checked boxes as they are, but
from the different installer options that appear. make sure “Install for all users” and “Add Python to
environment variables” are also checked. Then
click Install and Next at each prompt.
The version number might not be
exactly the same as this one—just
make sure it has 3 at the beginning. Click the
installer.
1 Download Python
Go to www.python.org and click on Downloads.
Remember this book uses Python 3, so
make sure you open the right version.
Click on the version of Python 3 that matches your You’ll find Python 3 in the Applications
operating system. The “Python.pkg” file will download menu on your Raspberry Pi. Open it
to your Mac automatically. now and check that it works.
The version number might not be exactly
the same as this one—just make sure it
has a 3 at the beginning.
Where should
I plug this in?
• Python 3.6.2 - 2017-08-15
• Download macOS X 64-bit/32-bit installer
2 Install Python
Double-click the “.pkg” file in the Downloads folder
to start the installation. Select Continue and then
Install to accept the default settings.
3 Start IDLE
Once the installation is complete, check that it was
successful by opening the IDLE program. Search for
it in Spotlight or select the Applications folder, then
the Python folder, and double-click IDLE. A window
like this should appear.
Command Prompt
c:\ python -m pip install -U pip
3 Install Pygame
Once the package manager is installed, 4 Install Pygame Zero
Finally, type this command. When you
type the following command and press press Enter, this will install Pygame Zero,
Enter. This uses pip to install Pygame. also known as pgzero for short.
1 Open Terminal
You’ll need to use the Terminal app to >_
This is what the
Terminal app
thumbnail looks like.
install the modules. You can find it in your
Applications folder, or you can search for
it with Spotlight. Follow the steps below,
making sure all the spellings are correct Type this line carefully in the
and the spaces are in the right place. Terminal window and check for any
spelling errors and extra spaces.
that makes it easier to install Pygame Zero Last login: Thu Sep 14 11:22:51 on ttys000
and other Python modules. Type in the LC-0926:~ rzvz ruby -e "$(curl -fsSL https://raw.git
command at right and press Enter to
install Homebrew. It might ask you to hubusercontent.com/Homebrew/install/master/install)"
enter your password again, and it will take
a short while to install, so don't panic if This should fit
nothing happens right away. on one line when
you type it in.
5 Install Pygame
Now it’s time to install Pygame. Type
pip3 install pygame
Pygame Zero.
20 G E T T I N G S TA R T E D
Shell window
△ Give the shell a test run
Type each of these code snippets into the shell window
Editor window and press Enter after each one. The first line displays a
message and the second line does a calculation. Can you
figure out what the third line does?
USING IDLE 21
The editor window ▽ The editor window
The shell window can’t save your code, so when you To open the editor window
close it, the code you typed is gone forever. When you in IDLE, click on the File menu at
are working on a game, you should use IDLE’s editor the top and choose New File.
An empty editor will then appear.
window. This will let you save your code. It also has You’ll use the editor window to
built-in tools to help you write your programs and write the programs for the games
troubleshoot any errors. in this book.
You type in the code The name of the You can run Python programs from
here. This program file is shown here. this menu, but you will run Pygame
prints a list that tells Zero programs a different way.
you which numbers
are even and which
EvensandOdds.py
ones are odd.
IDLE File Edit Format Run Window Help
The menu bar for
for count in range(10): the editor window is
different from the one
if ((count % 2) == 0):
for the shell window.
print(count)
Anything you tell
Python to print print("is even")
gets displayed in
the shell window. else:
print(count)
print("is odd")
EXPERT TIPS
Colors in the code
IDLE automatically colors Symbols and names Output
the text to highlight Most code text is colored Any text produced when
different parts of the code. black. a program runs is blue.
The colors make it easier to
understand the code, and
they’re useful when you’re Built-in commands Keywords
trying to spot mistakes. Python commands, Certain words, such as if and
such as print(), are else, are special Python keywords.
shown in purple. They are shown in orange.
How it works
This Python program will check if everything Start
1 Set up a folder
Before you start, create a folder called python-games
somewhere easy to find, such as on your Desktop.
Create another folder within your python-games
folder and call it hello. End
2 Start IDLE
Open IDLE on your
File
Hello!
computer. From the New File
File menu, choose
New File to create an Open...
empty editor window
where you can write Open Module...
your program.
Recent Files ▶
Class Browser
YO U R F I R S T P RO G R A M 23
def draw():
def draw():
EXPERT TIPS
Cancel Save
Hey, grab on!
I’ll save you!
EXPERT TIPS
Saving your code
Always save your code before you run it,
especially after you make any changes.
If you don’t, Python will run an out-of-date
version of your code.
24 G E T T I N G S TA R T E D
IMPORTANT!
Fix mistakes
If nothing happens when you run one of your programs, or if you
get an error message—don’t panic! It’s normal to experience I think there’s some bug
errors (these are called “bugs”) when coding a program. If an error spray on pages 44–47!
message appears, ask yourself the following questions:
P Y T H N
O
Exploring the Variety of Random
Documents with Different Content
“Now we must have white rosemary. Wilt thou not gather it for
me?” she added, as Hugh, inert and dazed, was looking at her with
that weary longing, that infinite tenderness, which always made my
heart ache for his pain. “Dost know that white rosemary spells
remembrance?”
“Rosemary for remembrance!” he repeated, as quietly he turned
and with loving care picked the humble blossom from out the crowd
of its more gorgeous sisters. He looked at it, and as the light
flickered, I could see two great tears glistening in his eyes.
“Rosemary for remembrance!”—the very words spoke of England
and of home, and brought to his weary heart probably, more strongly
than anything else could do, the thought of what he had lost.
“Rosemary for remembrance!”—we, in England, thousands of
miles away, speak of the little plant as meaning remembrance;
before me, in one moment, with those words, the gorgeousness of
mystic Kamt had disappeared, and I saw its loveliest daughter filling
the old Chestnuts with the radiance of her beauty, the melody of her
voice. She became the incarnation of womanhood, of her who alone
knows how to combine the tender friend, the wife, the mistress.
There was dead silence in the temple. Neit-akrit had taken the bunch
of rosemary and was fastening it to her posy, while Hugh watched
her silently.
Then, suddenly, from the farther end of the sacred edifice, there
where the great gateway faced directly towards the east, it seemed
to me that a dull yet rosy light began to creep gently through.
“Neit-akrit,” said Hugh, after a long while, “if thou didst wish to give
me that posy as an emblem of happiness, it was wrong to add
remembrance to it.”
“Why?”
“Because since I have smelt that sprig of rosemary my memory
has come back to me. Remembrance, like duty, is at times cold and
cruel, and her figure now stands beside that gate and points towards
the east.”
“Nay, not yet!” she pleaded; “ ’tis but the lights of the city of Tanis
gone crazy with joy.”
“Remembrance whispers, Neit-akrit, that I have plighted my troth,
and that if I stay beside thee and chase remembrance away, I break
the pledge which I gave to another.”
“Nay! the heralds of dawn have not yet sounded the trumpets.
Osiris is still well hidden behind the hills, and my nosegay is not
ready. I have no tuberose, which means passion, and no white
pansy, which means forgetfulness.”
“It is not in thy power, Neit-akrit, to put white pansy into thy bunch,
and I…”
“And thou?”
“I would not put them there if I could.”
“Then, thou hast no wish to forget?”
“Rosemary, which is for remembrance, will be my most cherished
flower. Give me that one, Neit-akrit, out of the bunch… touch it once
with thy lips and then let the poor fool go his way.”
Ill, paralysed, numbed as I was, all my hopes tended towards the
beautiful girl who alone could keep Hugh away from the terrible
danger. I longed to give her courage to tell Hugh all, yet feared every
moment that she would, and that he would not believe, that his
loyalty to his bride should not allow him to listen to terrible
accusations framed against her by another woman. The moments
were precious; already through the distant gate the light of dawn
grew stronger and more clear.
“Nay! what is the use?” she said, as she drew a step back from
him; “thou dost seem to remember all that thou dost wish—thy duty,
thy word; the white rosemary should not come from me.”
“Sweet Neit-akrit, thou art a child,” he said with almost rough
earnestness. “Thou dost not understand—how couldst thou? I am a
senseless fool.… Give me thy hands to kiss… place them both upon
my mouth… for I dare not take them in mine… lest their touch should
indeed make me forget…”
Quietly she pulled from out the canopy of flowers a bunch of white
pansies, and stretching out both her hands up to him she murmured:
“White pansies for forgetfulness!”
He had fallen on his knees, and his arms encircled her dainty
figure. She turned towards the statue of the goddess as if to beg of
the cold, immovable image inspiration and perhaps strength. Ay! she
needed all her woman’s wits; there were a hundred unseen enemies
to fight, and one whom she feared more than any—the other woman
to whom he had plighted his troth. I suppose she found it hard to say
to him, “Beware, that woman has murdered her son; she even now
has planned thy ruin.” Supposing his loyalty forbade him to listen!
Her accusations to him might sound like the words of a woman mad
with jealousy… and she thought that she could not prove them; she
did not know I, too, had seen and heard, that I was here, close by, a
caged and useless log, a dumb beast, while twenty priests—a
hundred, if need be—were ready to swear that she lied.
And I was helpless—a caged, helpless, dumb creature—and the
minutes were speeding so fast.
Suddenly from afar the sacred heralds of Osiris rang out upon
their golden trumpets the announcement of the sun-god’s approach.
In a moment Hugh had sprung to his feet, but Neit-akrit now would
not let him go; she clung to his garments, dragged at his cloak.
“Thou must not go,” she entreated. “The temple is dark and lonely
—I am frightened; for pity’s sake do not leave me!”
“Nay, sweet! thou art safe enough here… for the love of heaven
take thy dear hands from off my cloak!”
“Thou wilt not leave me?”
“I must go, sweet. Remember I have sworn! wouldst make a
coward of me?… Nay! thou hast all but succeeded… be satisfied
and let me go!”
She took her hands from off his cloak, but came up close to him
and whispered:
“See! I do not hold thee, and yet thou wilt not go… thou art free!
and yet thou wilt stay… thou hast smelt the perfume of the white
pansy, and thou wilt forget all… save that thou dost love me.…”
“Neit-akrit!”
“Nay! thou dost entreat in vain.… Sweet, I would not have thee go!
What is duty? what is the meaning of oath or pledge? Wouldst know
why I came to-night?… I came because I knew that danger doth
await thee outside this sacred temple.… I came because I knew that
thou dost love me… and I trusted that that love would make thee
forget the dawn, thy duty, thy pledged word, forget all, in order to
remain beside Neit-akrit.”
“Forget? Ay! I have forgotten but too long already. Neit-akrit, thou
speakest of danger; to me there is but one, and that is that I might
forget all—my manhood, my honour, my pledge, my word, might
forget thy innocence, and remember only that thou art fair.”
“But that is all I would have thee remember,” she whispered so
softly that her voice hardly sounded above the murmur of the flower,
which some stray current of air began gently to fan. “If I am fair it is
because Isis hath made me so in order that thou shouldst love me! I
am young, and I have waited for thee all these years because,
although I knew it not, I wished that my beauty should gladden thine
eyes. Nay! at first I longed for thy love out of sheer pride and
revenge. Dost remember the iridescent scarabæus, which should
have guarded thee from the peril of giving thy love to Neit-akrit?
Sweet! ’twas I stole the scarabæus. I would not have thee turn coldly
from me; thou hadst taken from me my crown and my throne; I
wished to steal thy heart from thee, and then to break it in wanton
cruelty, and I threw the charmed scarabæus into the lake.… Then
thou didst speak to me… I looked into thine eyes… I knew thy soul
was mine… but… I guessed even then, my sweet, that Isis had
made me doubly fair, had placed radiance over my body and my
soul, for that day I learnt… that I loved thee!”
“Neit-akrit, for pity’s sake,” he pleaded.
But her arms were round his neck, her sweet face quite close to
his, her eyes looked ardently into his own.
“I love thee!” she whispered.
“Wouldst make me mad, Neit-akrit?”
“I love thee!”
And far, very far away, the sacred heralds of Osiris rang out upon
their golden trumpets the announcement that the sun-god was
emerging behind the hills of Kamt, which hid the valley of death.
“I love thee, oh thou who art beloved of all the gods!”
Hugh had clasped her in his arms, and as I closed my burning
eyes and fell back fainting in my prison, I knew that he was safe, that
Neit-akrit had succeeded in making him forget.
CHAPTER XXVIII.
THE THREAT
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