python爱心代码集锦(18款)

        本文目录

一、代码效果预览索引图

二、爱心源代码集锦

1)、爱心图形1(弧线型)(显示的文字写在代码里)

(2)、爱心图形2(直线型)(显示的文字写在代码里)

(3)、爱心图形3(弧线型)(通过输入方式显示文字)

(4)、用python绘制爱心树

(5)、粒子系统组成爱心,中间加字

(6)、粒子系统组成爱心

(7)、粒子系统组成爱心

(8)、最先复现“李峋爱心”代码的作者

(9)、跳动爱心

(10)、空心爱心

11)、空心立体爱心

(12)、李峋同款爱心代码【有声版】

 三、python字符画爱心 

(1)、一行代码画爱心

(2)、用字符输出 I 爱 U (2种形式)

(3)、输出五个爱心,由Dear I love you forever! 五个单词填充而成

(4)、字符输出爱心曲线公式爱心1

(5)、字符输出爱心曲线公式爱心2


  这篇文章python 绘制爱心动画源码集锦合集,每篇都附有原作者名字博客链接使用时将代码复制后在相关编程语言环境运行

  代码仅供学习参考,请尊重原作者创作和版权!

  如果代码作者转载地址有错,请原作者留言,我会更正相应的代码作者博客链接。

一、代码效果预览索引

  本文收集python绘制的爱心动画代码,主要涉及的模块turtletkintermath等。代码涉及的库,学习研究运行前需确保代码需要的库函数安装

11、空心立体爱心 12、李峋同款爱心代码【有声版】

8、最先复现李峋爱心代码的作者 9、跳动爱心 10、爱心

5、粒子组成爱心,中间加字

6、粒子组成爱心

7、粒子组成爱心

1、(弧线型)(显示文字

2、(直线型)(显示文字

3、(弧线型)(输入显示文字)

4、爱心树

python字符画爱心

部分的代码会显示在终端,如果你没有打开终端会看不到效果。如果你没有专业的代码编辑器,只想试试效果,运行方式

1、安装python配置环境

2、将代码完整复制保存成:你的文件名.py

3、同时按window+R键,输入cmd,打开命令提示窗口

输入python 你的文件名.py

(1)一行代码画爱心 (2)用字符输出 I 爱 U (2种形式) (2)用字符输出 I 爱 U (2种形式)

(3)输出五个爱心(Dear I love you forever! )五个单词填充而成 (4)爱心 (5)爱心

二、爱心源代码集锦

如果你没有专业的代码编辑器,只想试试效果,运行方式

1、安装python配置环境

2、将代码完整复制保存成:你的文件名.py

3、同时按window+R键,输入cmd,打开命令提示窗口

输入python 你的文件名.py(注:python后面有空格

 (1)、爱心图形1(弧线型)(显示的文字写在代码里)

转载地址https://blog.csdn.net/qq_55342245/article/details/120904578

import turtle
import time
def LittleHeart():
    for i in range(200):
        turtle.right(1)
        turtle.forward(2)

# love = input('请输入表白语句然后回车,默认为"I Love You":n')
# me = input('请输入要表白的人:n')
# if love=='':
# # 如果未输入表白语句,则使用默认语句
#     love='I Love you'

love='I Love you'
me = '卧槽,这年轻人。'

turtle.setup(width=900,height=600)  # 爱心的画布大小
turtle.color('red','red')          # 爱心的颜色外边笔的颜色
turtle.pensize(5)                   # 画笔粗细
turtle.speed(1000000)               # 绘制速度

turtle.up()                         # 画笔向上

turtle.hideturtle()
turtle.goto(0,-180)
turtle.showturtle()
turtle.down()
turtle.speed(5)
turtle.begin_fill()

# 开始填充
turtle.left(140)
turtle.forward(224)
LittleHeart()
turtle.left(120)
LittleHeart()
turtle.forward(224)
turtle.end_fill()
turtle.pensize(5)
turtle.up()
turtle.hideturtle()
turtle.goto(0,0)
turtle.showturtle()
turtle.color('#CD5C5C','pink')
turtle.write(love,font=('gungsuh',30,),align="center")
turtle.up()
turtle.hideturtle()

if me !='':
    turtle.color('black', 'pink')
    time.sleep(2)
    turtle.goto(180,-180)
    turtle.showturtle()
    turtle.write(me, font=(20,), align="center", move=True)
    window=turtle.Screen()
    window.exitonclick()

 

(2)、爱心图形2(直线型)(显示的文字写在代码里)

转载地址:https://blog.csdn.net/qq_55342245/article/details/120904578

import turtle
import math
turtle.pen()
t=turtle
t.up()
t.goto(0,150)
t.down()
t.color('red')
t.begin_fill()
t.fillcolor('red')
t.speed(1)
t.left(45)
t.forward(150)
t.right(45)
t.forward(100)
t.right(45)
t.forward(100)
t.right(45)
t.forward(100)
t.right(45)
t.forward(250+math.sqrt(2)*100)
t.right (90)
t.speed(2)
t.forward(250+100*math.sqrt(2))
t.right(45)
t.forward(100)
t.right(45)
t.forward(100)
t.right(45)
t.forward(100)
t.right(45)
t.forward(150)
t.end_fill()
t.goto(-10,0)
t.pencolor('white')
# L
t.pensize(10)
t.goto(-50,0)
t.goto(-50,80)
t.up ()
# I
t.goto(-100,0)
t.down()
t.goto(-160,0)
t.goto(-130,0)
t.goto(-130,80)
t.goto(-160,80)
t.goto(-100,80)
t.up()
# O
t.goto(10,25)
t.down()
t.right(45)
t.circle(25,extent=180)
t.goto(60,55)
t.circle(25,extent=180)
t.goto(10,25)
t.up()
t.goto(75,80)
t.down()
t.goto(100,0)
t.goto(125,80)
t.up()
t.goto(180,80)
t.down()
t.goto(140,80)
t.goto(140,0)
t.goto(180,0)
t.up()
t.goto(180,40)
t.down()
t.goto(140,40)
# U
t.up()
t.goto(-40,-30)
t.down()
t.goto(-40,-80)
t.circle(40,extent=180)
t.goto(40,-30)
t.hideturtle()

window=turtle.Screen()
window.exitonclick()

(3)、爱心图形3(弧线型)(通过输入方式显示文字)

请输入表白语句然后回车,(例如“I Love You”):

请输入要表白的人(例如”李思思”):

转载地址:https://changxin.blog.csdn.net/article/details/109842795 

# coding: utf-8 
import turtle
import time
def LittleHeart():
    for i in range(200):
        turtle.right(1)
        turtle.forward(2)
love=input('请输入表白语句然后回车,(例如"I Love You"):n')
me=input('请输入要表白的人(例如"李思思"):n')
if love=='':                    
    love='I Love you'
turtle.setup(width=900,height=600)
turtle.color('red','pink')
turtle.pensize(15)
turtle.speed(1000)

turtle.up()

turtle.hideturtle()
turtle.goto(0,-180)
turtle.showturtle()
turtle.down()
turtle.speed(500)
turtle.begin_fill()
turtle.left(140)
turtle.forward(224)
LittleHeart()
turtle.left(120)
LittleHeart()
turtle.forward(224)
turtle.end_fill()
turtle.pensize(12)
turtle.up()
turtle.hideturtle()
turtle.goto(0,-20)
turtle.showturtle()
turtle.color('#CD5C5C','pink')
turtle.write(love,font=('gungsuh',50,),align="center")
turtle.up()
turtle.hideturtle()
if me !='':
    turtle.color('black', 'pink')
    time.sleep(1)
turtle.goto(180,-180)
turtle.showturtle()
turtle.write(me, font=(20,25), align="center", move=True)
window=turtle.Screen()
window.exitonclick()

(4)、用python绘制爱心树

转载地址:https://blog.csdn.net/qq_55342245/article/details/120904578

import turtle
import random

def love(x, y):         # 在(x,y)处画爱心lalala
    lv = turtle.Turtle()
    lv.hideturtle()
    lv.up()
    lv.goto(x, y)       # 定位到(x,y)

    def curvemove():    # 画圆弧
        for i in range(20):
            lv.right(10)
            lv.forward(2)

    lv.color('red', 'pink')
    lv.speed(10000000)
    lv.pensize(1)
    # 开始画爱心lalala
    lv.down()
    lv.begin_fill()
    lv.left(140)
    lv.forward(22)
    curvemove()
    lv.left(120)
    curvemove()
    lv.forward(22)
    lv.write("YZ", font=("Arial", 12, "normal"), align="center")  # 写上表白的人的名字
    lv.left(140)  # 画完复位
    lv.end_fill()


def tree(branchLen, t):
    if branchLen > 5:       # 剩余树枝太少要结束递归
        if branchLen < 20:  # 如果树枝剩余长度较短则变绿
            t.color("green")
            t.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))
            t.down()
            t.forward(branchLen)
            love(t.xcor(), t.ycor())  # 传输现在turtle的坐标
            t.up()
            t.backward(branchLen)
            t.color("brown")
            return
        t.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))
        t.down()
        t.forward(branchLen)
        # 以下递归
        ang = random.uniform(15, 45)
        t.right(ang)
        tree(branchLen - random.uniform(12, 16), t)  # 随机决定减小长度
        t.left(2 * ang)
        tree(branchLen - random.uniform(12, 16), t)  # 随机决定减小长度
        t.right(ang)
        t.up()
        t.backward(branchLen)

