diff --git a/danmaku/game/animated.py b/danmaku/game/animated.py index 9847ab6..c39ef68 100644 --- a/danmaku/game/animated.py +++ b/danmaku/game/animated.py @@ -155,3 +155,43 @@ def animate( self.animation_current ] self.last_direction = direction + + def animate_absolute( + self, time: int, direction_vector: tuple[int | float, int | float] = (0, 0) + ) -> None: + """Set frame according to time (milliseconds)""" + + direction = None + if direction_vector[0] == direction_vector[1] and direction_vector[0] == 0: + direction = Direction.STATIC + elif direction_vector[0] > 0: + direction = Direction.RIGHT + elif direction_vector[1] > 0: + direction = Direction.DOWN + elif direction_vector[0] < 0: + direction = Direction.LEFT + elif direction_vector[1] < 0: + direction = Direction.UP + + if direction == Direction.STATIC: + a = "" + if self.last_direction == Direction.UP: + a = "up" + elif self.last_direction == Direction.DOWN: + a = "down" + elif self.last_direction == Direction.RIGHT: + a = "right" + elif self.last_direction == Direction.LEFT: + a = "left" + for i in self.animation_frames[direction]: + if a in i: + self.texture_file = i + + elif direction is not None: + frame_duration = self.animation_period * 1000 + + self.animation_current = int(time // frame_duration) % len( + self.animation_frames[direction] + ) + self.texture_file = self.animation_frames[direction][self.animation_current] + self.last_direction = direction diff --git a/danmaku/game/game.py b/danmaku/game/game.py index 6b4b93d..4129083 100644 --- a/danmaku/game/game.py +++ b/danmaku/game/game.py @@ -169,6 +169,8 @@ def load(self): self.player.set_bounds(0, 0, self.game_border, self.height) + self.animation_start_time = pygame.time.get_ticks() + def update_pause(self): """Called from update loop if paused""" self.pause_object.update(self.pressed_keys) @@ -214,7 +216,6 @@ def update_game(self): self.player.vx, self.player.vy = vx, vy self.player.update(self.delta) - self.player.animate((self.player.vx, self.player.vy)) stage = self.levels[self.current_level].stage stage.update() @@ -229,7 +230,6 @@ def update_game(self): if self.player.collision(enemy): self.player.get_damage(enemy.damage / 100) self.bullets += enemy.shoot() - enemy.animate((enemy.vx, enemy.vy)) enemy.update(self.delta) if ( enemy.y > self.height / 2 and not 0 <= enemy.x < self.game_border @@ -278,8 +278,6 @@ def update_game(self): ): self.drops.remove(drop) - self.background_object.animate() - if len(self.enemies) == 0: self.next_level() @@ -335,12 +333,17 @@ def update(self): self.update_game() def draw(self): + animation_time = pygame.time.get_ticks() - self.animation_start_time + self.background_object.animate_absolute(animation_time) + self.graphics.rectangle((0, 0), (self.width, self.height), (30, 157, 214, 180)) self.graphics.draw_sprite(self.background_object) + self.player.animate_absolute(animation_time, (self.player.vx, self.player.vy)) self.player.draw(self.graphics) for enemy in self.enemies: + enemy.animate_absolute(animation_time, (enemy.vx, enemy.vy)) self.graphics.draw_sprite(enemy) for bullet in self.bullets: