python-turtle (turtle drawing) Christmas tree

Foreword:

1. You need to introduce the turtle library before starting. If you have not downloaded this library, you can copy the following statement and download it in the PyCharm terminal.

 

pip install turtle

2. Turtle library, this brush is on the horizontal axis x-axis of this window, and the vertical axis y-axis starts from the origin (0, 0), and uses function instructions to move the drawing.

text:

1: Introduce the required libraries before starting

# turtle as t 是将turtle替换为t
import turtle as t
# 随机库
import random
# 引入turtle库中的所有用法
from turtle import *

 2: Set window properties, RGB color properties and drawing dimensions

# 设置窗口大小和背景颜色
t.screensize(800,700,'black')

# RGB默认范围是0~1,通过Screen().colormode设置成0~255
Screen().colormode(255)

# 加快作图数度
t.speed(speed='fastest')

Three: Painting a Christmas tree

# 画树
t.left(90)
t.forward(-300)
t.pensize(5)
# 设置画笔颜色
t.color('green')
# 开始画树
def tree(d, s):
    if d <= 0:
        return
    forward(s)
    tree(d - 1, s * .8)
    right(120)
    tree(d - 3, s * .5)
    right(120)
    tree(d - 3, s * .5)
    right(120)
    backward(s)
# 调用画树函数
tree(15,100)
backward(50)

Four: Draw a five-pointed star

# 画五角星
def xin():
    t.right(90)
    # 设置画笔尺寸
    t.pensize(3)
    # t.circle(40)
    # 抬笔
    t.penup()
    # 将画笔移动到该位置
    t.goto(-25,190)
    # 落笔
    pendown()
    # 开始填充
    t.begin_fill()
    # 设置画笔颜色
    t.color('yellow')
    # 画五角星,一共需要五笔
    for i in range(5):
        t.forward(55)
        t.right(144)
    # 结束填充
    t.end_fill()

# 调用画五角星函数
xin()

Five: Draw the stars in the sky

# 满天繁星
# 用for循环画17颗星星
for i in range(17):
    # 设置随机坐标
    x=random.randint(-400,400)
    y=random.randint(240,330)
    # 设置星星随机大小
    a=random.randint(5,12)
    # 抬笔
    t.penup()
    # 将画笔移动到随机位置
    t.goto(x,y)
    # 落笔
    t.pendown()
    t.pensize(5)
    t.color('yellow')
    t.begin_fill()
    for i in range(4):
        t.forward(a)
        t.left(30)
        t.forward(a)
        t.right(120)
    # 每画完一颗星星将画笔方向向左改变30度,使星星看起来更加生动
    t.left(30)
    t.end_fill()

Six: Draw colorful balloons

#彩色气球
# 利用循环画20个气球
for i in range(20):
    # 设置随机位置
    X=random.randint(-400,400)
    Y=random.randint(-170,150)
    # 抬笔
    t.penup()
    # 将画笔位置移动到初始位置
    t.home()
    # 将画笔移动到随机位置
    t.goto(X, Y)
    # 设置RGB颜色范围
    red=random.randint(100,255)
    green=random.randint(50,155)
    blue=random.randint(100,255)
    # 画笔尺寸
    t.pensize(2)
    # 落笔
    t.pendown()
    # 设置气球线颜色
    t.color('white')
    t.left(90)
    t.circle(80,15)
    t.circle(-80,15)
    t.right(90)
    t.pensize(5)
    # 利用RGB颜色设置气球颜色
    t.color(red,green,blue)
    t.begin_fill()
    t.circle(15)
    t.end_fill()

Seven: Paint lanterns

# 彩灯
def light():
    # 隐藏笔头,ht=hideturtle
    t.hideturtle()
    # 利用for循环画100个彩灯
    for i in range(100):
        t.penup()
        # 设置彩灯随机位置
        x=random.randint(-300,300)
        y=random.randint(-350,-300)
        # 设置RGB颜色
        red=random.randint(100,255)
        green=random.randint(50,155)
        blue=random.randint(100,255)
        # 将画笔移动到随机位置
        t.goto(x,y)
        t.pendown()
        t.pensize(5)
        t.color(red,green,blue)
        t.begin_fill()
        t.circle(10)
        t.end_fill()
# 调用彩灯函数
light()

Eight: Paint the sky full of snow

# 雪花
def drawsnow():
    # 隐藏笔头,ht=hideturtle
    t.hideturtle()
    t.pensize(2)
    for i in range(200):
        t.pencolor("white")
        t.penup()
        # 设置雪花随机位置坐标
        x=random.randint(-400,400)
        y=random.randint(-250,300)
        # 将画笔移动到随机位置
        t.goto(x,y)
        t.pendown()
        # 雪花花瓣数
        petal = 6
        # 设置雪花随机大小
        snowsize = random.randint(1, 10)
        # print(type(snowsize))
        for j in range(petal):
            t.forward(snowsize)
            t.backward(snowsize)
            # 转动角度
            t.right(int(360 / petal))
# 调用雪花函数
drawsnow()

9: Draw blessing words

# 添加文字
t.penup()
t.goto(-300,-250)
t.color('red')
t.pendown()
t.write('祝 刘晓云 Merry Christmas!',font=('Mistral',42,'bold italic'))

Ten: Prevent the window from closing after drawing is completed

# 防止绘图完成后窗口秒关
t.done()

The final complete code is presented with both hands!

import turtle as t
# 随机库
import random
# 引入turtle库中的所有用法
from turtle import *
# 跳过绘图过程
t.tracer(False)
# 设置窗口大小和背景颜色
t.screensize(800,700,'black')

# RGB默认范围是0~1,通过Screen().colormode设置成0~255
Screen().colormode(255)

# 加快作图数度
t.speed(speed='fastest')


# 画树
t.left(90)
t.forward(-300)
t.pensize(5)
# 设置画笔颜色
t.color('green')
# 开始画树
def tree(d, s):
    if d <= 0:
        return
    forward(s)
    tree(d - 1, s * .8)
    right(120)
    tree(d - 3, s * .5)
    right(120)
    tree(d - 3, s * .5)
    right(120)
    backward(s)
# 调用画树函数
tree(15,100)
backward(50)


# 画五角星
def xin():
    t.right(90)
    # 设置画笔尺寸
    t.pensize(3)
    # t.circle(40)
    # 抬笔
    t.penup()
    # 将画笔移动到该位置
    t.goto(-25,190)
    # 落笔
    pendown()
    # 开始填充
    t.begin_fill()
    # 设置画笔颜色
    t.color('yellow')
    # 画五角星,一共需要五笔
    for i in range(5):
        t.forward(55)
        t.right(144)
    # 结束填充
    t.end_fill()

# 调用画五角星函数
xin()


# 满天繁星
# 用for循环画17颗星星
for i in range(17):
    # 设置随机坐标
    x=random.randint(-400,400)
    y=random.randint(240,330)
    # 设置星星随机大小
    a=random.randint(5,12)
    # 抬笔
    t.penup()
    # 将画笔移动到随机位置
    t.goto(x,y)
    # 落笔
    t.pendown()
    t.pensize(5)
    t.color('yellow')
    t.begin_fill()
    for i in range(4):
        t.forward(a)
        t.left(30)
        t.forward(a)
        t.right(120)
    # 每画完一颗星星将画笔方向向左改变30度,使星星看起来更加生动
    t.left(30)
    t.end_fill()




#彩色气球
# 利用循环画20个气球
for i in range(20):
    # 设置随机位置
    X=random.randint(-400,400)
    Y=random.randint(-170,150)
    # 抬笔
    t.penup()
    # 将画笔位置移动到初始位置
    t.home()
    # 将画笔移动到随机位置
    t.goto(X, Y)
    # 设置RGB颜色范围
    red=random.randint(100,255)
    green=random.randint(50,155)
    blue=random.randint(100,255)
    # 画笔尺寸
    t.pensize(2)
    # 落笔
    t.pendown()
    # 设置气球线颜色
    t.color('white')
    t.left(90)
    t.circle(80,15)
    t.circle(-80,15)
    t.right(90)
    t.pensize(5)
    # 利用RGB颜色设置气球颜色
    t.color(red,green,blue)
    t.begin_fill()
    t.circle(15)
    t.end_fill()


# 彩灯
def light():
    # 隐藏笔头,ht=hideturtle
    t.hideturtle()
    # 利用for循环画100个彩灯
    for i in range(100):
        t.penup()
        # 设置彩灯随机位置
        x=random.randint(-300,300)
        y=random.randint(-350,-300)
        # 设置RGB颜色
        red=random.randint(100,255)
        green=random.randint(50,155)
        blue=random.randint(100,255)
        # 将画笔移动到随机位置
        t.goto(x,y)
        t.pendown()
        t.pensize(5)
        t.color(red,green,blue)
        t.begin_fill()
        t.circle(10)
        t.end_fill()
# 调用彩灯函数
light()

# 雪花
def drawsnow():
    # 隐藏笔头,ht=hideturtle
    t.hideturtle()
    t.pensize(2)
    for i in range(200):
        t.pencolor("white")
        t.penup()
        # 设置雪花随机位置坐标
        x=random.randint(-400,400)
        y=random.randint(-250,300)
        # 将画笔移动到随机位置
        t.goto(x,y)
        t.pendown()
        # 雪花花瓣数
        petal = 6
        # 设置雪花随机大小
        snowsize = random.randint(1, 10)
        # print(type(snowsize))
        for j in range(petal):
            t.forward(snowsize)
            t.backward(snowsize)
            # 转动角度
            t.right(int(360 / petal))
# 调用雪花函数
drawsnow()


# 添加文字
t.penup()
t.goto(-300,-250)
t.color('red')
t.pendown()
t.write('祝 刘晓云 Merry Christmas!',font=('Mistral',42,'bold italic'))

# 防止绘图完成后窗口秒关
t.done()

The effect is as follows

 Recently I have been having a fever today, so I didn’t have time to upload it in a hurry, so I had to catch the last train hehe

Guess you like

Origin blog.csdn.net/m0_73463638/article/details/128437438