用Python给大家画个小月饼祝福

简介: 在拥堵的节日出行中,我们体会到生活不只是赶路,而是感受旅途。本文未分享文献,而是送上中秋祝福与一段Python代码,用turtle库绘制了一个精美的月饼图形,寓意双节快乐。代码中定义了`MoonCake`类,包含绘制月饼外形和纹理的方法,最后展示了一个带有"中秋节\n快乐吖"文字的月饼图像。鼓励读者自定义创新,享受编程的乐趣。

生活不是为了赶路,是为了感受路。

虽然大家都做“聪明人”想早一天开溜,但大家都这么想,提前3天开溜也还是哪哪都堵,真的是要高速上啃月饼渡中秋了

image.png

今天暂不分享文献了,不务正业一下

祝大家中秋国庆双节快乐!

image.png

附上代码:

import turtle as turtle
import math
import time

time.sleep(5)
turtle.hideturtle()
turtle.speed(10)


class MoonCake:
    def __init__(self, name: str):
        self.name = name

    # 绘制月饼的外部花边
    def external_pattern(self, r: int, n: int):
        turtle.penup()
        turtle.goto(0, -r)
        turtle.pendown()

        round_r = math.sin(math.pi / n) * r

        for i in range(n):
            turtle.penup()
            turtle.home()
            turtle.seth((360/n) * i)
            turtle.fd(r)
            turtle.left((360/n) * 0.5)
            turtle.pendown()
            turtle.color('#F0BE7C')
            turtle.begin_fill()
            turtle.circle(round_r, 180)
            turtle.end_fill()

    # 绘制月饼内部的纹理
    def internal_pattern(self):
        turtle.color('#F5E16F')
        turtle.goto(0, -180)
        for _ in range(8):
            turtle.begin_fill()
            turtle.circle(60, 120)
            turtle.left(180)
            turtle.circle(60, 120)
            turtle.end_fill()

    # 半径、线条大小和颜色
    def draw_circle(self, r: int, pensize: int, color1: str, color2: str):
        turtle.penup()
        turtle.goto(0, -r)
        turtle.seth(0)
        turtle.pendown()
        turtle.pensize(pensize)
        turtle.color(color1, color2)
        turtle.begin_fill()
        turtle.circle(r)
        turtle.end_fill()

    def draw(self):
        turtle.title("2023年双节高速堵爆了!!!")
        self.external_pattern(200, 12)
        self.draw_circle(200, 10, '#F0BE7C', '#F0BE7C')
        self.draw_circle(180, 10, '#F8CD32', '#FBA92D')
        self.internal_pattern()
        self.write_text(-105, -60)
        turtle.done()

    def write_text(self):
        turtle.penup()
        turtle.goto(0, -80)
        turtle.pendown()
        turtle.color('Gold')
        turtle.write(self.name, align="center", font=("华文隶书", 54, "bold"))

    def draw(self):
        turtle.title("2023年双节高速堵爆了!!!")
        self.external_pattern(200, 12)
        self.draw_circle(200, 10, '#F0BE7C', '#F0BE7C')
        self.draw_circle(180, 10, '#F8CD32', '#FBA92D')
        self.internal_pattern()
        self.write_text()
        time.sleep(5)  # Pause for 5 seconds after the entire drawing is complete
        turtle.done()


if __name__ == '__main__':
    MoonCake('中秋节\n快乐吖').draw()

这套代码应该不难,大家可以自定义换色和想想图案,水果五仁应有尽有无所不有....

目录
相关文章
|
19天前
|
搜索推荐 Python
用Python给大家画个小月饼祝福
本文分享了关于中秋的主题,指出生活不仅是赶路,更要感受沿途的美好。尽管人们常想提前躲避拥堵,但假期出行依然人多。文中未提供文献分享,而是祝大家中秋国庆双节快乐,并附上了一段Python代码,用于绘制月饼图形,鼓励读者自定义设计。最后提到代码相对简单,适合个性化创作。
12 0
|
6月前
|
存储 程序员 Python
Python浪漫520表白代码
Python浪漫520表白代码
76 0
|
9月前
|
Python
送给她最最浪漫的表白(Python代码实现)
送给她最最浪漫的表白(Python代码实现)
|
20天前
|
存储 定位技术 数据库
【python毕业设计】python基于Pygame的吃金币游戏设计与实现(源码+毕业论文)【独一无二】
【python毕业设计】python基于Pygame的吃金币游戏设计与实现(源码+毕业论文)【独一无二】
|
1月前
|
Python
python烟花绘制,春节祝福
python烟花绘制,春节祝福
24 0
|
3月前
|
图形学 Python
Python万圣节礼物
Python万圣节礼物
18 0
Python万圣节礼物
|
7月前
|
Python
用Python画浪漫樱花。
用Python语言进行编码,画浪漫樱花。
31 0
|
7月前
|
Python
用 Python 实现黑客帝国中代码雨
用 Python 实现黑客帝国中代码雨
97 0
用 Python 实现黑客帝国中代码雨
|
9月前
|
Python
送你一个Python做的Crossin牌“月饼”
画这样一个月饼,其实不需要太多的代码,笔者只用了不到50行代码,而且用的是我们经常用到的绘图库matplotlib,这对于我们大部分人来说是很容易上手的,下面笔者就详细介绍一下怎么画这样一个月饼。
|
9月前
|
机器人 Python
送小公主——哆啦A梦(Python代码实现)
送小公主——哆啦A梦(Python代码实现)
123 0

相关实验场景

更多