Click the blue text to follow us

Python Teaching

Hello everyone!
Today’s lesson is about
Drawing a Christmas tree with Python~
Let’s learn together!


Part 1
1. Import Libraries
from turtle import *
from random import *
import math

Part 2
2. Define Basic Drawing Directions
def Rightdraw(Range,Fd,Right):
for i in range(Range): # Number of iterations
fd(Fd) # Move forward Fd distance
right(Right)
def Leftdraw(Range,Fd,Left):
for i in range(Range): # Number of iterations
fd(Fd) # Move forward Fd distance
left(Left)
screensize(bg='black') # Set background
def changeMypos(x,y,range=heading(),Fd=0):
penup()
goto(x, y)
seth(range)
fd(Fd)
pendown()
def drawBranch(x,y,size=1):
changeMypos(x,y)
Leftdraw(6,3,9)
seth(0)
Rightdraw(6,3,9)
seth(0)
fd(6)

Part 3
3. Draw Decorative Items
(1) Draw a Star
def drawStar(x,y,Range,size):
pensize(1)
color("red","yellow")
begin_fill()
changeMypos(x,y,Range)
for i in range(5): # Draw a star
forward(10*size)
right(144) # Angle of the star
forward(10*size)
left(72) # Continue changing angle
end_fill()
right(126)
(2) Snowflakes
def drawSnow():
hideturtle()
speed(0)
pencolor("white")
pensize(2)
for i in range(80): # Number of snowflakes
changeMypos(randint(-248,248), randint(-100,248))
petalNumber = 6 # Number of petals for snowflakes
snowSize = int(randint(2,10))
for j in range(petalNumber):
fd(snowSize)
backward(snowSize)
right(360/petalNumber)
(3) Ribbons
def drawRibbon(x,y,range,size):
begin_fill()
color("red","red")
seth(range+40)
fd(15*size*math.tan(math.radians(range+40)))
seth(range+90)
fd(20/3*size)
seth(range-140)
fd(15*size*math.tan(math.radians(range+40)))
seth(range-90)
fd(20/3*size)
end_fill()
setup(500, 500, startx=None, starty=None)
title("Merry Christmas")
speed(0)
pencolor("green")
pensize(10)
hideturtle()
changeMypos(0, 185, 0)

Part 4
4. Draw the Tree Trunk
# Top layer
seth(-120)
Rightdraw(10,12,2)
changeMypos(0,185,-60)
Leftdraw(10,12,2)
changeMypos(xcor(),ycor(),-150,10)
# First layer wave
for i in range(4):
Rightdraw(5,7,15)
seth(-150)
penup()
fd(2)
pendown()
# Second layer
changeMypos(-55,70,-120)
Rightdraw(10,8,5)
changeMypos(50,73,-60)
Leftdraw(10,8,5)
changeMypos(xcor(),ycor(),-120,10)
seth(-145)
pendown()
# Second layer wave
for i in range(5):
Rightdraw(5,9,15)
seth(-152.5)
penup()
fd(3)
pendown()
# Third layer
changeMypos(-100,0,-120)
Rightdraw(10,6.5,4.5)
changeMypos(80,0,-50)
Leftdraw(10,6,3)
changeMypos(xcor(),ycor(),-120,10)
seth(-145)
# Third wave
for i in range(6):
Rightdraw(5,9,15)
seth(-152)
penup()
fd(3)
pendown()
# Fourth layer
changeMypos(-120,-55,-130)
Rightdraw(7,10,4)
changeMypos(100,-55,-50)
Leftdraw(7,10,5)
changeMypos(xcor(),ycor(),-120,10)
seth(-155)
# Fourth layer wave
for i in range(7):
Rightdraw(5,9,13)
seth(-155)
penup()
fd(3)
pendown()
# Tree roots
changeMypos(-70,-120,-85)
Leftdraw(3,8,3)
changeMypos(70,-120,-95)
Rightdraw(3,8,3)
changeMypos(xcor(),ycor(),-170,10)
Rightdraw(10,12,2)
# Draw branches
drawBranch(45,-80)
drawBranch(-70,-25)
drawBranch(-20,40)
# Add ornaments
drawStar(110, -90, 80, 1)
drawStar(-120, -100, 50, 1)
drawStar(-90, -50, 20, 1)
drawStar(90, -25, 30, 1)
drawStar(-20, 40, 30, 1)
drawStar(10, 120, 90, 1)
# Print greeting
color("dark red","red") # Define font color
penup()
goto(0,-230)
write("Merry Christmas",align="center",font=("Comic Sans MS",40,"bold"))
# Call the snow function
drawSnow()
done()

Result Image




This issue of Python learning ends here!
Thank you all for watching
Please continue to follow our public account
See you next time~

Illustration and Text | Chen Yun’er
Typesetting | Chen Yun’er
Initial Review | Xiao Junyu
Final Review | Luo Yangqianzi
Huang Guojie
Final Review | Huang Sheng
