Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #imports modules from libraries
- from turtle import *
- from random import randint
- import random
- bob = Turtle() #Creates a turtle
- col = ["Red", "Green", "Blue", "Pink"] #Creates a list and assigns values
- def area(l, h): #defines a function which calculates the area of a triangle
- a = l * h #multiples the length by height and stores in a variable
- return a #returns the area
- def perimeter(l, h):
- p = (l + h)* 2
- return p
- def randomplace(): #defines a function called randomplace
- x = randint(-200, 200) #creates a variable and assigns a random integer
- y = randint(-200, 200) #creates a variable and assigns a random integer
- bob.goto(x,y)
- def center_rectangle(l, h): #defines a function that returns the
- bob.right(90)
- bob.forward(l/2) #moves the turtle forward half the length of the rectangle
- bob.right(90)
- bob.forward(h/2) #moved the turtle forward half the width of the rectangle
- def draw_rect(l, h, cols): #defines a function and passes in three parameters
- hideturtle() #hides the turtle from view
- bob.penup() #the pen is not currently drawing
- randomplace() #calls the randomplace function to select random coordinates
- bob.pendown()
- bob.fillcolor(cols) #selects random colour from cols list
- bob.begin_fill() #fills in shape with selected colour
- bob.forward(l) #Moves the turtle forward a random number which has been passed in
- bob.right(90) #turns the turtle a 90 degrees angle is it is pointing the correct way
- bob.forward(h) #moves the turtle forward a random number which has been passed in
- bob.right(90)
- bob.forward(l)
- bob.right(90)
- bob.forward(h)
- bob.end_fill() #end the fill colour
- bob.penup()
- center_rectangle(l, h) #calls a fuction which will return the turtle to the center of the rectangle
- bob.pendown()
- bob.color("black") #wrties text in black
- bob.write("Area: " + str(area(l, h)) + "\nPerimeter:" + str(perimeter(l, h))) #prints the area and perimeter
- bob.penup()
- for i in range(5): #for loop which repeats 10 times
- draw_rect(random.randint(-100,100), random.randint(-100, 100), random.choice(col))
- #calls the draw_rect function and passess in three parameters
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement