下面是一个简短而精美的生日祝福动画代码,使用Python的Pygame库实现。这段代码将创建一个动画窗口,显示旋转的生日蛋糕、闪烁的蜡烛和生日祝福消息。
python
import pygame
import math
import random
import sys
# 初始化pygame
pygame.init
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("生日快乐!")
clock = pygame.time.Clock
# 颜色定义
BACKGROUND = (10, 10, 40)
CAKE_COLORS = [(255, 223, 196), (255, 192, 203), (173, 216, 230)]
CANDLE_COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255)]
TEXT_COLOR = (255, 215, 0)
FIRE_COLORS = [(255, 165, 0), (255, 69, 0), (255, 255, 0)]
# 蛋糕类
class Cake:
def __init__(self):
self.x = WIDTH // 2
self.y = HEIGHT // 2
self.size = 100
self.color = random.choice(CAKE_COLORS)
self.angle = 0
self.candles = [{"x": self.x
def draw(self):
# 绘制蛋糕底层
pygame.draw.rect(screen, self.color, (self.x-80, self.y-40, 160, 80))
# 绘制蛋糕顶层
pygame.draw.rect(screen, (min(255, self.color[0]+30), min(255, self.color[1]+30), min(255, self.color[2]+30)),
(self.x-60, self.y-60, 120, 20))
# 绘制蜡烛
for i, candle in enumerate(self.candles):
pygame.draw.rect(screen, CANDLE_COLORS[i], (candle["x"]-3, candle["y"]-20, 6, 20))
# 绘制火焰
if candle["on"]:
fire_color = random.choice(FIRE_COLORS)
pygame.draw.circle(screen, fire_color, (candle["x"], candle["y"]-25), 8)
def update(self):
self.angle = (self.angle + 0.5) % 360
# 让蜡烛随机闪烁
for candle in self.candles:
if random.random < 0.02:
candle["on"] = not candle["on"]
# 粒子效果
class Particle:
def __init__(self):
self.x = random.randint(0, WIDTH)
self.y = random.randint(0, HEIGHT)
self.size = random.randint(2, 6)
self.color = random.choice([(255, 215, 0), (255, 105, 180), (135, 206, 235)])
self.speed = random.uniform(1, 3)
self.angle = random.uniform(0, 2 * math.pi)
def move(self):
self.x += math.cos(self.angle) * self.speed
self.y += math.sin(self.angle) * self.speed
# 边界检查
if self.x < 0 or self.x > WIDTH or self.y < 0 or self.y > HEIGHT:
self.__init__
def draw(self):
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.size)
# 创建蛋糕和粒子
cake = Cake
particles = [Particle for _ in range(100)]
font = pygame.font.SysFont(None, 48)
small_font = pygame.font.SysFont(None, 28)
# 主循环
running = True
while running:
for event in pygame.event.get:
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
# 填充背景
screen.fill(BACKGROUND)
# 绘制粒子
for particle in particles:
particle.move
particle.draw
# 绘制蛋糕
cake.draw
cake.update
# 绘制文本
birthday_text = font.render("生日快乐!", True, TEXT_COLOR)
msg_text = small_font.render("愿你的每一天都像今天一样充满欢乐!", True, (255, 255, 255))
instruction = small_font.render("按ESC退出", True, (150, 150, 150))
screen.blit(birthday_text, (WIDTH//2
screen.blit(msg_text, (WIDTH//2
screen.blit(instruction, (WIDTH//2
# 绘制装饰
pygame.draw.circle(screen, (255, 215, 0), (50, 50), 20, 2)
pygame.draw.circle(screen, (255, 105, 180), (WIDTH-50, 50), 20, 2)
pygame.draw.circle(screen, (135, 206, 235), (50, HEIGHT-50), 20, 2)
pygame.draw.circle(screen, (50, 205, 50), (WIDTH-50, HEIGHT-50), 20, 2)
pygame.display.flip
clock.tick(60)
pygame.quit
sys.exit
如何运行这段代码
1. 确保你已安装Python(建议Python 3.6+)
2. 安装Pygame库:
pip install pygame
3. 将上面的代码保存为 `birthday.py`
4. 运行代码:
python birthday.py
代码说明
这段代码创建了一个生日祝福动画,包含以下元素:
自定义建议
你可以轻松修改代码中的以下部分来个性化祝福:
1. 修改祝福文本:在代码中找到`birthday_text`和`msg_text`部分,替换为你想要的祝福语
2. 更改颜色:调整`CAKE_COLORS`或`CANDLE_COLORS`列表中的颜色值
3. 添加更多蜡烛:修改`self.candles`列表的长度
4. 调整动画速度:修改`cake.angle`的增量值
这段代码简洁但效果精美,是作为编程生日礼物的完美选择!