Turtle graphics, draw a star?

ASm picture ASm · Oct 14, 2014 · Viewed 71.3k times · Source

I want to draw a filled-in star, such as:

http://www.seaviewstickers.co.uk/shop/media/catalog/product/cache/1/thumbnail/600x600/9df78eab33525d08d6e5fb8d27136e95/b/l/black_11.jpg

I have this code so far:

def draw_star(size,color):
    count = 0
    angle = 144
    while count <= 5:
        turtle.forward(size)
        turtle.right(angle)
        count += 1
    return

draw_star(100,"purple")

I want to fill in the star with whatever color the function is passed. How can I do this?

Answer

John La Rooy picture John La Rooy · Oct 14, 2014

To get a 5 pointed star, you should draw 2 lines for each side. The angles need to add to 72 (360/5)

import turtle
def draw_star(size, color):
    angle = 120
    turtle.fillcolor(color)
    turtle.begin_fill()

    for side in range(5):
        turtle.forward(size)
        turtle.right(angle)
        turtle.forward(size)
        turtle.right(72 - angle)
    turtle.end_fill()
    return

draw_star(100, "purple")

Experiment with different values of angle to get the shape you want