浪漫的生日烟花祝福代码Python
时间: 2024-01-21 15:14:39 浏览: 257
2023年浪漫烟花炫酷代码
5星 · 资源好评率100%
以下是一个浪漫的生日烟花祝福的Python代码示例:
```python
import pygame
import random
# 初始化pygame
pygame.init()
# 设置窗口大小和标题
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("生日烟花祝福")
# 加载背景图片
background = pygame.image.load("happy birthday.jpg")
# 定义烟花类
class Firework:
def __init__(self, x, y):
self.x = x
self.y = y
self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
self.radius = random.randint(5, 20)
self.speed = random.randint(1, 5)
def move(self):
self.y -= self.speed
def draw(self):
pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius)
# 创建烟花列表
fireworks = []
# 游戏主循环
running = True
while running:
# 填充背景颜色
screen.fill((0, 0, 0))
# 绘制背景图片
screen.blit(background, (0, 0))
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 生成新的烟花
if random.randint(1, 10) == 1:
fireworks.append(Firework(random.randint(0, screen_width), screen_height))
# 移动和绘制烟花
for firework in fireworks:
firework.move()
firework.draw()
# 删除已经消失的烟花
fireworks = [firework for firework in fireworks if firework.y > 0]
# 更新屏幕
pygame.display.flip()
# 退出游戏
pygame.quit()
```
阅读全文