In the book "How to Think Like A Computer Scientist" on the website openbookproject.net the tutorial leads you to draw a house with a pre-written script. It then instructs you to: # Wrap the house code in a function named draw_house().
# Run the script now. Do you see a house? Why not?
# Add a call to draw_house() at the botton of the script so
that the house returns to the screen.
# Parameterize the function with x and y parameters
-- the header should then become def draw_house(x, y):, so that
you can pass in the location of the house on the canvas.
# Use draw_house to place five houses on the canvas in
different locations.
So I wrote this (the parameters given in the definition were already given to draw a single house):
from gasp import *
def draw_house(x, y):
begin_graphics()
Box((20, 20), 100, 100) #the house
Box((55, 20), 30, 50) #the door
Box((40, 80), 20, 20) #the left window
Box((80, 80), 20, 20) #the right window
Line((20, 120), (70, 160)) #the left roof
Line((70, 160), (120, 120)) #the right roof
update_when('key_pressed')
end_graphics()
draw_house(5, 5)
draw_house(5, 100)
draw_house(30, 50)
draw_house(100, 5)
draw_house(100, 100)
The problem is, it only draws a single house at the bottom left of the canvas and does nothing else even with keystrokes. When I X-out of gasp, I get the following errors in the python shell"
>>>
Traceback (most recent call last):
Exception in thread Thread-14:
Traceback (most recent call last):
File "/usr/lib/python2.5/threading.py", line 486, in __bootstrap_inner
self.run()
File "/usr/lib/python2.5/site-packages/gasp/backend.py", line 396, in run
e = pygame.event.wait()
error
File "/home/me/draw_house.py", line 15, in <module>
draw_house(5, 100)
File "/home/me/draw_house.py", line 11, in draw_house
update_when('key_pressed')
File "/usr/lib/python2.5/site-packages/gasp/api.py", line 449, in update_when
backend.update_when(event_type)
File "/usr/lib/python2.5/site-packages/gasp/backend.py", line 482, in update_when
pygame.event.pump()
error: video system not initialized
>>>