When you have a project like this, it is best to test each shape you want to draw before you combine it all. Let's start out with a pygame template that you can use for each shape ...
import pygame
import random
pygame.init()
w = 640
h = 480
screen = pygame.display.set_mode((w, h))
# set (r, g, b) color tuple
color = (random.randrange(255), random.randrange(255),
random.randrange(255))
# ... your shape code goes here ...
# update display
pygame.display.flip()
# event loop ...
running = True
while running:
for event in pygame.event.get():
# quit when window corner x is clicked
if event.type == pygame.QUIT:
running = False
Now let's test the rectangle shape ...
import pygame
import random
pygame.init()
w = 640
h = 480
screen = pygame.display.set_mode((w, h))
# set (r, g, b) color tuple
color = (random.randrange(255), random.randrange(255),
random.randrange(255))
# set rect corner coordiates to draw a rectangle
# (x1, y1, x2, y2) upper left and lower right corner coordinates
rect = (random.randrange(w), random.randrange(h),
random.randrange(w), random.randrange(h))
# pygame.draw.rect(Surface, color, Rect, width=0)
# if width=0 (or not given) the rectangle is filled with color
pygame.draw.rect(screen, color, rect)
# update display
pygame.display.flip()
# event loop ...
running = True
while running:
for event in pygame.event.get():
# quit when window corner x is clicked
if event.type == pygame.QUIT:
running = False
... next test the line ...
import pygame
import random
pygame.init()
w = 640
h = 480
screen = pygame.display.set_mode((w, h))
# set (r, g, b) color tuple
color = (random.randrange(255), random.randrange(255),
random.randrange(255))
# set start and end position of line
start_pos = random.randrange(w), random.randrange(h)
end_pos = random.randrange(w), random.randrange(h)
# pygame.draw.line(Surface, color, start_pos, end_pos, width=1)
# width is thickness of line (default is 1)
pygame.draw.line(screen, color, start_pos, end_pos)
# update display
pygame.display.flip()
# event loop ...
running = True
while running:
for event in pygame.event.get():
# quit when window corner x is clicked
if event.type == pygame.QUIT:
running = False
... lastly test the circle ...
import pygame
import random
pygame.init()
w = 640
h = 480
screen = pygame.display.set_mode((w, h))
# set (r, g, b) color tuple
color = (random.randrange(255), random.randrange(255),
random.randrange(255))
# set center_pos for circle
center_pos = random.randrange(w), random.randrange(h)
# pygame.draw.circle(Surface, color, center_pos, radius, width=0)
# if width=0 (or not given) the circle is filled with color
radius = 50
pygame.draw.circle(screen, color, center_pos, radius)
# update display
pygame.display.flip()
# event loop ...
running = True
while running:
for event in pygame.event.get():
# quit when window corner x is clicked
if event.type == pygame.QUIT:
running = False