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.

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):

Tutorial 2 screen shot

If you’re 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.

Coordinates on a computer display

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.

class Particle:
  def __init__(self, (x, y), size):
    self.x = x
    self.y = y
    self.size = size
    self.colour = (0, 0, 255)
    self.thickness = 1

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

def display(self):
  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.

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

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.

particle_tutorial_2.txt755 bytes


I spotted a typo. In code line 21:

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

The variable name should be "my_first_particle" (without the final 's'), to match the call in the next line.

Just wanted to let you know, so that these excellent tutorials of yours are no less than perfect. ;)

Thanks, I've changed it.

Every time I re-read what I've written I spot a couple of typos (it's because I keep changing my mind about things - that and I'm careless). It really helps if other people look out for them too.

So far this tutorial has been a new experience for me.

However, I was stuck on this tutorial for at least one hour, as I was presented with an error message telling me "this constructor takes no arguments". After some research on the internet, I found out that there were two underscores on both sides of "init", not just one!

Maybe I'm a lone fool, but you could possibly update this content and briefly note how it is two underscores at each side, to allow for other users to overcome this problem.

As I say, apart from this(even though it was my own fault), so far so good!


Sorry about that David. I know how frustrating it can be to spend hours with an error only to find its a minor typographical issue. In my defence, the double underscore is a requirement of Python and not a Pygame thing, which is the focus of this tutorial. But I'll add a note to make it clear for everyone.

Thanks for your feedback.


Hi, Love this tutorial, but i'm stuck with this problem

when I run the code upto this point I get a syntax error at "(" which I have highlighted ,

Can you help me out with this. I'm using python 3.1 and compatiable pygame version. 

class Particle:

    def __init__(self, (x, y), size):


It seems that tuple parameter unpacking doesn't work in Python3. Try:

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





when I run your code I dont get any errors but the screen is first white then turns black and then stays white for ever. why is it getting that black in white any idea ??


Hi Faizan. I'm not sure what's happening when you run the program. Is the code exactly the same as I've written here? You could try moving the lines:





into the while loop so they are called every iteration, which would be a more normal way to do things.

This tutorial finally gave me a good grasp on the importance and efficiency of classes. Thanks so much!

"It seems that tuple parameter unpacking doesn't work in Python3. Try:

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



Hi, I tried what you suggested and it says:

particle_01 = Particle(150, 50, 15)
TypeError: __init__() takes exactly 3 positional arguments (4 given)



Sorry I should have said, you still create the particle with the same call, i.e:

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

I think that should work, but I don't have Python 3 to test it. If it doesn't then create the particle as you are now change the Particle class to:

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

this bit doesn't work in Python 3.1.1

class Particle:

    def __init__(self, x, y, size):

        self.x = x

        self.y = y


You might need to change the first line to,

class Particle(object):

This is what I did to get the code to work for Python3.3:

class Particle:
    def __init__(self, position, size):
        self.x = position[0]
        self.y = position[1]
        self.size = size
        self.colour = (0, 0, 255)
        self.thickness = 1

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

self.colour   should be self.color

I tried running the program but I keep getting an attributeerror " 'particle' object has no attribute 'display' "

How can I fix this?

I suspect you need to fix the indentation of the display method so it is inside the Particle class.

Post new comment

The content of this field is kept private and will not be shown publicly.