tortuga-pitón (dibujo de tortuga) árbol de Navidad

Prefacio:

1. Debe presentar la biblioteca de tortugas antes de comenzar. Si no ha descargado esta biblioteca, puede copiar la siguiente declaración y descargarla en la terminal PyCharm.

 

pip install turtle

2. Biblioteca Turtle, este pincel está en el eje x horizontal de esta ventana, y el eje y vertical comienza desde el origen (0, 0) y utiliza instrucciones de función para mover el dibujo.

texto:

1: Introduzca las bibliotecas requeridas antes de comenzar

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

 2: Establecer propiedades de ventana, propiedades de color RGB y dimensiones de dibujo

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

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

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

Tres: pintar un árbol de Navidad

# 画树
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)

Cuatro: dibuja una estrella de cinco puntas.

# 画五角星
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()

Cinco: dibuja las estrellas en el cielo.

# 满天繁星
# 用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()

Seis: dibuja globos de colores.

#彩色气球
# 利用循环画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()

Siete: pintar linternas

# 彩灯
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()

Ocho: Pintar el cielo lleno de nieve.

# 雪花
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: dibuja palabras de bendición

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

Diez: evitar que la ventana se cierre después de completar el dibujo

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

¡El código final completo se presenta con ambas manos!

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()

El efecto es el siguiente

 Recientemente he tenido fiebre hoy, así que no tuve tiempo de subirlo rápidamente, así que tuve que tomar el último tren jeje

Supongo que te gusta

Origin blog.csdn.net/m0_73463638/article/details/128437438
Recomendado
Clasificación