Advertisement
uottawamakermobile

Graphics Draw Python (entire code)

Apr 8th, 2020
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.94 KB | None | 0 0
  1. # Create a scene with a house, a tree, and a sun
  2. # Based on the picture at http://dragonometry.net/blog/?p=566
  3.  
  4. import turtle
  5. import math
  6.  
  7. # Set the background color
  8. screen = turtle.Screen()
  9. screen.bgcolor("skyblue")
  10.  
  11. # Create our turtle
  12. george = turtle.Turtle()
  13. george.color("black")
  14. george.shape("turtle")
  15. george.speed(10)
  16.  
  17. # Define a funtion to draw and fill a rectangle with the given
  18. # dimensions and color
  19. def drawRectangle(t, width, height, color):
  20.   t.fillcolor(color)
  21.   t.begin_fill()
  22.   t.forward(width)
  23.   t.left(90)
  24.   t.forward(height)
  25.   t.left(90)
  26.   t.forward(width)
  27.   t.left(90)
  28.   t.forward(height)
  29.   t.left(90)
  30.   t.end_fill()
  31.  
  32. # Define a function to draw and fill an equalateral right
  33. # triangle with the given hypotenuse length and color.  This
  34. # is used to create a roof shape.
  35. def drawTriangle(t, length, color):
  36.   t.fillcolor(color)
  37.   t.begin_fill()
  38.   t.forward(length)
  39.   t.left(135)
  40.   t.forward(length / math.sqrt(2))
  41.   t.left(90)
  42.   t.forward(length / math.sqrt(2))
  43.   t.left(135)
  44.   t.end_fill()
  45.  
  46. # Define a function to draw and fill a parallelogram, used to
  47. # draw the side of the house
  48. def drawParallelogram(t, width, height, color):
  49.   t.fillcolor(color)
  50.   t.begin_fill()
  51.   t.left(30)
  52.   t.forward(width)
  53.   t.left(60)
  54.   t.forward(height)
  55.   t.left(120)
  56.   t.forward(width)
  57.   t.left(60)
  58.   t.forward(height)
  59.   t.left(90)
  60.   t.end_fill()
  61.  
  62. # Define a function to draw four sun rays of the given length,
  63. # for the sun of the given radius.  The turtle starts in the
  64. # center of the circle.
  65. def drawFourRays(t, length, radius):
  66.   for i in range(4):
  67.     t.penup()
  68.     t.forward(radius)
  69.     t.pendown()
  70.     t.forward(length)
  71.     t.penup()
  72.     t.backward(length + radius)
  73.     t.left(90)
  74.  
  75. # Draw and fill the front of the house
  76. george.penup()
  77. george.goto(-150, -120)
  78. george.pendown()
  79. drawRectangle(george, 100, 110, "blue")
  80.  
  81. # Draw and fill the front door
  82. george.penup()
  83. george.goto(-120, -120)
  84. george.pendown()
  85. drawRectangle(george, 40, 60, "lightgreen")
  86.  
  87. # Front roof
  88. george.penup()
  89. george.goto(-150, -10)
  90. george.pendown()
  91. drawTriangle(george, 100, "magenta")
  92.  
  93. # Side of the house
  94. george.penup()
  95. george.goto(-50, -120)
  96. george.pendown()
  97. drawParallelogram(george, 60, 110, "yellow")
  98.  
  99. # Window
  100. george.penup()
  101. george.goto(-30, -60)
  102. george.pendown()
  103. drawParallelogram(george, 20, 30, "brown")
  104.  
  105. # Side roof
  106. george.penup()
  107. george.goto(-50, -10)
  108. george.pendown()
  109. george.fillcolor("orange")
  110. george.begin_fill()
  111. george.left(30)
  112. george.forward(60)
  113. george.left(105)
  114. george.forward(100 / math.sqrt(2))
  115. george.left(75)
  116. george.forward(60)
  117. george.left(105)
  118. george.forward(100 / math.sqrt(2))
  119. george.left(45)
  120. george.end_fill()
  121.  
  122. # Tree base
  123. george.penup()
  124. george.goto(100, -130)
  125. george.pendown()
  126. drawRectangle(george, 20, 40, "brown")
  127.  
  128. # Tree top
  129. george.penup()
  130. george.goto(65, -90)
  131. george.pendown()
  132. drawTriangle(george, 90, "lightgreen")
  133. george.penup()
  134. george.goto(70, -45)
  135. george.pendown()
  136. drawTriangle(george, 80, "lightgreen")
  137. george.penup()
  138. george.goto(75, -5)
  139. george.pendown()
  140. drawTriangle(george, 70, "lightgreen")
  141.  
  142. # Sun
  143. george.penup()
  144. george.goto(55, 110)
  145. george.fillcolor("yellow")
  146. george.pendown()
  147. george.begin_fill()
  148. george.circle(24)
  149. george.end_fill()
  150.  
  151. # Sun rays
  152. george.penup()
  153. george.goto(55, 134)
  154. george.pendown()
  155. drawFourRays(george, 25, 24)
  156. george.right(45)
  157. drawFourRays(george, 15, 24)
  158. george.left(45)
  159.  
  160. # Put down some labels
  161. george.color("black")
  162. george.penup()
  163. george.goto(-150, 70)
  164. george.pendown()
  165. george.write("House", True, align="right", font=("Arial",12, "bold"))
  166. george.penup()
  167. george.goto(150, -150)
  168. george.pendown()
  169. george.write("Tree", True, align="right", font=("Arial",12, "bold"))
  170. george.penup()
  171. george.goto(130, 150)
  172. george.pendown()
  173. george.write("Sun", True, align="right", font=("Arial",12, "bold"))
  174.  
  175. # Bring the turtle down to the front door, and we're done!
  176. george.penup()
  177. george.goto(-100, -150)
  178. george.left(90)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement