"当其他人在送花时,我直接送你个数字爱心!" 💻❤️🔥
作为被Python"附体"的钢铁直男,这个爱心动画项目简直是我写过最硬核的情书!下面请欣赏来自代码世界的终极浪漫——
🔥 "看好了!这是我的二进制浪漫!"
CodeBuddy直接帮我3分钟搞定!
当点击运行按钮的瞬间,屏幕突然炸开一颗半透明的大红心!这不是普通的爱心,而是用参数方程精准建模的赛博爱心:
看着红色液体从底部像岩浆般涌上来的那一刻,终于懂了什么叫"我的爱像代码一样严谨,像死循环一样永恒"!
🌊 "在内存海里为你掀起波浪!"
普通人的爱心会跳动,而我的爱心会流体力学模拟!通过实时计算正弦波:
wave_y = amplitude * sin(frequency*x + offset) # 动态波浪算法
当看到红色液体顶着波浪纹填满心形,连我自己都惊了——这特么不就是"我对你的思念像内存泄漏,根本停不下来!"的最佳写照吗?
🛡️ "我的爱绝不会溢出缓冲区!"
最硬核的是这个遮罩黑科技:
masked_surface.blit(heart_mask, special_flags=BLEND_RGBA_MULT) # 爱的结界!
就像我对你的承诺:"我的变量作用域里永远只有你,绝不会segmentation fault!" ❤️🔥
⚡ 技术宅的浪漫暴击
**结论:这个项目证明了三件事——
1. 程序员浪漫起来简直降维打击
2. 数学公式才是最美的情诗
3. 爱TA就为TA写个动画引擎!**
附完整代码如下:
import pygame
import math
import sys
from pygame.locals import *
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("爱心血液填充动画")
# 颜色定义
RED = (255, 0, 0)
PINK = (255, 192, 203)
BLACK = (0, 0, 0)
BUTTON_GREEN = (100, 200, 100)
WHITE = (255, 255, 255)
# 爱心参数
heart_center = (WIDTH//2, HEIGHT//2 + 10)
heart_size = 10 # 适当增大尺寸
fill_level = 0
filling = False
wave_offset = 0
# 按钮设置
button_rect = pygame.Rect(WIDTH//2 - 50, HEIGHT - 80, 100, 40)
button_font = pygame.font.SysFont('Arial', 24)
button_text = button_font.render("start", True, WHITE)
def get_heart_points():
points = []
for angle in range(0, 360, 2):
rad = math.radians(angle)
x = heart_center[0] + heart_size * (16 * math.sin(rad)**3)
y = heart_center[1] - heart_size * (13 * math.cos(rad)
- 5 * math.cos(2*rad)
- 2 * math.cos(3*rad)
- math.cos(4*rad))
points.append((x, y))
return points
def draw_blood_fill(surface, points, fill_ratio):
if fill_ratio <= 0:
return
# 创建临时表面
blood_surface = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
# 计算填充高度
min_y = min(p[1] for p in points)
max_y = max(p[1] for p in points)
fill_height = min_y + (max_y - min_y) * (1 - fill_ratio)
# 绘制填充区域
fill_points = []
for p in points:
if p[1] >= fill_height:
fill_points.append(p)
else:
fill_points.append((p[0], fill_height))
# 添加波浪效果
for i in range(len(fill_points)):
if abs(fill_points[i][1] - fill_height) < 5:
fill_points[i] = (fill_points[i][0],
fill_points[i][1] + 3 * math.sin(fill_points[i][0]/30 + wave_offset))
if len(fill_points) > 2:
pygame.draw.polygon(blood_surface, (*RED, 200), fill_points)
# 应用遮罩
mask = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
pygame.draw.polygon(mask, (255,255,255,255), points)
blood_surface.blit(mask, (0,0), special_flags=pygame.BLEND_RGBA_MULT)
surface.blit(blood_surface, (0,0))
def main():
global fill_level, filling, wave_offset
clock = pygame.time.Clock()
running = True
while running:
screen.fill(BLACK)
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == MOUSEBUTTONDOWN:
if button_rect.collidepoint(event.pos):
fill_level = 0
filling = True
if filling and fill_level < 1:
fill_level += 0.005
wave_offset += 0.1
else:
filling = False
heart_points = get_heart_points()
# 先绘制血液填充
if fill_level > 0:
draw_blood_fill(screen, heart_points, fill_level)
# 再绘制爱心轮廓
if heart_points:
pygame.draw.polygon(screen, PINK, heart_points, 3)
# 绘制按钮
pygame.draw.rect(screen, BUTTON_GREEN, button_rect, border_radius=5)
screen.blit(button_text, (button_rect.centerx - button_text.get_width()//2,
button_rect.centery - button_text.get_height()//2))
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
(屏幕前的你,要不要也造个心脏送给TA?Ctrl+C我的代码,Ctrl+V你的真心!)💘@CodeBuddy