You are on page 1of 2

Drawing challenges

Learning intention: sequence instructions to create graphics in Python


Use the snipping tool or similar to take images for your blog!
Challenge
Copy and paste the code on page 2 into a Python file and run (F5).
Can you change the window size?
Can you change the background colour?
Can you edit the title text at the top of the window from My Game to something of your
own choice?
Can you draw a rectangle on the screen using the code below, inserted below the comment
# PUT YOUR CODE FOR DRAWING BENEATH THIS COMMENT and indented to the same level
with the code above:

Completed? (add a
note if you wish)
Completed
Completed
Completed
Completed
Completed

pygame.draw.rect(screen, BLACK, [20, 20, 250, 100], 2)

Can you draw rectangles of different sizes in different locations with different border
thicknesses?
***dont forget to indent*** Can you draw a triangle using:

Completed
Completed

pygame.draw.polygon(screen, BLACK, [[100, 100], [0, 200], [200,


200]], 5)

Can you draw a variety of polygons around the screen, adapting the code above?
Can you use the code below to add text to your screen? ***Dont forget to indent in line
with the other text!****

Completed
Completed

# Select the font to use, size, bold, italics


font = pygame.font.SysFont('Calibri', 25, True, False)
# Render the text. "True" means anti-aliased text.
# Black is the color. This creates an image of the
# letters, but does not put it on the screen
text = font.render("My text", True, BLACK)
# Put the image of the text on the screen at 250x250
screen.blit(text, [250, 250])

Can you add some of your own text at different locations around the screen?
Can you use the loop below to draw repeated images?

Completed
Completed

# Draw on the screen several lines from (0,10) to (100,110)


# 5 pixels wide using a loop
for y_offset in range(0, 100, 10):
pygame.draw.line(screen, RED, [0, 10 + y_offset], [100, 110 +
y_offset], 5)

Can you edit the loop youve just added to draw the lines in different locations on the screen?
Can you edit the loop to draw lines spaced out differently?
Can you edit the loop to draw rectangles or other shapes?
Further drawing challenges:
You may wish to explore more commands from:
http://www.pygame.org/docs/ref/draw.html
or
http://programarcadegames.com/index.php?chapter=introduction_to_graphics&lang=en#se
ction_5 (scroll down quite a bit!)
Further reading:
http://pygame.org/
http://programarcadegames.com/

Completed
Completed
Completed
Completed

"""
Exploring graphics
Sample Python/Pygame Programs
Simpson College Computer Science
http://programarcadegames.com/
http://simpson.edu/computer-science/
"""
import pygame
# Define some colours using RGB values
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
pygame.init()
# Set the width and height of the screen [width, height]
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop ----------while not done:
# --- Main event loop
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
# --- Game logic should go here
# --- Drawing code should go here
# First, clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
screen.fill(WHITE)
# PUT YOUR CODE FOR DRAWING BENEATH THIS COMMENT

# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(60)
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()

You might also like