文件名称:
Make-Games-with-Python.pdf.pdf
开发工具:
文件大小: 10mb
下载次数: 0
上传时间: 2019-09-14
详细说明:Make-Games-with-Python.pdfWELCOME TO
MAKE GAMES
WITH PYTHON
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 GET IN TOUCH magpiraspberrypi org
EDITORIAL
DESIGN
Managing Editor: Russell Barnes
Critical Media: criticalmedia. co, uk
russellaraspberrypi org
Head of Design: Dougal Matthews
Technical Editor: David Whale
Designers: Lee Allen, Mike Kay
App store
Sub Editors: Lorna Lynch (with Laura Clay Phil King)
DISTRIBUTION
SUBSCRIPTIONS
Seymour Distribution Ltd
Select Publisher Services Ltd
2 East Poultry Ave
PO BOx 6337
coO London
Bournemouth
BH19EH|+44(0)1202586848
In print. this product is made using paper The MagPi magazine is published by Raspberry Pi(Trading)Ltd. Mount Pleasant House, Cambridge.
Recycle sourced from sustainable forests and
CB3 ORN. The publisher, editor and contributors accept no responsibility in respect of
any omissions
the printer operates an environmental
or errors relating to goods, products or services referred to or advertised in the magazine. Except
management system which has been
where otherwise noted, content in this magazine is licensed under a Creative Commons Attribution
assessed as conforming to ISO 14001
Noncommercial-Share Alike 3.0 Unported(CC BY-NC-SA 3.0). ISSN: 2051-9982
ESSENTIALS
I SEAN M.
CONTENTS
TRACEY]
05 I CHAPTER ONE I
ShaPes paths
WITH PYGAME
18 I CHAPTER TWO I
ANIMATING
ShaPes paths
Sean calls
28 I CHAPTER THREE I
himself a
TAKING CONTROL OF
technologist
which is a fancy
THE KEYboaRd MOUSe
way of saying
42 I CHAPTER FOUR I
he still hasn't
decided what he
YOUR FIRST GAME
wants to do with
technology
56[ CHAPTER FIVE I
other than
PYGAME SOUNDBOARD
everything
Sean has spent
68 CHAPTER SIX I
his career trying
PHYSICS FORCes
to avoid getting
proper jobs, and
80 I CHAPTER SEVEN I
as such has had
a hand in making
PhYSiCS COLlISIONs
a variety of fun
and interesting
94 I CHAPTER EIGHT I
projects, including
BUILDING CLASSES
a singing statue
of Lionel Richie
114 I CHAPTER NINE I
wearable drum
THE ALIENS ARE
kits, chopstick
b
atrIp
me-
TRYING TO KILL ME!
telling hats, and
130 I CHAPTER TEN I
a life-sized Elvis
Presley robot, to
THE ALIENS ARE hERE
name only a few
THEYRE COMING IN WAVES! sean.mtracey org
I Contents I MagPi4
I CHAPTER ONE
HAPES&PAT上
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
5 MagPi Chapter One]
MAKE GAMES WITH PYTHON
n this book, we are going to learn to make games on the
Raspberry pi with Pygame. We'll look at drawing, animation,
keyboard and mouse controls, sound, and physics. Each
chapter will add to our knowledge of raspberry Pi game development,
allowing us both to understand the games we play, and to create almost
anything our imaginations can come up with
This book isnt for absolute programming beginners, but it's not
far from it: we're going to assume that you've written some simple
Python (or similar) programs in the past, and are able to do things like
creating files and get around your Pis filesystem without too much
difficulty. If you haven't set up your Pi and are a little lost on how to
go about it, there are lots of easy-to-follow guides on the web which
will help bring you up to speed. You could point your web browser to
raspberrypi. org/resources to get started
In the first chapter, we're going to look at drawing and colouring
various shapes in a window. This isnt quite Grand Theft Auto V,
admittedly, but drawing shapes is the first step in building just
about anything
To start off, open your preferred text editor, create a new file, insert
the following code into it and save it as hello. py: Let's run that code
and see what it does. In your terminal window, enter python hello
py. If all has gone well, a new window will have opened showing you a
import pygame
Download
magpi cc/
pygame. inito
1johJYe
window= pygame. display set mode ((500, 400))
while true
pygame. draw rect (window,(255,0, 0)
(⊙,9,58,38)
pygame. display update)
I Shapes Paths with Pygame 1 Magpi5
Magpi
window= pygame. display set mode((500, 400))
window
Left here we can
see how each
variable in window
affects the
application
500pX
window' s shape
and size width
always comes
before height
red square on a black background in the top-left corner of the window
We've just created our first Pygame program: let's walk through it
Understanding hello.py
The first two lines of our first program are very simple: all we've done
s told Python that we want to use Pygame. import pygame loads all
of the Pygame code into our script, so we dont have to write all of that
code ourselves. Pygame is designed to make the creation of games and
interactive software easy pygame. init( tells Pygame that were
ready to start using it
Let's look at the third line of code
window pygame display set mode ((500, 400))
window is the parameter we're going to use to tell our Pygame
program about how it should look when it runs; each parameter affects
the application window's shape and size. Note that here, width always
comes before height. window is also the parameter that well use to
tell other lines of code the surface on which they should draw shapes
7 MagPi Chapter One 1
I MAKE GAMES WITH PYTHON I
and set colours With window, were calling the set_mode function
of Pygame's display module: the latter is responsible for how the
I PYGAME I
game window and surface(an informal term for the pixels we'll be
manipulating) behaves. We're passing a tuple(which we can think
Pygame is
of as a special list of things-in this case, a list of numbers)to set
installed on
Raspbian by
mode()to tell it how big we want our game window to be. In this case,
default. Find
the application window is 500 pixels wide by 400 pixels tall. If we pass
documentation
numbers that are bigger, the game window will be bigger; if we pass
detailing all
numbers that are smaller, the game window will be smaller
its features at
pygame. org/docs
The next few lines are where we make our program draw shapes on
that window. When programs run, they execute their code, and when
they're finished, they close themselves. Thats fine unless, of course,
you want your program to be interactive, or to draw or animate shapes
over time, which is exactly what we need from a game. So, in order to
keep our program from exiting, we make a while loop and put all our
code inside The while loop will never finish because True is always
True, so we can keep running our program and drawing our shapes for
as long as we like
The first thing we do in our while loop is draw a rectangle. A
rectangle is the simplest shape that we can draw in Pygame
pygame. draw rect(window,(255,0,0),(0,0,50, 30))
The parameters at the end are telling Pygame where we want to draw
our rectangle, the colour we want our rectangle to be, how we want to
draw it, and how big we want it to be
In our hello. py program, we've told Pygame to draw a rectangle
in our window -or at least the surface we create with our window
parameter. Next, we told Pygame what colour we wanted our
rectangle to be by passing it through a tuple (a special list of numbers
representing how much red, green, and blue the final colour should
have in it. We use red, green, and blue as these are the three colours
your screen combines to create every shade you can see on it. o means
that none of that colour should be used in the shape, 255 means that
the maximum amount of colour should be in that shape. We told our
rectangle that it should be the colour(255, 0, 0), which is pure red
I Shapes& Paths with Pygame 1 Magpi
Magpi
If we had told it to be(255, 0, 255), it would have been a bright
purple, because it's being drawn with the maximum amount of red
and the maximum amount of blue. If we had told our rectangle to be
coloured (100, 100, 100), it would be a dark grey, because all of the
colours would be equal
After weve passed through a colour for our rectangle to be, we have
to tell it where it should go and how big it should be. We do this by
passing a tuple of four numbers. The first number is an X coordinate,
which set out how far from the left side of the window the left edge of
our rectangle should be. The second number is a Y coordinate; this tells
the rectangle how far from the top of our window the top edge it should
sit. The third number gives the width of our rectangle, and the fourth
number defines its height. So, for example, if we wanted our rectangle
to be 5o pixels from the left side of the window, 100 pixels from the top
of our window, 20 pixels wide and 80 pixels tall, we would pass
(50, 100, 20, 80)to pygame. draw recto
Below heres a
Please note that the order never changes. If you tell Pygame how big
clear look at what you want the rectangle to be when it's expecting a colour or vice versa,
each variable
does to the shape the program may crash, so take your time
were drawing
Our last line in hello. py is nice and simple: it tells Pygame that
we're done drawing shapes for
pygame. display rect(window,(255,0,0),(100,100,50,50))
the moment and that it can now
pygame. display rect(window,(0, 255, 0),(200, 150, 50, 50))
fresh the window This saves
pygame. display rect(window,(0, 0, 255),(300, 200,50, 50))
our Pi having to draw and redraw
the screen for every shape ti
weve created; instead, it can get
them all drawn in one
100,100
Adding more shapes
We’ ve successfully d
one
0,150
shape, so let's draw a few more
We'll draw some squares around
the screen and mess around
300,200
with their properties a little bit
There's no need to create a new
file, so we'll stick with hello. py
for now. Edit the while loop so
it's the same as the following
Ma
I MAKE GAMES WITH PYTHON I
LINE
while true
WIDTH I
When drawing
pygame. draw rect(window,(255,0,0)
a rectangle
(108,1,58,58))
or ellipse, you
have the choice
pygame. draw rect(window,(0,255,0),
of passing a
(158,180,50,50)
Line width. If
pygame. draw rect(window,(0,0, 255),
you don't, the
shape will b
(20,100,50,50))
flLed solid
pygame. display update)
Now we should have three squares: red, blue, and green. So far, this
is nice and simple, but those squares are placed right next to each
other. What would happen if they were to overlap? Let's find out
Change your code once more to the following
while true
pygame. draw rect(window,(255,0,0)
(,9,50,50))
pygame. draw rect (window,(0,255,0),
49,8,59,50)
pygame. draw rect(window,(0,0, 255),
(8,,50,50))
pygame. display update)
This time we get two rectangles and a square, but that is not what
we asked for. So, what has gone wrong? When we execute our code,
it works through what it has to draw, and where it has to put it
line-by-line. If one item is drawn and then another is drawn
over it or on top of part of it, then we can no longer see what's
beneath that second shape. The pixels of the shape drawn first
I Shapes Paths with Pygame 1 MagPi[10
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.