28.02.2015 Views

MagPi31-single

MagPi31-single

MagPi31-single

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Tutorial<br />

WALKTHROUGH<br />

Right When drawing a<br />

circle, the last variable<br />

lets us know if the<br />

circle should be filled<br />

in or not<br />

while True:<br />

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

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

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

(100, 150, 80, 40))<br />

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

(100, 190, 60, 30))<br />

TUPLE<br />

pygame.display.update()<br />

A tuple is like a<br />

list, but unlike a<br />

standard list, a<br />

tuple’s contents<br />

can’t be changed<br />

(it’s immutable).<br />

python.org/docs<br />

Just as before, run your code. You should now see<br />

three ellipses – one red, one green, and one blue, each<br />

a different size. If you wanted to visualise how these<br />

shapes were generated, you could draw rectangles<br />

using the same coordinates as you used to draw an<br />

ellipse and it would fit perfectly inside that box.<br />

Guess what? That means you can also make circles by<br />

using pygame.draw.ellipse if the width and height<br />

parameters are the same.<br />

A new path<br />

So that’s rectangles, squares and circles, but what if<br />

we want to draw a triangle, a pentagon, a hexagon or<br />

an octagon? Are there functions for every <strong>single</strong> kind<br />

of shape? Well, no, but what we do have are paths.<br />

Paths let us draw irregular shapes by defining points<br />

in space, then joining them up with lines and filling<br />

in the space we’ve created.<br />

This is a little more complex, so it’s time to<br />

move on from our old friend hello.py. Create<br />

a new file, call it paths.py, and save it with the<br />

following inside:<br />

while True:<br />

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

(100, 100, 100, 50), 2)<br />

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

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

import pygame<br />

pygame.init()<br />

window = pygame.display.set_mode((500, 400))<br />

while True:<br />

pygame.display.update()<br />

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

(100, 150, 80, 40), 2)<br />

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

(100, 150, 80, 40))<br />

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

(100, 190, 60, 30), 2)<br />

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

(100, 190, 60, 30))<br />

This is just our bare-bones Pygame app again,<br />

in fact. If you want to make a copy of this for<br />

experimenting without breaking anything, now<br />

would be a good time to do so.<br />

#Circle<br />

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

(100, 250, 40, 40))<br />

pygame.display.update()<br />

Ellipses in the<br />

rectangles that<br />

bound them<br />

54 March 2015<br />

raspberrypi.org/magpi

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

Saved successfully!

Ooh no, something went wrong!