Skip to content

Commit a3392cf

Browse files
authored
Simplify animation (#2033)
1 parent 905447c commit a3392cf

File tree

2 files changed

+6
-34
lines changed

2 files changed

+6
-34
lines changed

arcade/sprite/animated.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class TextureKeyframe:
2222
:param duration: Duration in milliseconds to display this keyframe.
2323
:param tile_id: Tile ID for this keyframe (only used for tiled maps)
2424
"""
25+
__slots__ = ("texture", "duration", "tile_id")
2526
def __init__(
2627
self,
2728
texture: Texture,
@@ -46,10 +47,12 @@ class TextureAnimation:
4647
:param keyframes: List of keyframes for the animation.
4748
:param loop: If the animation should loop.
4849
"""
49-
def __init__(self, keyframes: Optional[List[TextureKeyframe]] = None):
50-
self._keyframes = keyframes or []
50+
__slots__ = ("_keyframes", "_duration_ms", "_timeline")
51+
52+
def __init__(self, keyframes: List[TextureKeyframe]):
53+
self._keyframes = keyframes
5154
self._duration_ms = 0
52-
self._timeline: List[int] = self._create_timeline(self._keyframes) if self._keyframes else []
55+
self._timeline: List[int] = self._create_timeline(self._keyframes)
5356

5457
@property
5558
def keyframes(self) -> Tuple[TextureKeyframe, ...]:
@@ -94,25 +97,6 @@ def _create_timeline(self, keyframes: List[TextureKeyframe]) -> List[int]:
9497
self._duration_ms = current_time_ms
9598
return timeline
9699

97-
def append_keyframe(self, keyframe: TextureKeyframe) -> None:
98-
"""
99-
Add a keyframe to the animation.
100-
101-
:param keyframe: Keyframe to add.
102-
"""
103-
self._keyframes.append(keyframe)
104-
self._timeline.append(self._duration_ms)
105-
self._timeline = self._create_timeline(self._keyframes)
106-
107-
def remove_keyframe(self, index: int) -> None:
108-
"""
109-
Remove a keyframe from the animation.
110-
111-
:param index: Index of the keyframe to remove.
112-
"""
113-
del self._keyframes[index]
114-
self._timeline = self._create_timeline(self._keyframes)
115-
116100
def get_keyframe(self, time: float, loop: bool = True) -> Tuple[int, TextureKeyframe]:
117101
"""
118102
Get the frame at a given time.

tests/unit/sprite/test_sprite_texture_animation.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,6 @@ def test_animation(keyframes):
3838
"""Test animation class"""
3939
anim = arcade.TextureAnimation(keyframes=keyframes)
4040

41-
# Add keyframe
42-
anim.append_keyframe(arcade.TextureKeyframe(keyframes[0].texture, 1000))
43-
assert anim.num_frames == 9
44-
assert anim.duration_ms == 9000
45-
assert anim.duration_seconds == 9.0
46-
47-
# Remove keyframe
48-
anim.remove_keyframe(8)
49-
assert anim.num_frames == 8
50-
assert anim.duration_ms == 8000
51-
assert anim.duration_seconds == 8.0
52-
5341
# Get keyframes at specific times (0.5s increments)
5442
for i in range(16):
5543
time = i / 2

0 commit comments

Comments
 (0)