Professional Documents
Culture Documents
pygame.display.flip()
#
#
#
#
#
pygame.time.wait(3000)
pygame.quit()
# pygame2.py
# A simple modification to the template allows us to focus on just what
# we care about (the drawing) and not get bogged down in all the pygame
# system stuff
import pygame
pygame.init()
SIZE = (800, 600)
screen = pygame.display.set_mode(SIZE)
def drawScene(screen):
RED = (255, 0, 0)
pygame.draw.line(screen, RED, (0,0), (100,50))
pygame.display.flip()
drawScene(screen)
pygame.time.wait(3000)
pygame.quit()
The next few examples all use the same template, so I will only show the drawScene. Do a saveAs to start
the new file so you don't have to re-type everything.
# pygame3.py
# basic drawing commands
def drawScene(screen):
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
SEA_GREEN = (67, 205, 128)
screen.fill(WHITE)
box = pygame.Rect([50,10,100,60])
pygame.draw.rect(screen, BLUE, box)
box = box.move([200,0])
pygame.draw.rect(screen, BLUE, box, 2) # Last parameter is width, if you don't
# add it, it will be 0. If it is 0 it will fill
pygame.draw.circle(screen, SEA_GREEN, (50, 200), 50)
pygame.draw.arc(screen, SEA_GREEN, pygame.Rect([150, 150, 250, 250]), 0, 3.14)
pygame.draw.line(screen, BLUE, (0,500), (800,500))
pygame.display.flip()
# pygame4.py
# using a for loop to draw patterns
from pygame import *
init()
size = width, height = 800, 600
screen = display.set_mode(size)
def drawScene(screen):
for x in range(0,width,10):
draw.line(screen, (0, 255, 0), (x,0), (x,height))
display.flip()
# pygame5.py
# nested for loop to draw a grid pattern
def drawScene(screen):
OLIVE = (107, 142, 35)
for x in range(20,width,40):
for y in range(20,height,40):
draw.circle(screen, OLIVE, (x,y), 18)
display.flip()
# pygame6.py
# nested for loop to draw a grid pattern
# creating colours "on the fly"
def drawScene(screen):
for x in range(20,width,40):
for y in range(20,height,40):
c = ((x*256)/width,(y*256)/height,0)
draw.circle(screen, c, (x,y), 18)
display.flip()
Draw a pattern of lines from the center of the screen to every 10th pixel on the perimeter of the
screen.
save as drawEx1.py
2.
Use graph paper to draw a scene (e.g. your house.) Determine which python shapes you would
need to draw this on the computer and write the important coordinates on the paper. Create a
program that draws your scene in python
save as drawEx2.py
3.
Create a program that draws an attractive black and white chess board.
4.
5.
6.
7.
Draw two randomly placed radius 10 circles on the screen then draw radius 2 circles every twenty
pixels from the center of one to the center of the other.
save as drawEx7.py
save as drawEx3.py