- How to draw a filled star with Python Turtle
- Start with a zig zag
- Rotate the zig zag
- Color the star
- Rotate the star
- How to Draw a Star in Python (Using Turtle): A Step-by-Step Guide
- 5 Steps to Draw a Star Using Python Turtle
- Full Code
- How to Color the Star?
- Summary
- Turtle Stars
- Star Geometry
- Example 1: One Simple Star
- Example 2: Yellow Star
- Example 3: Dark Blue Night Sky
- Example 4: Multiple Stars
- Example 5: Multi-color Multiple Stars
- Resources
- Summary
How to draw a filled star with Python Turtle
I overdid things a little when trying to teach 6th graders how to draw a star using Python Turtle. But I thought that it was fun anyways so I’m sharing it here. Note that this is also my first dev.to article. I appreciate any suggestion.
Start with a zig zag
import turtle as t angle=150 side=100 angle_left=angle angle_right=angle t.forward(side) t.right(angle_right) t.forward(side) t.left(angle_left) t.forward(side) t.right(angle_right) t.forward(side) t.left(angle_left) t.forward(side) t.right(angle_right) t.forward(side) t.left(angle_left)
Here’s what we get As we have repeating code we want to use a loop to reduce the code
import turtle as t angle=150 side=100 pointies = 10 angle_left=angle angle_right=angle for p in range(pointies): t.forward(side) t.right(angle_right) t.forward(side) t.left(angle_left)
Rotate the zig zag
import turtle as t angle=150 side=100 pointies = 10 ROTATION = 30 angle_left=angle angle_right=angle for p in range(pointies): t.forward(side) t.right(angle_right) t.forward(side) t.left(angle_left) t.right(ROTATION)
All we did above was added this statement t.right(ROTATION) . We also said ROTATION = 30 and here’s what we get But wouldn’t it be nice if the ends were connected? To achieve this we just need to set rotation to ROTATION = 360/pointies . Remember that 360 degrees is a full rotation.
import turtle as t angle=150 side=100 pointies = 10 ROTATION = 360/pointies angle_left=angle angle_right=angle for p in range(pointies): t.forward(side) t.right(angle_right) t.forward(side) t.left(angle_left) t.right(ROTATION)
Color the star
To color the star we first set the color using t.color(«red»,»red») . We then use the begin_fill and end_fill commands to start and stop filling the area. Here’s it is:
import turtle as t t.speed(0) angle=150 side=100 pointies = 10 ROTATION = 360/pointies angle_left=angle angle_right=angle t.color("black","red") t.begin_fill() for p in range(pointies): t.forward(side) t.right(angle_right) t.forward(side) t.left(angle_left) t.right(ROTATION) t.end_fill()
In the code above we have also added a statement t.speed(0) to speed things up We can now draw stars with any number of points of any color. Here are some examples:
Rotate the star
Why stop at drawing a star when we can rotate it! To do this we use a nested for-loop. This means a loop inside a loop
import turtle as t t.speed(0) angle=150 side=100 pointies = 5 ROTATION = 360/pointies angle_left=angle angle_right=angle t.color("red","black") for r in range(10): t.begin_fill() for p in range(pointies): t.forward(side) t.right(angle_right) t.forward(side) t.left(angle_left) t.right(ROTATION) t.end_fill() t.right(2)
In the code above we added an extra loop that runs 10 times — for r in range(10): . We have also added a t.right(2) to the end of the loop to make the turtle rotate slightly everytime the inner loop completes Check it out:
How to Draw a Star in Python (Using Turtle): A Step-by-Step Guide
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 turtle library in your Python code. This will give you access to the functions and methods you need to create turtle graphics.
- Use a for loop 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:
Full Code
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()
Summary
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!
Turtle Stars
Drawing with turtle library of Python can be very educational and fun.
In this tutorial we will focus on how to draw stars with turtle in Python.
We’re going to show you how to draw monocolor stars such as yellow stars as well as how to draw multicolor stars.
Then we will attempt to scatter a number of stars on a dark blue night sky using turtle drawing and Python. Let’s see.
Star Geometry
This makes everything so much easier as we love repetition in coding. Only 5 iterations with same turn angles will do.
Example 1: One Simple Star
After opening an image you can use the .resize() method on it to create a new image with a new size.
Please Note: Adding turtle.exitonclick() can help terminate turtle window once you’re done with your turtle drawing or observations. If you don’t add this little code piece, turtle tends to freeze which requires a kernel restart which can be annoying. So we will add it to the end of each code and you’re also advised to do so.
import turtle for i in range(5): turtle.forward(150) turtle.right(144) turtle.exitonclick()
Here is turtle in action. We figured out a single star. Yay!
It’s downhill from this point. Now we can add some spice. Maybe some colors. And definitely some more stars so they don’t feel lonely 😉
Example 2: Yellow Star
We can simply use .color method to give turtle a color: turtle.color(«yellow»)
import turtle turtle.color("yellow") for i in range(5): turtle.forward(150) turtle.right(144) turtle.exitonclick()
Great now our star has some color even. We can also transform the background to a color like dark blue which will make the star’s yellow pop better.
Example 3: Dark Blue Night Sky
We can use .bgcolor method to change the background color of a turtle drawing window: turtle.bgcolor(«darkblue»)
import turtle turtle.color("yellow") turtle.bgcolor("darkblue") for i in range(5): turtle.forward(150) turtle.right(144) turtle.exitonclick()
Perfect. On your computer it might look even better actually. On the website it’s being scaled up which makes the resolution appear worse than it is.
Now, let’s find a cure for loneliness.
Example 4: Multiple Stars
turtle’s setpos() or goto() methods can be used to make the cursor travel between different coordinates and this way we can draw a different star at each coordinate. We will have to adjust a few things:
- Let’s turn down the star size quite a bit. This can be easily adjusted by passing a smaller value to .forward() command which draws each side of the star.
- We can implement a random.randint() structure which tells a random x,y coordinate on our pseudosky.
- Also, let’s speed up the turtle so we don’t have to wait too long for each star to be drawn. This can be achieved with turtle.speed(0)
- 0 : fastest setting while
- 10 : slowest setting
Let’s try. Here is the Python code.
import turtle turtle.color("yellow") turtle.bgcolor("darkblue") turtle.speed(0) turtle.hideturtle() def starmaker(step,angle): for iter in range(5): turtle.forward(step) turtle.right(angle) for i in range(10): turtle.penup() turtle.setpos(random.randint(-350, 350), random.randint(100, 300)) turtle.pendown() starmaker(10,144) turtle.exitonclick()
Wow! Now we’re talking. Our turtle drawing is starting to look legit. It might even compete with Van Gogh’s Starry Night, what do you think? Alright, I think that’s an exaggeration too too. But it’s still pretty good for turtle digital art category.
Where can we go from here? Well, rather than a solid yellow, stars kind of flicker . Maybe we can draw stars with multiple colors just for fun?
Example 5: Multi-color Multiple Stars
So, how can we make each side of the star a different random color? Pretty easy actually.
By placing turtle.color() attribute inside the turtle making function’s for loop, we can make sure turle’s color is redefined at each iteration.
We can also create a list of predefined colors. Then using random library we can pick a random color by randomly accessing the indexes of this color list. Remember that list index starts at 0 in Python so random.randint() should go to list size-1 at maximum.
import turtle turtle.bgcolor("darkblue") turtle.speed(0) turtle.hideturtle() color_lst=["red", "orange", "cyan", "yellow", "gray", "lightgreen"] def starmaker(step,angle): for iter in range(5): turtle.color(color_lst[random.randint(0,5)]) turtle.forward(step) turtle.right(angle) for i in range(15): turtle.penup() turtle.setpos(random.randint(-350, 350), random.randint(100, 300)) turtle.pendown() starmaker(10,144) turtle.exitonclick()
Here we go we started with a single black & white star and discovered all the way up to multiple little stars with multiple colors on a dark blue night sky.
Resources
You can check out other turtle tutorials we have for more examples and turtle knowledge.
Summary
In this Python Tutorial, we have learned how to draw stars with turtle.
We have also advanced the simple idea of drawing a star and played with it a litte. We ended up drawing multiple good looking stars on a night sky.
This tutorial can be a great practice to demonstrate basic Python concepts. Feel free to freely use and share at your school or work, attributions or source credit are very welcome.
Thank you for your support!