myWin = turtle.Screen()
t = turtle.Turtle()
t.hideturtle()
t.speed(1000)
t.left(90)
t.up()
t.backward(200)
t.down()
t.color("brown")
t.pensize(32)
t.forward(60)
tree(100, t)
myWin.exitonclick()

(5)、粒子系统组成爱心,中间加字

注释

调参改变下面这里的代码 text=“”位置 改成自己想要的文字, 不想要可以直接去掉代码

text:想要表现的文本   fg:文字颜色   bg文字的背景颜色(其实有瑕疵的因为文字是更上一层的图层会有覆盖

    text1 = Label(root, text=”By SilverPriest ᏊˊꈊˋᏊ”, font=(“Helvetica“, 18), fg=”#c12bec“,bg = “black“)
    text1.place(x=650, y=500)
 
    text2 = Label(root, text=”爱你”,font = (“Helvetica“, 18), fg = “#c12bec” ,bg = “black”) #
    text2.place(x=460, y=350)

https://blog.csdn.net/CL_Young/article/details/127893960 

# Beating Heart
# default input
import random
from math import sin, cos, pi, log
from tkinter import *
 
CANVAS_WIDTH = 980  # frame_width
CANVAS_HEIGHT = 720  # frame_height
CANVAS_CENTER_X = CANVAS_WIDTH / 2  # frame_center_x
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2  # center_y
IMAGE_ENLARGE = 11  # ratio
# color list
HEART_COLOR_LIST = ["#d974ff", "#be77fa", "#a478f3", "#8b78ea", "#7377e0",
                    "#4871c6", "#5c74d3", "#fa6ea9", "#dc6db1", "#ec2c2c",
                    "#e91e41", "#8b4593", "#2bd3ec", "#00be93", "#2bec62"]
 
 
def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):
    """
    create a heart
    :param shrink_ratio: ratio
    :param t: parameter
    :return: x, y
    """
    # basic function, size
    x = 16 * (sin(t) ** 3)
    y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))
 
    # zoom
    x *= shrink_ratio
    y *= shrink_ratio
 
    # center
    x += CANVAS_CENTER_X
    y += CANVAS_CENTER_Y
 
    return int(x), int(y)
 
 
def scatter_inside(x, y, beta=1.15):
    """
    random inner spreading
    :param x: orig x
    :param y: orig y
    :param beta: strength
    :return: new x, y
    """
    ratio_x = - beta * log(random.random())
    ratio_y = - beta * log(random.random())
 
    dx = ratio_x * (x - CANVAS_CENTER_X)
    dy = ratio_y * (y - CANVAS_CENTER_Y)
 
    return x - dx, y - dy
 
 
def shrink(x, y, ratio):
    """
    shrink
    :param x: orig x
    :param y: orig y
    :param ratio: ratio
    :return: new x,y
    """
    force = -1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.6)  # 这个参数...
    dx = ratio * force * (x - CANVAS_CENTER_X)
    dy = ratio * force * (y - CANVAS_CENTER_Y)
    return x - dx, y - dy
 
 
def curve(p):
    """
    tune beating period
    :param p: parameter
    :return: sin
    """
    # alg
    return 2 * (2 * sin(4 * p)) / (2 * pi)
 
 
