三个 Python 爱心表白代码示例
本文提供了三个使用 Python turtle 模块绘制的爱心图形代码示例。第一个示例绘制紫色爱心并支持自定义文字;第二个示例利用三角函数公式绘制数学风格爱心及 Love 字样;第三个示例结合卡通元素绘制包含表情、腮红和书本的场景。文章详细解析了各段代码的逻辑原理,包括坐标系统、画笔控制、极坐标转换及模块化设计,并补充了环境准备、运行步骤及常见问题解答,帮助读者完整掌握图形编程基础。

本文提供了三个使用 Python turtle 模块绘制的爱心图形代码示例。第一个示例绘制紫色爱心并支持自定义文字;第二个示例利用三角函数公式绘制数学风格爱心及 Love 字样;第三个示例结合卡通元素绘制包含表情、腮红和书本的场景。文章详细解析了各段代码的逻辑原理,包括坐标系统、画笔控制、极坐标转换及模块化设计,并补充了环境准备、运行步骤及常见问题解答,帮助读者完整掌握图形编程基础。

本文介绍了使用 Python 的 turtle 模块绘制三种不同风格爱心图形的代码实现。这些示例适合初学者学习图形编程,也可用于表达情感或作为简单的创意项目。
运行以下代码需要安装 Python 3.x 版本(推荐 3.6+)。turtle 是 Python 标准库的一部分,无需额外安装第三方包。
确保在终端中确认 Python 环境:
python --version
该示例通过参数化方程绘制心形轮廓,并填充粉色背景,支持自定义表白文字和签名。
import turtle
import time
# 画心形圆弧
def hart_arc():
for i in range(200):
turtle.right(1)
turtle.forward(2)
# 移动画笔位置
def move_pen_position(x, y):
turtle.hideturtle()
turtle.up()
turtle.goto(x, y)
turtle.down()
turtle.showturtle()
# 配置表白内容
love = "听闻小姐治家有方,鄙人余生愿闻其详?"
signature = "先生"
date = "2024-05-20"
if not love:
love = 'I Love You'
# 初始化画布
turtle.setup(width=800, height=500)
turtle.color('black', 'Pink')
turtle.pensize(5)
turtle.speed(100)
move_pen_position(x=0, y=-180)
turtle.left(140)
turtle.begin_fill()
# 绘制左侧圆弧
turtle.forward(224)
hart_arc()
# 调整角度绘制右侧圆弧
turtle.left(120)
hart_arc()
# 绘制底部直线
turtle.forward(224)
turtle.end_fill()
# 添加装饰性圆弧
move_pen_position(x=70, y=160)
turtle.left(185)
turtle.circle(-110, 185)
turtle.forward(50)
# 绘制底部横线
move_pen_position(x=-180, y=-180)
turtle.left(180)
turtle.forward(600)
# 写入表白语
move_pen_position(0, 50)
turtle.hideturtle()
turtle.color('#CD5C5C', 'pink')
turtle.write(love, font=('Arial', 20, 'bold'), align="center")
# 写入署名和日期
if signature and date:
turtle.color('red', 'pink')
time.sleep(2)
move_pen_position(220, -180)
turtle.hideturtle()
turtle.write(signature, font=('Arial', 20), align="center")
move_pen_position(220, -220)
turtle.hideturtle()
turtle.write(date, font=('Arial', 20), align="center")
# 点击窗口关闭程序
turtle.Screen().exitonclick()
turtle 默认以屏幕中心为 (0,0),向右为 X 轴正方向,向上为 Y 轴正方向。up() 提起画笔不绘图,down() 落下画笔开始绘图。forward 和 right 的组合模拟心形上半部分的曲线,配合 begin_fill 和 end_fill 进行区域填充。本例利用三角函数计算心形极坐标方程,绘制更精确的心形图案,并组合成 "Love" 字样。
import turtle as t
import math as mt
if __name__ == "__main__":
t.screensize(800, 600, 'white')
t.pensize(10)
t.speed(10)
# 绘制第一个心形
t.color('black', 'pink')
t.begin_fill()
for i in range(-90, 90, 5):
x = mt.cos(mt.radians(i))
y = float(pow(mt.cos(mt.radians(i)), 2/3)) + float(mt.sin(mt.radians(i)))
t.penup()
t.goto(int(x * 50) + 50, int(y * 50) + 30)
t.pendown()
t.forward(1)
t.penup()
t.goto(-int(x * 50) + 50, int(y * 50) + 30)
t.pendown()
t.forward(1)
t.penup()
t.end_fill()
# 绘制第二个心形
t.goto(0, 10)
t.penup()
t.begin_fill()
for i in range(0, 360, 5):
r = 60 * (1 - mt.sin(mt.radians(i)))
t.penup()
t.left(5)
t.forward(r)
t.pendown()
t.forward(1)
t.penup()
t.backward(r + 1)
t.pendown()
t.end_fill()
# 绘制字母 L
t.penup()
t.goto(-200, 0)
t.left(90)
t.begin_fill()
t.pendown()
t.forward(100)
t.right(90)
t.forward(20)
t.right(90)
t.forward(80)
t.left(90)
t.forward(40)
t.right(90)
t.forward(20)
t.right(90)
t.forward(60)
t.end_fill()
# 绘制字母 o
t.penup()
t.goto(-80, 0)
t.pendown()
t.begin_fill()
t.circle(-50)
t.end_fill()
t.penup()
t.color('pink', 'black')
t.begin_fill()
t.goto(-80, 20)
t.pendown()
t.circle(-30)
t.end_fill()
t.color('black', 'pink')
# 绘制字母 E
t.penup()
t.goto(120, 0)
t.right(180)
t.left(90)
t.begin_fill()
t.pendown()
t.forward(100)
t.right(90)
t.forward(60)
t.right(90)
t.forward(20)
t.right(90)
t.forward(40)
t.left(90)
t.forward(20)
t.left(90)
t.forward(40)
t.right(90)
t.forward(20)
t.right(90)
t.forward(40)
t.left(90)
t.forward(20)
t.left(90)
t.forward(40)
t.right(90)
t.forward(20)
t.right(90)
t.forward(60)
t.end_fill()
t.mainloop()
range 遍历角度,计算对应的笛卡尔坐标 (x, y) 并移动画笔。forward, left, right) 手动构建,展示了基础几何绘图逻辑。此示例绘制了一个包含脸部表情、腮红、书本和爱心元素的卡通场景,更具趣味性。
import turtle as t
def face(x, y):
t.setheading(-90)
t.penup()
t.goto(x, y)
t.pendown()
t.backward(15)
t.circle(70, -80)
t.setheading(80)
t.circle(-150, 15)
t.circle(-15, 180)
t.setheading(-115)
t.circle(-150, 13)
t.setheading(10)
t.circle(-100, 10)
t.setheading(70)
t.circle(-150, 20)
t.circle(-15, 180)
t.circle(-150, 16)
t.setheading(10)
t.setheading(160)
t.circle(60, -130)
t.setheading(-75)
t.forward(40)
def word(x, y):
t.pensize(2)
t.pencolor("black")
t.setheading(0)
t.penup()
t.goto(x, y)
t.pendown()
# 此处省略具体笔画细节,实际运行时将显示'如何骗人'字样
t.forward(10)
t.penup()
t.setheading(90)
t.forward(8)
t.pendown()
t.setheading(-120)
t.forward(15)
t.setheading(-45)
t.forward(12)
t.penup()
t.setheading(80)
t.forward(15)
t.pendown()
t.setheading(-125)
t.forward(16)
t.penup()
t.setheading(42)
t.forward(16)
t.pendown()
t.setheading(-90)
t.forward(10)
t.penup()
t.backward(11)
t.pendown()
t.setheading(0)
t.forward(8)
t.setheading(-90)
t.forward(10)
t.penup()
t.setheading(180)
t.forward(8)
t.pendown()
t.setheading(0)
t.forward(8)
t.penup()
t.goto(x + 7, y - 18)
t.pendown()
t.setheading(-135)
t.forward(13)
t.penup()
t.goto(x + 5, y - 20)
t.pendown()
t.setheading(-90)
t.forward(16)
t.penup()
t.goto(x + 11, y - 18)
t.pendown()
t.setheading(0)
t.forward(13)
t.penup()
t.goto(x + 12, y - 22)
t.pendown()
t.setheading(-90)
t.forward(8)
t.penup()
t.goto(x + 12, y - 22)
t.pendown()
t.setheading(0)
t.forward(6)
t.setheading(-90)
t.forward(8)
t.penup()
t.goto(x + 11, y - 31)
t.pendown()
t.setheading(0)
t.forward(6)
t.penup()
t.goto(x + 21, y - 19)
t.pendown()
t.setheading(-90)
t.forward(18)
t.setheading(145)
t.forward(5)
t.penup()
t.goto(x + 40, y + 3)
t.pendown()
t.setheading(0)
t.forward(10)
t.setheading(-90)
t.forward(7)
t.penup()
t.goto(x + 45, y + 3)
t.pendown()
t.setheading(-90)
t.forward(10)
t.setheading(0)
t.forward(7)
t.setheading(-100)
t.forward(10)
t.setheading(145)
t.forward(4)
t.penup()
t.goto(x + 38, y - 12)
t.pendown()
t.setheading(0)
t.forward(11)
t.penup()
t.goto(x + 57, y + 9)
t.pendown()
t.setheading(-45)
t.forward(4)
t.penup()
t.goto(x + 54, y + 3)
t.pendown()
t.setheading(0)
t.forward(13)
t.setheading(-90)
t.forward(5)
t.setheading(180)
t.forward(12)
t.penup()
t.goto(x + 54, y + 3)
t.pendown()
t.setheading(90)
t.circle(90, -10)
t.penup()
t.goto(x + 56, y - 5)
t.pendown()
t.setheading(-90)
t.forward(11)
t.penup()
t.goto(x + 56, y - 5)
t.pendown()
t.setheading(0)
t.forward(13)
t.setheading(-90)
t.forward(12)
t.setheading(145)
t.forward(4)
t.penup()
t.goto(x + 56, y - 10)
t.pendown()
t.setheading(0)
t.forward(13)
t.penup()
t.goto(x + 56, y - 10)
t.pendown()
t.setheading(0)
t.forward(13)
t.penup()
t.goto(x + 60, y - 4)
t.pendown()
t.setheading(-90)
t.forward(10)
t.penup()
t.goto(x + 64, y - 4)
t.pendown()
t.setheading(-90)
t.forward(10)
t.penup()
t.goto(x + 60, y - 19)
t.pendown()
t.setheading(70)
t.circle(50, -30)
t.penup()
t.goto(x + 56, y - 27)
t.pendown()
t.setheading(130)
t.circle(-50, -20)
def book(x, y):
t.setheading(-90)
t.penup()
t.goto(x, y)
t.fillcolor("red")
t.begin_fill()
t.pendown()
t.forward(60)
t.setheading(0)
t.circle(-100, 25)
t.setheading(90)
t.forward(59)
t.setheading(-25)
t.circle(-100, -25)
t.penup()
t.setheading(-14)
t.forward(46)
t.pendown()
t.setheading(15)
t.circle(-100, 25)
t.setheading(-90)
t.forward(58)
t.setheading(-11)
t.circle(-105, -25)
t.end_fill()
def eyes(x, y):
t.setheading(-20)
t.penup()
t.goto(x, y)
t.pendown()
t.forward(10)
t.setheading(0)
t.penup()
t.forward(10)
t.pendown()
t.setheading(20)
t.forward(10)
t.setheading(-160)
t.penup()
t.forward(50)
t.pendown()
t.setheading(0)
t.forward(15)
t.penup()
t.forward(25)
t.pendown()
t.forward(18)
t.setheading(-145)
t.penup()
t.forward(45)
t.pendown()
t.setheading(0)
t.forward(10)
def cheek(x, y):
t.setheading(0)
for i in range(1, 4):
t.color("pink")
t.pensize(4)
t.penup()
t.right(110)
t.goto(x, y)
t.pendown()
t.forward(9)
t.left(110)
x += 9
t.pencolor("black")
def hands(x, y):
t.penup()
t.goto(x, y)
t.pendown()
t.fillcolor("white")
t.begin_fill()
t.circle(10)
t.end_fill()
t.penup()
t.setheading(5)
t.forward(120)
t.pendown()
t.fillcolor("white")
t.begin_fill()
t.setheading(-165)
t.forward(35)
t.circle(10, 180)
t.forward(15)
t.end_fill()
def heart(x, y, z):
t.setheading(110)
t.pensize(2)
t.pencolor("black")
t.penup()
t.goto(x, y)
t.pendown()
t.fillcolor("red")
t.begin_fill()
t.circle(50, 180)
t.circle(180, 37)
t.left(46)
t.circle(180, 37)
t.circle(50, 182)
t.end_fill()
def main():
t.pensize(0)
t.speed(6)
face(-95, 55)
eyes(-45, 110)
cheek(-80, 80)
cheek(0, 80)
book(-110, 55)
hands(-110, 5)
word(-100, 35)
heart(150, 0, 1)
t.hideturtle()
t.exitonclick()
if __name__ == "__main__":
main()
goto 和 circle 组合完成复杂形状,体现了对画笔轨迹的精细控制。fillcolor 设置填充色,pencolor 设置线条色,增强视觉效果。.py 文件(例如 heart1.py, heart2.py, heart3.py)。python heart1.py。font 参数为英文字体。turtle.done() 替代 exitonclick()。turtle.speed(1) 降低绘制速度以便观察过程。以上代码均基于标准 Python 语法编写,无外部依赖,可直接复制运行。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online