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.

25, 75 75, 75<br />

MAKE GAMES WITH PYTHON<br />

Tutorial<br />

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

while True:<br />

250,200<br />

20px<br />

#Just like before to help us remember<br />

#pygame.draw.circle(WHERE TO DRAW, (RED, GREEN,<br />

BLUE), (X COORDINATE, Y COORDINATE), RADIUS, HEIGHT,<br />

WIDTH)<br />

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

(250, 200), 20, 0)<br />

pygame.display.update()<br />

Above Here’s how the variables affect the drawing of a circle<br />

while True:<br />

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

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

#pygame.draw.rect(window, (0,255,0),<br />

#(40, 0, 50, 50)) FROM HERE<br />

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

(80, 0, 50, 50))<br />

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

(40, 0, 50, 50)) #TO HERE<br />

Just like drawing a rectangle, we tell Pygame on which<br />

surface we want to draw our circle, what colour we want it to<br />

be, and where it should go. The radius is specific to drawing<br />

this particular shape. You might have noticed that we put<br />

a 0 after our radius; this is a value used to determine the<br />

width of the line that draws our circle. If we pass 0, the circle<br />

is filled; but if we pass 2, for instance, we get a 2-pixel-wide<br />

line with an empty centre:<br />

Rectangles are awesome, but<br />

Pygame lets us draw all kinds<br />

of other shapes too<br />

pygame.display.update()<br />

while True:<br />

Now we get rectangle, square, rectangle. This is<br />

because the red and blue squares were drawn first<br />

and then the green square was drawn over the top of<br />

them. We still got squares but we just can’t see all of<br />

them, so they look like rectangles.<br />

But enough with rectangles. Rectangles are awesome<br />

and we can build many things with them, but Pygame<br />

lets us draw all kinds of other shapes too. We can draw<br />

circles, ellipses and paths (which are made up of many<br />

lines between multiple points).<br />

Drawing circles<br />

Drawing a circle is much like drawing a square except<br />

instead of passing a width and a height, we pass a<br />

radius and a point, around which we draw our circle. If<br />

we wanted to draw a yellow circle in the middle of our<br />

window with a diameter of 40 pixels, we would use the<br />

following code to replace the while loop in hello.py:<br />

#Filled<br />

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

(200, 200), 20, 0)<br />

#Not filled<br />

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

(300, 200), 20, 2)<br />

pygame.display.update()<br />

What about ellipses? They are a slightly strange<br />

cross between drawing rectangles and circles. It’s<br />

the same as drawing a rectangle: we pass an X<br />

coordinate, a Y coordinate, a width, and a height,<br />

but we end up with a circle(ish) shape. Let’s draw an<br />

ellipse or two…<br />

raspberrypi.org/magpi March 2015 53

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

Saved successfully!

Ooh no, something went wrong!