Turtle drawingBeginner 56 lines

    Draw a Christmas Tree with Python Turtle (Full Code)

    This little program draws a whole Christmas tree with Python's turtle: the turtle moves like a pen on paper, drawing three overlapping green triangles, a brown trunk, six coloured ornaments, a gold five-pointed star on top and a "Merry Christmas!" message underneath.

    It only uses a handful of turtle commands (forward, left, right, goto, begin_fill and end_fill, dot and write), so it is a great first graphics project. Draw it right here in your browser, then change the colours, sizes and ornaments to make your own tree.

    What you will learn

    • Moving the turtle with forward, left, right and goto
    • Drawing filled shapes with begin_fill and end_fill
    • Loops that repeat a shape: triangles, a rectangle, a star
    • Looping over a list of tuples (the ornaments)
    • dot() for circles and write() for text

    How the code works

    1. 1

      Set up the turtle

      turtle.Turtle() creates the pen and turtle.bgcolor("midnightblue") paints the night sky. speed(0) makes it draw as fast as possible.

    2. 2

      Three triangles make the tree

      A loop goes through three (size, y) pairs. For each one, the turtle jumps to the bottom-left corner with penup and goto, then draws a filled triangle: forward(size) and left(120), three times. Each triangle is smaller and higher than the last.

    3. 3

      The trunk

      A brown rectangle under the tree: forward and right(90) four times, with the sides 50, 60, 50 and 60 long.

    4. 4

      Ornaments

      The ornaments list holds (x, y, colour) for each bauble. The loop goes to each position and calls dot(24, colour) to draw a coloured circle.

    5. 5

      The star

      forward(40) and right(144), five times, draws a five-pointed star: turning 144 degrees at each point makes the lines cross. Filled with gold, it sits on top of the tree.

    6. 6

      The message

      write() prints "Merry Christmas!" centred under the tree in bold white text, and hideturtle() hides the pen when the drawing is done.

    Full source code

    Copy it, download it, or press Run it to play and change it in your browser.

    main.py56 lines
    import turtle
    
    t = turtle.Turtle()
    turtle.bgcolor("midnightblue")
    t.speed(0)
    
    # the tree: three green triangles
    t.color("forestgreen")
    for size, y in [(300, -150), (230, -40), (160, 60)]:
        t.penup()
        t.goto(-size / 2, y)
        t.pendown()
        t.begin_fill()
        for side in range(3):
            t.forward(size)
            t.left(120)
        t.end_fill()
    
    # the trunk
    t.color("saddlebrown")
    t.penup()
    t.goto(-25, -150)
    t.pendown()
    t.begin_fill()
    for side in [50, 60, 50, 60]:
        t.forward(side)
        t.right(90)
    t.end_fill()
    
    # ornaments
    ornaments = [(-70, -115, "red"), (60, -100, "gold"),
                 (-25, -25, "deepskyblue"), (45, 20, "red"),
                 (-30, 95, "gold"), (15, 150, "deepskyblue")]
    for x, y, c in ornaments:
        t.penup()
        t.goto(x, y)
        t.dot(24, c)
    
    # the star
    t.color("gold")
    t.goto(-20, 225)
    t.pendown()
    t.begin_fill()
    for point in range(5):
        t.forward(40)
        t.right(144)
    t.end_fill()
    
    # the message
    t.penup()
    t.goto(0, -270)
    t.color("white")
    t.write("Merry Christmas!", align="center",
            font=("Arial", 28, "bold"))
    t.hideturtle()
    turtle.done()

    Run it on your computer

    1. Install Python 3 if you do not have it yet.
    2. Save the code as main.py and run python main.py

    More Python projects

    ← All Python projects