Essentials_Games_v1.pdf

(10331 KB) Pobierz
ESSENTIALS
GAMES
TH
WI
MAKE
CREATE YOUR OWN
ENTERTAINMENT WITH
PYTHON
Raspberry Pi
Written by
Sean M. Tracey
SUBSCRIBE
Number one for Raspberry Pi
raspberrypi.org/magpi
TODAY
FROM
£13 / $37.50
PAGES OF
HACKING
& MAKING
100
raspberrypi.org/magpi
WELCOME TO
MAKE GAMES
WITH PYTHON
W
hile countless millions of us take great
pleasure spending hours racking up high
scores in our favourite games, few of us are
ever exposed to the delights of making them in the
first place. It’s far from easy, but learning to code
your own shoot-’em-up is infinitely more satisfying
than beating any end-of-level boss.
Although this book is designed to help you learn
many of the essential skills you’ll need to make
games with Python and Pygame on your Raspberry
Pi, it’s by no means definitive. Frankly, you could
read a dozen books on the subject and still not have
the skills you need to succeed. As with most things,
nothing replaces good old-fashioned practice. I
should know: I have 30 cookery books lining my shelf
and I still burnt my toast this morning.
Making games is a brilliant way to learn to code,
though, so I hope this book helps you to get started
on your next big adventure.
Russell Barnes
Managing Editor, Raspberry Pi
FIND US ONLINE
raspberrypi.org/magpi
EDITORIAL
GET IN TOUCH
magpi@raspberrypi.org
DESIGN
Managing Editor:
Russell Barnes
russell@raspberrypi.org
Technical Editor:
David Whale
Sub Editors:
Lorna Lynch (with Laura Clay & Phil King)
Critical Media:
criticalmedia.co.uk
Head of Design:
Dougal Matthews
Designers:
Lee Allen, Mike Kay
DISTRIBUTION
Seymour Distribution Ltd
2 East Poultry Ave,
London
EC1A 9PT |
+44 (0)207 429 4000
In print, this product is made using paper
sourced from sustainable forests and
the printer operates an environmental
[
management system which has been
Chapter One
]
assessed as conforming to ISO 14001.
SUBSCRIPTIONS
Select Publisher Services Ltd
PO Box 6337
Bournemouth
BH1 9EH |
+44 (0)1202 586 848
The MagPi magazine is published by Raspberry Pi (Trading) Ltd., Mount Pleasant House, Cambridge,
CB3 0RN. The publisher, editor and contributors accept no responsibility in respect of any omissions
or errors relating to goods, products or services referred to or advertised in the magazine. Except
where otherwise noted, content in this magazine is licensed under a Creative Commons Attribution-
NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0). ISSN: 2051-9982.
3
[ CONQUER THE COMMAND LINE ]
ESSENTIALS
CONTENTS
05
[
CHAPTER ONE
]
SHAPES & PATHS
WITH PYGAME
18
[
CHAPTER TWO
]
ANIMATING
SHAPES & PATHS
28
[
CHAPTER THREE
]
TAKING CONTROL OF
THE KEYBOARD & MOUSE
42
[
CHAPTER FOUR
]
YOUR FIRST GAME
56
[
CHAPTER FIVE
]
PYGAME SOUNDBOARD
68
[
CHAPTER SIX
]
PHYSICS & FORCES
80
[
CHAPTER SEVEN
]
PHYSICS & COLLISIONS
94
[
CHAPTER EIGHT
]
BUILDING CLASSES
114
[
CHAPTER NINE
]
THE ALIENS ARE
TRYING TO KILL ME!
130
[
CHAPTER TEN
]
THE ALIENS ARE HERE &
THEY’RE COMING IN WAVES!
[
SEAN M.
TRACEY
]
Sean calls
himself a
technologist,
which is a fancy
way of saying
he still hasn’t
decided what he
wants to do with
technology –
other than
everything.
Sean has spent
his career trying
to avoid getting
‘proper’ jobs, and
as such has had
a hand in making
a variety of fun
and interesting
projects, including
a singing statue
of Lionel Richie,
wearable drum
kits, chopstick
bagpipes, time-
telling hats, and
a life-sized Elvis
Presley robot, to
name only a few.
sean.mtracey.org
[
Contents
]
4
ESSENTIALS
ESSENTIALS
[
CHAPTER
SHAPES & PATHS
WITH PYGAME
We are going to learn how to make a game on our
Raspberry Pi from the ground up. In the first chapter, we
learn the basics.
ONE
]
5
[
Chapter One
]
Zgłoś jeśli naruszono regulamin