draw a christmas tree in python

To draw a Christmas tree in Python, we can use the turtle graphics library. Here's an example program that draws a simple Christmas tree:

main.py
import turtle

# set up the turtle
t = turtle.Turtle()
t.speed(0)  # set the drawing speed to maximum

# draw the trunk and the foliage
t.color('brown')
t.pensize(15)
t.pendown()
t.forward(100)
t.penup()
t.right(90)
t.forward(10)
t.right(90)
t.pendown()
t.color('#3CB371')  # green
t.begin_fill()
t.circle(50, steps=3)
t.end_fill()

# draw some decorations
t.penup()
t.color('red')
t.goto(-30, 20)
t.begin_fill()
t.circle(10)
t.end_fill()

t.color('gold')
t.goto(0, 30)
t.begin_fill()
t.circle(10)
t.end_fill()

t.color('darkorchid')  # purple
t.goto(30, 20)
t.begin_fill()
t.circle(10)
t.end_fill()

# draw a star on top
t.color('yellow')
t.goto(0, 100)
t.pendown()
t.begin_fill()
for _ in range(5):
    t.forward(50)
    t.right(144)
t.end_fill()

# hide the turtle cursor
t.hideturtle()

# keep the window open until it's clicked
turtle.mainloop()
851 chars
57 lines

This program uses the turtle to draw the trunk, the foliage, some decorations, and a star on top of the tree. Note that the steps parameter for the circle method is used to draw a triangle (with three steps) for the foliage. You can modify this code to draw a more complex Christmas tree or add more decorations.

related categories

gistlibby LogSnag