Draw Christmas Tree with Python: Turtle Tutorial

You can draw a Christmas tree using Python by utilizing the turtle library. Below is a simple example code for drawing a Christmas tree.

import turtle

def draw_triangle(x, y, size, color):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    turtle.fillcolor(color)
    turtle.begin_fill()
    for _ in range(3):
        turtle.forward(size)
        turtle.right(120)
    turtle.end_fill()

def draw_tree(x, y, size):
    draw_triangle(x, y, size, "green")
    draw_triangle(x - size/2, y + size, size, "green")
    draw_triangle(x + size/2, y + size, size, "green")
    draw_triangle(x - size, y + size*2, size, "green")
    draw_triangle(x + size, y + size*2, size, "green")
    draw_triangle(x - size*1.5, y + size*3, size, "green")
    draw_triangle(x + size*1.5, y + size*3, size, "green")
    draw_triangle(x - size*2, y + size*4, size, "green")
    draw_triangle(x + size*2, y + size*4, size, "green")

def draw_star(x, y, size):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    turtle.fillcolor("yellow")
    turtle.begin_fill()
    for _ in range(5):
        turtle.forward(size)
        turtle.right(144)
    turtle.end_fill()

def main():
    turtle.speed(0)
    turtle.bgcolor("black")
    draw_tree(0, -200, 50)
    draw_star(0, 100, 20)
    turtle.hideturtle()
    turtle.done()

if __name__ == "__main__":
    main()

After running the code, a simple Christmas tree will be displayed in the window. You can adjust the size and position of the tree as needed.

bannerAds