-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_character.py
More file actions
65 lines (57 loc) · 2.5 KB
/
base_character.py
File metadata and controls
65 lines (57 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pygame as py
from game_settings import FPS
from utils import get_collision
class Character(py.sprite.Sprite):
def __init__(self, position: tuple, sprite: py.Surface):
super().__init__()
self.image = sprite
self.rect = self.image.get_rect(topleft=position)
self.direction = py.math.Vector2(0, 0)
self.movement_speed = 4
self.frame_index = 0
self.animation_speed = 0.5
self.lose_animation_timer = FPS * 2
self.is_hit = False
def animate_walking(self, animations: dict, has_idle_state: bool = False):
if has_idle_state:
if self.direction == [0, 0]:
self.image = animations["idle"]
self.frame_index += self.animation_speed
if self.direction.x < 0:
if self.frame_index >= len(animations["walk_left"]):
self.frame_index = 0
self.image = animations["walk_left"][int(self.frame_index)]
if self.direction.x > 0:
if self.frame_index >= len(animations["walk_right"]):
self.frame_index = 0
self.image = animations["walk_right"][int(self.frame_index)]
if self.direction.y > 0:
if self.frame_index >= len(animations["walk_down"]):
self.frame_index = 0
self.image = animations["walk_down"][int(self.frame_index)]
elif self.direction.y < 0:
if self.frame_index >= len(animations["walk_up"]):
self.frame_index = 0
self.image = animations["walk_up"][int(self.frame_index)]
def move(self, obstacles) -> None:
"""
Move the character's rect
Check for a potential collision on each axis
:param obstacles: list of all obstacle tiles
"""
if self.direction.x != 0:
self.rect.x += self.direction.x * self.movement_speed
collision = get_collision(self.rect, obstacles)
if collision:
if self.direction.x < 0:
self.rect.left = collision.rect.right
elif self.direction.x > 0:
self.rect.right = collision.rect.left
if self.direction.y != 0:
self.rect.y += self.direction.y * self.movement_speed
collision = get_collision(self.rect, obstacles)
if collision:
if self.direction.y > 0:
self.rect.bottom = collision.rect.top
elif self.direction.y < 0:
self.rect.top = collision.rect.bottom