How to Draw Simple Shapes in Pygame

82
rate or flag this page

By dwolters


Programming 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.


Learning Python (Animal Guide) Learning Python (Animal Guide)
Price: $29.50
List Price: $54.99
Python Essential Reference (4th Edition) Python Essential Reference (4th Edition)
Price: $24.74
List Price: $44.99
Python Programming for the Absolute Beginner Python Programming for the Absolute Beginner
Price: $18.70
List Price: $29.99
Python Cookbook Python Cookbook
Price: $28.96
List Price: $49.95

Print   —   Rate it:  up  down  flag this hub

Comments

RSS for comments on this Hub

Jack  says:
3 months ago

i need help please my screen dosent work... what happens is the command line thing pops up for a second then goes away and dosent boot up the program... here is my program.. can you please help me and fix it cause i am just starting python and pygame... by the way the bg.jpg was supposed to be the background but i dont know how to draw one

import pygame

#load pygame module

w = 640 #set width of screen

h = 480 #set height

screen = pygame.display.set_mode((640,360),0,32)), ((w, h))#make screen

bif="bg.jpg"

mif= pygame.draw.circle(screen, (0, 0, 255), (w/22, h/2), 50)

pygame.display.flip()

import pygame, sys

from pygame.locals import *

pygame.init()

background=pygame.image.load(bif).convert()

mouse_c=pygame.image.load(mif).convert_alpha()

while True:

for event in oygame.event.get():

if event.type == QUIT:

pygame.quit()

sys.exit()

screen.blit(background, (0,0))

x,y = pygame.mouse.get_pos()

x -= mouse_c.get_width()/2

y -= mouse_c.get_height()/2

screen.blit(mouse_c(x,y))

pygame.display.update()

Jack  says:
3 months ago

i need help please my screen dosent work... what happens is the command line thing pops up for a second then goes away and dosent boot up the program... here is my program.. can you please help me and fix it cause i am just starting python and pygame... by the way the bg.jpg was supposed to be the background but i dont know how to draw one

import pygame

#load pygame module

w = 640 #set width of screen

h = 480 #set height

screen = pygame.display.set_mode((640,360),0,32)), ((w, h))#make screen

bif="bg.jpg"

mif= pygame.draw.circle(screen, (0, 0, 255), (w/22, h/2), 50)

pygame.display.flip()

import pygame, sys

from pygame.locals import *

pygame.init()

background=pygame.image.load(bif).convert()

mouse_c=pygame.image.load(mif).convert_alpha()

while True:

for event in oygame.event.get():

if event.type == QUIT:

pygame.quit()

sys.exit()

screen.blit(background, (0,0))

x,y = pygame.mouse.get_pos()

x -= mouse_c.get_width()/2

y -= mouse_c.get_height()/2

screen.blit(mouse_c(x,y))

pygame.display.update()

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


  • No HTML is allowed in comments, but URLs will be hyperlinked
  • Comments are not for promoting your hubs or other sites

working