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

Turtle graphics is a simple and fun way to create drawings with Python code. It uses a virtual turtle that you can command to move around the screen and draw lines, shapes, and more. This makes it an excellent tool for learning basic programming concepts and exploring visual outputs with code. We will use Turtle to draw randomly generated stars, demonstrating how to control the turtle’s movement, color, and shape properties programmatically.

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()

Understanding the star Function

The star(color, side_length, x, y) function is responsible for drawing a single star at a specified position with a given color and size. Let’s examine its steps:

  • turtle.color(color): Sets the fill color and outline color of the turtle to the provided color argument.
  • turtle.begin_fill() and turtle.end_fill(): These commands enclose the drawing of the star. Shapes drawn between begin_fill() and end_fill() will be filled with the currently set fill color.
  • turtle.penup() and turtle.goto(x, y) and turtle.pendown(): These commands control the turtle’s movement without drawing. turtle.penup() lifts the pen up, preventing drawing while moving. turtle.goto(x, y) moves the turtle to the specified coordinates (x, y). turtle.pendown() puts the pen down, so drawing resumes. This sequence positions the turtle at the starting point for drawing the star without drawing a line to that point.
  • for i in range(5): ... loop: This loop iterates five times to draw the five points of the star. Inside the loop:
    • turtle.forward(side_length): Moves the turtle forward by the specified side_length, drawing a line.
    • turtle.right(144): Turns the turtle 144 degrees to the right. The 144-degree angle is crucial for creating the five-pointed star shape.
    • turtle.forward(side_length): Moves forward again by side_length, drawing another side of the star.
See also  How to Create a Wheel in Python Turtle (with Images and Code)

Understanding Randomness Functions (random_color, length, xcoord, ycoord)

The script uses several functions to introduce randomness to the star drawing process:

  • random_color(): This function selects a random color from a predefined list L = ['black', 'purple', 'brown', 'green', 'violet', 'orange']. random.choice(L) could also be used for simpler color selection from a list.
  • length(): This function generates a random side length for the star, between 35 and 200 pixels, using random.randint(35, 200). This makes each star a different size.
  • xcoord() and ycoord(): These functions generate random x and y coordinates, respectively, to position each star randomly on the screen. xcoord() generates x-coordinates between -150 and 151, and ycoord() generates y-coordinates between -250 and 251. These ranges define the area within which the stars will be drawn.
See also  How to generate random matrix in Numpy?

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