How to draw a filled star with Python Turtle
Tags: python, tutorial, turtle, graphics
Published: 2021-05-15 07:48:32

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

A zig-zag can be simple to explain. We just have to move the cursor left then right over and over

```python
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

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5knph26x07gjf63fa9hj.png)

As we have repeating code we want to use a loop to reduce the code

```python
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)
```

Above we can change the value of `pointies = 10` to change the number of zig zags

# Rotate the zig zag

What happens if we rotate the zig zag by a certain value

```python
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

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1upvyah5dnrl30ci9lot.png)

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. 

```python
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)
```

The result:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2h22oyvy4qpudr6fnakm.png)

# 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:

```python
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


![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpqvyig3h0s2ch4oy7t2.png)
 
We can now draw stars with any number of points of any color. Here are some examples:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fca830e9iutxec535uh0.png)
 
 
# 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

```python
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:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dk95731due7ltfm0pmy1.png)