The Turtle module in Python is a library that allows users to create simple graphics programs using a turtle. This guide teaches you how to use Turtle to draw a star with 5 edges as well as 10 edges.
In Python, Turtle is an object that can be moved around the screen, and it can be given commands to draw lines, change its color, and more. By moving the Turtle object in a specific way with a specific angle, you can generate a star-like shape.
Let’s jump into it!
5 Steps to Draw a Star Using Python Turtle
To draw a star with Python Turtle, you can use the following steps:
- Import the
turtlelibrary in your Python code. This will give you access to the functions and methods you need to create turtle graphics.
- Create a new
Turtleobject. This will be the turtle that you use to draw the star.
t = turtle.Turtle()
- Use a
forloop to repeat a sequence of steps that will draw the star. In each iteration of the loop, use the
t.forward()method to move the turtle forward by a specified distance, and the
t.right()method to turn the turtle to the right by a certain number of degrees.
for i in range(5): t.forward(100) t.right(144)
This code draws a line 100 pixels forward and then turns 144 degrees towards the left. After doing this five times, you end up turning the turtle around by 5 * 144 = 720 degrees to stop it at the same place it started forming a star shape.
If you run this code, you should see a star with 5 edges:
- To make the star have more edges, tweak the angle that the turtle turns after each forward movement. Instead of turning by 144 degrees, turn 108 degrees. This makes the turtle’s turns take place at less steep angles which means it will complete the star shape with more strokes. If you’re using 108 degrees as the angle of your star, it requires 1080 degrees of rotation to get the Turtle back to its starting position.
for i in range(10): t.forward(100) t.right(108)
Running this piece of code gives you a star that looks like this:
Here’s the complete code to draw a star with 5 edges in Python Turtle:
import turtle t = turtle.Turtle() for i in range(5): t.forward(100) t.right(144 - 36)
And here’s the code to draw a star with 10 edges:
import turtle t = turtle.Turtle() for i in range(10): t.forward(100) t.right(108)
How to Color the Star?
If you want to add color to the star in Turtle, you can use the fillcolor() method to shade the area restricted by the Turtle strokes.
Here’s what it looks like in the code:
import turtle my_turtle = turtle.Turtle() # Set the turtle's pen color my_turtle.pencolor("black") # Set the star's fill color my_turtle.fillcolor("yellow") # Begin filling the star my_turtle.begin_fill() # Draw the star for i in range(5): my_turtle.forward(100) my_turtle.left(144) # End filling the star my_turtle.end_fill() # Keep the turtle window open turtle.done()
Notice how the edge that points toward the top left looks out of place because there’s no stroke that would strike through the edge similar to the other edges. To overcome this issue, repeat the first stroke by adding one more iteration to the for loop:
import turtle my_turtle = turtle.Turtle() # Set the turtle's pen color my_turtle.pencolor("black") # Set the star's fill color my_turtle.fillcolor("yellow") # Begin filling the star my_turtle.begin_fill() # Draw the star for i in range(6): my_turtle.forward(100) my_turtle.left(144) # End filling the star my_turtle.end_fill() # Keep the turtle window open turtle.done()
Today you learned how to use Turtle to draw a star in Python.
To recap, Python Turtle is a module that allows users to create simple graphics using turtle graphics in the Python programming language. It is commonly used to introduce beginners to the basics of programming and creating simple shapes and images.
The Turtle can be moved and instructed to draw lines, shapes, and other designs, making it a useful tool for creating interactive and visually appealing programs.
One great example is drawing a star with it which is what you learned today.
Thanks for reading. Happy coding!
About the Author
- I'm an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.
- Artificial Intelligence2023.01.10How to Use AI Art Generators—Input Design (Prompt Engineering)
- Favorites2022.11.30ChatGPT—What Is It? How to Use It? (Examples, Tips & More)