class Heart:
    def __init__(self, generate_frame=20):
        self._points = set()
        self._edge_diffusion_points = set()
        self._center_diffusion_points = set()
        self.all_points = {}
        self.build(2000)
        self.random_halo = 1000
        self.generate_frame = generate_frame
        for frame in range(generate_frame):
            self.calc(frame)
 
    def build(self, number):
        # heart
        for _ in range(number):
            t = random.uniform(0, 2 * pi)
            x, y = heart_function(t)
            self._points.add((x, y))
 
        # inner heart 1
        for _x, _y in list(self._points):
            for _ in range(3):
                x, y = scatter_inside(_x, _y, 0.05)
                self._edge_diffusion_points.add((x, y))
 
        # inner heart 2
        point_list = list(self._points)
        for _ in range(6000):
            x, y = random.choice(point_list)
            x, y = scatter_inside(x, y, 0.17)
            self._center_diffusion_points.add((x, y))
 
    @staticmethod
    def calc_position(x, y, ratio):
        # tune ratio
        force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.520)  # alg
 
        dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)
        dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)
 
        return x - dx, y - dy
 
    def calc(self, generate_frame):
        ratio = 10 * curve(generate_frame / 10 * pi)  # curve
 
        halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))
        halo_number = int(3000 + 6000 * abs(curve(generate_frame / 10 * pi) ** 2))
 
        all_points = []
 
        # ring
        heart_halo_point = set()  # x,y of ring pts
        for _ in range(halo_number):
            t = random.uniform(0, 2 * pi)
            x, y = heart_function(t, shrink_ratio=11.6)  # alg
            x, y = shrink(x, y, halo_radius)
            if (x, y) not in heart_halo_point:
                # new pts
                heart_halo_point.add((x, y))
                x += random.randint(-14, 14)
                y += random.randint(-14, 14)
                size = random.choice((1, 2, 2))
                all_points.append((x, y, size))
 
        # appearance
        for x, y in self._points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 3)
            all_points.append((x, y, size))
 
        # content
        for x, y in self._edge_diffusion_points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 2)
            all_points.append((x, y, size))
 
        for x, y in self._center_diffusion_points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 2)
            all_points.append((x, y, size))
 
        self.all_points[generate_frame] = all_points
 
    def render(self, render_canvas, render_frame):
        for x, y, size in self.all_points[render_frame % self.generate_frame]:
            render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=random.choice(HEART_COLOR_LIST))
 
 
def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):
    render_canvas.delete('all')
    render_heart.render(render_canvas, render_frame)
    main.after(70, draw, main, render_canvas, render_heart, render_frame + 1)
 
 
if __name__ == '__main__':
    root = Tk()  # Tk
    canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
    canvas.pack()
    heart = Heart()
    draw(root, canvas, heart)  # draw
    text1 = Label(root, text="By SilverPriest ᏊˊꈊˋᏊ", font=("Helvetica", 18), fg="#c12bec",bg = "black")
    text1.place(x=650, y=500)
 
    text2 = Label(root, text="爱你",font = ("Helvetica", 18), fg = "#c12bec" ,bg = "black") #
    text2.place(x=460, y=350)
 
    root.mainloop()

(6)、粒子系统组成爱心

https://blog.csdn.net/CL_Young/article/details/127808312

import random
from math import sin, cos, pi, log
from tkinter import *
 
CANVAS_WIDTH = 840  # 画布的宽
CANVAS_HEIGHT = 680  # 画布的高
CANVAS_CENTER_X = CANVAS_WIDTH / 2  # 画布中心的X轴坐标
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2  # 画布中心的Y轴坐标
IMAGE_ENLARGE = 11  # 放大比例
 
HEART_COLOR = "#EEAEEE"  #引号修改颜色!颜色代码放在文章末尾
 
def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):
    """
    “爱心函数生成器”
    :param shrink_ratio: 放大比例
    :param t: 参数
    :return: 坐标
    """
    # 基础函数
    x = 17 * (sin(t) ** 3)
    y = -(16 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(3 * t))
 
    # 放大
    #x *= shrink_ratio
    #y *= shrink_ratio
    x*=IMAGE_ENLARGE
    y*=IMAGE_ENLARGE
    # 移到画布中央
    x += CANVAS_CENTER_X
    y += CANVAS_CENTER_Y
 
    return int(x), int(y)
 
 
def scatter_inside(x, y, beta=0.15):
    """
    随机内部扩散
    :param x: 原x
    :param y: 原y
    :param beta: 强度
    :return: 新坐标
    """
    ratio_x = - beta * log(random.random())
    ratio_y = - beta * log(random.random())
 
    dx = ratio_x * (x - CANVAS_CENTER_X)
    dy = ratio_y * (y - CANVAS_CENTER_Y)
 
    return x - dx, y - dy
 
 
def shrink(x, y, ratio):
    """
    抖动
    :param x: 原x
    :param y: 原y
    :param ratio: 比例
    :return: 新坐标
    """
    force = -1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.6)  # 这个参数...
    dx = ratio * force * (x - CANVAS_CENTER_X)
    dy = ratio * force * (y - CANVAS_CENTER_Y)
    return x - dx, y - dy
 
 
def curve(p):
    """
    自定义曲线函数,调整跳动周期
    :param p: 参数
    :return: 正弦
    """
    # 可以尝试换其他的动态函数,达到更有力量的效果(贝塞尔?)
    return 2 * (2 * sin(4 * p)) / (2 * pi)
 
 
class Heart:
    """
    爱心类
    """
 
    def __init__(self, generate_frame=20):
        self._points = set()  # 原始爱心坐标集合
        self._edge_diffusion_points = set()  # 边缘扩散效果点坐标集合
        self._center_diffusion_points = set()  # 中心扩散效果点坐标集合
        self.all_points = {}  # 每帧动态点坐标
        self.build(2000)
 
        self.random_halo = 1000
 
        self.generate_frame = generate_frame
        for frame in range(generate_frame):
            self.calc(frame)
 
    def build(self, number):
        # 爱心
        for _ in range(number):
            t = random.uniform(0, 2 * pi)  # 随机不到的地方造成爱心有缺口
            x, y = heart_function(t)
            self._points.add((x, y))
 
        # 爱心内扩散
        for _x, _y in list(self._points):
            for _ in range(3):
                x, y = scatter_inside(_x, _y, 0.05)
                self._edge_diffusion_points.add((x, y))
 
        # 爱心内再次扩散
        point_list = list(self._points)
        for _ in range(10000):
            x, y = random.choice(point_list)
            x, y = scatter_inside(x, y, 0.27)
            self._center_diffusion_points.add((x, y))
 
    @staticmethod
    def calc_position(x, y, ratio):
        # 调整缩放比例
        force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.420)  # 魔法参数
 
        dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)
        dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)
 
        return x - dx, y - dy
 
    def calc(self, generate_frame):
        ratio = 15 * curve(generate_frame / 10 * pi)  # 圆滑的周期的缩放比例
 
        halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))
        halo_number = int(3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))
 
        all_points = []
 
        # 光环
        heart_halo_point = set()  # 光环的点坐标集合
        for _ in range(halo_number):
            t = random.uniform(0, 2 * pi)  # 随机不到的地方造成爱心有缺口
            x, y = heart_function(t, shrink_ratio=-15)  # 魔法参数
            x, y = shrink(x, y, halo_radius)
            if (x, y) not in heart_halo_point:
                # 处理新的点
                heart_halo_point.add((x, y))
                x += random.randint(-60, 60)
                y += random.randint(-60, 60)
                size = random.choice((1, 1, 2))
                all_points.append((x, y, size))
                all_points.append((x+20, y+20, size))
                all_points.append((x-20, y -20, size))
                all_points.append((x+20, y - 20, size))
                all_points.append((x - 20, y +20, size))
 
        # 轮廓
        for x, y in self._points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 3)
            all_points.append((x, y, size))
 
        # 内容
        for x, y in self._edge_diffusion_points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 2)
            all_points.append((x, y, size))
 
        for x, y in self._center_diffusion_points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 2)
            all_points.append((x, y, size))
 
        self.all_points[generate_frame] = all_points
 
    def render(self, render_canvas, render_frame):
        for x, y, size in self.all_points[render_frame % self.generate_frame]:
            render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=HEART_COLOR)
 
 
