Why Can't I Draw Multiple Houses in Python?

  • Python
  • Thread starter brushman
  • Start date
  • Tags
    Python
In summary, the code above allows for the drawing of a house at specific coordinates using the draw_house function, but the canvas is closed after the first house is drawn, preventing the second house from being drawn.
  • #1
brushman
113
1
Basically, I have a function that draws a house. You enter (x, y) coordinates to pick where the house is drawn. However, it only let's me draw one house. Why can't I draw more then one house by calling the draw_house(x, y) function multiple times? Here's the code:

Code:
from gasp import *          # import everything from the gasp library

begin_graphics()            # open the graphics canvas


def draw_house(x, y):
    Box((x, y), 100, 100)         # the house
    Box((x+35, y), 30, 50)           # the door
    Box((x+20, y+60), 20, 20)           # the left window
    Box((x+60, y+60), 20, 20)           # the right window
    Line((x, y+100), (x+50, y+140))      # the left roof
    Line((x+50, y+140), (x+100, y+100))     # the right roof

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

draw_house(90, 90)
draw_house(200, 200)

end_graphics()              # close the canvas

In the code above, is draws the first house at (90, 90), but why doesn't it draw the second house?
 
Technology news on Phys.org
  • #2
Caveat emptor: I don't know anything about python, so I might be way off in left field here.

The code starts off by the call to begin_graphics(), which opens the graphics canvas. Then there is the call to draw_house(90, 90), which causes the body of the draw_house function to execute, drawing a house whose opposite corners are at (90, 90) and (190, 190).

The last line of code in the draw_house function calls update_when, which, when a key is pressed, closes the graphics canvas (which I infer from the comment), so the call to draw_house(200, 200) fails, probably because the graphics canvas is closed.

That's what I think is going on.
 
  • #3
Thanks, that was it.
 

Related to Why Can't I Draw Multiple Houses in Python?

1. Why is it difficult to draw multiple houses in Python?

Drawing multiple houses in Python can be challenging because it requires a combination of coding skills, mathematical understanding, and artistic creativity. It involves manipulating coordinates, creating loops, and understanding the concept of object-oriented programming.

2. Can I use any library in Python to draw multiple houses?

Yes, there are several libraries available in Python that can help with drawing multiple houses. Some popular ones include Turtle, PyGame, and Matplotlib. These libraries provide functions and tools specifically designed for creating graphics and visuals.

3. How can I improve my skills in drawing multiple houses in Python?

Practice is key when it comes to improving your skills in drawing multiple houses in Python. Start with simpler shapes and gradually move on to more complex ones. It's also helpful to study and understand the code written by experienced programmers.

4. Are there any specific techniques for drawing multiple houses in Python?

Yes, there are various techniques that can be used to draw multiple houses in Python. One common approach is to use a combination of loops and functions to draw individual components of the house, such as walls, windows, and doors. Another technique is to use the object-oriented programming concept to create house objects with different properties.

5. Can drawing multiple houses in Python be useful for data visualization?

Yes, drawing multiple houses in Python can be useful for data visualization. It can be used to represent data in a more visually appealing and engaging way, making it easier for the audience to understand and interpret the data. This can be particularly useful in fields like real estate, urban planning, and architecture.

Similar threads

  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
960
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top