Skip to content
This repository has been archived by the owner on Feb 18, 2025. It is now read-only.

Commit

Permalink
split animations
Browse files Browse the repository at this point in the history
  • Loading branch information
Virashu committed Feb 18, 2024
1 parent de783c0 commit 9dc8b86
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 59 deletions.
92 changes: 53 additions & 39 deletions danmaku/game/animated.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Animated(GameObject):
width_height (tuple[int | float, int | float]): Width and height of the object.
speed (int | float): Speed of the object.
frames (list[str]): List of frames
freq (int | float): Frequency of animation
freq (int | float | None, optional): Frequency of animation
period (int | float | None, optional): Period of animation. Defaults to None.
You can pass freq as '0' and just use period
Expand All @@ -26,8 +26,8 @@ def __init__(
xy: tuple[int | float, int | float],
width_height: tuple[int | float, int | float],
speed: int | float,
frames: dict[Direction, list[str]],
freq: int | float,
frames: list[str],
freq: int | float | None = None,
period: int | float | None = None,
) -> None:
super().__init__(xy, width_height, speed)
Expand All @@ -37,17 +37,17 @@ def __init__(
if period is not None:
self.animation_period = period
self.animation_freq = 1 / period
else:
elif freq is not None:
self.animation_freq = freq
self.animation_period = 1 / freq
else:
raise ValueError("You must pass period or freq")
self.animation_current = 0
self.animation_last = 0
self.last_direction = 0

if len(frames):
self.texture_file = self.animation_frames[Direction.STATIC][
self.animation_current
]
self.texture_file = self.animation_frames[self.animation_current]

def can_animate(self) -> bool:
"""Check if possible to animate"""
Expand All @@ -58,15 +58,56 @@ def can_animate(self) -> bool:
return True
return False

def animate(self) -> None:
"""Animate one frame if possible"""
if self.can_animate():
self.animation_current = (self.animation_current + 1) % len(
self.animation_frames
)
self.texture_file = self.animation_frames[self.animation_current]


class AnimatedDirectional(Animated):
def __init__(
self,
xy: tuple[int | float, int | float],
width_height: tuple[int | float, int | float],
speed: int | float,
frames: list[str],
freq: int | float | None = None,
period: int | float | None = None,
) -> None:
super().__init__(xy, width_height, speed, [], freq, period)
self.last_direction = 0

self.animation_frames = {
Direction.LEFT: [],
Direction.RIGHT: [],
Direction.UP: [],
Direction.DOWN: [],
Direction.STATIC: [],
}

for file in frames:
if "left" in file:
self.animation_frames[Direction.LEFT].append(file)
if "right" in file:
self.animation_frames[Direction.RIGHT].append(file)
if "up" in file:
self.animation_frames[Direction.UP].append(file)
if "down" in file:
self.animation_frames[Direction.DOWN].append(file)
if "static" in file or "idle" in file:
self.animation_frames[Direction.STATIC].append(file)

self.texture_file = self.animation_frames[Direction.STATIC][
self.animation_current
]

def animate(
self, direction_vector: tuple[int | float, int | float] = (0, 0)
) -> None:
"""Animate one frame if possible"""
# if self.can_animate():
# self.animation_current = (self.animation_current + 1) % len(
# self.animation_frames
# )
# self.texture_file = self.animation_frames[self.animation_current]

if self.can_animate():
direction = None
Expand Down Expand Up @@ -101,30 +142,3 @@ def animate(
self.animation_current
]
self.last_direction = direction

def frames_from_str(self, str_frames, adress):
files: list[str] = str_frames.split(";")
self.animation_frames = {
Direction.LEFT: [],
Direction.RIGHT: [],
Direction.UP: [],
Direction.DOWN: [],
Direction.STATIC: [],
}

for i in files:
path = f"/{adress}/{i}"
if "left" in i:
self.animation_frames[Direction.LEFT].append(path)
if "right" in i:
self.animation_frames[Direction.RIGHT].append(path)
if "up" in i:
self.animation_frames[Direction.UP].append(path)
if "down" in i:
self.animation_frames[Direction.DOWN].append(path)
if "static" in i or "idle" in i:
self.animation_frames[Direction.STATIC].append(path)

self.texture_file = self.animation_frames[Direction.STATIC][
self.animation_current
]
11 changes: 1 addition & 10 deletions danmaku/game/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,10 @@ def __init__(
pass

super().__init__((x, y), (width, height), 0, frames, 0, period=0.1)
self.texture_size = self.width, self.height
self.texture_size = int(self.width), int(self.height)

def draw(self, graphics: vgame.graphics.Graphics):
graphics.draw_sprite(self)

def update(self, delta: int | float):
self.animate()

def animate(
self, direction_vector: tuple[int | float, int | float] = (0, 0)
) -> None:
if self.can_animate():
self.animation_current = (self.animation_current + 1) % len(
self.animation_frames
)
self.texture_file = self.animation_frames[self.animation_current]
14 changes: 9 additions & 5 deletions danmaku/game/enemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from random import randint, choices
from math import sin, cos, pi

from danmaku.game.animated import Animated
from danmaku.game.animated import AnimatedDirectional
from danmaku.game.bullet import Bullet
from danmaku.database import get_enemy_type
from danmaku.game.shooter import Shooter
from danmaku.game.drop import PowerUp, Points, Drop


class Enemy(Shooter, Animated):
class Enemy(Shooter, AnimatedDirectional):
"""Enemy object."""

def __init__(
Expand Down Expand Up @@ -38,10 +38,14 @@ def __init__(
)

frames = list(map(lambda x: f"/enemy/{x}", args["texture_file"].split(";")))
Animated.__init__(
self, xy, args["texture_size"], args["speed"], frames, 0, period=0.1
AnimatedDirectional.__init__(
self,
xy,
args["texture_size"],
args["speed"],
frames,
period=0.1,
)
self.frames_from_str(args["texture_file"], "enemy")
self.texture_size = args["texture_size"]
self.my_type = object_type
self.cost: int = args["cost"]
Expand Down
16 changes: 11 additions & 5 deletions danmaku/game/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from danmaku.database import get_player_type
from danmaku.utils import constrain
from danmaku.game.shooter import Shooter
from danmaku.game.animated import Animated
from danmaku.game.animated import AnimatedDirectional


class Player(Shooter, Animated):
class Player(Shooter, AnimatedDirectional):
"""Player object."""

def __init__(
Expand Down Expand Up @@ -38,10 +38,16 @@ def __init__(

# Animation

Animated.__init__(
self, xy, args["texture_size"], args["speed"], [], 0, period=0.1
address = "player"

AnimatedDirectional.__init__(
self,
xy,
args["texture_size"],
args["speed"],
[f"{address}/{i}" for i in args["texture_file"].split(";")],
period=0.1,
)
self.frames_from_str(args["texture_file"], "player")

self.texture_size = args["texture_size"]

Expand Down

0 comments on commit 9dc8b86

Please sign in to comment.