def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):
    render_canvas.delete('all')
    render_heart.render(render_canvas, render_frame)
    main.after(1, draw, main, render_canvas, render_heart, render_frame + 1)
 
 
if __name__ == '__main__':
    root = Tk()
    canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
    canvas.pack()
    heart = Heart()
    draw(root, canvas, heart)
    root.mainloop()

 (7)、粒子系统组成爱心

https://blog.csdn.net/CL_Young/article/details/1270831

import random
from math import sin, cos, pi, log
from tkinter import *
 
CANVAS_WIDTH = 640  # 画布的宽
CANVAS_HEIGHT = 480  # 画布的高
CANVAS_CENTER_X = CANVAS_WIDTH / 2  # 画布中心的X轴坐标
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2  # 画布中心的Y轴坐标
IMAGE_ENLARGE = 11  # 放大比例
HEART_COLOR = "pink"  # 心的颜色
 
 
def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):
    """
    “爱心函数生成器”
    :param shrink_ratio: 放大比例
    :param t: 参数
    :return: 坐标
    """
    # 基础函数
    x = 16 * (sin(t) ** 3)
    y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))
 
    # 放大
    x *= shrink_ratio
    y *= shrink_ratio
 
    # 移到画布中央
    x += CANVAS_CENTER_X
    y += CANVAS_CENTER_Y
 
    return int(x), int(y)
 
 
def scatter_inside(x, y, beta=0.15):
    """
    随机内部扩散
    :param x: 原x
    :param y: 原y
    :param beta: 强度
    :return: 新坐标
    """
    ratio_x = - beta * log(random.random())
    ratio_y = - beta * log(random.random())
 
    dx = ratio_x * (x - CANVAS_CENTER_X)
    dy = ratio_y * (y - CANVAS_CENTER_Y)
 
    return x - dx, y - dy
 
 
def shrink(x, y, ratio):
    """
    抖动
    :param x: 原x
    :param y: 原y
    :param ratio: 比例
    :return: 新坐标
    """
    force = -1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.6)  # 这个参数...
    dx = ratio * force * (x - CANVAS_CENTER_X)
    dy = ratio * force * (y - CANVAS_CENTER_Y)
    return x - dx, y - dy
 
 
def curve(p):
    """
    自定义曲线函数,调整跳动周期
    :param p: 参数
    :return: 正弦
    """
    return 4 * (2 * sin(4 * p)) / (2 * pi)
 
 
class Heart:
    """
    爱心类
    """
 
    def __init__(self, generate_frame=20):
        self._points = set()  # 原始爱心坐标集合
        self._edge_diffusion_points = set()  # 边缘扩散效果点坐标集合
        self._center_diffusion_points = set()  # 中心扩散效果点坐标集合
        self.all_points = {}  # 每帧动态点坐标
        self.build(2000)
 
        self.random_halo = 1000
 
        self.generate_frame = generate_frame
        for frame in range(generate_frame):
            self.calc(frame)
 
    def build(self, number):
        # 爱心
        for _ in range(number):
            t = random.uniform(0, 2 * pi)  # 随机不到的地方造成爱心有缺口
            x, y = heart_function(t)
            self._points.add((x, y))
 
        # 爱心内扩散
        for _x, _y in list(self._points):
            for _ in range(3):
                x, y = scatter_inside(_x, _y, 0.05)
                self._edge_diffusion_points.add((x, y))
 
        # 爱心内再次扩散
        point_list = list(self._points)
        for _ in range(4000):
            x, y = random.choice(point_list)
            x, y = scatter_inside(x, y, 0.17)
            self._center_diffusion_points.add((x, y))
 
    @staticmethod
    def calc_position(x, y, ratio):
        # 调整缩放比例
        force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.520)
 
        dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)
        dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)
 
        return x - dx, y - dy
 
    def calc(self, generate_frame):
        ratio = 10 * curve(generate_frame / 10 * pi)  # 圆滑的周期的缩放比例
 
        halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))
        halo_number = int(3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))
 
        all_points = []
 
        # 光环
        heart_halo_point = set()  # 光环的点坐标集合
        for _ in range(halo_number):
            t = random.uniform(0, 2 * pi)  # 随机不到的地方造成爱心有缺口
            x, y = heart_function(t, shrink_ratio=11)
            x, y = shrink(x, y, halo_radius)
            if (x, y) not in heart_halo_point:
                # 处理新的点
                heart_halo_point.add((x, y))
                x += random.randint(-11, 11)
                y += random.randint(-11, 11)
                size = random.choice((1, 2, 2))#控制外围粒子大小
                all_points.append((x, y, size))
 
        # 轮廓
        for x, y in self._points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 3)
            all_points.append((x, y, size))
 
        # 内容
        for x, y in self._center_diffusion_points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 2)
            all_points.append((x, y, size))
 
        self.all_points[generate_frame] = all_points
 
    def render(self, render_canvas, render_frame):
        for x, y, size in self.all_points[render_frame % self.generate_frame]:
            render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=HEART_COLOR)
 
 
def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):
    render_canvas.delete('all')
    render_heart.render(render_canvas, render_frame)
    main.after(160, draw, main, render_canvas, render_heart, render_frame + 1)
 
 
if __name__ == '__main__':
    root = Tk()  # 一个Tk
    canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
    canvas.pack()
    heart = Heart()  # 心
    draw(root, canvas, heart)  # 开始画画~
    root.mainloop()

 (8)、最先复现李峋爱心”代码的作者

电视剧 【点燃我,温暖你】让“李峋爱心”红起来,网上有很多转载的,原来的作者是谁,我一直找不到,刚刚才发现作者Littlesbai,“李峋爱心代码”发布博客时间是 2022.11.08,而且文中有对代码优化部分应该是最先复现李峋爱心”代码的作者。原创不易,请大家支持创作者。

转载地址:李峋爱心代码_Littlesbai的博客-CSDN博客

import random
from math import sin, cos, pi, log
from tkinter import *
CANVAS_WIDTH = 640
CANVAS_HEIGHT = 480
CANVAS_CENTER_X = CANVAS_WIDTH / 2
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2
IMAGE_ENLARGE = 11
HEART_COLOR = "#FF99CC"


