【Python绘图】朋友过生日不知道送什么?免费的蛋糕送给他

基于Python的Turtle库实现生日蛋糕的绘制 本文介绍了如何使用Python中的Turtle库来实现生日蛋糕的绘制及源代码的分享,Turtle库是一个强大的绘图工具,它通过模拟海龟的移动和绘制来进行图形绘制。 我们提供了完整的Python代码和效果展示,帮助读者理解和实践。通过本文的学习,读者可以掌握使用Python的Turtle库来创造出独特生日蛋糕的绘制技巧,为朋友或家人带来惊喜和欢乐。 阅读详情

🌈据说,看我文章时 关注、点赞、收藏 的 帅哥美女们 心情都会不自觉的好起来。

前言:
🧡作者简介:大家好我是 user_from_future ,意思是 “ 来自未来的用户 ” ,寓意着未来的自己一定很棒~
✨个人主页:点我直达,在这里肯定能找到你想要的~
👍专栏介绍:Python绘图 ,一个专注于分享绘图案例与教学的专栏~

人生苦短,我用python

前言

今天为什么突然想创建这个专栏呢,主要就是因为今天是农历二月甘四,是博主的生日👑,所以想给自己画一个大大的蛋糕🎁,同时也是为这新的专栏剪彩~ 虽然我这画的可能比较简陋,但毕竟是博主第一次画蛋糕送给自己…

成果图

在这里插入图片描述

源代码

# 画生日蛋糕
import math
import turtle
# 元素函数


def draw_candle(location, width, height):
    """
    画蜡烛。
    :param location: 蜡烛底部中心坐标
    :param width: 蜡烛宽度
    :param height: 蜡烛高度
    """
    color = turtle.color()
    angle = turtle.heading()
    turtle.penup()
    turtle.color('skyblue')
    turtle.setheading(0)
    turtle.goto(*location)
    turtle.pendown()
    turtle.begin_fill()
    turtle.forward(width / 2)
    turtle.setheading(90)
    turtle.forward(height)
    turtle.setheading(180)
    turtle.forward(width)
    turtle.setheading(270)
    turtle.forward(height)
    turtle.setheading(0)
    turtle.forward(width / 2)
    turtle.end_fill()
    turtle.penup()
    turtle.setheading(90)
    turtle.forward(height)
    turtle.setheading(20)
    turtle.color('red')
    turtle.pendown()
    turtle.begin_fill()
    turtle.circle(width / 2, 180)
    turtle.setheading(20)
    turtle.circle(-width / 4, 180)
    turtle.setheading(200)
    turtle.circle(width / 4, 180)
    turtle.end_fill()
    turtle.penup()
    turtle.color(*color)
    turtle.goto(*location)
    turtle.setheading(angle)


