Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #### ======= HW4 template ======= ####
- from graphics import *
- from time import *
- import math
- import random
- # draw cloud on the left and right side of the sky based on the points using function. this function should return alist of circles.
- def draw_cloud(win, x1,x2,y1,y2):
- cloud_list = []
- for i in range(11):
- x = random.randint(x1, x2)
- y = random.randint(y1, y2)
- p = Point(x,y)
- cloud = Circle(p,2)
- cloud.setFill("white")
- cloud.setOutline("white")
- cloud.draw(win)
- cloud_list.append(cloud)
- return cloud_list
- # draw the trunk of the tree with two points as a rectangle, calculate the circle above the tree trunk.
- def draw_house_body(win, p1, p2):
- # drawing rectangle as house body.
- rect = Rectangle(p1, p2)
- rect.draw(win)
- rect.setFill("red")
- return
- def draw_house(win,p1,p2,p3):
- # finding width and height of the rectangle.
- width = p2.x - p1.x
- height = p2.y - p1.y
- # finding the points for the roof polygon.
- leftroof = Point((p1.x + width/3), (p3.y))
- rightroof = Point((p2.x + width/3), (p3.y))
- # hint : for x values of the roof, add p1.x and p2.x by (width/3)
- # drawing the roof
- roof = Polygon(Point(p1.x, p2.y), leftroof, rightroof, Point(p2.x, p2.y))
- roof.draw(win)
- roof.setFill("brown")
- # draw the front polygon
- y1 = Point(p2.x, p1.y)
- y3 = Point(p2.x + 2*width/3, p2.y)
- y4 = Point(p2.x + 2*width/3, p2.y)
- y5 = Point(p2.x + 2*width/3, p1.y)
- front = Polygon(y1, p2, rightroof, y4, y5)
- front.draw(win)
- front.setFill("pink")
- return
- class Background:
- def __init__(self, theSky, theSun, theClouds):
- self.theSky = theSky
- self.theSun = theSun
- self.theClouds = theClouds
- def main():
- # Initializing Speed
- Speed = 0.4
- # creating the graphic window
- win = GraphWin("Blue sky and green field!", 800, 600)
- win.setCoords(0, 0, 40, 30)
- # set the background color (width is 40, height is 30)
- BackGround = Rectangle(Point(0,15), Point(40, 30))
- BackGround.setFill("light blue")
- BackGround.draw(win)
- # set alternate views
- b1 = Background("deep sky blue", "dark orange", "light gray")
- b2 = Background("navy", "navy", "gray")
- b3 = Background("light blue", "yellow", "white")
- # set the Ground color
- Ground = Rectangle(Point(0,0) , Point(40, 15))
- Ground.setFill("light Green")
- Ground.draw(win)
- # draw the sun
- sun = Circle(Point(20,25), 2)
- sun.setFill("Yellow")
- sun.setOutline("Yellow")
- sun.draw(win)
- # draw the mountain1
- Mountain1 = Polygon(Point(0,15), Point(15, 28), Point(30,15))
- Mountain1.setFill("dark green")
- Mountain1.draw(win)
- # draw the mountain2
- Mountain2 = Polygon(Point(10,15), Point(23,26), Point(40,15))
- Mountain2.setFill("dark green")
- Mountain2.draw(win)
- # === call draw_cloud function for the left side of the sky ===
- m1 = draw_cloud(win, 5,10,20,21)
- # === call draw_cloud function for the right side of the sky ===
- m2 = draw_cloud(win, 24,29,22,23)
- # === set text for asking user to click a point ===
- pntMsg = Point(20, 29)
- txtMsg = Text(pntMsg, "Please click the left bottom point of the house.")
- txtMsg.setStyle("bold")
- txtMsg.setTextColor("red")
- txtMsg.draw(win)
- # === get the lower point from user ===
- p1 = win.getMouse()
- p1.draw(win)
- p1.setFill("brown")
- # === change the text to ask user to click another point ===
- txtMsg.setText("Now click the upper right point of the house.")
- # === get the upper point from user ===
- p2 = win.getMouse()
- p2.draw(win)
- p1.setFill("brown")
- # === call function to draw the house body ===
- draw_house_body(win, p1, p2)
- # === change the text to ask user to click another point ===
- txtMsg.setText("Now click the roof top point of the house.")
- # === get the roof top point from user ===
- # TODO: get point p3
- p3 = win.getMouse()
- p3.draw(win)
- p3.setFill("brown")
- # === call function to draw the roof and front of the house ===
- draw_house(win, p1, p2, p3)
- # === change the text inside text box ===
- txtMsg.setText("Please click a button.")
- # === View button ===
- button_1 = Rectangle(Point(2,3), Point(6, 5))
- button_1.setFill("yellow")
- button_1.draw(win)
- ViewMsg = Text(Point(4, 4), "Views")
- ViewMsg.setStyle("bold")
- ViewMsg.setTextColor("black")
- ViewMsg.draw(win)
- # === Wind buttton ===
- # TODO: details of button 2
- button_2 = Rectangle(Point(7,3), Point(11, 5))
- button_2.setFill("yellow")
- button_2.draw(win)
- ViewMsg2 = Text(Point(9, 4), "Wind")
- ViewMsg2.setStyle("bold")
- ViewMsg2.setTextColor("black")
- ViewMsg2.draw(win)
- # === Speed Button ===
- # TODO: details of button 3
- button_3 = Rectangle(Point(12,3), Point(16, 5))
- button_3.setFill("yellow")
- button_3.draw(win)
- ViewMsg3 = Text(Point(14, 4), "Speed")
- ViewMsg3.setStyle("bold")
- ViewMsg3.setTextColor("black")
- ViewMsg3.draw(win)
- # === Exit Button ===
- # TODO: details of button 4
- button_4 = Rectangle(Point(17,3), Point(21, 5))
- button_4.setFill("grey")
- button_4.draw(win)
- ViewMsg4 = Text(Point(19, 4), "Exit")
- ViewMsg4.setStyle("bold")
- ViewMsg4.setTextColor("black")
- ViewMsg4.draw(win)
- # check if user clicks any of the three buttons
- while True:
- continue
- pClick = win.getMouse()
- x = pClick.getX()
- y = pClick.getY()
- #exit button
- if (x>=17 and x<=21) and (y>=3 and y<=5):
- break
- #view button
- #elif (x>=2 and x<=6) and (y>=3 and y<=5):
- #wind button
- #elif (x>=2 and x<=11) and (y>=3 and y<=5):
- # TODO: Move clouds
- #speed button
- elif (x>=12 and x<=16) and (y>=3 and y<=5):
- for x in range(4):
- cloud_list.move(speed)
- sleep(0.1)
- # TODO: Change speed and speed button text.
- txtMsg.setText("Good Bye!")
- win.close()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement