-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparticles.py
More file actions
115 lines (95 loc) · 3.56 KB
/
Copy pathparticles.py
File metadata and controls
115 lines (95 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
简单粒子特效系统
"""
import random
import math
import pygame
class Particle:
"""单个粒子"""
SHAPES = ["circle", "square", "star"]
x: float
y: float
color: tuple[int, int, int]
vx: float
vy: float
life: float
max_life: float
size: int
shape: str
def __init__(self, x: float, y: float, color: tuple[int, int, int],
speed: float, life: float, shape: str | None = None) -> None:
self.x = x
self.y = y
self.color = color
angle = random.uniform(0, math.pi * 2)
self.vx = math.cos(angle) * speed * random.uniform(0.5, 1.5)
self.vy = math.sin(angle) * speed * random.uniform(0.5, 1.5)
self.life = life
self.max_life = life
self.size = random.randint(2, 5)
self.shape = shape if shape else random.choice(self.SHAPES)
@property
def alive(self) -> bool:
return self.life > 0
@property
def alpha(self) -> int:
return int(255 * (self.life / self.max_life))
def update(self, dt: float) -> None:
self.x += self.vx * dt
self.y += self.vy * dt
self.life -= dt
def draw(self, screen: pygame.Surface) -> None:
c = self.color
a = self.alpha
if a < 0:
return
color = (c[0], c[1], c[2], a)
if self.shape == "circle":
surf = pygame.Surface((self.size * 2, self.size * 2), pygame.SRCALPHA)
pygame.draw.circle(surf, color, (self.size, self.size), self.size)
screen.blit(surf, (int(self.x - self.size), int(self.y - self.size)))
elif self.shape == "square":
surf = pygame.Surface((self.size * 2, self.size * 2), pygame.SRCALPHA)
half = self.size
rect = (half - self.size // 2, half - self.size // 2, self.size, self.size)
pygame.draw.rect(surf, color, rect)
screen.blit(surf, (int(self.x - self.size), int(self.y - self.size)))
elif self.shape == "star":
surf = pygame.Surface((self.size * 2, self.size * 2), pygame.SRCALPHA)
cx = cy = self.size
r = self.size
points = []
for i in range(8):
angle = i * math.pi / 4
dist = r if i % 2 == 0 else r * 0.4
points.append((cx + math.cos(angle) * dist,
cy + math.sin(angle) * dist))
if len(points) >= 3:
pygame.draw.polygon(surf, color, points)
screen.blit(surf, (int(self.x - self.size), int(self.y - self.size)))
class ParticleSystem:
"""管理所有活跃粒子"""
particles: list[Particle]
def __init__(self) -> None:
self.particles = []
def burst(self, x: float, y: float, color: tuple[int, int, int],
count: int, speed: float) -> None:
"""在 (x, y) 爆发 count 个粒子,随机混合形状(Item 15)"""
for _ in range(count):
life = random.uniform(0.3, 0.8)
shape = random.choice(["circle", "square", "star"])
self.particles.append(Particle(x, y, color, speed, life, shape=shape))
def update(self, dt: float) -> None:
# alive-list 重建:避免 O(n) 的 list.remove()
alive = []
for p in self.particles:
p.update(dt)
if p.alive:
alive.append(p)
self.particles = alive
def draw(self, screen: pygame.Surface) -> None:
for p in self.particles:
p.draw(screen)
@property
def is_empty(self) -> bool:
return len(self.particles) == 0