http://www.daniweb.com/software-development/python/threads/187060/1572491#post1572491

based on a previous thread I would like to know how to replicate the appearance of a house five times on the gasp interface via the script below:

#!/usr/bin/env python

from gasp import * # import everything from the gasp library

begin_graphics(title="Houses At Night", background=color.BLACK) # open the graphics canvas

def draw_house(x, y):

Box((20, 20), 100, 100, filled=True, color=color.BLUE) # the house

Box((55, 20), 30, 50, filled=True, color=color.GREEN) # the door

Box((40, 80), 20, 20, filled=True, color=color.YELLOW) # the left window
Box((80, 80), 20, 20, filled=True, color=color.YELLOW) # the right window

a = (20, 120)

b = (70, 160)

c = (70, 120)

Polygon (([a, b , c]), filled=True, color=color.RED) #Left Roof

d = (70, 160)

e = (120, 120)

f = (70, 120)

Polygon (([d, e, f]), filled=True, color=color.RED) #Right Roof

draw_house(20, 100)
#Box((20 + x, 20 + y), 100 + x, 100 + y)

update_when('key_pressed') # keep the canvas open until a key is pressed

end_graphics() # close the canvas (which would happen
# anyway, since the script ends here, but it
# is better to be explicit).

However I am not too sure how to call the function draw_house() 5 times in order to appear on the screen, any ideas?

Code tags are generally good idea of getting response:

#!/usr/bin/env python

from gasp import *          # import everything from the gasp library

begin_graphics(title="Houses At Night", background=color.BLACK) # open the graphics canvas

def draw_house(x, y):
         
	Box((20, 20), 100, 100, filled=True, color=color.BLUE)     # the house

	Box((55, 20), 30, 50, filled=True, color=color.GREEN)       # the door
    
	Box((40, 80), 20, 20, filled=True, color=color.YELLOW)       # the left window
	Box((80, 80), 20, 20, filled=True, color=color.YELLOW)       # the right window

	a = (20, 120)

	b = (70, 160)

	c = (70, 120)
    
	Polygon (([a, b , c]), filled=True, color=color.RED) #Left Roof

	d = (70, 160)

	e = (120, 120)

	f = (70, 120)

	Polygon (([d, e, f]), filled=True, color=color.RED) #Right Roof

draw_house(20, 100)
#Box((20 + x, 20 + y), 100 + x, 100 + y)
    
update_when('key_pressed')  # keep the canvas open until a key is pressed

end_graphics()              # close the canvas (which would happen
                            # anyway, since the script ends here, but it
                            # is better to be explicit).

Also showing some of your failures, and how you tried to correct those, would help enormously getting sympathy to you. Don't be shy!

The fame of gasp as outdated stuff could hinder you, though.

gasp has had it's last gasp (no pun intended) a long time ago

For those who are curious, look here:
https://launchpad.net/gasp

Please don't mark this thread as solved when it is not and I know gasp is outdated but for the purposes of following a tutorial for learning python I would like to attempt to get this script working.

Here is the error code:

#!/usr/bin/env python

from gasp import *          # import everything from the gasp library

begin_graphics(title="Houses At Night", background=color.BLACK) # open the graphics canvas

def draw_house(x, y):
         
	Box((20, 20), 100, 100, filled=True, color=color.BLUE)     # the house

	Box((55, 20), 30, 50, filled=True, color=color.GREEN)       # the door
    
	Box((40, 80), 20, 20, filled=True, color=color.YELLOW)       # the left window
	Box((80, 80), 20, 20, filled=True, color=color.YELLOW)       # the right window

	a = (20, 120)

	b = (70, 160)

	c = (70, 120)
    
	Polygon (([a, b , c]), filled=True, color=color.RED) #Left Roof

	d = (70, 160)

	e = (120, 120)

	f = (70, 120)

	Polygon (([d, e, f]), filled=True, color=color.RED) #Right Roof

draw_house(20, 100)
Box((20 + x, 20 + y), 100 + x, 100 + y)
Polygon((20 + x, 20 + y), 100 + x, 100 + y)
    
update_when('key_pressed')  # keep the canvas open until a key is pressed

end_graphics()              # close the canvas (which would happen
                            # anyway, since the script ends here, but it
                            # is better to be explicit).

ERROR here:

