关注

Python创意爱心代码合集(7种实现方案)

方案一:动态粒子爱心(Pygame实现)
import pygame
import math
import random

pygame.init()
W, H = 1200, 800
screen = pygame.display.set_mode((W, H))
particles = []

class Particle:
    def __init__(self):
        self.angle = random.uniform(0, 2*math.pi)
        self.radius = random.uniform(50, 150)
        self.speed = random.uniform(0.02, 0.1)
        self.color = (random.randint(200,255), 0, random.randint(100,200))
    
    def update(self):
        x = 16 * (math.sin(self.angle)**3)
        y = 13 * math.cos(self.angle) - 5*math.cos(2*self.angle) - 2*math.cos(3*self.angle) - math.cos(4*self.angle)
        self.pos = (W//2 + int(x*self.radius), H//2 - int(y*self.radius))
        self.angle += self.speed

for _ in range(500):
    particles.append(Particle())

running = True
while running:
    screen.fill((0,0,30))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    for p in particles:
        p.update()
        pygame.draw.circle(screen, p.color, p.pos, 2)
    
    pygame.display.flip()
    pygame.time.delay(30)

pygame.quit()

效果:500个彩色粒子组成旋转的3D爱心,具有星空视觉效果


方案二:ASCII心跳动画
import time
import os

def heartbeat():
    phases = [
        r'''
         ♥ ♥ 
        ♥   ♥
         ♥ ♥ 
          ♥  
        ''',
        r'''
         ♥ ♥ 
        ♥ ♥ ♥
         ♥ ♥ 
          ♥  
        ''

转载自CSDN-专业IT技术社区

原文链接:https://blog.csdn.net/2301_77292307/article/details/148219961

评论

赞0

评论列表

微信小程序
QQ小程序

关于作者

点赞数:0
关注数:0
粉丝:0
文章:0
关注标签:0
加入于:--