def draw_oval(location, width, height, fill=True, color='#000000'):
    """
    画椭圆。
    :param location: 椭圆中心坐标
    :param width: 椭圆长轴(横向)
    :param height: 椭圆短轴(纵向)
    :param fill: 是否填充
    :param color: 椭圆颜色
    """
    assert width >= height, ValueError('目前只能长轴是 x 轴,即 width >= height (width == height 时是圆)!')
    a = width / 2
    b = height / 2
    color_old = turtle.color()
    angle = turtle.heading()
    turtle.color(color)
    turtle.penup()
    turtle.goto(*location)
    turtle.setheading(0)
    turtle.forward(width / 2)
    turtle.setheading(90)
    turtle.pendown()
    if fill:
        turtle.begin_fill()
    if width < 180:  # 通过椭圆直角坐标方程计算描点绘画
        #                     ((a ** 2 * b ** 2 - b ** 2 * x ** 2) / a ** 2) ** 0.5
        #                     (((a ** 2 - x ** 2) * b ** 2) / a ** 2) ** 0.5
        #                     (((a + x) * (a - x) * b ** 2) / a ** 2) ** 0.5
        for x in range(width // 2, width // -2, -1):  # 以 y = location[1] 的单位上半部分描点
            y = location[1] + (((a ** 2 - x ** 2) * b ** 2) / a ** 2) ** 0.5
            turtle.goto(x, y)
        for x in range(width // -2, width // 2 + 1):  # 以 y = location[1] 的单位下半部分描点
            y = location[1] - (((a ** 2 - x ** 2) * b ** 2) / a ** 2) ** 0.5
            turtle.goto(x, y)
    else:  # 通过椭圆极坐标方程计算描点绘画
        for ang in range(360):
            turtle.goto(width / 2 * math.cos(math.radians(ang + 1)), location[1] + height / 2 * math.sin(math.radians(ang + 1)))
    if fill:
        turtle.end_fill()
    turtle.penup()
    turtle.color(*color_old)
    turtle.goto(*location)
    turtle.setheading(angle)


def draw_cylinder(location, width, height, altitude, fill=True, color1='#000000', color2='#000000'):
    """
    画椭圆柱。
    :param location: 底部椭圆中心坐标
    :param width: 底部椭圆长轴(横向)
    :param height: 底部椭圆短轴(纵向)
    :param altitude: 椭圆柱高度
    :param fill: 是否填充
    :param color1: 椭圆柱面绘图颜色
    :param color2: 椭圆柱顶绘图颜色
    """
    color_old = turtle.color()
    angle = turtle.heading()
    draw_oval(location, width, height, fill=fill, color=color1)
    turtle.penup()
    turtle.color(color1)
    turtle.goto(*location)
    turtle.setheading(0)
    turtle.forward(width / 2)
    turtle.pendown()
    if fill:
        turtle.begin_fill()
        turtle.setheading(90)
        turtle.forward(altitude)
        turtle.setheading(180)
        turtle.forward(width)
        turtle.setheading(270)
        turtle.forward(altitude)
        turtle.setheading(0)
        turtle.forward(width)
        turtle.end_fill()
    turtle.penup()
    location = (location[0], location[1] + altitude)
    draw_oval(location, width, height, fill=fill, color=color2)
    turtle.penup()
    turtle.color(*color_old)
    turtle.goto(*location)
    turtle.setheading(angle)


def draw_wave(location, width, height, space, pensize=10, color='#000000', towards='right'):
    """
    画波浪线。
    :param location: 波浪线起点(中心点)
    :param width: 波浪线总宽度
    :param height: 波浪线高度
    :param space: 波浪间距
    :param pensize: 波浪粗细
    :param color: 波浪颜色
    :param towards: 破浪线朝向
    """
    assert towards.lower() in ['left', 'right'], '方向只能是 left向左 或 right向右 !'
    color_old = turtle.color()
    angle = turtle.heading()
    thickness = turtle.pensize()
    turtle.color(color)
    turtle.penup()
    turtle.goto(*location)
    turtle.pensize(pensize)
    turtle.pendown()
    if towards.lower() == 'right':
        turtle.setheading(0)
    else:
        turtle.setheading(180)
    for x in range(width):
        turtle.goto(location[0] + x, location[1] + height / 2 * math.sin(math.radians(x * 180 / space)))
    turtle.penup()
    turtle.color(*color_old)
    turtle.pensize(thickness)
    turtle.goto(*location)
    turtle.setheading(angle)


def draw_love(location, side, color='pink'):
    """
    画爱心。
    :param location: 爱心的上方顶点坐标
    :param side: 爱心边长 或 直径(爱心由一个正方形和两个圆形组成)
    :param color: 爱心颜色
    :return:
    """
    color_old = turtle.color()
    angle = turtle.heading()
    turtle.penup()
    turtle.goto(*location)
    turtle.color(color)
    turtle.pendown()
    turtle.right(45)
    turtle.begin_fill()
    turtle.forward(side)
    turtle.right(90)
    turtle.forward(side)
    turtle.right(90)
    turtle.forward(side)
    turtle.right(90)
    turtle.forward(side)
    turtle.end_fill()
    turtle.left(90)
    turtle.begin_fill()
    turtle.circle(side / 2, 360)
    turtle.end_fill()
    turtle.left(90)
    turtle.begin_fill()
    turtle.circle(side / 2, 360)
    turtle.end_fill()
    turtle.penup()
    turtle.color(*color_old)
    turtle.goto(*location)
    turtle.setheading(angle)


turtle.speed(0)
turtle.tracer(True)
turtle.title('生日快乐~')
turtle.setup(600, 600)
turtle.bgcolor("#D3DAE8")
draw_cylinder((0, -100), 400, 150, 10, True, '#EEEEEE', 'white')  # 绘制最底层白色餐盘
draw_cylinder((0, -90), 320, 120, 80, True, 'skyblue', 'white')  # 绘制第一层蛋糕
draw_wave((-160, -100), 320, 25, 40, 5, 'red')  # 绘制第一层蛋糕侧边曲线1
draw_wave((-160, -95), 320, 25, 40, 5, 'orange')  # 绘制第一层蛋糕侧边曲线2
draw_wave((-160, -90), 320, 30, 40, 5, 'yellow')  # 绘制第一层蛋糕侧边曲线3
draw_cylinder((0, 0), 240, 90, 80, True, 'pink', 'white')  # 绘制第二层蛋糕
draw_wave((-120, -5), 240, 30, 40, 5, 'green')  # 绘制第二层蛋糕侧边曲线1
draw_wave((-120, 0), 240, 30, 40, 5, 'blue')  # 绘制第二层蛋糕侧边曲线2
draw_wave((-120, 5), 240, 30, 40, 5, 'cyan')  # 绘制第二层蛋糕侧边曲线3
draw_wave((-120, 10), 240, 30, 40, 5, 'purple')  # 绘制第二层蛋糕侧边曲线4
# 绘制蜡烛
for x_ in range(-90, 91, 30):
    draw_candle((x_, 100), 10, 80)
for x_ in range(-100, 100, 28):
    draw_candle((x_, 80), 10, 80)
for x_ in range(-90, 91, 30):
    draw_candle((x_, 60), 10, 80)
turtle.penup()
draw_love((-200, 100), 50)
draw_love((200, 100), 50)
turtle.color('#F1ADD1')
turtle.goto(-200, 200)
turtle.write("祝博主生日快乐~", font=('Curlz MT', 40, 'bold italic'))
turtle.goto(-150, -270)
turtle.write("码龄 + 1 ~", font=('Curlz MT', 50, 'bold italic'))
turtle.done()

结束语

本篇主要展示实现成果,后续将逐一从基础元素和众多案例示范教学Python绘图库turtle的玩法~
关注专栏不迷路,如果有什么画图的案例要贡献,可以私信评论哦~

用Python写一个生日蛋糕 相信大家对生日蛋糕不陌生吧! 今天给大家分享一下用代码写个生日蛋糕出来~ 引入需要的模块 import turtle as t import math as m import random as r def drawX(a, i): angle = m.radians(i) return a * m.cos(angle) def drawY(b, i): angle = m.radians(i) return b * m.sin(angle) 设置背景颜色,窗口 阅读详情

相关推荐

COMSOL网格划分技巧总结

在对马赫曾德尔型光学传感器进行模拟仿真时,在采用自动物理场自动控制网格-极细化的情况下透射谱曲线仍不理想,因此需要手动划分网格来对其进行更精细的剖分。 手动划分的基本原则是在作用边界,热点,曲边处做细致划分, 基底,包层等做粗略划分。对于光波导型的微纳结构,在仿真时一般需要用到创建边界层网格。 1.COMSOL所需要的网格数量 网格单元数越多,结果精细度越高,计算所需内存越多。在细化到一定程度后网格收敛,仿真结果提升不明显。 2.网格剖分类型 ...

qq_42453337的博客 4万+

你能用C语言写个代码展示出一个蛋糕的图案吗,并且蛋糕上要有颜色

可以的,下面是一个使用 C 语言绘制蛋糕的示例代码: #include <stdio.h> #include <graphics.h> int main() { int gd = DETECT, gm; initgraph(&amp;gd, &amp;gm, ""); // 绘制蛋糕的底部 setcolor(YELLOW)...

weixin_35752122的博客 6691

通达信双紫擒龙指标实战指南:从源码解析到精准安装

本文详细解析了通达信双紫擒龙指标的核心逻辑、源码获取与安全验证、安装流程及实战应用技巧。通过紫紫红黄四种颜色信号捕捉趋势转折点,结合MACD、布林通道和成交量加权算法,提升交易准确性。文章还提供了个性化设置、信号解读及常见问题解决方案,帮助投资者高效运用该指标。

weixin_29248611的博客 1045

用Python制作生日蛋糕、生日快乐,生日祝福代码

如果你也喜欢编程,想通过学习Python转行、做副业或者提升工作效率,我也为大家整理了一份【最新全套Python学习资料】一定对你有用!如果你是零基础小白,想快速入门Python是可以考虑的!1、学习时间相对较短,学习内容更全面更集中2、可以找到适合自己的学习方案这份资料包含:Python安装包+激活码、Python web开发,Python爬虫,Python数据分析,人工智能、机器学习等教程,带你从零开始系统性的学好Python!最新全套【Python入门到进阶资料 & 实战源码 & 安装工具】

python03011的博客 1万+

基于Lumerical的1550 nm InGaAs/InP与InGaAs/InAlAs APD设计与功率饱和度优化

摘要 本研究利用Ansys Lumerical软件对1550 nm激光雷达应用的InGaAs/InP和InGaAs/InAlAs雪崩光电二极管(APD)进行仿真设计与优化。通过FDTD光学仿真获取光吸收分布,结合CHARGE模块的电学仿真分析暗电流、光电流及雪崩增益特性。重点研究了两种材料体系的性能差异,并针对近距离强反射导致的功率饱和问题提出结构优化方案。仿真结果表明,InGaAs/InAlAs APD具有更优的过量噪声特性,而优化的电荷层设计可显著提升器件的功率饱和阈值。研究提供了完整的仿真流程与代码实

huanghm88的专栏 95

视频理解与处理——OpenClaw视频能力深度解析(2026视频版)

本文介绍了OpenClaw视频理解与处理的核心技术,重点分析了视频预处理流程。通过VideoProcessor类实现视频加载、帧采样、尺寸调整和归一化等基础处理功能,支持本地和在线视频源处理,为后续视频分析任务提供标准化输入。预处理模块采用均匀采样和标准化方法优化视频数据,确保动作识别、场景分析等高级应用的准确性,展现了OpenClaw强大的视频处理能力。

一键难忘的博客 464

Python生日蛋糕

Python生日蛋糕代码。

AIGC 6万+

【Simulink教程案例9】基于simulink的BPSK,QPSK调制解调通信系统建模与仿真——使用simulink自带模块设计

BPSK (Binary Phase Shift Keying)-------二进制相移键控。是把模拟信号转换成数据值的转换方式之一,利用偏离相位的复数波浪组合来表现信息键控移相方式。BPSK使用了基准的正弦波和相位反转的波浪,使一方为0,另一方为1,从而可以同时传送接受2值(1比特)的信息。BPSK的映射关系如下所示:BPSK调制过程将输入的1010二进制数据中的0都转化为-1,使得非极性码变为极性码。

FPGA/MATLAB学习教程/源码/项目合作开发 3483

C语言给朋友写生日祝福(输出一个生日蛋糕形状)

【代码】C语言给朋友写生日祝福(输出一个生日蛋糕形状)

Q2918402071的博客 2万+

【2026最新版】python点云处理算法汇总(长期更新版)

python点云处理算法整理。博客长期更新,最近一次更新时间为:2026年10月4日。

点云侠的博客 42万+

从0开始图形学(光栅化)

从0开始图形学

A112459的专栏 2395

程序员成神之路,一年挖坑,五年扛旗,十年成神

自人类社会诞生以来,等级就一直存在,有人指点江山,称之为“大神” 有人卸瓦搬砖,称之为“小白” 在程序员的世界里,等级同样森严。特别是在1年、5年、10年时会有大不同 据说到达上面每一个级别,就会脱胎换骨一次 我们一起来看看。 每个人都做过小白,但不是人人都能成为大神 一年到十年,磨的不是时间而是汗水和经验 小白们,莫急莫燥且听我一言所谓“十年磨一剑”。听内心之声音,找前进之方向按自己之快慢! 关于Python技术储备 学好 Python 不论是就业还是做副业赚钱都不错,但要学会 P

Javachichi的博客 3300

Python 爬虫实战:豆瓣电影海报批量获取(Top250 榜单 + 可视化展示)

在信息爆炸的时代,数据已成为决策的核心,而数据的获取是数据分析的第一步。本文将详细介绍如何利用 Python 爬虫获取豆瓣电影 Top250 榜单的海报,并对数据进行可视化展示,助你更好地了解电影市场的趋势和用户的喜好。

yansideyucsdn的博客 717

【Python绘图】学习绘图第一步,先要知道哪些操作是常用的

Turtle常用方法一览表

user_from_future的博客 1509

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

【Python绘图】绘图常见的构成元素2

用python绘制常见的构成元素2

user_from_future的博客 1062

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

【Python绘图】绘图常见的构成元素3

用python绘制常见的构成元素3

user_from_future的博客 1717

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

【Python绘图】绘图常见的构成元素1

用python绘制常见的构成元素1

user_from_future的博客 1072

【Python绘图】绘制Flag

使用Python绘制Flag

user_from_future的博客 972

【Python绘图】绘制Mascot

使用Python绘制Mascot

user_from_future的博客 729

专栏博文目录

专栏博文目录

qq_39603829的博客 870

HTML5七夕情人节表白网页制作 __浪漫空中散落的花瓣3D相册__ HTML+CSS+JavaScript

HTML5七夕情人节表白网页制作 ❤浪漫空中散落的花瓣3D相册❤ HTML+CSS+JavaScript 这是程序员表白系列中的100款网站表白之一,旨在让任何人都能使用并创建自己的表白网站给心爱的人看。 此波共有100个表白网站,可以任意修改和使用,源码已上传,演示网址如下。 ????文章末尾-已经附上源码下载地址 ????作者主页-更多源码 ????七夕情人节专栏文章 作品介绍 1.网页作品简介 :基于 HTML+CSS+JavaScript 制作七夕情人节表白网页, 生日祝福, 七夕告白, 求婚,

HTML5学生网页设计作业成品模板 6044

【C语言】生日快乐代码!!!

【C语言】生日快乐代码!!!

zore__的博客 906

小明过生日,输出大小为N的生日蛋糕

这里写自定义目录标题1.题目2.分析方法1:外层从大到小方法2:外层从小到大3.总结4.更新日志 1.题目 题目链接 题目描述 小明快要过生日了,他希望编程输出大小为N的生日蛋糕。 输入描述: 本题包含多组数据,每组数据输入一个数字N。 1<=N<=10 输出描述: 输出对应大小的图形。 注意每行图形末尾都没有多余空格。 示例1 输入 1 2 3 4 输出 2.分析 方法1:外层从大到小 #include <stdio.h> int main() { int i,j

陕西师范大学 人工智能 周一卓 的博客 4154

CSS中的动画效果

每一帧图片连接到一块就组成了一个动画。小熊奔跑,背景及小熊。

2401_83641491的博客 773

python如何写生日快乐说说_自己的生日怎么写说说

一、亲爱的自己,今天是你的生日,生日快乐,永远不要为难自己,比如不吃饭哭泣自闭抑郁,这些都是傻瓜才做的事。二、噢耶,祝自己生日爆爆乐。三、你是我的姐妹,你是我的宝贝,不管相隔多远,我都一直珍惜这份感情。记得你的好,记得你的可爱,记得你的任性,记得我们在一起时最美好的时光!四、天空的雨不下了,眼眶的泪干涸了。带着轻飘飘的脚步游走,风吹过,带走了迷惘的身影,留下了一张依然无色的脸。五、看着橱窗那个漂亮...

weixin_39630744的博客 350
上一篇: 【Python实践】你可能没有见过的码代码小技巧1
下一篇: 【Python绘图】学习绘图第一步,先要知道哪些操作是常用的
user_from_future
博客等级 码龄5年 4066粉丝 115原创
评论 12
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值