import pygame
import random
import math
from pygame.locals import *
# 初始化pygame
pygame.init()
# 设置屏幕大小
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置标题
pygame.display.set_caption('春节烟花')
# 定义烟花参数
firework_speed = 5
firework_radius = 2
firework_explosion_radius = 60
colors = [
(255, 0, 0), # Red
(0, 255, 0), # Green
(0, 0, 255), # Blue
(255, 255, 0), # Yellow
(255, 165, 0), # Orange
(255, 255, 255) # White
]
# 定义Firework类
class Firework:
def __init__(self, x, y, color, exploded=False):
self.x = x
self.y = y
self.color = color
self.exploded = exploded
self.particles = []
def move(self):
if not self.exploded:
self.y -= firework_speed
def explode(self):
if not self.exploded:
for angle in range(0, 360, 5):
dir_x = math.cos(math.radians(angle))
dir_y = math.sin(math.radians(angle))
self.particles.append((self.x, self.y, dir_x, dir_y, self.color))
self.exploded = True
def update(self):
if self.exploded:
for particle in self.particles:
index = self.particles.index(particle)
particle_x, particle_y, dir_x, dir_y, color = particle
particle_x += dir_x * 2
particle_y += dir_y * 2
self.particles[index] = (particle_x, particle_y, dir_x, dir_y, color)
if self.distance(particle_x, particle_y) > firework_explosion_radius:
self.particles.pop(index)
def show(self):
if not self.exploded:
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), firework_radius)
else:
for particle in self.particles:
particle_x, particle_y, dir_x, dir_y, color = particle
pygame.draw.circle(screen, color, (int(particle_x), int(particle_y)), firework_radius)
def distance(self, x, y):
return math.sqrt((self.x - x) ** 2 + (self.y - y) ** 2)
fireworks = [Firework(random.randint(0, screen_width), screen_height - 10, random.choice(colors))]
# 游戏主循环
running = True
while running:
screen.fill((0, 0, 0)) # use a dark sky background
# 执行事件循环
for event in pygame.event.get():
if event.type == QUIT:
running = False
# 更新和显示烟花
for firework in fireworks:
if not firework.exploded and firework.y < screen_height / 2 + random.randint(-100, 100):
firework.explode()
firework.move()
firework.update()
firework.show()
# 随机发射新的烟花
if random.randint(0, 20) == 1:
fireworks.append(Firework(random.randint(0, screen_width), screen_height - 10, random.choice(colors)))
# 删除已完成的烟花
for firework in fireworks:
if firework.exploded and len(firework.particles) == 0:
fireworks.remove(firework)
pygame.display.flip()
pygame.time.Clock().tick(30) # 控制游戏最大帧率为30fps
pygame.quit()

Scikit-learn
- 粉丝: 6557
最新资源
- 物联网仓储系统课件(1).pptx
- 购物中心营销五大新趋势-五类大数据值得整合挖掘(1).docx
- 生物1.2基因工程的基本操作程序教学课(1).pptx
- 电子商务企业自媒体营销对品牌忠诚度的影响研究(1).docx
- 互联网金融对传统商业银行功能与盈利的影响分析(1).docx
- MATLAB课件-第六章-字符串、单元数组和结构体.ppt
- 交通灯PLC课程设计(1).doc
- 用plc直流电机控制设计(1).doc
- 内蒙电大毕业论文试论电子商务工商行政监管制度的完善(1).doc
- 基于ransac算法的sift特征匹配研究(OpenCV-VS2010)(最终版)(1).doc
- 初二英语上学期revisionm12外研英语(1).pptx
- 理学电子与通信现代交换技术(1).pptx
- 互联网金融监管(1)(1).pdf
- 【机械要点】“互联网+”开启环保新思维-给环保筑法网(1).pdf
- Flash动画设计基础知识(1).pptx
- 社会经济信息化(1).pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


