pygame粒子烟花
时间: 2023-10-09 13:08:47 浏览: 140
可以使用Pygame库创建粒子烟花效果。下面是一个简单的实现示例:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 设置窗口尺寸和标题
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("粒子烟花")
# 定义烟花粒子类
class Particle:
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(2, 10)
self.dx = random.uniform(-1, 1)
self.dy = random.uniform(-1, 1)
self.gravity = 0.1
def move(self):
self.x += self.dx
self.y += self.dy
self.dy += self.gravity
def draw(self):
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
# 创建粒子列表
particles = []
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0, 0, 0))
# 在鼠标位置创建新的粒子
if pygame.mouse.get_pressed()[0]:
particle = Particle(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])
particles.append(particle)
# 更新并绘制粒子
for particle in particles:
particle.move()
particle.draw()
# 删除已经移动出窗口外的粒子
if particle.y > height:
particles.remove(particle)
pygame.display.flip()
# 退出程序时关闭Pygame
pygame.quit()
```
这个示例使用鼠标左键点击的位置创建新的粒子,并根据重力和速度模拟粒子的运动。粒子移动出窗口外后会被删除。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20250102104920.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)