crzcas

Turtle rectangle with return

Jan 7th, 2021
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. # assignment lesson 1.9
  2.  
  3. import turtle
  4. turtle.setup(500,500)
  5. board = turtle.Turtle()
  6.  
  7. def area_rectangle(width, height):
  8.     return width*height
  9.  
  10. def perimeter_rectangle(width, height):
  11.     return (2*width)+(2*height)
  12.  
  13. # draws a rectangle given top left position of a rectangle
  14. def draw_filled_rectangle(board,x,y,width,height,size,color,fill):
  15.     board.fillcolor(fill)
  16.     board.pencolor(color)
  17.     board.pensize(size)
  18.     board.setheading(0)
  19.  
  20.     board.begin_fill()
  21.     board.up()
  22.     board.goto(x,y)
  23.     board.down()
  24.     # draw top
  25.     board.forward(width)
  26.     # draw right
  27.     board.right(90)
  28.     board.forward(height)
  29.     # draw bottom
  30.     board.right(90)
  31.     board.forward(width)
  32.     # draw left
  33.     board.right(90)
  34.     board.forward(height)
  35.     board.end_fill()
  36.     board.penup()
  37.     board.goto(-80, 0)
  38.     board.pendown()
  39.     board.write("perimeter: " + str(perimeter_rectangle(width, height)) + "        area: " + str(area_rectangle(width, height)), align="left")
  40.     #board.write("area: " + str(area_rectangle(width, height)) + ";       perimeter: " + str(perimeter_rectangle(width, height)), align="left")    
  41.    
  42. # The center of the canvas in turtle is 0,0
  43. # Then the position the rectangle in the center
  44. # Note: We need to pass the top left co-ordinates of rectangle
  45. # The rectangle has 200 pixels in width and 100 pixels in height
  46. # Also specifies the rectangle color and the fill color
  47. draw_filled_rectangle(board,-100,50,200,100,5,"blue","lightgreen")
  48. turtle.done()
Advertisement
Add Comment
Please, Sign In to add comment