首页 分享 浪漫的不是代码,而是运行结果皆如你所愿:用 Python 画一个爱心表白

浪漫的不是代码,而是运行结果皆如你所愿:用 Python 画一个爱心表白

来源:花匠小妙招 时间:2024-12-01 13:22

在编程的世界里,浪漫可以通过代码实现,用简单的逻辑和几行代码,就能创造出令人心动的瞬间。今天,就用 Python 带你实现一个浪漫的创意——画一个动态的爱心,并向你的特别之人表白!话不多说直接先看效果图!

一、工具准备

在开始之前,确保你的 Python 环境中安装了 pygame 库。如果没有,可以运行以下命令安装:

pip install pygame 二、代码实现

以下是完整代码,运行后会显示一个动态跳动的有背景音乐且能变色的爱心图形,同时在屏幕上展示你的表白文字:

import pygame

import random

import math

import os

pygame.init()

info = pygame.display.Info()

width, height = info.current_w, info.current_h

screen = pygame.display.set_mode((width, height), pygame.FULLSCREEN)

pygame.display.set_caption("扑通扑通爱心粒子表白效果")

clock = pygame.time.Clock()

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

pygame.mixer.init()

pygame.mixer.music.load(os.path.join(BASE_DIR, "Falling_You.mp3"))

pygame.mixer.music.play(-1)

class ColorGradient:

"""生成颜色渐变效果"""

def __init__(self, max_value):

"""

初始化 ColorGradient 实例,设置最大值。

:param max_value: 渐变计算的最大值,用于决定颜色变化的范围。

"""

self.max_value = max_value

def get_color(self, value):

"""

根据输入的值生成 RGB 颜色值。颜色值会在红、绿、蓝三色之间变化。

:param value: 当前的值,用于计算颜色。

:return: 一个包含 RGB 值的元组。

"""

r = int(255 * (0.5 + 0.5 * math.sin(value / self.max_value * math.pi * 2)))

g = int(255 * (0.5 + 0.5 * math.sin(value / self.max_value * math.pi * 2 + 2)))

b = int(255 * (0.5 + 0.5 * math.sin(value / self.max_value * math.pi * 2 + 4)))

return r, g, b

class HeartShape:

"""生成爱心形状的点"""

def __init__(self, width, height):

"""

初始化 HeartShape 实例,设置画布的宽度和高度。

:param width: 画布的宽度,用于计算爱心的 X 坐标。

:param height: 画布的高度,用于计算爱心的 Y 坐标。

"""

self.width = width

self.height = height

def generate_point(self, t):

"""

根据给定的时间 t 计算爱心形状上的一个点的位置。

:param t: 当前时间或参数,用于生成动态的爱心形状。

:return: 爱心形状上的一个 (x, y) 坐标点。

"""

scale_factor = 1 + 0.1 * math.sin(t * 2)

x = int(self.width / 2 + 16 * math.sin(t) ** 3 * 20 * scale_factor)

y = int(self.height / 2 - (

13 * math.cos(t) - 5 * math.cos(2 * t) - 2 * math.cos(3 * t) - math.cos(4 * t)) * 20 * scale_factor)

return x, y

class Particle:

"""粒子类,控制粒子的运动、大小和形状"""

def __init__(self, x, y, color, lifetime, shape="circle", text=None):

"""

初始化粒子类实例,设置粒子的位置、颜色、生命周期、大小等属性。

:param x: 粒子的初始 X 坐标。

:param y: 粒子的初始 Y 坐标。

:param color: 粒子的颜色。

:param lifetime: 粒子的生命周期(持续时间)。

:param shape: 粒子的形状,默认为圆形,可以设置为 "heart" 来绘制爱心形状。

:param text: 如果粒子上要显示文字,传入文字内容。默认为 None。

"""

self.x = x

self.y = y

self.color = color

self.lifetime = lifetime

self.size = random.randint(2, 4)

self.vx = random.uniform(-1, 1)

self.vy = random.uniform(-1, 1)

self.shape = shape

self.text = text

def move(self):

"""更新粒子的位置和生命周期"""

self.x += self.vx

