28.02.2015 Views

MagPi31-single

MagPi31-single

MagPi31-single

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

MAKE GAMES WITH PYTHON<br />

Tutorial<br />

pygame.draw.line(window,(255,255,255),(50,50),(75,75),1)<br />

pygame.draw.line(window,(255,255,255),(75,75),(25,75),1)<br />

pygame.draw.line(window,(255,255,255),(25,75),(50,50),1)<br />

pygame.draw.line(window,(255,255,255),True,((50,50),(75,7<br />

(500,400))<br />

0,100,50,50))<br />

0,150,50,50))<br />

0,200,50,50))<br />

50, 50<br />

pygame.draw.line(window,(255,255,255),(75,75),(25,75),1) There! We have a friendly,<br />

pygame.draw.line(window,(255,255,255),(25,75),(50,50),1)<br />

white triangle with a 1px<br />

edge. But when we look<br />

Right You<br />

50, 50<br />

at that code, it looks like<br />

can make 25, 75 75, 75<br />

a triangle<br />

a lot, doesn’t it? So many<br />

from three<br />

things, like the colour<br />

separate<br />

lines<br />

or the width, are being<br />

25, 75 75, 75<br />

written again and again<br />

just for the sake of it.<br />

Each path is made of joined-together lines, but<br />

pygame.draw.circle(window,(255,255,0),(250,200),20,1)<br />

before we start joining things up, let’s draw a couple<br />

There must be a better<br />

way, and indeed there is!<br />

pygame.draw.circle(window,(255,255,0),(250,200),20,1)<br />

of standalone lines to familiarise ourselves with All we need is<br />

them. We can do this with pygame.draw.line(). Edit<br />

paths.py so your while loop is like the following:<br />

pygame.draw.lines().<br />

While pygame.draw.<br />

line() lets us draw a line<br />

250,200 between two points, pygame.draw.lines() enables us Above This<br />

while True: 250,200<br />

20px<br />

to draw a sequence of lines between numerous points.<br />

Each XY-coordinate point will be joined up to the next<br />

20px<br />

pygame.draw.line(window, (255,255,255), XY-coordinate point, which will be joined up to the next<br />

(0, 0), (500, 400), 1) XY-coordinate point, and so on.<br />

After running the code you’ll see that it’s exactly the<br />

pygame.display.update()<br />

same, except we did it in one line of code instead of<br />

three. You might have noticed that we didn’t actually<br />

close the triangle – Pygame did it for us. Just before we<br />

If you run this code now, you’ll see a 1-pixel-wide<br />

white line going from the top left to the bottom right<br />

of our Pygame window. The parameters we pass to<br />

pygame.draw.line start off the same way rectangles<br />

and ellipses do. We first tell Pygame where we want to<br />

draw the shape and then we choose a colour. Now things<br />

change a little. The next argument is a tuple of the X and<br />

pass the points for our shape to be drawn from, we can<br />

pass either a True of a False value that will let Pygame<br />

know that we want it to close our shapes for us. Change<br />

it to False and we get the first two lines of our shape,<br />

but not the third.<br />

What if we want a more complex shape? We simply<br />

add more points like so:<br />

Y coordinates for where we want our line to start, and the<br />

third argument is a tuple with the X and Y coordinates for<br />

where we want our line to end. These are the two points while True:<br />

between which our line will be drawn. The final argument<br />

is the width of the line that is being drawn in pixels.<br />

With this, we can now create shapes by defining<br />

points in our window. Let’s draw that triangle we<br />

#pygame.draw.lines(WHERE TO DRAW,<br />

COLOUR, CLOSE THE SHAPE FOR US?, THE POINTS<br />

TO DRAW, LINE WIDTH)<br />

talked about earlier:<br />

pygame.draw.lines(window,(255,255,255),<br />

True, ((50, 50), (75, 75), (63, 100), (38,<br />

100), (25, 75)), 1)<br />

while True:<br />

pygame.draw.line(window, (255,255,255),<br />

(50, 50), (75, 75), True)<br />

pygame.draw.line(window, (255,255,255),<br />

(75, 75), (25, 75), True)<br />

pygame.draw.line(window, (255,255,255),<br />

(25, 75), (50, 50), True)<br />

pygame.display.update()<br />

pygame.draw.line(window,(255,255,255),(50,50),(75,75),1)<br />

pygame.display.update()<br />

pygame.draw.line(window,(255,255,255),True,((50,50),(75,75),(25,75)),1)<br />

There you have it: a pentagon. If you want a hexagon or<br />

even a triacontagon, just add more points. Give it a go.<br />

So that’s how you draw shapes, lines and paths in<br />

Pygame. Already we know enough to make programs<br />

that could be used to display pixel art to our friends<br />

and family.<br />

triangle is made<br />

up of one line with<br />

multiple points.<br />

Follow the colours<br />

to see which<br />

variable is which<br />

NEXT<br />

MONTH<br />

In part two,<br />

things will get<br />

a little more<br />

complicated<br />

– we’ll be<br />

animating all<br />

of the shapes<br />

we’ve learnt<br />

to draw so<br />

far. They’ll<br />

bounce,<br />

stretch and<br />

spin, which<br />

is where the<br />

games begin…<br />

raspberrypi.org/magpi March 2015 55

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!