pyforgefx 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyforgefx/__init__.py +25 -0
- pyforgefx/camera/__init__.py +4 -0
- pyforgefx/camera/parallax.py +83 -0
- pyforgefx/camera/screen_shake.py +34 -0
- pyforgefx/particles/__init__.py +4 -0
- pyforgefx/particles/emitter.py +178 -0
- pyforgefx/particles/shockwave.py +85 -0
- pyforgefx/sprites/__init__.py +5 -0
- pyforgefx/sprites/animator.py +132 -0
- pyforgefx/sprites/generator.py +208 -0
- pyforgefx/sprites/spritesheet.py +117 -0
- pyforgefx/trails/__init__.py +3 -0
- pyforgefx/trails/trail_renderer.py +48 -0
- pyforgefx-1.0.0.dist-info/METADATA +94 -0
- pyforgefx-1.0.0.dist-info/RECORD +18 -0
- pyforgefx-1.0.0.dist-info/WHEEL +5 -0
- pyforgefx-1.0.0.dist-info/licenses/LICENSE.md +21 -0
- pyforgefx-1.0.0.dist-info/top_level.txt +1 -0
pyforgefx/__init__.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""PyForgeFX: Modular 2D SpriteSheet Animation, Procedural Frame Generation, Particles, Motion Trails, and Camera Effects library for Pygame."""
|
|
2
|
+
|
|
3
|
+
from .camera import ScreenShake, StarNode, StarfieldParallax
|
|
4
|
+
from .particles import Particle, ParticleEmitter, ShockwaveManager, ShockwaveRing
|
|
5
|
+
from .sprites import AnimationClip, Animator, SpriteFXGenerator, SpriteSheet
|
|
6
|
+
from .trails import TrailRenderer
|
|
7
|
+
|
|
8
|
+
__version__ = "1.0.0"
|
|
9
|
+
__author__ = "Drakonosinc"
|
|
10
|
+
|
|
11
|
+
__all__ = [
|
|
12
|
+
"__version__",
|
|
13
|
+
"AnimationClip",
|
|
14
|
+
"Animator",
|
|
15
|
+
"Particle",
|
|
16
|
+
"ParticleEmitter",
|
|
17
|
+
"ScreenShake",
|
|
18
|
+
"ShockwaveManager",
|
|
19
|
+
"ShockwaveRing",
|
|
20
|
+
"SpriteFXGenerator",
|
|
21
|
+
"SpriteSheet",
|
|
22
|
+
"StarNode",
|
|
23
|
+
"StarfieldParallax",
|
|
24
|
+
"TrailRenderer",
|
|
25
|
+
]
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""Multi-layer procedural twinkling starfield & parallax overlay (`StarfieldParallax`)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
import math
|
|
7
|
+
import random
|
|
8
|
+
import pygame
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(slots=True)
|
|
12
|
+
class StarNode:
|
|
13
|
+
x: float
|
|
14
|
+
y: float
|
|
15
|
+
speed: float
|
|
16
|
+
radius: int
|
|
17
|
+
phase: float
|
|
18
|
+
color: tuple[int, int, int]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class StarfieldParallax:
|
|
22
|
+
"""Renders a multi-depth scrolling and twinkling starfield over or behind any background."""
|
|
23
|
+
|
|
24
|
+
def __init__(
|
|
25
|
+
self,
|
|
26
|
+
width: int,
|
|
27
|
+
height: int,
|
|
28
|
+
num_stars: int = 55,
|
|
29
|
+
seed: int | None = 42,
|
|
30
|
+
) -> None:
|
|
31
|
+
self.width = max(64, int(width))
|
|
32
|
+
self.height = max(64, int(height))
|
|
33
|
+
self._rng = random.Random(seed)
|
|
34
|
+
self.time: float = 0.0
|
|
35
|
+
self.stars: list[StarNode] = []
|
|
36
|
+
self._init_stars(int(num_stars))
|
|
37
|
+
|
|
38
|
+
def _init_stars(self, count: int) -> None:
|
|
39
|
+
palette = [
|
|
40
|
+
(226, 232, 240),
|
|
41
|
+
(125, 211, 252),
|
|
42
|
+
(196, 181, 253),
|
|
43
|
+
(253, 230, 138),
|
|
44
|
+
]
|
|
45
|
+
self.stars.clear()
|
|
46
|
+
for _ in range(max(5, count)):
|
|
47
|
+
depth = self._rng.choice((1, 2, 3))
|
|
48
|
+
self.stars.append(
|
|
49
|
+
StarNode(
|
|
50
|
+
x=self._rng.uniform(0, self.width),
|
|
51
|
+
y=self._rng.uniform(0, self.height),
|
|
52
|
+
speed=12.0 * depth,
|
|
53
|
+
radius=1 if depth < 3 else 2,
|
|
54
|
+
phase=self._rng.uniform(0.0, math.pi * 2.0),
|
|
55
|
+
color=self._rng.choice(palette),
|
|
56
|
+
)
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
def resize(self, width: int, height: int) -> None:
|
|
60
|
+
if (int(width), int(height)) != (self.width, self.height):
|
|
61
|
+
self.width = max(64, int(width))
|
|
62
|
+
self.height = max(64, int(height))
|
|
63
|
+
self._init_stars(len(self.stars))
|
|
64
|
+
|
|
65
|
+
def update_and_draw(
|
|
66
|
+
self,
|
|
67
|
+
surface: pygame.Surface,
|
|
68
|
+
dt: float,
|
|
69
|
+
drift_x: float = -1.0,
|
|
70
|
+
drift_y: float = 0.2,
|
|
71
|
+
) -> None:
|
|
72
|
+
step = max(0.0, float(dt))
|
|
73
|
+
self.time += step
|
|
74
|
+
for star in self.stars:
|
|
75
|
+
star.x = (star.x + drift_x * star.speed * step) % self.width
|
|
76
|
+
star.y = (star.y + drift_y * star.speed * step) % self.height
|
|
77
|
+
twinkle = 0.55 + 0.45 * math.sin(self.time * 3.2 + star.phase)
|
|
78
|
+
c = (
|
|
79
|
+
int(star.color[0] * twinkle),
|
|
80
|
+
int(star.color[1] * twinkle),
|
|
81
|
+
int(star.color[2] * twinkle),
|
|
82
|
+
)
|
|
83
|
+
pygame.draw.circle(surface, c, (int(star.x), int(star.y)), star.radius)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"""Camera trauma and screen-shake controller (`ScreenShake`) for 2D games."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import random
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ScreenShake:
|
|
9
|
+
"""Computes damped `(offset_x, offset_y)` camera shake displacements on impacts."""
|
|
10
|
+
|
|
11
|
+
def __init__(self, seed: int | None = None) -> None:
|
|
12
|
+
self.intensity: float = 0.0
|
|
13
|
+
self.duration: float = 0.0
|
|
14
|
+
self.elapsed: float = 0.0
|
|
15
|
+
self._rng = random.Random(seed)
|
|
16
|
+
|
|
17
|
+
def trigger(self, intensity: float = 7.0, duration: float = 0.22) -> None:
|
|
18
|
+
"""Start or amplify a screen-shake impulse."""
|
|
19
|
+
self.intensity = max(self.intensity, float(intensity))
|
|
20
|
+
self.duration = max(0.05, float(duration))
|
|
21
|
+
self.elapsed = 0.0
|
|
22
|
+
|
|
23
|
+
def update(self, dt: float) -> tuple[int, int]:
|
|
24
|
+
"""Advance the shake timer by `dt` seconds and return `(offset_x, offset_y)`."""
|
|
25
|
+
if self.intensity <= 0.01 or self.elapsed >= self.duration:
|
|
26
|
+
self.intensity = 0.0
|
|
27
|
+
return 0, 0
|
|
28
|
+
|
|
29
|
+
self.elapsed += max(0.0, float(dt))
|
|
30
|
+
remaining = max(0.0, 1.0 - (self.elapsed / self.duration))
|
|
31
|
+
mag = self.intensity * remaining
|
|
32
|
+
ox = int(round(self._rng.uniform(-mag, mag)))
|
|
33
|
+
oy = int(round(self._rng.uniform(-mag, mag)))
|
|
34
|
+
return ox, oy
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"""Lightweight 2D Particle System and preset emitter (`ParticleEmitter`) for Pygame."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
import math
|
|
7
|
+
import random
|
|
8
|
+
import pygame
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(slots=True)
|
|
12
|
+
class Particle:
|
|
13
|
+
x: float
|
|
14
|
+
y: float
|
|
15
|
+
vx: float
|
|
16
|
+
vy: float
|
|
17
|
+
radius: float
|
|
18
|
+
color: tuple[int, int, int]
|
|
19
|
+
life: float
|
|
20
|
+
max_life: float
|
|
21
|
+
gravity: float = 0.0
|
|
22
|
+
drag: float = 0.98
|
|
23
|
+
glow: bool = True
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class ParticleEmitter:
|
|
27
|
+
"""Standalone 2D particle emitter supporting sparks, explosions, thrusters, and ambient effects."""
|
|
28
|
+
|
|
29
|
+
def __init__(self, max_particles: int = 450, seed: int | None = None) -> None:
|
|
30
|
+
self.max_particles = max(10, int(max_particles))
|
|
31
|
+
self.particles: list[Particle] = []
|
|
32
|
+
self._rng = random.Random(seed)
|
|
33
|
+
|
|
34
|
+
def clear(self) -> None:
|
|
35
|
+
self.particles.clear()
|
|
36
|
+
|
|
37
|
+
def emit_burst(
|
|
38
|
+
self,
|
|
39
|
+
x: float,
|
|
40
|
+
y: float,
|
|
41
|
+
count: int = 18,
|
|
42
|
+
color: tuple[int, int, int] = (255, 190, 80),
|
|
43
|
+
secondary_color: tuple[int, int, int] | None = (255, 90, 30),
|
|
44
|
+
speed_range: tuple[float, float] = (60.0, 240.0),
|
|
45
|
+
size_range: tuple[float, float] = (2.5, 6.0),
|
|
46
|
+
life_range: tuple[float, float] = (0.22, 0.55),
|
|
47
|
+
angle_range_deg: tuple[float, float] = (0.0, 360.0),
|
|
48
|
+
gravity: float = 0.0,
|
|
49
|
+
) -> None:
|
|
50
|
+
"""Spawn a radial or directional burst of particles at `(x, y)`."""
|
|
51
|
+
palette = [color]
|
|
52
|
+
if secondary_color is not None:
|
|
53
|
+
palette.append(secondary_color)
|
|
54
|
+
|
|
55
|
+
for _ in range(int(count)):
|
|
56
|
+
if len(self.particles) >= self.max_particles:
|
|
57
|
+
self.particles.pop(0)
|
|
58
|
+
angle = math.radians(self._rng.uniform(angle_range_deg[0], angle_range_deg[1]))
|
|
59
|
+
speed = self._rng.uniform(speed_range[0], speed_range[1])
|
|
60
|
+
life = self._rng.uniform(life_range[0], life_range[1])
|
|
61
|
+
radius = self._rng.uniform(size_range[0], size_range[1])
|
|
62
|
+
chosen_color = self._rng.choice(palette)
|
|
63
|
+
self.particles.append(
|
|
64
|
+
Particle(
|
|
65
|
+
x=float(x),
|
|
66
|
+
y=float(y),
|
|
67
|
+
vx=math.cos(angle) * speed,
|
|
68
|
+
vy=math.sin(angle) * speed,
|
|
69
|
+
radius=radius,
|
|
70
|
+
color=chosen_color,
|
|
71
|
+
life=life,
|
|
72
|
+
max_life=life,
|
|
73
|
+
gravity=float(gravity),
|
|
74
|
+
)
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
def emit_preset(self, x: float, y: float, preset: str = "sparks") -> None:
|
|
78
|
+
"""Emit particles using a built-in visual preset (`sparks`, `explosion`, `thruster`, `goal`)."""
|
|
79
|
+
mode = preset.strip().lower()
|
|
80
|
+
if mode == "explosion":
|
|
81
|
+
self.emit_burst(
|
|
82
|
+
x,
|
|
83
|
+
y,
|
|
84
|
+
count=32,
|
|
85
|
+
color=(255, 220, 90),
|
|
86
|
+
secondary_color=(255, 70, 30),
|
|
87
|
+
speed_range=(80.0, 310.0),
|
|
88
|
+
size_range=(3.0, 7.5),
|
|
89
|
+
life_range=(0.3, 0.75),
|
|
90
|
+
)
|
|
91
|
+
elif mode == "goal":
|
|
92
|
+
self.emit_burst(
|
|
93
|
+
x,
|
|
94
|
+
y,
|
|
95
|
+
count=40,
|
|
96
|
+
color=(56, 189, 248),
|
|
97
|
+
secondary_color=(168, 85, 247),
|
|
98
|
+
speed_range=(90.0, 340.0),
|
|
99
|
+
size_range=(3.5, 8.0),
|
|
100
|
+
life_range=(0.4, 0.85),
|
|
101
|
+
)
|
|
102
|
+
elif mode == "thruster":
|
|
103
|
+
self.emit_burst(
|
|
104
|
+
x,
|
|
105
|
+
y,
|
|
106
|
+
count=3,
|
|
107
|
+
color=(125, 211, 252),
|
|
108
|
+
secondary_color=(59, 130, 246),
|
|
109
|
+
speed_range=(25.0, 75.0),
|
|
110
|
+
size_range=(2.0, 4.2),
|
|
111
|
+
life_range=(0.12, 0.26),
|
|
112
|
+
)
|
|
113
|
+
else:
|
|
114
|
+
# Default: collision sparks
|
|
115
|
+
self.emit_burst(
|
|
116
|
+
x,
|
|
117
|
+
y,
|
|
118
|
+
count=16,
|
|
119
|
+
color=(255, 240, 150),
|
|
120
|
+
secondary_color=(56, 189, 248),
|
|
121
|
+
speed_range=(70.0, 220.0),
|
|
122
|
+
size_range=(2.2, 5.0),
|
|
123
|
+
life_range=(0.18, 0.42),
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
def update(self, dt: float) -> None:
|
|
127
|
+
"""Advance particle physics by `dt` seconds and prune expired particles."""
|
|
128
|
+
step = max(0.0, float(dt))
|
|
129
|
+
alive: list[Particle] = []
|
|
130
|
+
for p in self.particles:
|
|
131
|
+
p.life -= step
|
|
132
|
+
if p.life <= 0.0:
|
|
133
|
+
continue
|
|
134
|
+
p.vy += p.gravity * step
|
|
135
|
+
p.vx *= p.drag
|
|
136
|
+
p.vy *= p.drag
|
|
137
|
+
p.x += p.vx * step
|
|
138
|
+
p.y += p.vy * step
|
|
139
|
+
alive.append(p)
|
|
140
|
+
self.particles = alive
|
|
141
|
+
|
|
142
|
+
def draw(self, surface: pygame.Surface, offset: tuple[int, int] = (0, 0)) -> None:
|
|
143
|
+
"""Render active particles with smooth alpha decay onto `surface`."""
|
|
144
|
+
ox, oy = int(offset[0]), int(offset[1])
|
|
145
|
+
for p in self.particles:
|
|
146
|
+
ratio = max(0.0, min(1.0, p.life / max(1e-5, p.max_life)))
|
|
147
|
+
cur_r = max(1, int(round(p.radius * ratio)))
|
|
148
|
+
alpha = int(245 * ratio)
|
|
149
|
+
px = int(round(p.x)) + ox
|
|
150
|
+
py = int(round(p.y)) + oy
|
|
151
|
+
|
|
152
|
+
if p.glow and cur_r >= 2:
|
|
153
|
+
glow_r = cur_r * 2
|
|
154
|
+
glow_surf = pygame.Surface((glow_r * 2 + 2, glow_r * 2 + 2), pygame.SRCALPHA)
|
|
155
|
+
pygame.draw.circle(
|
|
156
|
+
glow_surf,
|
|
157
|
+
(*p.color[:3], max(15, alpha // 3)),
|
|
158
|
+
(glow_r + 1, glow_r + 1),
|
|
159
|
+
glow_r,
|
|
160
|
+
)
|
|
161
|
+
pygame.draw.circle(
|
|
162
|
+
glow_surf,
|
|
163
|
+
(*p.color[:3], alpha),
|
|
164
|
+
(glow_r + 1, glow_r + 1),
|
|
165
|
+
cur_r,
|
|
166
|
+
)
|
|
167
|
+
surface.blit(glow_surf, (px - glow_r - 1, py - glow_r - 1))
|
|
168
|
+
else:
|
|
169
|
+
pygame.draw.circle(surface, p.color, (px, py), cur_r)
|
|
170
|
+
|
|
171
|
+
def update_and_draw(
|
|
172
|
+
self,
|
|
173
|
+
surface: pygame.Surface,
|
|
174
|
+
dt: float,
|
|
175
|
+
offset: tuple[int, int] = (0, 0),
|
|
176
|
+
) -> None:
|
|
177
|
+
self.update(dt)
|
|
178
|
+
self.draw(surface, offset=offset)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""Expanding shockwave ring effect (`ShockwaveManager`) for impact and scoring feedback."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
import pygame
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass(slots=True)
|
|
10
|
+
class ShockwaveRing:
|
|
11
|
+
x: float
|
|
12
|
+
y: float
|
|
13
|
+
radius: float
|
|
14
|
+
max_radius: float
|
|
15
|
+
speed: float
|
|
16
|
+
color: tuple[int, int, int]
|
|
17
|
+
width: int = 3
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class ShockwaveManager:
|
|
21
|
+
"""Manages expanding circular shockwaves with smooth alpha fade."""
|
|
22
|
+
|
|
23
|
+
def __init__(self) -> None:
|
|
24
|
+
self.rings: list[ShockwaveRing] = []
|
|
25
|
+
|
|
26
|
+
def clear(self) -> None:
|
|
27
|
+
self.rings.clear()
|
|
28
|
+
|
|
29
|
+
def spawn(
|
|
30
|
+
self,
|
|
31
|
+
x: float,
|
|
32
|
+
y: float,
|
|
33
|
+
max_radius: float = 65.0,
|
|
34
|
+
speed: float = 240.0,
|
|
35
|
+
color: tuple[int, int, int] = (56, 189, 248),
|
|
36
|
+
width: int = 3,
|
|
37
|
+
) -> None:
|
|
38
|
+
self.rings.append(
|
|
39
|
+
ShockwaveRing(
|
|
40
|
+
x=float(x),
|
|
41
|
+
y=float(y),
|
|
42
|
+
radius=4.0,
|
|
43
|
+
max_radius=max(10.0, float(max_radius)),
|
|
44
|
+
speed=max(20.0, float(speed)),
|
|
45
|
+
color=color,
|
|
46
|
+
width=max(1, int(width)),
|
|
47
|
+
)
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
def update(self, dt: float) -> None:
|
|
51
|
+
step = max(0.0, float(dt))
|
|
52
|
+
active: list[ShockwaveRing] = []
|
|
53
|
+
for r in self.rings:
|
|
54
|
+
r.radius += r.speed * step
|
|
55
|
+
if r.radius < r.max_radius:
|
|
56
|
+
active.append(r)
|
|
57
|
+
self.rings = active
|
|
58
|
+
|
|
59
|
+
def draw(self, surface: pygame.Surface, offset: tuple[int, int] = (0, 0)) -> None:
|
|
60
|
+
ox, oy = int(offset[0]), int(offset[1])
|
|
61
|
+
for r in self.rings:
|
|
62
|
+
progress = max(0.0, min(1.0, r.radius / r.max_radius))
|
|
63
|
+
alpha = int(210 * (1.0 - progress))
|
|
64
|
+
rad = int(round(r.radius))
|
|
65
|
+
if alpha <= 4 or rad <= 1:
|
|
66
|
+
continue
|
|
67
|
+
diam = rad * 2 + 4
|
|
68
|
+
ring_surf = pygame.Surface((diam, diam), pygame.SRCALPHA)
|
|
69
|
+
pygame.draw.circle(
|
|
70
|
+
ring_surf,
|
|
71
|
+
(*r.color[:3], alpha),
|
|
72
|
+
(diam // 2, diam // 2),
|
|
73
|
+
rad,
|
|
74
|
+
width=min(rad, r.width),
|
|
75
|
+
)
|
|
76
|
+
surface.blit(ring_surf, (int(round(r.x)) + ox - diam // 2, int(round(r.y)) + oy - diam // 2))
|
|
77
|
+
|
|
78
|
+
def update_and_draw(
|
|
79
|
+
self,
|
|
80
|
+
surface: pygame.Surface,
|
|
81
|
+
dt: float,
|
|
82
|
+
offset: tuple[int, int] = (0, 0),
|
|
83
|
+
) -> None:
|
|
84
|
+
self.update(dt)
|
|
85
|
+
self.draw(surface, offset=offset)
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"""State-machine animation controller (`Animator` & `AnimationClip`) for 2D Pygame sprites."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from typing import Callable, Sequence
|
|
7
|
+
import pygame
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass
|
|
11
|
+
class AnimationClip:
|
|
12
|
+
"""Represents a named sequence of `pygame.Surface` frames with its playback rate and loop policy."""
|
|
13
|
+
|
|
14
|
+
name: str
|
|
15
|
+
frames: list[pygame.Surface]
|
|
16
|
+
fps: float = 12.0
|
|
17
|
+
loop: bool = True
|
|
18
|
+
on_complete: Callable[[], None] | None = None
|
|
19
|
+
_scaled_cache: dict[tuple[int, int, bool, bool], list[pygame.Surface]] = field(
|
|
20
|
+
default_factory=dict,
|
|
21
|
+
init=False,
|
|
22
|
+
repr=False,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
def get_transformed_frames(
|
|
26
|
+
self,
|
|
27
|
+
scale: tuple[int, int] | None = None,
|
|
28
|
+
flip_x: bool = False,
|
|
29
|
+
flip_y: bool = False,
|
|
30
|
+
) -> list[pygame.Surface]:
|
|
31
|
+
if scale is None and not flip_x and not flip_y:
|
|
32
|
+
return self.frames
|
|
33
|
+
w = int(scale[0]) if scale is not None else self.frames[0].get_width()
|
|
34
|
+
h = int(scale[1]) if scale is not None else self.frames[0].get_height()
|
|
35
|
+
key = (w, h, bool(flip_x), bool(flip_y))
|
|
36
|
+
if key not in self._scaled_cache:
|
|
37
|
+
transformed: list[pygame.Surface] = []
|
|
38
|
+
for f in self.frames:
|
|
39
|
+
surf = f
|
|
40
|
+
if flip_x or flip_y:
|
|
41
|
+
surf = pygame.transform.flip(surf, flip_x, flip_y)
|
|
42
|
+
if (surf.get_width(), surf.get_height()) != (w, h):
|
|
43
|
+
surf = pygame.transform.smoothscale(surf, (w, h))
|
|
44
|
+
transformed.append(surf)
|
|
45
|
+
self._scaled_cache[key] = transformed
|
|
46
|
+
return self._scaled_cache[key]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class Animator:
|
|
50
|
+
"""Multi-state sprite animation controller (`idle`, `thrust`, `hit`, `shield`, etc.)."""
|
|
51
|
+
|
|
52
|
+
def __init__(self, default_state: str = "idle") -> None:
|
|
53
|
+
self.clips: dict[str, AnimationClip] = {}
|
|
54
|
+
self.current_state: str = default_state
|
|
55
|
+
self.default_state: str = default_state
|
|
56
|
+
self.frame_index: int = 0
|
|
57
|
+
self.elapsed: float = 0.0
|
|
58
|
+
self.finished: bool = False
|
|
59
|
+
|
|
60
|
+
def add_clip(
|
|
61
|
+
self,
|
|
62
|
+
name: str,
|
|
63
|
+
frames: Sequence[pygame.Surface],
|
|
64
|
+
fps: float = 12.0,
|
|
65
|
+
loop: bool = True,
|
|
66
|
+
on_complete: Callable[[], None] | None = None,
|
|
67
|
+
) -> Animator:
|
|
68
|
+
frame_list = list(frames)
|
|
69
|
+
if not frame_list:
|
|
70
|
+
raise ValueError(f"AnimationClip '{name}' requires at least 1 frame.")
|
|
71
|
+
self.clips[name] = AnimationClip(
|
|
72
|
+
name=name,
|
|
73
|
+
frames=frame_list,
|
|
74
|
+
fps=max(0.5, float(fps)),
|
|
75
|
+
loop=bool(loop),
|
|
76
|
+
on_complete=on_complete,
|
|
77
|
+
)
|
|
78
|
+
if len(self.clips) == 1:
|
|
79
|
+
self.current_state = name
|
|
80
|
+
self.default_state = name
|
|
81
|
+
return self
|
|
82
|
+
|
|
83
|
+
def play(self, state: str, force_restart: bool = False) -> None:
|
|
84
|
+
"""Switch to `state`. If already playing `state` and `force_restart` is False, continues smoothly."""
|
|
85
|
+
if state not in self.clips:
|
|
86
|
+
return
|
|
87
|
+
if self.current_state != state or force_restart or self.finished:
|
|
88
|
+
self.current_state = state
|
|
89
|
+
self.frame_index = 0
|
|
90
|
+
self.elapsed = 0.0
|
|
91
|
+
self.finished = False
|
|
92
|
+
|
|
93
|
+
def update(self, dt: float) -> pygame.Surface:
|
|
94
|
+
"""Advance the animation clock by `dt` seconds and return the active frame."""
|
|
95
|
+
clip = self.clips.get(self.current_state) or self.clips.get(self.default_state)
|
|
96
|
+
if clip is None:
|
|
97
|
+
raise RuntimeError("Animator has no registered AnimationClips.")
|
|
98
|
+
|
|
99
|
+
if not self.finished and len(clip.frames) > 1:
|
|
100
|
+
self.elapsed += max(0.0, float(dt))
|
|
101
|
+
frame_duration = 1.0 / clip.fps
|
|
102
|
+
while self.elapsed >= frame_duration:
|
|
103
|
+
self.elapsed -= frame_duration
|
|
104
|
+
self.frame_index += 1
|
|
105
|
+
if self.frame_index >= len(clip.frames):
|
|
106
|
+
if clip.loop:
|
|
107
|
+
self.frame_index = 0
|
|
108
|
+
else:
|
|
109
|
+
self.frame_index = len(clip.frames) - 1
|
|
110
|
+
self.finished = True
|
|
111
|
+
if clip.on_complete is not None:
|
|
112
|
+
clip.on_complete()
|
|
113
|
+
# Automatically return to default_state after one-shot clip finishes
|
|
114
|
+
if self.current_state != self.default_state and self.default_state in self.clips:
|
|
115
|
+
self.play(self.default_state, force_restart=True)
|
|
116
|
+
break
|
|
117
|
+
|
|
118
|
+
return self.get_frame()
|
|
119
|
+
|
|
120
|
+
def get_frame(
|
|
121
|
+
self,
|
|
122
|
+
scale: tuple[int, int] | None = None,
|
|
123
|
+
flip_x: bool = False,
|
|
124
|
+
flip_y: bool = False,
|
|
125
|
+
) -> pygame.Surface:
|
|
126
|
+
"""Return the current active `pygame.Surface` frame, optionally scaled/flipped with caching."""
|
|
127
|
+
clip = self.clips.get(self.current_state) or self.clips.get(self.default_state)
|
|
128
|
+
if clip is None:
|
|
129
|
+
raise RuntimeError("Animator has no registered AnimationClips.")
|
|
130
|
+
frames = clip.get_transformed_frames(scale=scale, flip_x=flip_x, flip_y=flip_y)
|
|
131
|
+
idx = max(0, min(self.frame_index, len(frames) - 1))
|
|
132
|
+
return frames[idx]
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"""Procedural multi-frame SpriteSheet generator (`SpriteFXGenerator`).
|
|
2
|
+
|
|
3
|
+
Transforms any static 2D sprite (`pygame.Surface` or PNG file) into rich multi-frame animation strips
|
|
4
|
+
(plasma thrusters, energy shields, 360-degree smooth rotations with corona glow, hit flashes, and
|
|
5
|
+
floating idle animations) that can be used directly in memory or saved as PNG spritesheets on disk.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import math
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import Sequence
|
|
13
|
+
import pygame
|
|
14
|
+
|
|
15
|
+
from .spritesheet import SpriteSheet
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _load_surface(source: str | Path | pygame.Surface, size: tuple[int, int] | None = None) -> pygame.Surface:
|
|
19
|
+
if isinstance(source, pygame.Surface):
|
|
20
|
+
surf = source.copy()
|
|
21
|
+
else:
|
|
22
|
+
loaded = pygame.image.load(str(source))
|
|
23
|
+
surf = loaded.convert_alpha() if pygame.display.get_init() and pygame.display.get_surface() else loaded
|
|
24
|
+
if size is not None and (surf.get_width(), surf.get_height()) != (int(size[0]), int(size[1])):
|
|
25
|
+
surf = pygame.transform.smoothscale(surf, (int(size[0]), int(size[1])))
|
|
26
|
+
return surf
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class SpriteFXGenerator:
|
|
30
|
+
"""Generates multi-frame animated `SpriteSheet` strips from static images or Pygame surfaces."""
|
|
31
|
+
|
|
32
|
+
@staticmethod
|
|
33
|
+
def generate_bobbing_strip(
|
|
34
|
+
source: str | Path | pygame.Surface,
|
|
35
|
+
num_frames: int = 8,
|
|
36
|
+
amplitude_px: int = 3,
|
|
37
|
+
glow_color: tuple[int, int, int] | None = (80, 180, 255),
|
|
38
|
+
size: tuple[int, int] | None = None,
|
|
39
|
+
) -> SpriteSheet:
|
|
40
|
+
"""Create a smooth vertical hover/breathing animation strip from a single static image."""
|
|
41
|
+
base = _load_surface(source, size=size)
|
|
42
|
+
n = max(2, int(num_frames))
|
|
43
|
+
pad = max(2, int(amplitude_px) + 4)
|
|
44
|
+
fw = base.get_width() + pad * 2
|
|
45
|
+
fh = base.get_height() + pad * 2
|
|
46
|
+
strip = pygame.Surface((fw * n, fh), pygame.SRCALPHA)
|
|
47
|
+
|
|
48
|
+
for i in range(n):
|
|
49
|
+
phase = (2.0 * math.pi * i) / n
|
|
50
|
+
dy = int(round(math.sin(phase) * amplitude_px))
|
|
51
|
+
frame = pygame.Surface((fw, fh), pygame.SRCALPHA)
|
|
52
|
+
|
|
53
|
+
if glow_color is not None:
|
|
54
|
+
pulse_alpha = int(35 + 25 * (0.5 + 0.5 * math.sin(phase)))
|
|
55
|
+
glow_radius = min(fw, fh) // 2 - 2
|
|
56
|
+
glow_surf = pygame.Surface((fw, fh), pygame.SRCALPHA)
|
|
57
|
+
pygame.draw.circle(
|
|
58
|
+
glow_surf,
|
|
59
|
+
(*glow_color[:3], pulse_alpha),
|
|
60
|
+
(fw // 2, fh // 2 + dy),
|
|
61
|
+
max(4, glow_radius),
|
|
62
|
+
)
|
|
63
|
+
frame.blit(glow_surf, (0, 0))
|
|
64
|
+
|
|
65
|
+
frame.blit(base, (pad, pad + dy))
|
|
66
|
+
strip.blit(frame, (i * fw, 0))
|
|
67
|
+
|
|
68
|
+
return SpriteSheet(strip)
|
|
69
|
+
|
|
70
|
+
@staticmethod
|
|
71
|
+
def generate_thruster_strip(
|
|
72
|
+
source: str | Path | pygame.Surface,
|
|
73
|
+
num_frames: int = 8,
|
|
74
|
+
flame_color_core: tuple[int, int, int] = (200, 245, 255),
|
|
75
|
+
flame_color_outer: tuple[int, int, int] = (0, 150, 255),
|
|
76
|
+
nozzle_side: str = "bottom",
|
|
77
|
+
size: tuple[int, int] | None = None,
|
|
78
|
+
) -> SpriteSheet:
|
|
79
|
+
"""Generate an animated plasma thruster strip for a spaceship, paddle, or character."""
|
|
80
|
+
base = _load_surface(source, size=size)
|
|
81
|
+
n = max(2, int(num_frames))
|
|
82
|
+
pad = 6
|
|
83
|
+
fw = base.get_width() + pad * 2
|
|
84
|
+
fh = base.get_height() + pad * 2
|
|
85
|
+
strip = pygame.Surface((fw * n, fh), pygame.SRCALPHA)
|
|
86
|
+
|
|
87
|
+
for i in range(n):
|
|
88
|
+
phase = (2.0 * math.pi * i) / n
|
|
89
|
+
frame = pygame.Surface((fw, fh), pygame.SRCALPHA)
|
|
90
|
+
|
|
91
|
+
# Draw oscillating plasma thruster plumes at top & bottom or behind nozzle
|
|
92
|
+
flame_len = int(6 + 5 * abs(math.sin(phase * 1.5)))
|
|
93
|
+
cx = fw // 2
|
|
94
|
+
|
|
95
|
+
if nozzle_side in ("both", "vertical", "bottom"):
|
|
96
|
+
# Bottom thruster plume
|
|
97
|
+
by = fh - pad - 2
|
|
98
|
+
pts_outer = [(cx - 7, by - 4), (cx + 7, by - 4), (cx, by + flame_len)]
|
|
99
|
+
pts_core = [(cx - 4, by - 3), (cx + 4, by - 3), (cx, by + max(3, flame_len - 3))]
|
|
100
|
+
pygame.draw.polygon(frame, (*flame_color_outer[:3], 210), pts_outer)
|
|
101
|
+
pygame.draw.polygon(frame, (*flame_color_core[:3], 245), pts_core)
|
|
102
|
+
|
|
103
|
+
if nozzle_side in ("both", "vertical", "top"):
|
|
104
|
+
ty = pad + 2
|
|
105
|
+
pts_outer_t = [(cx - 7, ty + 4), (cx + 7, ty + 4), (cx, ty - flame_len)]
|
|
106
|
+
pts_core_t = [(cx - 4, ty + 3), (cx + 4, ty + 3), (cx, ty - max(3, flame_len - 3))]
|
|
107
|
+
pygame.draw.polygon(frame, (*flame_color_outer[:3], 210), pts_outer_t)
|
|
108
|
+
pygame.draw.polygon(frame, (*flame_color_core[:3], 245), pts_core_t)
|
|
109
|
+
|
|
110
|
+
# Subtle engine rim glow
|
|
111
|
+
rim_alpha = int(45 + 35 * abs(math.cos(phase)))
|
|
112
|
+
pygame.draw.ellipse(
|
|
113
|
+
frame,
|
|
114
|
+
(*flame_color_outer[:3], rim_alpha),
|
|
115
|
+
pygame.Rect(2, 4, fw - 4, fh - 8),
|
|
116
|
+
width=2,
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
frame.blit(base, (pad, pad))
|
|
120
|
+
strip.blit(frame, (i * fw, 0))
|
|
121
|
+
|
|
122
|
+
return SpriteSheet(strip)
|
|
123
|
+
|
|
124
|
+
@staticmethod
|
|
125
|
+
def generate_shield_hit_strip(
|
|
126
|
+
source: str | Path | pygame.Surface,
|
|
127
|
+
num_frames: int = 6,
|
|
128
|
+
shield_color: tuple[int, int, int] = (56, 189, 248),
|
|
129
|
+
size: tuple[int, int] | None = None,
|
|
130
|
+
) -> SpriteSheet:
|
|
131
|
+
"""Generate an impact energy-shield pulse strip for collision feedback."""
|
|
132
|
+
base = _load_surface(source, size=size)
|
|
133
|
+
n = max(2, int(num_frames))
|
|
134
|
+
pad = 6
|
|
135
|
+
fw = base.get_width() + pad * 2
|
|
136
|
+
fh = base.get_height() + pad * 2
|
|
137
|
+
strip = pygame.Surface((fw * n, fh), pygame.SRCALPHA)
|
|
138
|
+
|
|
139
|
+
for i in range(n):
|
|
140
|
+
progress = i / max(1, n - 1)
|
|
141
|
+
envelope = math.sin(progress * math.pi)
|
|
142
|
+
frame = pygame.Surface((fw, fh), pygame.SRCALPHA)
|
|
143
|
+
frame.blit(base, (pad, pad))
|
|
144
|
+
|
|
145
|
+
# Energy shield aura expanding and fading
|
|
146
|
+
alpha = int(190 * envelope)
|
|
147
|
+
thickness = max(2, int(4 * envelope))
|
|
148
|
+
if alpha > 5:
|
|
149
|
+
rect = pygame.Rect(1, 1, fw - 2, fh - 2)
|
|
150
|
+
pygame.draw.ellipse(frame, (*shield_color[:3], alpha), rect, width=thickness)
|
|
151
|
+
inner_rect = pygame.Rect(4, 4, fw - 8, fh - 8)
|
|
152
|
+
pygame.draw.ellipse(frame, (255, 255, 255, int(alpha * 0.7)), inner_rect, width=1)
|
|
153
|
+
|
|
154
|
+
strip.blit(frame, (i * fw, 0))
|
|
155
|
+
|
|
156
|
+
return SpriteSheet(strip)
|
|
157
|
+
|
|
158
|
+
@staticmethod
|
|
159
|
+
def generate_rotation_strip(
|
|
160
|
+
source: str | Path | pygame.Surface,
|
|
161
|
+
num_frames: int = 12,
|
|
162
|
+
aura_color: tuple[int, int, int] | None = (255, 120, 40),
|
|
163
|
+
size: tuple[int, int] | None = None,
|
|
164
|
+
) -> SpriteSheet:
|
|
165
|
+
"""Generate a 360-degree smooth rotation strip with optional pulsing plasma/fire corona."""
|
|
166
|
+
base = _load_surface(source, size=size)
|
|
167
|
+
n = max(4, int(num_frames))
|
|
168
|
+
fw = base.get_width()
|
|
169
|
+
fh = base.get_height()
|
|
170
|
+
strip = pygame.Surface((fw * n, fh), pygame.SRCALPHA)
|
|
171
|
+
|
|
172
|
+
for i in range(n):
|
|
173
|
+
angle = -(360.0 * i) / n
|
|
174
|
+
phase = (2.0 * math.pi * i) / n
|
|
175
|
+
frame = pygame.Surface((fw, fh), pygame.SRCALPHA)
|
|
176
|
+
|
|
177
|
+
if aura_color is not None:
|
|
178
|
+
pulse_r = max(4, min(fw, fh) // 2 - 1)
|
|
179
|
+
alpha = int(60 + 40 * math.sin(phase * 2.0))
|
|
180
|
+
pygame.draw.circle(
|
|
181
|
+
frame,
|
|
182
|
+
(*aura_color[:3], alpha),
|
|
183
|
+
(fw // 2, fh // 2),
|
|
184
|
+
pulse_r,
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
rotated = pygame.transform.rotozoom(base, angle, 1.0)
|
|
188
|
+
rx = (fw - rotated.get_width()) // 2
|
|
189
|
+
ry = (fh - rotated.get_height()) // 2
|
|
190
|
+
frame.blit(rotated, (rx, ry))
|
|
191
|
+
strip.blit(frame, (i * fw, 0))
|
|
192
|
+
|
|
193
|
+
return SpriteSheet(strip)
|
|
194
|
+
|
|
195
|
+
@staticmethod
|
|
196
|
+
def build_multi_row_sheet(strips: Sequence[SpriteSheet], output_path: str | Path | None = None) -> SpriteSheet:
|
|
197
|
+
"""Combine multiple horizontal `SpriteSheet` strips into a single multi-row master spritesheet."""
|
|
198
|
+
if not strips:
|
|
199
|
+
raise ValueError("At least one SpriteSheet strip is required.")
|
|
200
|
+
max_w = max(s.width for s in strips)
|
|
201
|
+
row_h = max(s.height for s in strips)
|
|
202
|
+
master = pygame.Surface((max_w, row_h * len(strips)), pygame.SRCALPHA)
|
|
203
|
+
for idx, s in enumerate(strips):
|
|
204
|
+
master.blit(s.sheet, (0, idx * row_h))
|
|
205
|
+
sheet = SpriteSheet(master)
|
|
206
|
+
if output_path is not None:
|
|
207
|
+
sheet.save(output_path)
|
|
208
|
+
return sheet
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"""SpriteSheet loader and frame slicer for Pygame surfaces and image files."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Sequence
|
|
7
|
+
import pygame
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class SpriteSheet:
|
|
11
|
+
"""Slices any `pygame.Surface` or image file into individual animation frames.
|
|
12
|
+
|
|
13
|
+
Completely decoupled from any specific game architecture: accepts standard
|
|
14
|
+
file paths or `pygame.Surface` instances and returns lists of `pygame.Surface` frames.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(self, source: str | Path | pygame.Surface) -> None:
|
|
18
|
+
if isinstance(source, pygame.Surface):
|
|
19
|
+
self.sheet = source.convert_alpha() if pygame.display.get_init() and pygame.display.get_surface() else source.copy()
|
|
20
|
+
else:
|
|
21
|
+
loaded = pygame.image.load(str(source))
|
|
22
|
+
self.sheet = loaded.convert_alpha() if pygame.display.get_init() and pygame.display.get_surface() else loaded
|
|
23
|
+
|
|
24
|
+
@property
|
|
25
|
+
def width(self) -> int:
|
|
26
|
+
return self.sheet.get_width()
|
|
27
|
+
|
|
28
|
+
@property
|
|
29
|
+
def height(self) -> int:
|
|
30
|
+
return self.sheet.get_height()
|
|
31
|
+
|
|
32
|
+
def get_frame(
|
|
33
|
+
self,
|
|
34
|
+
x: int,
|
|
35
|
+
y: int,
|
|
36
|
+
width: int,
|
|
37
|
+
height: int,
|
|
38
|
+
scale: tuple[int, int] | None = None,
|
|
39
|
+
flip_x: bool = False,
|
|
40
|
+
flip_y: bool = False,
|
|
41
|
+
) -> pygame.Surface:
|
|
42
|
+
"""Extract a rectangular region `(x, y, width, height)` as an independent alpha Surface."""
|
|
43
|
+
frame = pygame.Surface((int(width), int(height)), pygame.SRCALPHA)
|
|
44
|
+
frame.blit(self.sheet, (0, 0), pygame.Rect(int(x), int(y), int(width), int(height)))
|
|
45
|
+
if flip_x or flip_y:
|
|
46
|
+
frame = pygame.transform.flip(frame, flip_x, flip_y)
|
|
47
|
+
if scale is not None and (frame.get_width(), frame.get_height()) != (int(scale[0]), int(scale[1])):
|
|
48
|
+
frame = pygame.transform.smoothscale(frame, (int(scale[0]), int(scale[1])))
|
|
49
|
+
return frame
|
|
50
|
+
|
|
51
|
+
def slice_grid(
|
|
52
|
+
self,
|
|
53
|
+
frame_width: int,
|
|
54
|
+
frame_height: int,
|
|
55
|
+
num_frames: int | None = None,
|
|
56
|
+
row: int | None = None,
|
|
57
|
+
scale: tuple[int, int] | None = None,
|
|
58
|
+
flip_x: bool = False,
|
|
59
|
+
flip_y: bool = False,
|
|
60
|
+
) -> list[pygame.Surface]:
|
|
61
|
+
"""Slice the sprite sheet into a list of frames using a fixed cell size `(frame_width, frame_height)`."""
|
|
62
|
+
fw = max(1, int(frame_width))
|
|
63
|
+
fh = max(1, int(frame_height))
|
|
64
|
+
cols = max(1, self.width // fw)
|
|
65
|
+
rows = max(1, self.height // fh)
|
|
66
|
+
|
|
67
|
+
row_indices: Sequence[int] = [int(row)] if row is not None else range(rows)
|
|
68
|
+
frames: list[pygame.Surface] = []
|
|
69
|
+
for r in row_indices:
|
|
70
|
+
if r < 0 or r >= rows:
|
|
71
|
+
continue
|
|
72
|
+
for c in range(cols):
|
|
73
|
+
frames.append(
|
|
74
|
+
self.get_frame(
|
|
75
|
+
c * fw,
|
|
76
|
+
r * fh,
|
|
77
|
+
fw,
|
|
78
|
+
fh,
|
|
79
|
+
scale=scale,
|
|
80
|
+
flip_x=flip_x,
|
|
81
|
+
flip_y=flip_y,
|
|
82
|
+
)
|
|
83
|
+
)
|
|
84
|
+
if num_frames is not None and len(frames) >= int(num_frames):
|
|
85
|
+
return frames
|
|
86
|
+
return frames
|
|
87
|
+
|
|
88
|
+
def slice_strip(
|
|
89
|
+
self,
|
|
90
|
+
num_frames: int,
|
|
91
|
+
row: int = 0,
|
|
92
|
+
total_rows: int = 1,
|
|
93
|
+
scale: tuple[int, int] | None = None,
|
|
94
|
+
flip_x: bool = False,
|
|
95
|
+
flip_y: bool = False,
|
|
96
|
+
) -> list[pygame.Surface]:
|
|
97
|
+
"""Slice a horizontal strip of `num_frames` equal-width cells from `row`."""
|
|
98
|
+
n = max(1, int(num_frames))
|
|
99
|
+
r_total = max(1, int(total_rows))
|
|
100
|
+
fw = max(1, self.width // n)
|
|
101
|
+
fh = max(1, self.height // r_total)
|
|
102
|
+
return self.slice_grid(
|
|
103
|
+
frame_width=fw,
|
|
104
|
+
frame_height=fh,
|
|
105
|
+
num_frames=n,
|
|
106
|
+
row=row,
|
|
107
|
+
scale=scale,
|
|
108
|
+
flip_x=flip_x,
|
|
109
|
+
flip_y=flip_y,
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
def save(self, output_path: str | Path) -> Path:
|
|
113
|
+
"""Persist the underlying spritesheet surface to a PNG file on disk."""
|
|
114
|
+
out = Path(output_path)
|
|
115
|
+
out.parent.mkdir(parents=True, exist_ok=True)
|
|
116
|
+
pygame.image.save(self.sheet, str(out))
|
|
117
|
+
return out
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""Velocity-reactive tapering motion trail renderer (`TrailRenderer`) for projectiles and characters."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections import deque
|
|
6
|
+
import pygame
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class TrailRenderer:
|
|
10
|
+
"""Renders a smooth, tapering, alpha-faded motion ribbon behind any moving `(x, y)` coordinate."""
|
|
11
|
+
|
|
12
|
+
def __init__(
|
|
13
|
+
self,
|
|
14
|
+
max_points: int = 16,
|
|
15
|
+
start_radius: float = 9.0,
|
|
16
|
+
color: tuple[int, int, int] = (56, 189, 248),
|
|
17
|
+
tail_color: tuple[int, int, int] | None = (99, 102, 241),
|
|
18
|
+
) -> None:
|
|
19
|
+
self.points: deque[tuple[float, float]] = deque(maxlen=max(2, int(max_points)))
|
|
20
|
+
self.start_radius = max(1.0, float(start_radius))
|
|
21
|
+
self.color = color
|
|
22
|
+
self.tail_color = tail_color if tail_color is not None else color
|
|
23
|
+
|
|
24
|
+
def clear(self) -> None:
|
|
25
|
+
self.points.clear()
|
|
26
|
+
|
|
27
|
+
def push(self, x: float, y: float) -> None:
|
|
28
|
+
"""Record the latest center position `(x, y)` of the tracked object."""
|
|
29
|
+
self.points.append((float(x), float(y)))
|
|
30
|
+
|
|
31
|
+
def draw(self, surface: pygame.Surface, offset: tuple[int, int] = (0, 0)) -> None:
|
|
32
|
+
"""Draw the tapering trail onto `surface`."""
|
|
33
|
+
n = len(self.points)
|
|
34
|
+
if n < 2:
|
|
35
|
+
return
|
|
36
|
+
ox, oy = int(offset[0]), int(offset[1])
|
|
37
|
+
for idx, (px, py) in enumerate(self.points):
|
|
38
|
+
t = (idx + 1) / n
|
|
39
|
+
radius = max(1, int(round(self.start_radius * t * 0.85)))
|
|
40
|
+
alpha = int(145 * (t ** 1.6))
|
|
41
|
+
r = int(self.tail_color[0] + (self.color[0] - self.tail_color[0]) * t)
|
|
42
|
+
g = int(self.tail_color[1] + (self.color[1] - self.tail_color[1]) * t)
|
|
43
|
+
b = int(self.tail_color[2] + (self.color[2] - self.tail_color[2]) * t)
|
|
44
|
+
|
|
45
|
+
diam = radius * 2 + 2
|
|
46
|
+
node_surf = pygame.Surface((diam, diam), pygame.SRCALPHA)
|
|
47
|
+
pygame.draw.circle(node_surf, (r, g, b, alpha), (diam // 2, diam // 2), radius)
|
|
48
|
+
surface.blit(node_surf, (int(round(px)) + ox - diam // 2, int(round(py)) + oy - diam // 2))
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyforgefx
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Modular 2D SpriteSheet Animation, Procedural Frame Generation, Particles, Motion Trails, and Camera Effects library for Pygame.
|
|
5
|
+
Author: Drakonosinc
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Drakonosinc/PyForgeFX
|
|
8
|
+
Project-URL: Repository, https://github.com/Drakonosinc/PyForgeFX
|
|
9
|
+
Keywords: pygame,spritesheet,animation,particles,visual-effects,2d-graphics,gamedev
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Topic :: Games/Entertainment
|
|
15
|
+
Classifier: Topic :: Multimedia :: Graphics
|
|
16
|
+
Requires-Python: >=3.9
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE.md
|
|
19
|
+
Requires-Dist: pygame-ce>=2.4.0; python_version >= "3.12"
|
|
20
|
+
Requires-Dist: Pillow>=10.0.0
|
|
21
|
+
Requires-Dist: numpy>=1.24.0
|
|
22
|
+
Dynamic: license-file
|
|
23
|
+
|
|
24
|
+
# ✨ PyForgeFX — Modular 2D SpriteSheet Animation, Procedural FX & Particle Engine for Pygame
|
|
25
|
+
|
|
26
|
+
[](https://deepwiki.com/Drakonosinc/PyForgeFX)
|
|
27
|
+

|
|
28
|
+

|
|
29
|
+

|
|
30
|
+
|
|
31
|
+
**PyForgeFX** (`pyforgefx`) is a lightweight, 100% framework-agnostic **2D Animation, Procedural SpriteSheet Generation, Particle System, Motion Trail, and Camera Effects library** built on top of `pygame` / `pygame-ce`.
|
|
32
|
+
|
|
33
|
+
Every component accepts standard Pygame primitives (`pygame.Surface`, `(x, y)` coordinates, `dt: float`) with **zero coupling** to any specific game loop or project structure.
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## 🚀 Key Features
|
|
38
|
+
|
|
39
|
+
| Module | Classes | Description |
|
|
40
|
+
| :--- | :--- | :--- |
|
|
41
|
+
| **`pyforgefx.sprites`** | `SpriteSheet`, `Animator`, `AnimationClip`, `SpriteFXGenerator` | Slice any spritesheet into frames, manage multi-state animations (`idle`, `thrust`, `hit`, `shield`), or procedurally generate animated spritesheets from a single static PNG. |
|
|
42
|
+
| **`pyforgefx.particles`** | `ParticleEmitter`, `ShockwaveManager` | Emit radial or directional particle bursts (`sparks`, `explosion`, `thruster`, `goal`) and expanding alpha-faded shockwave rings. |
|
|
43
|
+
| **`pyforgefx.trails`** | `TrailRenderer` | Smooth, tapering, color-interpolated motion ribbon behind fast-moving projectiles or characters. |
|
|
44
|
+
| **`pyforgefx.camera`** | `ScreenShake`, `StarfieldParallax` | Damped trauma camera shake `(offset_x, offset_y)` and multi-depth twinkling parallax starfield. |
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## ⚡ Quick Usage Example (Drop-in into any Pygame script)
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
import pygame
|
|
52
|
+
from pyforgefx import (
|
|
53
|
+
Animator,
|
|
54
|
+
ParticleEmitter,
|
|
55
|
+
ScreenShake,
|
|
56
|
+
ShockwaveManager,
|
|
57
|
+
SpriteFXGenerator,
|
|
58
|
+
TrailRenderer,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
# 1. Turn any static sprite into an animated multi-state Animator
|
|
62
|
+
idle_sheet = SpriteFXGenerator.generate_bobbing_strip("spaceship.png", num_frames=8)
|
|
63
|
+
thrust_sheet = SpriteFXGenerator.generate_thruster_strip("spaceship.png", num_frames=8)
|
|
64
|
+
hit_sheet = SpriteFXGenerator.generate_shield_hit_strip("spaceship.png", num_frames=6)
|
|
65
|
+
|
|
66
|
+
animator = (
|
|
67
|
+
Animator(default_state="idle")
|
|
68
|
+
.add_clip("idle", idle_sheet.slice_strip(8), fps=10, loop=True)
|
|
69
|
+
.add_clip("thrust", thrust_sheet.slice_strip(8), fps=16, loop=True)
|
|
70
|
+
.add_clip("hit", hit_sheet.slice_strip(6), fps=18, loop=False)
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# 2. Initialize standalone FX controllers
|
|
74
|
+
particles = ParticleEmitter()
|
|
75
|
+
shockwaves = ShockwaveManager()
|
|
76
|
+
trail = TrailRenderer(max_points=18, start_radius=10.0)
|
|
77
|
+
shake = ScreenShake()
|
|
78
|
+
|
|
79
|
+
# Inside your game loop (dt in seconds):
|
|
80
|
+
frame_surface = animator.update(dt)
|
|
81
|
+
ox, oy = shake.update(dt)
|
|
82
|
+
trail.push(ball_x, ball_y)
|
|
83
|
+
trail.draw(screen, offset=(ox, oy))
|
|
84
|
+
particles.update_and_draw(screen, dt, offset=(ox, oy))
|
|
85
|
+
shockwaves.update_and_draw(screen, dt, offset=(ox, oy))
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## 🧪 Running Tests
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
python -m unittest discover -s tests -v
|
|
94
|
+
```
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
pyforgefx/__init__.py,sha256=DndjN6Yw_qkGjUv8oFPYQbz5QW62cCCE70LBJ_xxW-E,730
|
|
2
|
+
pyforgefx/camera/__init__.py,sha256=saxgRnUigrXI1qK4zBuDTh3U50IO95uxFGCR2t9Mer8,148
|
|
3
|
+
pyforgefx/camera/parallax.py,sha256=UhFJ3t1eoGZRf8xrX0ZN0GdxK9OQOyNcMwXSA8izJ3U,2576
|
|
4
|
+
pyforgefx/camera/screen_shake.py,sha256=sAHieMwxbF4CjN9fJPGcE_1835ggJmtoFuDe-2IpLrU,1267
|
|
5
|
+
pyforgefx/particles/__init__.py,sha256=jfswv8D3CABSPCe5BA8wwDrgrEfFrkxW-TUhc4cz_Nw,182
|
|
6
|
+
pyforgefx/particles/emitter.py,sha256=tkuEmYAUHuf-qIxGABm6rEUAhXAMicMnKEJz7Xy3C-M,6074
|
|
7
|
+
pyforgefx/particles/shockwave.py,sha256=5NN-mvu3DIPJbpns59ka73uXPUvSF4iCAom6lY1jsrM,2456
|
|
8
|
+
pyforgefx/sprites/__init__.py,sha256=HganDAfUgyYL3TugLjAp-0EXA0KuaX5MlD_9el8bQ_w,201
|
|
9
|
+
pyforgefx/sprites/animator.py,sha256=BmPJxCHdkzruHC7l0pg8TWQjxkEONZxp2ElqGvyULYE,5209
|
|
10
|
+
pyforgefx/sprites/generator.py,sha256=__Sm3y8btxEGyX87d_ZFnXbpDykkcNGZVPSA2nK_YeM,8536
|
|
11
|
+
pyforgefx/sprites/spritesheet.py,sha256=vzfgedvY5cAkdmayYe1X4xC4dv2-Ja7vcO6uGqe5cMI,4181
|
|
12
|
+
pyforgefx/trails/__init__.py,sha256=yoD_JCFHA8PYbGOzMmlSMHpKDZW_ummITm0vtietiN8,71
|
|
13
|
+
pyforgefx/trails/trail_renderer.py,sha256=fNYc6bLIZoFK1dCSar0kk80nmxAJmSiEbydwea062OE,2029
|
|
14
|
+
pyforgefx-1.0.0.dist-info/licenses/LICENSE.md,sha256=x8s2HRsd0gxpsrFqAlcb7IJQtmeHlejtdJCO181QbO4,1068
|
|
15
|
+
pyforgefx-1.0.0.dist-info/METADATA,sha256=FaN3b9j1ahS19szeulAVYQFN7J3ZrDVp8McBDkL4yuY,4134
|
|
16
|
+
pyforgefx-1.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
17
|
+
pyforgefx-1.0.0.dist-info/top_level.txt,sha256=DwA_b0C7S-9wZ8ldkp0M8tpvL83FKcmrFJiGCkwxUPE,10
|
|
18
|
+
pyforgefx-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Drakonosinc
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyforgefx
|