Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. import random
  2. import math
  3. import turtle
  4.  
  5. def draw_pond(size):
  6. '''
  7. Draw the square with size size
  8. Precondition: Turtle is in center facing right with pen down and color black
  9. Postcondition: Turtle ends in the top left corner of the pond with pen down and color black
  10. '''
  11. turtle.up()
  12. turtle.right(180)
  13. turtle.forward(POND_SIZE/2) #Go to the left half way to keep the pond centered
  14. turtle.right(90)
  15. turtle.forward(POND_SIZE/2) #Go up half way to keep the pond centered.
  16. turtle.right(90)
  17. turtle.down()
  18. turtle.color('powder blue')
  19. turtle.begin_fill()
  20. turtle.forward(size)
  21. turtle.right(90)
  22. turtle.forward(size)
  23. turtle.right(90)
  24. turtle.forward(size)
  25. turtle.right(90)
  26. turtle.forward(size)
  27. turtle.right(90)
  28. turtle.end_fill()
  29. turtle.color('black')
  30.  
  31. def go_random(radius, circles):
  32. '''
  33. Jump to a random location
  34. The location will be acceptable so that none of the ripples are outside of the pond area
  35. '''
  36. turtle.up()
  37. turtle.setpos(random.randint(xmin+(radius * (circles+1)), (xmin+POND_SIZE) - (radius * (circles+1))), random.randint((ymin-POND_SIZE) + (radius * (circles+1)), ymin-(radius * (circles+1)))) #Generate a random position for the turtle to go to that keeps the ripples inside the pond area and jump to it.
  38. turtle.down()
  39.  
  40. def draw_drop(radius):
  41. '''
  42. Draw a single drop with radius radius, a random color, and a random ammount of ripples
  43. '''
  44. ripples = random.randint(MIN_RIPPLES, MAX_RIPPLES) #Generate a random number of ripples for the circle
  45. go_random(radius, ripples) #Jump to a random location
  46. #Go to the center
  47. turtle.right(90)
  48. turtle.up()
  49. turtle.forward(2 * radius) #First ripple has a radius of 2R
  50. turtle.down()
  51. turtle.left(90)
  52. turtle.color('black',(random.randint(0,255),random.randint(0,255),random.randint(0,255))) #Generate a random color to fill with
  53. turtle.begin_fill()
  54. turtle.circle(2 * radius) #First ripple has a radius of 2R
  55. turtle.end_fill()
  56. turtle.color('black')
  57. turtle.left(90)
  58. turtle.up()
  59. turtle.forward(2 * radius) #Return to center again
  60. turtle.right(90)
  61. turtle.down()
  62. init_radius = radius #Set initial radius
  63. radius = radius * 2 #Set radius to the second value so it doesn't draw a circle inside the center
  64. while ripples > 0:
  65. #Draw the drops, returns to the center each time
  66. turtle.right(90)
  67. turtle.up()
  68. turtle.forward(radius)
  69. turtle.left(90)
  70. turtle.down()
  71. turtle.circle(radius)
  72. turtle.left(90)
  73. turtle.up()
  74. turtle.forward(radius)
  75. turtle.right(90)
  76. turtle.down()
  77. radius += init_radius #Add the radius
  78. ripples -= 1 #Subtract a ripple
  79.  
  80. def drops_recursive(drops, total):
  81. '''
  82. Recursively draw the correct number of rain drops
  83. '''
  84. if drops > 0:
  85. radius = random.randint(MIN_DROP_RADIUS, MAX_DROP_RADIUS) #Generate a radius inbetween the min and max
  86. draw_drop(radius) #Draw a drop with radius from above
  87. total = total + (math.pi * (2 * radius)) #Add the circumference to the total
  88. drops_recursive(drops - 1, total) #Recurse
  89. else:
  90. print(total)
  91. return total
  92. '''
  93. Define constants
  94. '''
  95. MIN_RIPPLES = 3 #Minimum number of ripples to draw on each drop
  96. MAX_RIPPLES = 8 #Maximum number of ripples to draw on each drop
  97. POND_SIZE = 600 #The side length of the "pond area" that everything sits inside
  98. MIN_DROP_RADIUS = 1 #Minimum colored center of a drop to draw
  99. MAX_DROP_RADIUS = 20 #Maximum colored center of a drop to draw
  100. MINIMUM_DROPS = 1 #Minimum number of drops allowed to be drawn
  101. MAXIMUM_DROPS = 100 #Maximum number of drops allowed to be drawns
  102.  
  103. number_of_drops = int(input("Raindrops (1-100): ")) #Ask the user for the number of drops to draw
  104. if number_of_drops > MAXIMUM_DROPS or number_of_drops < MINIMUM_DROPS: #If the drops are outside of acceptable range
  105. print("Raindrops must be between", MINIMUM_DROPS, "and", MAXIMUM_DROPS, "inclusive.") #Warn the user
  106. else:
  107. turtle.speed(100) #Remove before flight
  108. turtle.colormode(255) #Allow the turtle tot take RGB values
  109. draw_pond(POND_SIZE) #Draw the pond
  110. xmin = math.floor(turtle.xcor()) #Needed to calculate acceptable positions for the drops
  111. ymin = math.floor(turtle.ycor()) #Needed to calculate acceptable positions for the drops
  112. print(drops_recursive(number_of_drops, 0)) #Run the actual program
  113. turtle.done() #Stop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement