Hello everyone, I'm obviously having trouble with a program that wants me to create a house with just five mouse clicks.

My program is simple create a house using only 5 mouse clicks.

This problem is from the book Python Programming by John Zelle

Its on page 162

This is what the book says on the problem:

You are to write a program that allows the user to draw a simple house using five mouse clicks. The first two clicks will be the opposite corners of the rectangle frame of the house. The third click will indicate the center of teh top edge of a rectangular door. The door should have a total width that is 1/5th of the width of the house frame. The sides of the door should extend from the corners of the top down to the bottom of the frame. The fourth click will indicate the center of a square window. The window is half as wide as the door. The last click will indicate the peak of the roof. The edges of the roof will extend from the point at the peak to the corners of the top edge of the house frame.


Thanks for the help in advance.
PS I'm using python 2.2.3 version

So far this is what I have come up with:

from graphics22 import *

def main():

win = GraphWin("house.py", 500, 500)
win.setCoords(0,0, 4,4)

p1 = win.getMouse()
p2 = win.getMouse()
p3 = win.getMouse()
p4 = win.getMouse()
p5 = win.getMouse()

house = Rectangle(p1, p2)
house.setFill("Red")
house.draw(win)

roof = Polygon(p1, p3, p4)
roof.setFill("Black")
roof.draw(win)

#door = Rectangle ()
#door.setFill("Brown")
#door.draw(win)

#window = Rectangle()
#window.setFill("White")
#window.draw(win)

win.getMouse()
win.close()
main()

I can create the rectangle for the base of the house and the roof but then I'm already at 4 mouse clicks.

I know this sounds funny, but you need to get out a piece of construction paper and some crayons and draw yourself a house.

Then, using your finger, 'click' on the picture where the instructions say and in the order they say. For example,
1: click on lower-left corner of house
2: click on upper-right corner of house
3: click on the center of the top edge of the front door
4: click on the center of the window
5: click on the top point of the roof

As you do that, think about what information you must calculate at each step. You will have to reuse information you gained at previous steps.

I also recommend that you draw something the instant you can to provide user feedback.
So after the first two clicks, draw the rectangle.
After the next click, draw the door.
After the next click, draw the window.
And after the final click, draw the roof.

Hope this helps.

Does win.getMouse() wait for the mouse to be clicked before it returns? If not, then you may have a problem ... because it will fire 5 times instantaneously, and get the same mouse coordinates for p1 through p5.

Jeff

No, he is using a package called graphics.py. It is designed for beginners, so it is fairly convenient and simple to use. The getMouse() function waits for you to click and returns the coordinates. That is why it would be easy to say:

get click one
get click two
draw house rect
get click three
draw door rect
...

#This is the working code I wrote for you

from graphics import *

win = GraphWin("house.py", 500, 500)
win.setCoords(0,0, 4,4)

p1 = win.getMouse()

x=p1.getX()
y=p1.getY()
p6=Point(x+0.05,y+0.05)
Pt = Oval(p1,p6)
Pt.setFill(color_rgb(255,255,0))
Pt.setOutline(color_rgb(255,255,0))
Pt.draw(win)

p2 = win.getMouse()

x=p2.getX()
y=p2.getY()
p6=Point(x+0.05,y+0.05)
Pt = Oval(p2,p6)
Pt.setFill(color_rgb(255,255,0))
Pt.setOutline(color_rgb(255,255,0))
Pt.draw(win)

p3 = win.getMouse()

x=p3.getX()
y=p3.getY()
p6=Point(x+0.05,y+0.05)
Pt = Oval(p3,p6)
Pt.setFill(color_rgb(255,255,0))
Pt.setOutline(color_rgb(255,255,0))
Pt.draw(win)


p4 = win.getMouse()

x=p4.getX()
y=p4.getY()
p6=Point(x+0.05,y+0.05)
Pt = Oval(p4,p6)
Pt.setFill(color_rgb(255,255,0))
Pt.setOutline(color_rgb(255,255,0))
Pt.draw(win)


p5 = win.getMouse()

x=p5.getX()
y=p5.getY()
p6=Point(x+0.05,y+0.05)
Pt = Oval(p5,p6)
Pt.setFill(color_rgb(255,255,0))
Pt.setOutline(color_rgb(255,255,0))
Pt.draw(win)


p7=Point(p2.getX(),p1.getY())

house = Rectangle(p1, p2)
house.setFill("Red")
house.draw(win)

roof = Polygon(p1, p7, p3)
roof.setFill("Black")
roof.draw(win)

p8=Point(p4.getX()+0.5,p2.getY())

door = Rectangle(p4,p8)
door.setFill("Brown")
door.draw(win)

p9=Point(p5.getX()+0.25,p5.getY()-0.25)
window = Rectangle(p5,p9)
window.setFill("White")
window.draw(win)

win.getMouse()
win.close()

from graphics import *

win = GraphWin("house.py", 500, 500)
win.setCoords(0,0, 4,4)

p1 = win.getMouse()

x=p1.getX()
y=p1.getY()
p6=Point(x+0.05,y+0.05)
Pt = Oval(p1,p6)
Pt.setFill(color_rgb(255,255,0))
Pt.setOutline(color_rgb(255,255,0))
Pt.draw(win)

p2 = win.getMouse()

x=p2.getX()
y=p2.getY()
p6=Point(x+0.05,y+0.05)
Pt = Oval(p2,p6)
Pt.setFill(color_rgb(255,255,0))
Pt.setOutline(color_rgb(255,255,0))
Pt.draw(win)

house = Rectangle(p1, p2)
house.setFill("Red")
house.draw(win)

p3 = win.getMouse()

x=p3.getX()
y=p3.getY()
p6=Point(x+0.05,y+0.05)
Pt = Oval(p3,p6)
Pt.setFill(color_rgb(255,255,0))
Pt.setOutline(color_rgb(255,255,0))
Pt.draw(win)

p7=Point(p2.getX(),p1.getY())

roof = Polygon(p1, p7, p3)
roof.setFill("Black")
roof.draw(win)

p4 = win.getMouse()

x=p4.getX()
y=p4.getY()
p6=Point(x+0.05,y+0.05)
Pt = Oval(p4,p6)
Pt.setFill(color_rgb(255,255,0))
Pt.setOutline(color_rgb(255,255,0))
Pt.draw(win)

p8=Point(p4.getX()+0.5,p2.getY())

door = Rectangle(p4,p8)
door.setFill("Brown")
door.draw(win)

p5 = win.getMouse()

x=p5.getX()
y=p5.getY()
p6=Point(x+0.05,y+0.05)
Pt = Oval(p5,p6)
Pt.setFill(color_rgb(255,255,0))
Pt.setOutline(color_rgb(255,255,0))
Pt.draw(win)

p9=Point(p5.getX()+0.25,p5.getY()-0.25)

window = Rectangle(p5,p9)
window.setFill("White")
window.draw(win)

win.getMouse()
win.close()

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.