def center_window(root, width, height):
    screenwidth = root.winfo_screenwidth()  # 获取显示屏宽度
    screenheight = root.winfo_screenheight()  # 获取显示屏高度
    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) /
                            2, (screenheight - height) / 2)  # 设置窗口居中参数
    root.geometry(size)  # 让窗口居中显示


def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):
    x = 16 * (sin(t) ** 3)
    y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))
    # 放大
    x *= shrink_ratio
    y *= shrink_ratio
    # 移到画布中央
    x += CANVAS_CENTER_X
    y += CANVAS_CENTER_Y
    return int(x), int(y)


def scatter_inside(x, y, beta=0.15):
    ratio_x = - beta * log(random.random())
    ratio_y = - beta * log(random.random())
    dx = ratio_x * (x - CANVAS_CENTER_X)
    dy = ratio_y * (y - CANVAS_CENTER_Y)
    return x - dx, y - dy


def shrink(x, y, ratio):
    force = -1 / (((x - CANVAS_CENTER_X) ** 2 +
                  (y - CANVAS_CENTER_Y) ** 2) ** 0.6)
    dx = ratio * force * (x - CANVAS_CENTER_X)
    dy = ratio * force * (y - CANVAS_CENTER_Y)
    return x - dx, y - dy


def curve(p):
    return 2 * (2 * sin(4 * p)) / (2 * pi)


class Heart:
    def __init__(self, generate_frame=20):
        self._points = set()  # 原始爱心坐标集合
        self._edge_diffusion_points = set()  # 边缘扩散效果点坐标集合
        self._center_diffusion_points = set()  # 中心扩散效果点坐标集合
        self.all_points = {}  # 每帧动态点坐标
        self.build(2000)
        self.random_halo = 1000
        self.generate_frame = generate_frame
        for frame in range(generate_frame):
            self.calc(frame)

    def build(self, number):
        for _ in range(number):
            t = random.uniform(0, 2 * pi)
            x, y = heart_function(t)
            self._points.add((x, y))
        # 爱心内扩散
        for _x, _y in list(self._points):
            for _ in range(3):
                x, y = scatter_inside(_x, _y, 0.05)
                self._edge_diffusion_points.add((x, y))
        # 爱心内再次扩散
        point_list = list(self._points)
        for _ in range(4000):
            x, y = random.choice(point_list)
            x, y = scatter_inside(x, y, 0.17)
            self._center_diffusion_points.add((x, y))

    @staticmethod
    def calc_position(x, y, ratio):
        force = 1 / (((x - CANVAS_CENTER_X) ** 2 +
                      (y - CANVAS_CENTER_Y) ** 2) ** 0.520)
        dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)
        dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)
        return x - dx, y - dy

    def calc(self, generate_frame):
        ratio = 10 * curve(generate_frame / 10 * pi)
        halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))
        halo_number = int(
            3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))
        all_points = []
        # 光环
        heart_halo_point = set()
        for _ in range(halo_number):
            t = random.uniform(0, 2 * pi)
            x, y = heart_function(t, shrink_ratio=11.6)
            x, y = shrink(x, y, halo_radius)
            if (x, y) not in heart_halo_point:
                heart_halo_point.add((x, y))
                x += random.randint(-14, 14)
                y += random.randint(-14, 14)
                size = random.choice((1, 2, 2))
                all_points.append((x, y, size))
        # 轮廓
        for x, y in self._points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 3)
            all_points.append((x, y, size))
        # 内容
        for x, y in self._edge_diffusion_points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 2)
            all_points.append((x, y, size))
        self.all_points[generate_frame] = all_points
        for x, y in self._center_diffusion_points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 2)
            all_points.append((x, y, size))
        self.all_points[generate_frame] = all_points

    def render(self, render_canvas, render_frame):
        for x, y, size in self.all_points[render_frame % self.generate_frame]:
            render_canvas.create_rectangle(
                x, y, x + size, y + size, width=0, fill=HEART_COLOR)


def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):
    render_canvas.delete('all')
    render_heart.render(render_canvas, render_frame)
    main.after(160, draw, main, render_canvas, render_heart, render_frame + 1)


if __name__ == '__main__':
    root = Tk()
    root.title("爱心")
    center_window(root, CANVAS_WIDTH, CANVAS_HEIGHT)  # 窗口居中显示
    canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
    canvas.pack()
    heart = Heart()
    draw(root, canvas, heart)
    Label(root, text="你想要替换的文字", bg="black", fg="#FF99CC", font="Helvetic 20 bold").place(
        relx=.5, rely=.5, anchor=CENTER)
    root.mainloop()


 (9)、跳动爱心

此代码需要用到import pgzrun安装方式不是pgzrun,而是pgzero,请留意
pip install pgzero

以下文字转自作者:
https://blog.csdn.net/qq_40523737/article/details/127967411
1、用pgzero 库。然后结合最后那个极坐标爱心曲线代码,先绘制出曲线上离散的点。
2. 把点的数量增加,同时沿着原点到每个点的径向加一个随机数,并且这个随机数是按照正态分布来的(半个正态分布),大概率分布在曲线上,向曲线内部递减。这样,就得到这样一个随机分布的爱心效果。
3. 下面就是让点动起来,将每个点自定义成了一个Particle类的实例。从原理上来说,就是给每个加一个缩放系数,这个系数是根据时间变化的正弦函数,看起来就会像呼吸的节律一样。
4. 剧中爱心跳动时,靠中间的点波动的幅度更大,有一种扩张的效果。所以再根据每个距离原点的远近,再加上一个系数,离得越近,系数越大。
5. 最后再用同样的方法画一个更大一点的爱心,这个爱心不需要跳动,只要每一帧随机绘制就可以了。
合在一起,搞定!
总结一下,就是在原本的基础爱心曲线上加上一个正态分布的随机量、一个随时间变化的正弦函数和一个跟距离成反比的系数,外面再套一层更大的随机爱心,就得到类似剧中的跳动爱心效果。

电视剧里的代码真能运行吗?_Crossin的编程教室的博客-CSDN博客


# 代码使用pygame-zero 框架,看起来与一般代码稍有不同,会有很多未定义方法变量,
# 在一些编辑器里会报错,但其实是可以运行的,无需手动增加 import。
# pgzero有两种方式运行(https://pygame-zero.readthedocs.io/zh_CN/latest/ide-mode.html)
# 本代码用的是第二种直接运行的方式(需新版pgzero)。
# 有部分读者反馈此代码在spyder上无法运行,类似情况可以尝试第一种传统方法:
# 把最后pgzrun.go()去掉然后直接在命令行目录下运行: pgzrun sheep.py

