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

Commit

Permalink
Move animations to draw thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Virashu committed Feb 20, 2024
1 parent 9985f47 commit 8c3da3b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
40 changes: 40 additions & 0 deletions danmaku/game/animated.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 7 additions & 4 deletions danmaku/game/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -278,8 +278,6 @@ def update_game(self):
):
self.drops.remove(drop)

self.background_object.animate()

if len(self.enemies) == 0:
self.next_level()

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 8c3da3b

Please sign in to comment.