Christmas is coming, let's draw a Christmas tree with Python Turtle

How to achieve the above effect? let's start!

First, import turtle and random

from turtle import *
import random as rd

Then, write a function that will be used later to randomly generate True and False

def true_or_false(percent=50):
    n=rd.randint(1,100)
    if n<=percent:
        return True
    else:
        return False

Then initialize it, set the color mode to 255 (RGB mode), set the canvas, initial position, drawing speed, etc.

# initial
colormode(255)
screensize(600,600,"darkblue")
seth(90)
pu()
bk(180)
speed(0)
tracer(2)

some constants

# constants
BROWN=160,82,45
TRUNK_LENGTH=350
TRUNK_PENSIZE=15
DECORATE_BALL_PENSIZE=4
DECORATE_COLORS=[
    (200,0,0),
    (220,220,0),
    (255,97,3)
]

trunk

# trunk
pu()
seth(90)
xx=0
goto(xx,-180)
pd()
pensize(TRUNK_PENSIZE)
pencolor(BROWN)
fd(TRUNK_LENGTH)
bk(TRUNK_LENGTH)

The leaves on both sides, the leaves on the left face to the right (random degree), and the leaves on the right face to the left (random degree), this code is not difficult to understand

# leaf
pu()
x,y=pos()

for n in range(18): # left
    seth(90)
    if n==0:
        fd(100)
    else:
        fd(20)
    seth(180)
    length=200-n*10
    fd(length)
    seth(0)
    for i in range(length):
        fd(1)
        if i%6==0:
            pd()
            seth(rd.randint(45,75))
            pencolor((0,rd.randint(110,180),0) if true_or_false(80) else (255,255,255))
            pensize(10)
            fd(30)
            bk(30)
            seth(0)
            pu()

pu()
goto(x,y)
for n in range(19): # right
    seth(90)
    if n==0:
        fd(100)
    else:
        fd(20)
    seth(0)
    length=200-n*10
    fd(length)
    seth(280)
    for i in range(length):
        fd(1)
        if i%6==0:
            pd()
            seth(rd.randint(105,135))
            pencolor((0,rd.randint(110,180),0) if true_or_false(80) else (255,255,255))
            pensize(10)
            fd(30)
            bk(30)
            seth(180)
            pu()

The star of Bethlehem, the drawing position was not right at the beginning, after several adjustments, it was determined as the following code

# star
pu()
goto(xx,pos()[1])
seth(180)
fd(20) # micro moving
seth(90)
fd(20) # micro moving

pd()
color((255,0,0))
seth(72)
begin_fill()
for i in range(5):
    fd(80)
    right(180-180/5)
end_fill()

some decorations

# ball (decorate)
positions=[(-159,-55),(-91,14),(-83,-36),(85,-54),(-22,-40),(-3,34),(0,160),(-47,152),(19,215),(115,22),(68,107),(141,-48)]
pensize(DECORATE_BALL_PENSIZE)
for x,y in positions:
    pu()
    goto(x,y)
    r=rd.randint(12,22)
    seth(90)
    fd(r)
    seth(0)
    fillcolor(rd.choice(DECORATE_COLORS))
    pencolor(rd.choice(DECORATE_COLORS))
    pd()
    begin_fill()
    circle(r)
    end_fill()

DONE!!

done()

Ok, let's take a look at the whole code!

from turtle import *
import random as rd

def true_or_false(percent=50):
    n=rd.randint(1,100)
    if n<=percent:
        return True
    else:
        return False

# initial
colormode(255)
screensize(600,600,"darkblue")
seth(90)
pu()
bk(180)
speed(0)
tracer(2)

# constants
BROWN=160,82,45
TRUNK_LENGTH=350
TRUNK_PENSIZE=15
DECORATE_BALL_PENSIZE=4
DECORATE_COLORS=[
    (200,0,0),
    (220,220,0),
    (255,97,3)
]

# trunk
pu()
seth(90)
xx=0
goto(xx,-180)
pd()
pensize(TRUNK_PENSIZE)
pencolor(BROWN)
fd(TRUNK_LENGTH)
bk(TRUNK_LENGTH)

# leaf
pu()
x,y=pos()

for n in range(18): # left
    seth(90)
    if n==0:
        fd(100)
    else:
        fd(20)
    seth(180)
    length=200-n*10
    fd(length)
    seth(0)
    for i in range(length):
        fd(1)
        if i%6==0:
            pd()
            seth(rd.randint(45,75))
            pencolor((0,rd.randint(110,180),0) if true_or_false(80) else (255,255,255))
            pensize(10)
            fd(30)
            bk(30)
            seth(0)
            pu()

pu()
goto(x,y)
for n in range(19): # right
    seth(90)
    if n==0:
        fd(100)
    else:
        fd(20)
    seth(0)
    length=200-n*10
    fd(length)
    seth(280)
    for i in range(length):
        fd(1)
        if i%6==0:
            pd()
            seth(rd.randint(105,135))
            pencolor((0,rd.randint(110,180),0) if true_or_false(80) else (255,255,255))
            pensize(10)
            fd(30)
            bk(30)
            seth(180)
            pu()

# star
pu()
goto(xx,pos()[1])
seth(180)
fd(20) # micro moving
seth(90)
fd(20) # micro moving

pd()
color((255,0,0))
seth(72)
begin_fill()
for i in range(5):
    fd(80)
    right(180-180/5)
end_fill()

# ball (decorate)
positions=[(-159,-55),(-91,14),(-83,-36),(85,-54),(-22,-40),(-3,34),(0,160),(-47,152),(19,215),(115,22),(68,107),(141,-48)]
pensize(DECORATE_BALL_PENSIZE)
for x,y in positions:
    pu()
    goto(x,y)
    r=rd.randint(12,22)
    seth(90)
    fd(r)
    seth(0)
    fillcolor(rd.choice(DECORATE_COLORS))
    pencolor(rd.choice(DECORATE_COLORS))
    pd()
    begin_fill()
    circle(r)
    end_fill()

done()

The parameters inside can be modified by yourself, if you like it, come to a 3-link~~~

Guess you like

Origin blog.csdn.net/leleprogrammer/article/details/128258107