Você está na página 1de 3

Drawing circles

In the previous tutorial we created a blank white screen; now we want to display
some graphics on it. In this tutorial, we will:
Draw a circle in our window
Define a Particle class
Create and display a Particle object
This program builds on the code from the previous tutorial. The line numbers show
where code should be added assuming the program is written exactly like the
program attached in the previous tutorial (including blank lines). In this tutorial and
all the following tutorials, the final code is available at the bottom of the page as a
txt file.

Drawing with Pygame


In Pygame there are various functions for drawing simple shapes. We will display
our particles as circles so use the pygame.draw.circle() function. This function
takes several parameters:

A surface where the circle will be drawn (here it's our screen)
A colour
An (x, y) coordinate
A radius
A thickness (optional)

For example.
9 pygame.draw.circle(screen, (0,0,255), (150, 50), 15, 1)

This draws a blue ((0,0,255) is blue in RGB notation) circle centred at (150, 50), with
radius 15 and thickness of 1 pixel. Note that this function must be called after
the screen.fill(background_colour) command and before the flip() command.
If you run the program now you should see something like this (I also changed the
title of window, but it's not important):

If youre not familiar with how computer displays work, you might have expected
the circle to be near the bottom of the screen, since the particle's y-coordinate is 50
and the screen is 200 units high. However, on a computer display, the origin (0, 0)
is in the top, left of the screen, with the x-axis increasing from left-to-right and the
y-axis increasing from top-to-bottom; the circle is therefore centred 50 pixels down
from the top of the screen.

The Particle object


In our final simulation we will want several particles, each of which will have the
same type of attributes. Thus it makes sense to define a Particle class. When we
create each Particle object with the __init__ function (note the double underscores
which indicates a built-in method), we will give it an x,y coordinate and a size. The
colour and line thickness will be given default values. We'll add more attributes as
we go along.
06
07
08

class Particle:
def __init__(self, (x, y), size):
self.x = x

09

self.y = y

10

self.size = size

11

self.colour = (0, 0, 255)

12

self.thickness = 1

Next we add a display() method to our Particle object that will draw it (as a circle)
on the screen.
14 def display(self):
15

pygame.draw.circle(screen, self.colour,
(self.x, self.y),self.size, self.thickness)

Now we can remove the pygame.draw.circle() call and replace it by code that
creates a particle object (defining its x, y coordinates and size) then calling
its display() method. If you're not familiar with object-orientated programming,
this may seem more long winded, but it will make building on our program a lot
easier later on.
21

my_first_particle = Particle((150, 50), 15)

22

my_first_particle.display()

If you run the program now, it should look exactly the same as before. However,
in the next tutorial I'll show you how the code now can be easily changed to display
many more circles. I'll also introduce the random module which is very handy when
creating simulations.

Você também pode gostar