How to Draw Simple Shapes in Pygame
82Programming Pygame - Draw Lines, Rectangles and Circles
This tutorial will show you how to draw lines, rectangles and circles by programming in pygame. In order to follow along you will need python and the library for it, pygame, installed on to your computer. I also recommend a basic knowledge of programming in python.
To get python(free): http://www.python.org/download
To get pygame(free): www.pygame.org/download.shtml
If you don't know a thing about programming in python I recommend you check out my Python for N00Bs tutorial. A little knowledge goes a long way. Click here for a very basic example of a game you can make with these shapes and a basic knowledge of python: Tic-Tac-Toe)
Ok Now that we've got all the formalities out of the way, lets move on to the nitty gritty of this tutorial. First off, a very very basic example of....
How to draw a line.
import pygame #load pygame module
w = 640 #set width of screen
h = 480 #set height
screen = pygame.display.set_mode((w, h)) #make screen
#draw a line from upper-left corner to lower-right corner
pygame.draw.line(screen, (255, 0, 0), (0, 0), (w, h))
pygame.display.flip() #show our line
And that's it! For the draw.line function, first you tell it what surface you would like to draw on. In our case, the main screen "screen". Next you tell it the color. If you go into paint, you can see the values of the colors. Red, green, blue. Then we give our starting coordinates (x, y) and our ending coordinates (x, y). X(vertical) starts counts pixels from left to right. Y counts pixels from top to bottom. pygame.display.flip() updates the screen. If you want to show anything on the screen you will need to use this or pygame.display.update(). If all went well you should see a line going diagonal across the screen.
One more thing: you can add one more option to our the draw.line function for thickness in pixels. For example:
pygame.draw.line(screen, (255, 0, 0), (0, 0), (w, h), 10)
This will draw the line 10 pixels thick. Ok, now moving on to...
How to draw a rectangle
import pygame #load pygame module
w = 640 #set width of screen
h = 480 #set height
screen = pygame.display.set_mode((w, h)) #make screen
pygame.draw.rect(screen, (0, 255, 0), (50, 50, 100, 100))
pygame.display.flip()
And there we have our rectangle. The first two things it asks for are the same as the line: the surface to draw to, and the color. Next it wants the starting upper-right corner. After that it wants the length, then the width in pixels. Just like the lines, there is an optional field for the thickness of the outer edge. If you leave this blank, pygame will automatically fill your rectangle.
How to draw a circle
import pygame #load pygame module
w = 640 #set width of screen
h = 480 #set height
screen = pygame.display.set_mode((w, h)) #make screen
pygame.draw.circle(screen, (0, 0, 255), (w/2, h/2), 50 )
pygame.display.flip()
Ok, by now the first two fields of draw.circle should be obvious: surface and color. The next field is the (x, y) coordinates of the center of your circle. I wanted my circle to be in the center of the screen so i just told it i wanted the center of the circle to be at half the width of the screen and half the height. After that comes radius in pixels (radius is the measurement from the center of the circle to the edge. Again, just like with the lines and rectangles, there is an optional field for the thickness. If this is not used your circle will be autofilled.
That is the end to this tutorial. I hope you were able to make these shapes appear on your screen ;) . Play around with them. Practice makes perfect. If you happened to run into any difficulties with this let me know and I will assist you any way that I can.
- Why Python?
If you are new to programming, this site may clarify why python is a good language to start with.
![]() | Amazon Price: $34.98 List Price: $64.99 |
![]() | Amazon Price: $20.34 List Price: $34.99 |
![]() | Amazon Price: $39.00 List Price: $45.00 |
![]() | Amazon Price: $19.99 List Price: $39.99 |
vote upvote downshareprintflag
- Useful (1)
- Funny (1)
- Awesome (1)
- Beautiful (1)
- Interesting
CommentsLoading...
Thanks for this tutorial. I am currently using Panda3D for the simple game. Pygame looks interesting and I would love to try it. :)












Eric Dexter 20 months ago
It does seem that the example doesn't stay on the screen and I thought it was unusual that you didn't have any pygame books on your hub... I may be in the market for a couple of books, drop on by dex tracker if you are intrested in a debugging chalenge.