import pgzrun
from math import pi, sin, cos
import random

# 粒子类图像上每一个小点都是一个粒子对象
class Particle():
    def __init__(self, pos, size, f):
        self.pos = pos    # 粒子当前位置(后面会变动)
        self.pos0 = pos   # 粒子的原始位置
        self.size = size  # 粒子大小
        self.f = f        # 粒子的随机位移比例

    def draw(self):
        global L
        # 用矩形绘制粒子
        screen.draw.filled_rect(Rect((L*self.f*self.pos[0] + 400, -L*self.f*self.pos[1] + 300), self.size), 'hot pink')

    def update(self, t):
        # 根据程序运行时间计算一个正弦函数作为位移量
        # 如果要调整爱心跳动的频率、幅度等效果,可修改里面数字
        df = 1 + (4 - 3 * self.f) * sin(t * 3) / 12
        self.pos = self.pos0[0] * df, self.pos0[1] * df

tt = [105, 102, 98, 115, 117, 33, 112, 103, 33, 106, 108, 118, 111, 33, 46, 33, 68, 115, 112, 116, 116, 106, 111, 30341, 32535, 31244, 25946, 23461]
no_p = 20000
dt = 2*pi/no_p
particles = []
t = 0
c = 0
# 采用极坐标下的爱心曲线计算出爱心图案上的基准点,创建粒子对象
# 每个点会有一个延轴向的随机位移,随机采用正态分布
while t < 2*pi:
    c += 1
    sigma = 0.15 if c % 5 else 0.3
    f = 1 - abs(random.gauss(1, sigma) - 1)
    x = 16*sin(t)**3
    y = 13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t)
    size = (random.uniform(0.5,2.5), random.uniform(0.5,2.5))
    particles.append(Particle((x, y), size, f))
    t += dt

def draw():
    screen.clear()
    # 绘制爱心粒子
    for p in particles:
        p.draw()

    if L == 10:
        # 采用同样原理,绘制外层大爱心,但生成粒子,只是每帧随机绘制
        t = 0
        while t < 2*pi:
            f = random.gauss(1.1, 0.1)
            x = 16*sin(t)**3
            y = 13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t)
            size = (random.uniform(0.5,2.5), random.uniform(0.5,2.5))
            screen.draw.filled_rect(Rect((10*f*x + 400, -10*f*y + 300), size), 'hot pink')
            t += dt * 3
    screen.draw.filled_rect(Rect((-10*11 + 400, 11*20 + 200), (2, 2)), 'hot pink')

TITLE = ''.join([chr(i-1) for i in tt])
status = 0
L = 100
elapsed = 0
def update(dt):
    global elapsed, L, status
    elapsed += dt
    if status == 0:
        # 为了初始的集聚效果,加了一个很大的倍数L,并不断缩小至正常值
        L -= dt * 200
        if L <= 10:
            status = 1
            L = 10
    elif status == 2:
        L += dt * 200
    # 根据时间更新粒子位置
    for p in particles:
        p.update(elapsed)

TITLE = 'heart of ikun - Crossin的编程教室'

pgzrun.go()

 (10)、空心爱心

此代码需要用到import pgzrun安装方式不是pgzrun,而是pgzero,请留意
pip install pgzero

基于极坐标的爱心曲线,是遍历角度来计算点的位置

电视剧里的代码真能运行吗?_Crossin的编程教室的博客-CSDN博客

import pgzrun
from math import pi, sin, cos
 
no_p = 100
dt = 2*3/no_p
t = 0
x = []
y = []
while t <= 2*3:
    x.append(16*sin(t)**3)
    y.append(13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t))
    t += dt
 
def draw():
    screen.clear()
    for i in range(len(x)):
        screen.draw.filled_rect(Rect((x[i]*10+400, -y[i]*10+300), (4, 4)), 'pink')
 
pgzrun.go()

11、空心立体爱心

import turtle as t
t.penup()
t.seth(-90)
t.fd(160)
t.pendown()
t.pensize(20)
t.colormode(255)
for j in range(10):
  t.speed(0)
  t.pencolor(20*j,2*j,20*j)
  t.seth(130)
  t.fd(215)
  for i in range(23):
    t.circle(-80,10)
  t.seth(100)
  for i in range(23):
    t.circle(-80,10)
  t.fd(215)
t.hideturtle()
t.done()

 12、李峋同款爱心代码【有声版】

李峋同款爱心代码【有声版】_李峋爱心代码_cs阿坤dn的博客-CSDN博客
建立mp3目录下载
https://img-qn.51miz.com/preview/sound/00/28/31/51miz-S283175-043A2FE9.mp3
保存到mp3目录
安装相应的模块
运行代码,有心跳声音
import random                           # 随机函数模块
import pyttsx3                          # 文字转语音模块
from pygame import mixer                # mp3播放模块
from math import sin, cos, pi, log      # 核心模块,数学函数
from tkinter import *                   # 窗口主体模块


CANVAS_WIDTH = 1920   # 画布的宽
CANVAS_HEIGHT = 1080  # 画布的高
CANVAS_CENTER_X = CANVAS_WIDTH / 2   # 画布中心的X轴坐标
CANVAS_CENTER_Y = CANVAS_HEIGHT / 2  # 画布中心的Y轴坐标
IMAGE_ENLARGE = 15  # 放大比例
HEART_COLOR = "#Fd789f"  # 心的颜色,少女粉


def speak_voice(message, speak_mode=0):
    """
    “文字转语音”
    :param message: 文本消息
    :param speak_mode: 语音包,0 中文女声,1 英文女声,2 英文男声
    """
    # 初始化
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')

    # 改变语速  范围为0-200   默认值为200
    rate = engine.getProperty('rate')
    engine.setProperty('rate', rate - 100)

    # 设置音量  范围为0.0-1.0  默认值为1.0
    engine.setProperty('volume', 0.8)

    # 设置发音人,目前只有0可以中文
    engine.setProperty('voice', voices[speak_mode].id)

    # 预设要朗读的文本数据
    engine.say(message)

    # 读出声音
    engine.runAndWait()


def heart_voice(file_path, playback_mode=-1):
    """
    “噗噗的心跳声”
    :param file_path: mp3文件路径
    :param playback_mode: 播放模式,-1 为循环播放
    """
    # 初始化
    mixer.init()

    # 加载文件路径
    mixer.music.load(file_path)

    # 播放mp3文件
    mixer.music.play(playback_mode)


def heart_function(t, shrink_ratio: float = IMAGE_ENLARGE):
    """
    “爱心函数生成器”
    :param shrink_ratio: 放大比例
    :param t: 参数
    :return: 坐标
    """
    # 基础函数
    x = 16 * (sin(t) ** 3)
    y = -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t))

    # 放大
    x *= shrink_ratio
    y *= shrink_ratio

    # 移到画布中央
    x += CANVAS_CENTER_X
    y += CANVAS_CENTER_Y

    return int(x), int(y)