self.y += self.vy

self.lifetime -= 1

self.size = max(1, self.size - 0.05)

def draw(self, screen):

"""绘制粒子"""

if self.lifetime > 0:

if self.shape == "circle":

pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), int(self.size))

elif self.shape == "heart":

self.draw_heart(screen)

if self.text:

self.display_text(screen)

def draw_heart(self, screen):

"""绘制爱心形状粒子"""

size = int(self.size)

points = [

(self.x, self.y - size),

(self.x - size, self.y - size // 2),

(self.x - size, self.y + size // 2),

(self.x + size, self.y + size // 2),

(self.x + size, self.y - size // 2),

]

pygame.draw.polygon(screen, self.color, points)

def display_text(self, screen):

"""在粒子上显示中文文字(我爱你)"""

font_path = os.path.join(BASE_DIR, "simhei.ttf")

font = pygame.font.Font(font_path, 20)

text_surface = font.render(self.text, True, self.color)

screen.blit(text_surface, (self.x, self.y))

class ParticleSystem:

"""粒子系统,控制粒子的生成、更新和绘制"""

def __init__(self, heart_shape, color_gradient):

self.particles = []

self.heart_shape = heart_shape

self.color_gradient = color_gradient

self.last_particle_time = 0

def generate_particles(self, t):

"""生成新的粒子,控制生成频率"""

current_time = pygame.time.get_ticks()

if current_time - self.last_particle_time > 100:

for _ in range(520):

x, y = self.heart_shape.generate_point(t + random.uniform(0, 2 * math.pi))

color = self.color_gradient.get_color(t)

lifetime = random.randint(40, 100)

shape = "heart" if random.random() < 0.1 else "circle"

text = "我喜欢你" if random.random() < 0.00052 else None

self.particles.append(Particle(x, y, color, lifetime, shape, text))

self.last_particle_time = current_time

def update_particles(self):

"""更新和绘制粒子"""

for particle in self.particles[:]:

particle.move()

particle.draw(screen)

if particle.lifetime <= 0:

self.particles.remove(particle)

class HeartParticleEffect:

"""主动画类,负责控制整个效果的运行"""

def __init__(self, width, height):

self.width = width

self.height = height

self.heart_shape = HeartShape(width, height)

self.color_gradient = ColorGradient(100)

self.particle_system = ParticleSystem(self.heart_shape, self.color_gradient)

self.t = 0

self.running = True

def run(self):

"""运行主动画循环"""

while self.running:

screen.fill((0, 0, 0))

self.t += 0.1

self.particle_system.generate_particles(self.t)

self.particle_system.update_particles()

for event in pygame.event.get():

if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):

self.running = False

pygame.display.flip()

clock.tick(60)

if __name__ == "__main__":

effect = HeartParticleEffect(width, height)

effect.run()

pygame.quit()

三、总结

用代码表达情感是一件很特别的事情,它不仅让人感受到编程的乐趣,更赋予了程序新的意义。希望这颗 Python 爱心能带给你和特别的人一个难忘的瞬间!

浪漫的不是代码,而是那些充满爱与诚意的运行结果。

相关知识

程序员的浪漫!用Python实现表白代码!
python画一朵花的代码
python玫瑰花代码讲解
Python浪漫表白源码合集(爱心、玫瑰花、照片墙、星空下的告白)
❤️马上七夕,不懂浪漫?带你用Python“码”上七夕【建议收藏】❤️
七夕最浪漫的表白,最真挚的感情(Python代码实现)
python浪漫表白,表白代码——绘制3D玫瑰花
用python画简单图案并运行,用python画简单的花代码
python实现元旦倒计时、圣诞树、跨年烟花的绘画马上双旦了给大家带来一些python代码 1.元旦节日倒计时代码的实现
三行代码情书(1)

网址: 浪漫的不是代码,而是运行结果皆如你所愿:用 Python 画一个爱心表白 https://www.huajiangbk.com/newsview797639.html

所属分类:花卉
上一篇: 最浪漫的表白方式,最能打动人心的
下一篇: 向她告白,趁现在!

推荐分享