Editor

Moon and Stars in Python Turtle

Intermediate · Nature · 69 lines · draws in about 10 seconds

This program paints a night sky with a crescent moon drawn as two arcs of the same circle size, a soft glow and gold stars.

Moon and Stars in Python Turtle: the finished drawing
import random
import turtle
random.seed(12)
SKY = "#0b1a3a"
MOON = "#fff2b3"
screen = turtle.Screen()
screen.title("Moon and Stars")
screen.bgcolor(SKY)
t = turtle.Turtle()
def star(x, y, radius, color):
"""A filled five-pointed star centred at (x, y)."""
t.penup()
t.goto(x, y + radius)
t.setheading(288)
t.pendown()
t.color(color)
t.begin_fill()
for point in range(5):
t.forward(radius * 0.7265)
t.left(72)
t.forward(radius * 0.7265)
t.right(144)
t.end_fill()
# Tiny far-away stars. Speed 0 puts them all down at once, instead of
# making the turtle walk across the sky to each one.
t.speed(0)
t.penup()
for twinkle in range(90):
t.goto(random.randint(-300, 300), random.randint(-215, 215))
t.dot(random.choice([2, 3, 4]), "white")
# A soft glow round the moon: big dots, each a little lighter
t.speed(7)
t.goto(-60, 10) # the middle of the moon's circle
for size, color in [(420, "#0f2147"), (370, "#142a55"), (330, "#1a3363")]:
t.dot(size, color)
# The crescent: two arcs of the same circle size that meet at the tips.
# Start at the top tip, 75 degrees round from the right-hand side.
radius = 150
t.setheading(75)
t.forward(radius)
t.left(90)
t.pendown()
t.color(MOON)
t.begin_fill()
t.circle(radius, 210) # the outside edge, round the left
t.left(150)
t.circle(-radius, 150) # the inside edge, curving back up
t.end_fill()
# Bright stars, one sitting between the tips of the moon
star(70, 40, 30, "gold")
star(200, 160, 18, "gold")
star(230, -60, 22, "gold")
star(120, -150, 14, "gold")
star(-230, 180, 12, "gold")
star(20, 185, 10, "gold")
t.hideturtle()
turtle.done()

How the code works

Stars from an outline

The turtle starts at the top point and turns left 72 degrees at each inner corner and right 144 degrees at each tip. Tracing the outline lets begin_fill() fill the whole star.

def star(x, y, radius, color):
    """A filled five-pointed star centred at (x, y)."""
    t.penup()
    t.goto(x, y + radius)
    t.setheading(288)
    t.pendown()
    t.color(color)
    t.begin_fill()
    for point in range(5):
        t.forward(radius * 0.7265)
        t.left(72)
        t.forward(radius * 0.7265)
        t.right(144)
    t.end_fill()

Tiny stars at random

random.seed(12) makes the random places the same on every run. t.speed(0) before the loop puts all 90 dots down at once.

for twinkle in range(90):
    t.goto(random.randint(-300, 300), random.randint(-215, 215))
    t.dot(random.choice([2, 3, 4]), "white")

A crescent from two arcs

From the middle of the moon's circle the turtle walks out to the top tip and turns to follow the edge. It draws 210 degrees of the circle round the left, turns left 150 degrees at the bottom tip, and draws 150 degrees of a circle of the same size the other way, back up to the top tip.

radius = 150
t.setheading(75)
t.forward(radius)
t.left(90)
t.pendown()
t.color(MOON)
t.begin_fill()
t.circle(radius, 210)    # the outside edge, round the left
t.left(150)
t.circle(-radius, 150)   # the inside edge, curving back up
t.end_fill()

Make it yours

Change the code above and press Run. Some ideas:

  • For a fatter moon, change 75 to 60, 210 to 240, and both 150s in the crescent to 120.
  • Add another bright star with a line such as star(-200, -150, 16, "gold").
  • Change range(90) to range(200) for a sky full of tiny stars.

What it uses

Every turtle command the site supports is listed on Python turtle online.