def scatter_inside(x, y, beta=0.15):
    """
    随机内部扩散
    :param x: 原x
    :param y: 原y
    :param beta: 强度
    :return: 新坐标
    """
    ratio_x = - beta * log(random.random())
    ratio_y = - beta * log(random.random())

    dx = ratio_x * (x - CANVAS_CENTER_X)
    dy = ratio_y * (y - CANVAS_CENTER_Y)

    return x - dx, y - dy


def shrink(x, y, ratio):
    """
    抖动
    :param x: 原x
    :param y: 原y
    :param ratio: 比例
    :return: 新坐标
    """
    force = -1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.6)  # 这个参数...
    dx = ratio * force * (x - CANVAS_CENTER_X)
    dy = ratio * force * (y - CANVAS_CENTER_Y)
    return x - dx, y - dy


def curve(p):
    """
    自定义曲线函数,调整跳动周期
    :param p: 参数
    :return: 正弦
    """
    # 可以尝试换其他的动态函数,达到更有力量的效果(贝塞尔?)
    return 2 * (2 * sin(4 * p)) / (2 * pi)


class Heart:
    """
    爱心类
    """

    def __init__(self, generate_frame=20):
        self._points = set()  # 原始爱心坐标集合
        self._edge_diffusion_points = set()  # 边缘扩散效果点坐标集合
        self._center_diffusion_points = set()  # 中心扩散效果点坐标集合
        self.all_points = {}  # 每帧动态点坐标
        self.build(2000)

        self.random_halo = 1000

        self.generate_frame = generate_frame
        for frame in range(generate_frame):
            self.calc(frame)

    def build(self, number):
        # 爱心
        for _ in range(number):
            t = random.uniform(0, 2 * pi)  # 随机不到的地方造成爱心有缺口
            x, y = heart_function(t)
            self._points.add((x, y))

        # 爱心内扩散
        for _x, _y in list(self._points):
            for _ in range(3):
                x, y = scatter_inside(_x, _y, 0.05)
                self._edge_diffusion_points.add((x, y))

        # 爱心内再次扩散
        point_list = list(self._points)
        for _ in range(4000):
            x, y = random.choice(point_list)
            x, y = scatter_inside(x, y, 0.17)
            self._center_diffusion_points.add((x, y))

    @staticmethod
    def calc_position(x, y, ratio):
        # 调整缩放比例
        force = 1 / (((x - CANVAS_CENTER_X) ** 2 + (y - CANVAS_CENTER_Y) ** 2) ** 0.520)  # 魔法参数

        dx = ratio * force * (x - CANVAS_CENTER_X) + random.randint(-1, 1)
        dy = ratio * force * (y - CANVAS_CENTER_Y) + random.randint(-1, 1)

        return x - dx, y - dy

    def calc(self, generate_frame):
        ratio = 10 * curve(generate_frame / 10 * pi)  # 圆滑的周期的缩放比例

        halo_radius = int(4 + 6 * (1 + curve(generate_frame / 10 * pi)))
        halo_number = int(3000 + 4000 * abs(curve(generate_frame / 10 * pi) ** 2))

        all_points = []

        # 光环
        heart_halo_point = set()  # 光环的点坐标集合
        for _ in range(halo_number):
            t = random.uniform(0, 2 * pi)  # 随机不到的地方造成爱心有缺口
            x, y = heart_function(t, shrink_ratio=11.6)  # 魔法参数
            x, y = shrink(x, y, halo_radius)
            if (x, y) not in heart_halo_point:
                # 处理新的点
                heart_halo_point.add((x, y))
                x += random.randint(-14, 14)
                y += random.randint(-14, 14)
                size = random.choice((1, 2, 2))
                all_points.append((x, y, size))

        # 轮廓
        for x, y in self._points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 3)
            all_points.append((x, y, size))

        # 内容
        for x, y in self._edge_diffusion_points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 2)
            all_points.append((x, y, size))

        for x, y in self._center_diffusion_points:
            x, y = self.calc_position(x, y, ratio)
            size = random.randint(1, 2)
            all_points.append((x, y, size))

        self.all_points[generate_frame] = all_points

    def render(self, render_canvas, render_frame):
        for x, y, size in self.all_points[render_frame % self.generate_frame]:
            render_canvas.create_rectangle(x, y, x + size, y + size, width=0, fill=HEART_COLOR)


def draw(main: Tk, render_canvas: Canvas, render_heart: Heart, render_frame=0):
    render_canvas.delete('all')
    render_heart.render(render_canvas, render_frame)
    main.after(160, draw, main, render_canvas, render_heart, render_frame + 1)


if __name__ == '__main__':
    root = Tk()  # 一个Tk
    root.title("李峋同款代码【有声版】")   #标题
    canvas = Canvas(root, bg='black', height=CANVAS_HEIGHT, width=CANVAS_WIDTH)
    canvas.pack()
    heart = Heart()  # 心
    draw(root, canvas, heart)    # 开始画画
    speak_voice("请收下我的心!")   # 要对她说的话
    heart_voice("mp3/51miz-S283175-043A2FE9.mp3")   # 心跳声音
    root.mainloop()

 三、python字符画爱心 

部分的代码会显示在终端,如果你没有打开终端会看不到效果。如果你没有专业的代码编辑器,只想试试效果,运行方式:

1、安装python配置环境

2、将代码完整复制保存成:你的文件名.py

3、同时按window+R键,输入cmd,打开命令提示窗口

输入:python 你的文件名.py

 (1)、一行代码画爱心

print(‘n’.join([”.join([(‘Love'[(x-y)%len(‘Love’)] if ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0 else‘ ‘) for x in range(-30,30)]) for y in range(15,-15,-1)]))

a = [
        ”.join(
            [
                (
                    ‘Love'[(x-y)%len(‘Love’)]
                    if ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3<=0 else‘ ‘
                )
                for x in range(-30,30)
            ]
        )
        for y in range(15,-15,-1)
    ]

print(‘n’.join(a))
 

 (2)、用字符输出 I 爱 U (2种形式)

import time
y = 2.5
while y>=-1.6:
    x = -3.0
    while x<=4.0:
        if (x*x+y*y-1)**3<=3.6*x*x*y*y*y or (x>-2.4 and x<-2.1 and y<1.5 and y>-1) or (((x<2.5 and x>2.2)or(x>3.4 and x<3.7)) and y>-1 and y<1.5) or (y>-1 and y<-0.6 and x<3.7 and x>2.2):
            print(' ',end="")
        else:
            print('*',end="")
        x += 0.1
    print()
    time.sleep(0.25)
    y -= 0.2

参考https://blog.csdn.net/qq_55342245/article/details/120904578

import time
y = 2.5
while y>=-1.6:
    x = -3.0
    while x<=4.0:
        if (x*x+y*y-1)**3<=3.6*x*x*y*y*y or (x>-2.4 and x<-2.1 and y<1.5 and y>-1) or (((x<2.5 and x>2.2)or(x>3.4 and x<3.7)) and y>-1 and y<1.5) or (y>-1 and y<-0.6 and x<3.7 and x>2.2):
            print('*',end="")
        else:
            print(' ',end="")
        x += 0.1
    print()
    time.sleep(0.25)
    y -= 0.2

 (3)、输出五个爱心,由Dear I love you forever! 五个单词填充而成

转载地址:https://blog.csdn.net/qq_55342245/article/details/120904578

import time
sentence = "Dear, I love you forever!"
for char in sentence.split():
   allChar = []
   for y in range(12, -12, -1):
       lst = []
       lst_con = ''
       for x in range(-30, 30):
            formula = ((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y*0.1)**3
            if formula <= 0:
                lst_con += char[(x) % len(char)]
            else:
                lst_con += ' '
       lst.append(lst_con)
       allChar += lst
   print('n'.join(allChar))
   time.sleep(1)

 (4)、字符输出爱心曲线公式爱心1

用是这个爱心曲线公式然后遍历一个15*17的方阵,计算每个坐标是在曲线内还是曲线外,在内部就输出#或*,外部就是-

电视剧里的代码真能运行吗?_Crossin的编程教室的博客-CSDN博客
for y in range(9, -6, -1):
    for x in range(-8, 9):
        print(‘*##*'[(x+10)%4] if (x*x+y*y-25)**3 < 25*x*x*y*y*y else ‘_’, end=’ ‘)
    print()

(5)、字符输出爱心曲线公式爱心2

用是这个爱心曲线公式,然后遍历一个15*17的方阵,计算每个坐标是在曲线内还是曲线外

电视剧里的代码真能运行吗?_Crossin的编程教室的博客-CSDN博客
for y in range(9, -6, -1):
    for x in range(-8, 9):
        print(‘❤’ if (x*x+y*y-25)**3 < 25*x*x*y*y*y else ‘ ‘, end=’ ‘)
    print()

持续更新中。。。

           推荐阅读

让Ai帮我们画个粽子,它会画成什么样呢?
29

SVG实例详解系列(一)(svg概述、位图和矢量图区别(图解)、SVG应用实例)

28

查看jdk安装路径,在windows上实现多个java jdk的共存解决办法,安装java19后终端乱码的解决

27 bba02a1c4617422c9fbccbf5325850d9.png

别具一格,原创唯美浪漫情人节表白专辑,(复制就可用)(html5,css3,svg)表白爱心代码(1)

26 fea225cb9ec14b60b2d1b797dd8278a2.png

2023年春节祝福第二弹——送你一只守护兔,让它温暖每一个你【html5 css3】画会动的小兔子,炫酷充电,字体特

25 1f53fb9c6e8b4482813326affe6a82ff.png

2023春节祝福系列第一弹(上)(放飞祈福孔明灯,祝福大家身体健康)(附完整源代码及资源免费下载)

24 6176c4061c72430eb100750af6fc4d0e.png

HTML+CSS+svg绘制精美彩色闪灯圣诞树,HTML+CSS+Js实时新年时间倒数倒计时(附源代码)

23 17b403c4307c4141b8544d02f95ea06c.png

​草莓熊python绘图(春节版,圣诞倒数雪花版)附源代码

22 5d409c8f397a45c986ca2af7b7e725c9.png

【程序人生】卡塔尔世界杯元素python海龟绘图(附源代码),世界杯主题前端特效5个(附源码)

21 0a4256d5e96d4624bdca36433237080b.png

python爱心源代码集锦(18款)

20 4d9032c9cdf54f5f9193e45e4532898c.png

巴斯光年python turtle绘图__附源代码

19 074cd3c255224c5aa21ff18fdc25053c.png

Three.js实例详解___旋转的精灵女孩(附完整代码和资源)(一)

18 daecd7067e7c45abb875fc7a1a469f23.png

​草莓熊python turtle绘图代码(玫瑰花版)附源代码

17 fe88b78e78694570bf2d850ce83b1f69.png

立体多层玫瑰绘图源码__玫瑰花python 绘图源码集锦

16 c5feeb25880d49c085b808bf4e041c86.png

皮卡丘python turtle海龟绘图(电力球版)附源代码

15 38266b5036414624875447abd5311e4d.png

【CSDN云IDE】个人使用体验和建议(含超详细操作教程)(python、webGL方向)

14 03ed644f9b1d411ba41c59e0a5bdcc61.png

草莓熊python turtle绘图(风车版)附源代码

13 09e08f86f127431cbfdfe395aa2f8bc9.png

用代码过中秋,python海龟月饼你要不要尝一口?

12 40e8b4631e2b486bab2a4ebb5bc9f410.png

《 Python List 列表全实例详解系列(一)》__系列总目录、列表概念

11 938bc5a8bb454a41bfe0d4185da845dc.jpeg

用代码写出浪漫__合集(python、matplotlib、Matlab、java绘制爱心、玫瑰花、前端特效玫瑰、爱心)

10 0f09e73712d149ff90f0048a096596c6.png

Python函数方法实例详解全集(更新中…)

9 93d65dbd09604c4a8ed2c01df0eebc38.png

matplotlib 自带绘图样式效果展示速查(28种,全)

8 aa17177aec9b4e5eb19b5d9675302de8.png

手机屏幕坏了____怎么把里面的资料导出(18种方法)

7 1750390dd9da4b39938a23ab447c6fb6.jpeg

2023年6月TIOBE 指数头条:编程语言 Go 进入 TIOBE 指数前 10 名,多家权威机构____编程语言排行榜__薪酬状

6 dc8796ddccbf4aec98ac5d3e09001348.jpeg

Python中Print()函数的用法___实例详解(全,例多)

5 1ab685d264ed4ae5b510dc7fbd0d1e55.jpeg

色彩颜色对照表(一)(16进制、RGB、CMYK、HSV、中英文名)

4 80007dbf51944725bf9cf4cfc75c5a13.png

Node.js (v19.1.0npm 8.19.3) vue.js安装配置教程(超详细)

3 c6374d75c29942f2aa577ce9c5c2e12b.png

Tomcat 启动闪退问题解决集(八大类详细)

2 5218ac5338014f389c21bdf1bfa1c599.png

Tomcat端口配置(详细)

1 fffa2098008b4dc68c00a172f67c538d.png

tomcat11、tomcat10 安装配置(Windows环境)(详细图文)

原文地址:https://blog.csdn.net/weixin_69553582/article/details/127941542

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_24908.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注