DEV Community

Cover image for How to draw a filled star with Python Turtle
taarimalta
taarimalta

Posted on • Edited on

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

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

 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) 
Enter fullscreen mode Exit fullscreen mode

Here's what we get

image

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) 
Enter fullscreen mode Exit fullscreen mode

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

 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) 
Enter fullscreen mode Exit fullscreen mode

All we did above was added this statement t.right(ROTATION). We also said ROTATION = 30

and here's what we get

image

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) 
Enter fullscreen mode Exit fullscreen mode

The result:

image

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() 
Enter fullscreen mode Exit fullscreen mode

In the code above we have also added a statement t.speed(0) to speed things up

image

We can now draw stars with any number of points of any color. Here are some examples:

image

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) 
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)