SHARE
TWEET

Centered colour rectangle

chrisby12 Dec 5th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import turtle
  2.  
  3. rectangle = turtle.Turtle()
  4.  
  5. def rectangle_area(width, height):  # function to get the Area
  6.     return width * height
  7.  
  8.  
  9. def rectangle_perimeter(height, width):  # function to get the perimeter of the rectangle
  10.     return (height + width) * 2
  11.  
  12.  
  13. def draw_rectangle(rectangle, width, height, color):  # function to draw a rectangle
  14.     rectangle.fillcolor()
  15.     rectangle.penup()                                 # function to position start point
  16.     rectangle.goto(-300, -20)
  17.     rectangle.pendown()
  18.  
  19.     for i in range(2):  # loops twice i.e. top, bottom, left, right.
  20.         rectangle.begin_fill()
  21.         rectangle.color(color)
  22.         rectangle.forward(width)
  23.         rectangle.left(90)  # 90 Degrees
  24.         rectangle.forward(height)
  25.         rectangle.left(90)  # 90 Degrees
  26.         rectangle.end_fill()
  27.  
  28.     rectangle.color("brown")
  29.     rectangle.penup()
  30.     rectangle.goto(-30, 80)
  31.     rectangle.write("Perimeter: " + str(rectangle_perimeter(height, width)), align="center")
  32.     rectangle.penup()
  33.     rectangle.goto(10, -20)
  34.  
  35.     rectangle.color("black")
  36.     rectangle.goto(-30, 70)
  37.     rectangle.write("Area: " + str(rectangle_area(width, height)), align="center")
  38.     rectangle.goto(10, -20)
  39.     rectangle.penup()
  40.     rectangle.setx(-300)
  41.     rectangle.sety(-50)
  42.     rectangle.pendown()
  43.     rectangle.hideturtle()
  44.  
  45.  
  46. draw_rectangle(rectangle, 550, 200, "green")  # Call the function and pass in values
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top