无聊的时间,乌龟画了一只小猪佩奇还有她的蛋糕

本文利用Python的Turtle库详细介绍了如何绘制小猪佩奇和生日蛋糕,包括初始设置、画蛋糕、添加随机彩点、写文字及小猪佩奇的参数设置,展示了Turtle库在图形绘制中的应用。
摘要由CSDN通过智能技术生成

生日蛋糕和小猪佩奇搭配 Python乌龟(turtle库)

在这里插入图片描述


前言

画画这辈子是不可能画画的,看到很多视频平板画,咱用来用乌龟库来试试,论坛很多大佬用这个花了很多奇特的东西,咋也花点时间搞搞

Turtle是什么?

Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。
turtle名称含义为“海龟”,我们想象一只海龟,位于显示器上窗体的正中心,在画布上游走,它游走的轨迹就形成了绘制的图形。
海龟的运动是由程序控制的,它可以变换颜色,改变大小(宽度)等

内容方面

废话不多说,主函数代码如下:

from turtle import*
import drawbirth

drawbirth.initial_background()
pencolor("white")
drawbirth.cake()        # 蛋糕画线填充
drawbirth.ranges()      # 随机彩点
drawbirth.texts()       # 文字
ts = getscreen()
ts.getcanvas().postscript(file="ts1.eps")
drawbirth.setting()     # 画布设置
drawbirth.brandon()
tss = getscreen()
tss.getcanvas().postscript(file="tss1.eps")
done()

对进行初始设置,定义划线函数

比较基本的一些准备

def initial_background():
    # 设置背景颜色,窗口位置以及大小
    bgcolor("#d3dae8")
    setup(1000, 800)  # set(width, height):输入宽和高为整数时,表示像素;为小数时,表示占据电脑屏幕的比例
    speed(500)
    penup()  # 画笔抬起,移动时不绘制图形
    goto(150, 0)  # 将画笔移动到坐标为(x,y)的位置
    pendown()

def draw_y(period, angle):
    radian = math.radians(angle)
    return period * math.sin(radian)

def draw_x(period, angle):
    # 定义x,y的划线函数
    radian = math.radians(angle)
    return period * math.cos(radian)

画蛋糕

画蛋糕步骤比较多,主要控制划线想X,Y(数学真的很重要QAQ!!)

def cake():
    for i in range(360):
        x = draw_x(150, i)
        y = draw_y(60, i)
        goto(x, y)
    fillcolor("#fef5f7")
    end_fill()

    begin_fill()
    for i in range(180):
        x = draw_x(150, -i)
        y = draw_y(70, -i)
        goto(x, y)
    for i in range(180, 360):
        x = draw_x(150, i)
        y = draw_y(60, i)
        goto(x, y)
    fillcolor("#f2d7dd")
    end_fill()

    pu()
    goto(120, 0)
    pd()
    begin_fill()
    for i in range(360):
        x = draw_x(120, i)
        y = draw_y(48, i)
        goto(x, y)
    fillcolor("#cbd9f9")
    end_fill()

    begin_fill()
    pencolor("#fee48c")
    for i in range(540):
        x = draw_x(120, i)
        y = draw_y(48, i) + 70
        goto(x, y)
    goto(-120, 0)
    fillcolor("#cbd9f9")
    end_fill()

    pu()
    goto(120, 70)
    pd()
    pencolor("#fff0f3")
    begin_fill()
    for i in range(360):
        x = draw_x(120, i)
        y = draw_y(48, i) + 70
        goto(x, y)
    fillcolor("#fff0f3")
    end_fill()

    pu()
    goto(110, 70)
    pd()
    pencolor("#fff9fb")
    begin_fill()
    for i in range(360):
        x = draw_x(110, i)
        y = draw_y(44, i) + 70
        goto(x, y)
    fillcolor("#fff9fb")
    end_fill()

    pu()
    goto(120, 0)
    pd()
    begin_fill()
    pencolor("#ffa79d")
    for i in range(180):
        x = draw_x(120, -i)
        y = draw_y(48, -i) + 10
        goto(x, y)
    goto(-120, 0)
    for i in range(180, 360):
        x = draw_x(120, i)
        y = draw_y(48, i)
        goto(x, y)
    fillcolor("#ffa79d")
    end_fill()

    pu()
    goto(120, 70)
    pd()
    begin_fill()
    pensize(4)
    pencolor("#fff0f3")
    for i in range(1800):
        x = draw_x(120, 0.1 * i)
        y = draw_y(-18, i) + 10
        goto(x, y)
    goto(-120, 70)
    pensize(1)
    for i in range(180, 360):
        x = draw_x(120, i)
        y = draw_y(48, i) + 70
        goto(x, y)
    fillcolor("#fff0f3")
    end_fill()

    pu()
    goto(80, 70)
    pd()
    begin_fill()
    pencolor("#6f3732")
    goto(80, 120)
    for i in range(180):
        x = draw_x(80, i)
        y = draw_y
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值