DEV Community

Cover image for Draw stars with Python Turtle
petercour
petercour

Posted on

Draw stars with Python Turtle

Turtle module lets you draw. Its a module for kids, that lets a turtle move and leave a trail.

Its like a piece of paper, in which you can move a pen. By default the pen is on the paper.

Lets draw stars, the program below has 5 sides.

#!/usr/bin/python3
import turtle

def main():
    count = 1
    while count <= 5:
        turtle.forward(300)
        turtle.right(144)
        count = count + 1
        print(count)
    turtle.exitonclick()

if __name__ == "__main__":
    main()

Then

So that's a star with 5 sides. What about 6 sides? It can be made with 2 triangles. Because they are not connected triangles, the pen needs to be moved up and down.

#!/usr/bin/python3
import turtle

def main():
    count = 1
    while count <= 3:
        turtle.forward(300)
        turtle.right(120)
        count = count + 1
        print(count)

    turtle.penup()
    turtle.goto( turtle.pos() + (0,-160) )
    turtle.pendown()

    count = 1
    while count <= 3:
        turtle.forward(300)
        turtle.right(-120)
        count = count + 1
        print(count)

    turtle.exitonclick()

if __name__ == "__main__":
    main()

Then:

That's how you can draw stars with turtle. If you want a challenge, try to draw it with a different amount of lines.

Python resources:

Top comments (0)