python龙卷风粒子特效代码
时间: 2023-11-08 15:02:57 浏览: 110
以下是一个简单的 Python 龙卷风粒子特效代码示例:
```python
import pygame
import random
# 初始化 Pygame
pygame.init()
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
GREEN = (0, 255, 0)
# 设定屏幕大小
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Tornado Particle Effect")
# 创建一个粒子类
class Particle(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface([5, 5])
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.x = SCREEN_WIDTH / 2
self.rect.y = SCREEN_HEIGHT / 2
self.angle = random.randint(0, 360)
self.distance = random.randint(50, 200)
self.speed = random.randint(1, 3)
self.size = random.randint(1, 5)
def update(self):
# 计算新的位置
radians = self.angle * math.pi / 180
self.rect.x = SCREEN_WIDTH / 2 + math.cos(radians) * self.distance
self.rect.y = SCREEN_HEIGHT / 2 + math.sin(radians) * self.distance
# 更新角度和距离
self.angle += random.randint(-10, 10)
self.distance -= random.randint(1, 2)
# 调整速度和大小
self.speed -= 0.05
if self.speed < 0:
self.speed = 0
self.size -= 0.05
if self.size < 0:
self.size = 0
# 调整颜色
self.image.set_alpha(int(self.speed * 255))
self.image = pygame.transform.scale(self.image, [int(self.size), int(self.size)])
# 创建一个粒子组
particle_group = pygame.sprite.Group()
# 主循环
done = False
clock = pygame.time.Clock()
while not done:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# 创建新的粒子并添加到组中
new_particle = Particle()
particle_group.add(new_particle)
# 更新所有粒子的位置、速度和大小
particle_group.update()
# 绘制所有粒子
screen.fill(BLACK)
particle_group.draw(screen)
# 更新屏幕
pygame.display.flip()
# 控制帧率
clock.tick(60)
# 退出 Pygame
pygame.quit()
```