How to draw a triangle, square, pentagon, hexagon, heptagon, octagon, nonagon, and decagon in one go using turtle class of python?
Code:
from turtle import Turtle,Screen
import random
# to take the turtle up on screen so that all the shapes can get enough space
tim=Turtle()
tim.penup()
tim.left(90)
tim.forward(100)
tim.left(90)
tim.forward(150)
tim.right(90)
tim.forward(50)
tim.right(90)
tim.pendown()
# Now turtle is at the right place
# Main code begins now:
colours = ["orange","CornflowerBlue", "DarkOrchid", "IndianRed", "DeepSkyBlue",
"LightSeaGreen", "wheat", "SlateGray", "SeaGreen"]
def draw_shape(num_of_sides,color):
tim.color(color)
angle=360/num_of_sides #will find out the angle according to the given shape
for _ in range(num_of_sides):
tim.forward(100)
tim.right(angle)
for shape_of_side_n in range(3,11):
color=random.choice(colours) #Selects the color randomly for each shape
draw_shape(shape_of_side_n,color) #will draw shapes from triangle to decagon
screen=Screen()
screen.exitonclick()
No comments:
Post a Comment