~$ python house.py
Traceback (most recent call last):
File "house.py", line 33, in <module>
Box((20 + x, 20 + y), 100 + x, 100 + y)
NameError: name 'x' is not defined

  1. You have not set value for x. About code I can not say anything if you do not use CODE tags.

You have not set value for x. About code I can not say anything if you do not use [code] tags.

Yes but the draw house function needs to edited in order to call the preceding function correctly as currently it still does not work even if x is defined as below:

draw_house(20, 100)
Box((20 + x, 20 + y), 100 + x, 100 + y)
Polygon((20 + x, 20 + y), 100 + x, 100 + y)

Any more suggestions to the issue above?

I did something similar a long back when I was playing around with Python. You can find my scribblings here; just adjust the method calls and you should be able to understand how it works. The trick here is to think in terms of co-ordinate, try to plot stuff on a piece of paper and then code for it. Also, please make sure you use the code only as a reference and try to come up with your own implementation. ;-)

Try it this way ...

#!/usr/bin/env python

from gasp import *          # import everything from the gasp library

begin_graphics(title="Houses At Night", background=color.BLACK) # open the graphics canvas

def draw_house(x, y):
         
	Box((20, 20), 100, 100, filled=True, color=color.BLUE)     # the house

	Box((55, 20), 30, 50, filled=True, color=color.GREEN)       # the door
    
	Box((40, 80), 20, 20, filled=True, color=color.YELLOW)       # the left window
	Box((80, 80), 20, 20, filled=True, color=color.YELLOW)       # the right window

	a = (20, 120)

	b = (70, 160)

	c = (70, 120)
    
	Polygon (([a, b , c]), filled=True, color=color.RED) #Left Roof

	d = (70, 160)

	e = (120, 120)

	f = (70, 120)

	Polygon (([d, e, f]), filled=True, color=color.RED) #Right Roof

x = 20
y = 100
draw_house(x, y)
# now x and y are set
Box((20 + x, 20 + y), 100 + x, 100 + y)
Polygon((20 + x, 20 + y), 100 + x, 100 + y)
    
update_when('key_pressed')  # keep the canvas open until a key is pressed

end_graphics()              # close the canvas (which would happen
                            # anyway, since the script ends here, but it
                            # is better to be explicit).

Received this error message:

:~$ python house.py
Traceback (most recent call last):
File "house.py", line 37, in <module>
Polygon((20 + x, 20 + y), 100 + x, 100 + y)
File "/usr/lib/pymodules/python2.6/gasp/api.py", line 264, in __init__
else: raise backend.GaspException("points must be a list")
gasp.backend.GaspException: 'points must be a list'

Thanks to ~s.o.s.~ for his coding as a reference, I understood that variables x and y must be part of the draw_house function in order to work as shown below:

#!/usr/bin/env python

from gasp import *          # import everything from the gasp library

begin_graphics(title="Houses At Night", background=color.BLACK) # open the graphics canvas

def draw_house(x, y):
         
	Box((x + 20, y + 20), 100, 100, filled=True, color=color.BLUE)     # the house

	Box((x + 55, y + 20), 30, 50, filled=True, color=color.GREEN)       # the door
    
	Box((x + 40, y + 80), 20, 20, filled=True, color=color.YELLOW)       # the left window
	Box((x + 80, y + 80), 20, 20, filled=True, color=color.YELLOW)       # the right window

	a = (x + 20, y + 120)

	b = (x + 70, y + 160)

	c = (x + 70, y + 120)
    
	Polygon (([a, b , c]), filled=True, color=color.RED) #Left Roof

	d = (x + 70, y + 160)

	e = (x + 120, y + 120)

	f = (x + 70, y + 120)

	Polygon (([d, e, f]), filled=True, color=color.RED) #Right Roof

#bottom row
draw_house(0, 0)
draw_house(200, 0)
draw_house(400, 0)

#middle row
draw_house(100, 140)
draw_house(300, 140)

#top row
draw_house(200, 280)


    
update_when('key_pressed')  # keep the canvas open until a key is pressed

end_graphics()              # close the canvas (which would happen
                            # anyway, since the script ends here, but it
                            # is better to be explicit).

Glad it worked out well for you, good luck with your project! :-)

I have one question. Why did you make 2 different polygons for roofs. Just wondering because I'm doing a similar program and the polygon isn't working.

Those are not really required, it's just another different (more verbose?) way of drawing the roof. My code snippet referenced on the first page works fine with one polygon.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.