turtle—‘小小爱心火柴人’

这篇博客展示了作者使用Python的turtle模块创作的七夕主题编程作品,包括绘制火柴人与爱心,以及模拟满天星的爱心飘动效果。通过定义不同的函数,实现了复杂图形的绘制,展现了Python在图形艺术创作中的应用。代码中包含颜色、位置控制等技巧,适合初学者学习和借鉴。
摘要由CSDN通过智能技术生成

这两天在学习Python的turtle,昨天下午用 goto() 语句画了一个火柴人,今天改进了一下,并加了两个爱心。
#改进前代码

from turtle import *
#color("red", "yellow")
bgcolor('azure')
pensize(10)
pencolor("yellow")
goto(0,100)
color("yellow","pink")
begin_fill()
circle(30)
end_fill()
goto(0,0)
goto(-70.7,-70.7)
goto(0,0)
goto(70.7,-70.7)
goto(0,0)
goto(0,50)
goto(42.5,75)
pencolor("pink")
goto(42.5,90)
goto(42.5,75)
pencolor("yellow")
goto(0,50)
goto(-42.5,25)
pencolor("pink")
goto(-42.5,10)
hideturtle()
done()

运行效果
在这里插入图片描述
改进后的代码

'''小小爱心火柴人'''
from turtle import *
from time import sleep

bgcolor('azure')
pensize(6)
pencolor("yellow")


def hand_leg_color():
    pencolor("pink")
    forward(20)
    pencolor("yellow")


def go_to(x, y):
    up()
    goto(x, y)
    down()


def head(x, y, r):
    go_to(x, y)
    fillcolor("pink")
    begin_fill()
    circle(r)
    end_fill()
    leg(x, y)


def leg(x, y):
    right(90)
    forward(100)
    right(45)
    forward(60)
    hand_leg_color()
    go_to(x, y-100)
    left(90)
    forward(60)
    hand_leg_color()
    hand(x, y)


def hand(x, y):
    go_to(x, y-50)
    left(75)
    forward(75)
    left(60)
    hand_leg_color()
    go_to(x, y-50)
    #left(120)
    left(60)
    forward(75)
    #left(60)
    right(60)
    hand_leg_color()
    #left(90)
    right(90)


def big_circle(size):
    speed(0)
    for i in range(100):
        forward(size)
        right(0.45)


def line(size):
    speed(0)
    forward(63 * size)


def small_circle(size):
    speed(0)
    for i in range(180):
        forward(size)
        right(0.917)


def heart(x, y, size):
    go_to(x, y)
    left(120)
    pencolor('red')
    pensize(2)
    fillcolor("pink")
    begin_fill()
    line(size)
    big_circle(size)
    small_circle(size)
    left(120)
    small_circle(size)
    big_circle(size)
    line(size)
    end_fill()


def main():
    head(0, 100, 30)
    heart(150, 50, 1)
    right(120)
    heart(-150, 50, 1)
    go_to(-195, -150)
    pensize(2)
    write("To: the fairy with wisdom and beauty!", move=True, align="left", font=("华文行楷", 20, "normal"))
    hideturtle()
    done()


main()

运行效果
在这里插入图片描述
七夕节,想着写一个什么复习一下前几天学的东西,看文章有一个漫天星的想法很不错,并且也切合这个主题,于是就写了一个漫天心的代码

'''七夕,满天心'''
import turtle as t
import random

t.bgcolor('azure')  #画板颜色,浅蓝


def go_to(x, y):
    t.up()
    t.goto(x, y)
    t.down()


#画心
def draw_heart(size, color_):
    #color('red', color_)
    t.speed(0)
    t.colormode(255)
    t.color(color_)
    t.begin_fill()
    #t.left(120)  效果不同
    t.setheading(120)  #爱心正
    t.forward(30 * size)
    t.circle(-8.66*size, 180)
    t.left(120)
    t.circle(-8.66*size, 180)
    t.forward(30 * size)
    t.end_fill()


#漫天心
def draw_pic():
    # color  三原色
    colors_1 = random.randint(0, 255)
    colors_2 = random.randint(0, 255)
    colors_3 = random.randint(0, 255)
    t.penup()
    # pos
    x = random.randint(-400, 400)
    y = random.randint(-200, 200)
    go_to(x, y)
    # size
    size = random.randint(0, 4)
    draw_heart(size, (colors_1, colors_2, colors_3))


def main():
    t.hideturtle()
    t.setup(900, 700) #画屏大小
    # number
    for i in range(20):
        draw_pic()
    go_to(-100, 0)
    t.pencolor('red')
    t.write("七夕,在一起!", move=True, align="left", font=("华文行楷", 20, "normal"))
    go_to(-445, 315)
    t.write("To:My dear!", move=True, align="left", font=("Comic Sans MS", 20, "normal"))
    t.done()


main()

运行的效果:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

T1009∞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值