Draw random stars using turtle

Enjoy creating random objects with Python. Now, let’s have some fun drawing stars in various positions and colors.

python turtle draw stars

Draw three stars

from turtle import *
import random
import turtle

def star(color, side_length, x, y):
    turtle.color(color)
    turtle.begin_fill()
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    for i in range(5):
        turtle.forward(side_length)
        turtle.right(144)
        turtle.forward(side_length)
    turtle.end_fill()


def random_color():
    colvar=random.randint(0,5)
    L=['black','purple','brown','green','violet','orange'] 
    result=L[colvar]
    return result
    
def length():
    z1=random.randint(35,200)
    return z1


def xcoord(): 
    z2=random.randint(-150, 151)
    return z2


def ycoord():
    z3=random.randint(-250, 251)
    return z3


def drawastar():
    num=3
    for i in range(num):
        color=random_color()
        side_length=length()
        x=xcoord()
        y=ycoord()
        star(color, side_length, x, y)
        
drawastar()

Using Turtle, you can also create a variety of shapes including triangles, squares, rectangles, and more.

See also  How to create histogram in Matplotlib and Numpy the easiest way?