Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import math
- import turtle
- def draw_pond(size):
- '''
- Draw the square with size size
- Precondition: Turtle is in center facing right with pen down and color black
- Postcondition: Turtle ends in the top left corner of the pond with pen down and color black
- '''
- turtle.up()
- turtle.right(180)
- turtle.forward(POND_SIZE/2) #Go to the left half way to keep the pond centered
- turtle.right(90)
- turtle.forward(POND_SIZE/2) #Go up half way to keep the pond centered.
- turtle.right(90)
- turtle.down()
- turtle.color('powder blue')
- turtle.begin_fill()
- turtle.forward(size)
- turtle.right(90)
- turtle.forward(size)
- turtle.right(90)
- turtle.forward(size)
- turtle.right(90)
- turtle.forward(size)
- turtle.right(90)
- turtle.end_fill()
- turtle.color('black')
- def go_random(radius, circles):
- '''
- Jump to a random location
- The location will be acceptable so that none of the ripples are outside of the pond area
- '''
- turtle.up()
- 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.
- turtle.down()
- def draw_drop(radius):
- '''
- Draw a single drop with radius radius, a random color, and a random ammount of ripples
- '''
- ripples = random.randint(MIN_RIPPLES, MAX_RIPPLES) #Generate a random number of ripples for the circle
- go_random(radius, ripples) #Jump to a random location
- #Go to the center
- turtle.right(90)
- turtle.up()
- turtle.forward(2 * radius) #First ripple has a radius of 2R
- turtle.down()
- turtle.left(90)
- turtle.color('black',(random.randint(0,255),random.randint(0,255),random.randint(0,255))) #Generate a random color to fill with
- turtle.begin_fill()
- turtle.circle(2 * radius) #First ripple has a radius of 2R
- turtle.end_fill()
- turtle.color('black')
- turtle.left(90)
- turtle.up()
- turtle.forward(2 * radius) #Return to center again
- turtle.right(90)
- turtle.down()
- init_radius = radius #Set initial radius
- radius = radius * 2 #Set radius to the second value so it doesn't draw a circle inside the center
- while ripples > 0:
- #Draw the drops, returns to the center each time
- turtle.right(90)
- turtle.up()
- turtle.forward(radius)
- turtle.left(90)
- turtle.down()
- turtle.circle(radius)
- turtle.left(90)
- turtle.up()
- turtle.forward(radius)
- turtle.right(90)
- turtle.down()
- radius += init_radius #Add the radius
- ripples -= 1 #Subtract a ripple
- def drops_recursive(drops, total):
- '''
- Recursively draw the correct number of rain drops
- '''
- if drops > 0:
- radius = random.randint(MIN_DROP_RADIUS, MAX_DROP_RADIUS) #Generate a radius inbetween the min and max
- draw_drop(radius) #Draw a drop with radius from above
- total = total + (math.pi * (2 * radius)) #Add the circumference to the total
- drops_recursive(drops - 1, total) #Recurse
- else:
- print(total)
- return total
- '''
- Define constants
- '''
- MIN_RIPPLES = 3 #Minimum number of ripples to draw on each drop
- MAX_RIPPLES = 8 #Maximum number of ripples to draw on each drop
- POND_SIZE = 600 #The side length of the "pond area" that everything sits inside
- MIN_DROP_RADIUS = 1 #Minimum colored center of a drop to draw
- MAX_DROP_RADIUS = 20 #Maximum colored center of a drop to draw
- MINIMUM_DROPS = 1 #Minimum number of drops allowed to be drawn
- MAXIMUM_DROPS = 100 #Maximum number of drops allowed to be drawns
- number_of_drops = int(input("Raindrops (1-100): ")) #Ask the user for the number of drops to draw
- if number_of_drops > MAXIMUM_DROPS or number_of_drops < MINIMUM_DROPS: #If the drops are outside of acceptable range
- print("Raindrops must be between", MINIMUM_DROPS, "and", MAXIMUM_DROPS, "inclusive.") #Warn the user
- else:
- turtle.speed(100) #Remove before flight
- turtle.colormode(255) #Allow the turtle tot take RGB values
- draw_pond(POND_SIZE) #Draw the pond
- xmin = math.floor(turtle.xcor()) #Needed to calculate acceptable positions for the drops
- ymin = math.floor(turtle.ycor()) #Needed to calculate acceptable positions for the drops
- print(drops_recursive(number_of_drops, 0)) #Run the actual program
- turtle.done() #Stop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement