Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. #### ======= HW4 template ======= ####
  2.  
  3. from graphics import *
  4. from time import *
  5. import math
  6. import random
  7.  
  8. # draw cloud on the left and right side of the sky based on the points using function. this function should return alist of circles.
  9. def draw_cloud(win, x1,x2,y1,y2):
  10. cloud_list = []
  11. for i in range(11):
  12. x = random.randint(x1, x2)
  13. y = random.randint(y1, y2)
  14. p = Point(x,y)
  15. cloud = Circle(p,2)
  16. cloud.setFill("white")
  17. cloud.setOutline("white")
  18. cloud.draw(win)
  19. cloud_list.append(cloud)
  20. return cloud_list
  21. # draw the trunk of the tree with two points as a rectangle, calculate the circle above the tree trunk.
  22. def draw_house_body(win, p1, p2):
  23. # drawing rectangle as house body.
  24. rect = Rectangle(p1, p2)
  25. rect.draw(win)
  26. rect.setFill("red")
  27. return
  28. def draw_house(win,p1,p2,p3):
  29. # finding width and height of the rectangle.
  30. width = p2.x - p1.x
  31. height = p2.y - p1.y
  32. # finding the points for the roof polygon.
  33. leftroof = Point((p1.x + width/3), (p3.y))
  34. rightroof = Point((p2.x + width/3), (p3.y))
  35. # hint : for x values of the roof, add p1.x and p2.x by (width/3)
  36. # drawing the roof
  37. roof = Polygon(Point(p1.x, p2.y), leftroof, rightroof, Point(p2.x, p2.y))
  38. roof.draw(win)
  39. roof.setFill("brown")
  40. # draw the front polygon
  41. y1 = Point(p2.x, p1.y)
  42. y3 = Point(p2.x + 2*width/3, p2.y)
  43. y4 = Point(p2.x + 2*width/3, p2.y)
  44. y5 = Point(p2.x + 2*width/3, p1.y)
  45. front = Polygon(y1, p2, rightroof, y4, y5)
  46. front.draw(win)
  47. front.setFill("pink")
  48. return
  49. class Background:
  50. def __init__(self, theSky, theSun, theClouds):
  51. self.theSky = theSky
  52. self.theSun = theSun
  53. self.theClouds = theClouds
  54. def main():
  55. # Initializing Speed
  56. Speed = 0.4
  57. # creating the graphic window
  58. win = GraphWin("Blue sky and green field!", 800, 600)
  59. win.setCoords(0, 0, 40, 30)
  60. # set the background color (width is 40, height is 30)
  61. BackGround = Rectangle(Point(0,15), Point(40, 30))
  62. BackGround.setFill("light blue")
  63. BackGround.draw(win)
  64. # set alternate views
  65. b1 = Background("deep sky blue", "dark orange", "light gray")
  66. b2 = Background("navy", "navy", "gray")
  67. b3 = Background("light blue", "yellow", "white")
  68. # set the Ground color
  69. Ground = Rectangle(Point(0,0) , Point(40, 15))
  70. Ground.setFill("light Green")
  71. Ground.draw(win)
  72. # draw the sun
  73. sun = Circle(Point(20,25), 2)
  74. sun.setFill("Yellow")
  75. sun.setOutline("Yellow")
  76. sun.draw(win)
  77. # draw the mountain1
  78. Mountain1 = Polygon(Point(0,15), Point(15, 28), Point(30,15))
  79. Mountain1.setFill("dark green")
  80. Mountain1.draw(win)
  81. # draw the mountain2
  82. Mountain2 = Polygon(Point(10,15), Point(23,26), Point(40,15))
  83. Mountain2.setFill("dark green")
  84. Mountain2.draw(win)
  85. # === call draw_cloud function for the left side of the sky ===
  86. m1 = draw_cloud(win, 5,10,20,21)
  87. # === call draw_cloud function for the right side of the sky ===
  88. m2 = draw_cloud(win, 24,29,22,23)
  89. # === set text for asking user to click a point ===
  90. pntMsg = Point(20, 29)
  91. txtMsg = Text(pntMsg, "Please click the left bottom point of the house.")
  92. txtMsg.setStyle("bold")
  93. txtMsg.setTextColor("red")
  94. txtMsg.draw(win)
  95. # === get the lower point from user ===
  96. p1 = win.getMouse()
  97. p1.draw(win)
  98. p1.setFill("brown")
  99. # === change the text to ask user to click another point ===
  100. txtMsg.setText("Now click the upper right point of the house.")
  101. # === get the upper point from user ===
  102. p2 = win.getMouse()
  103. p2.draw(win)
  104. p1.setFill("brown")
  105. # === call function to draw the house body ===
  106. draw_house_body(win, p1, p2)
  107. # === change the text to ask user to click another point ===
  108. txtMsg.setText("Now click the roof top point of the house.")
  109. # === get the roof top point from user ===
  110. # TODO: get point p3
  111. p3 = win.getMouse()
  112. p3.draw(win)
  113. p3.setFill("brown")
  114. # === call function to draw the roof and front of the house ===
  115. draw_house(win, p1, p2, p3)
  116. # === change the text inside text box ===
  117. txtMsg.setText("Please click a button.")
  118. # === View button ===
  119. button_1 = Rectangle(Point(2,3), Point(6, 5))
  120. button_1.setFill("yellow")
  121. button_1.draw(win)
  122. ViewMsg = Text(Point(4, 4), "Views")
  123. ViewMsg.setStyle("bold")
  124. ViewMsg.setTextColor("black")
  125. ViewMsg.draw(win)
  126. # === Wind buttton ===
  127. # TODO: details of button 2
  128. button_2 = Rectangle(Point(7,3), Point(11, 5))
  129. button_2.setFill("yellow")
  130. button_2.draw(win)
  131. ViewMsg2 = Text(Point(9, 4), "Wind")
  132. ViewMsg2.setStyle("bold")
  133. ViewMsg2.setTextColor("black")
  134. ViewMsg2.draw(win)
  135. # === Speed Button ===
  136. # TODO: details of button 3
  137. button_3 = Rectangle(Point(12,3), Point(16, 5))
  138. button_3.setFill("yellow")
  139. button_3.draw(win)
  140. ViewMsg3 = Text(Point(14, 4), "Speed")
  141. ViewMsg3.setStyle("bold")
  142. ViewMsg3.setTextColor("black")
  143. ViewMsg3.draw(win)
  144. # === Exit Button ===
  145. # TODO: details of button 4
  146. button_4 = Rectangle(Point(17,3), Point(21, 5))
  147. button_4.setFill("grey")
  148. button_4.draw(win)
  149. ViewMsg4 = Text(Point(19, 4), "Exit")
  150. ViewMsg4.setStyle("bold")
  151. ViewMsg4.setTextColor("black")
  152. ViewMsg4.draw(win)
  153. # check if user clicks any of the three buttons
  154. while True:
  155. continue
  156. pClick = win.getMouse()
  157. x = pClick.getX()
  158. y = pClick.getY()
  159. #exit button
  160. if (x>=17 and x<=21) and (y>=3 and y<=5):
  161. break
  162. #view button
  163. #elif (x>=2 and x<=6) and (y>=3 and y<=5):
  164.  
  165. #wind button
  166. #elif (x>=2 and x<=11) and (y>=3 and y<=5):
  167.  
  168. # TODO: Move clouds
  169. #speed button
  170. elif (x>=12 and x<=16) and (y>=3 and y<=5):
  171. for x in range(4):
  172. cloud_list.move(speed)
  173. sleep(0.1)
  174. # TODO: Change speed and speed button text.
  175.  
  176.  
  177. txtMsg.setText("Good Bye!")
  178. win.close()
  179.  
  180.  
  181. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement