Hi, i'm new to Python and i'm currently following the "How to Think like a Computer Scientist" tutorials. I'm at Chapter 4: Conditionals and doing the last exercise where i need to create six colored houses on a black background.

I've successfully created the house at the bottom left, but when i fill the "house" box with blue color, it blocks out the left window and the door. The right window, miraculously, remain unblocked. (I've left the background color as WHITE for now)

Anybody know why? Do you get the same result with the following code?

from gasp import *

begin_graphics(title="Houses At Night", background=color.WHITE)

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
Polygon(((20, 120), (70, 160), (70, 120)), filled=True, color=color.RED) #Left Roof
Polygon(((70, 160), (120, 120), (70, 120)), filled=True, color=color.RED) #Right Roof

update_when('key_pressed')

end_graphics()

Dani AI

Generated

Short, practical diagnosis and guidance (adds context to ’s report and ’s fix).

GASP paints screen objects in the order they are created: objects drawn later are placed on top of earlier ones. That means a filled, later-drawn rectangle will obscure any earlier-drawn details that lie under it. The symptom described (house fill covering the door/window) is the classic z-order effect; the behaviour is expected rather than a mysterious bug.

Recommended patterns and quick fixes:

  • Draw from back to front: background first, large filled shapes next, then small details (windows, doors, trim) last.
  • When composing many items, use a two-pass approach: create all house bodies first, then create all doors/windows/roofs so details are guaranteed to be on top.
  • Keep handles to objects when future moves or reordering are needed (store the return from the constructor). To change stacking dynamically, remove and redraw the affected object(s) (GASP supports removing objects and redrawing them in a new order).

Example (conceptual, not a duplicate of thread code):

# two-pass pattern (conceptual)
bodies = [Box(pos, w, h, filled=True, color=... ) for pos in house_positions]
for pos in house_positions:
    # draw windows/door/roof for this house so they appear above the body
    Box(window_pos, ..., filled=True, color=...)
    Box(door_pos, ..., filled=True, color=...)

Troubleshooting notes and cautions:

  • Confirm coordinates — a misplaced window/door can fall outside expected bounds and be hidden by another shape.
  • If reordering appears inconsistent across machines, check GASP/Pygame compatibility; older GASP wrappers and newer Pygame versions can behave differently on some platforms.
  • When dynamic z-ordering is required at runtime, the simplest reliable method in GASP is to remove and recreate objects in the intended order rather than relying on a built-in z-index.

This explains why the re-arrangement in the thread solved the problem and gives safer drawing patterns for future scenes.

Recommended Answers

All 2 Replies

My recommendation, don't work with an old ditty like module gasp. It is a wrapper for PyGame and doesn't work very well with newer versions of PyGame on Windows.

I got it to go this way ...

# for Windows and Python26 dowmload and install gasp from:
# http://edge.launchpad.net/gasp-core/0.1.x/0.2.0beta1/+download/python-gasp-0.2.0beta1.win32.exe
# needs PyGame installed too

from gasp import *

begin_graphics(title="Houses At Night", background=color.WHITE)

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

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
Polygon(((20, 120), (70, 160), (70, 120)), filled=True, color=color.RED) #Left Roof
Polygon(((70, 160), (120, 120), (70, 120)), filled=True, color=color.RED) #Right Roof

update_when('key_pressed')  # wait

end_graphics()

Thanks alot. LOL a simple re-arrangement did the trick. It really doesn't make sense at all because i swear my code was alright.

But i guess if i have to write any other things using GASP, i should just simply re-arrange if there are any semantic errors.

Once again. Thanks for the help.

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.