WMICraft/models/knight.py

81 lines
2.8 KiB
Python
Raw Permalink Normal View History

2022-03-24 12:54:22 +01:00
import random
2022-03-11 19:42:17 +01:00
2022-04-12 20:21:29 +02:00
import pygame.image
2022-04-11 00:01:57 +02:00
from common.constants import GRID_CELL_SIZE, Direction
from common.helpers import parse_cord
from logic.health_bar import HealthBar
2022-04-10 20:28:50 +02:00
def load_knight_textures():
random_index = random.randint(1, 4)
states = [
pygame.image.load(f'resources/textures/knight_{random_index}_up.png').convert_alpha(), # up = 0
pygame.image.load(f'resources/textures/knight_{random_index}_right.png').convert_alpha(), # right = 1
pygame.image.load(f'resources/textures/knight_{random_index}_down.png').convert_alpha(), # down = 2
pygame.image.load(f'resources/textures/knight_{random_index}_left.png').convert_alpha(), # left = 3
]
return states
2022-03-11 19:42:17 +01:00
class Knight(pygame.sprite.Sprite):
2022-04-10 20:28:50 +02:00
def __init__(self, screen, position, group, team):
2022-04-10 20:28:50 +02:00
super().__init__(group)
2022-04-11 00:01:57 +02:00
self.direction = Direction.DOWN
2022-04-10 20:28:50 +02:00
self.states = load_knight_textures()
2022-04-11 00:01:57 +02:00
self.image = self.states[self.direction.value]
self.position = position
self._layer = 1
2022-04-11 12:00:15 +02:00
position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
2022-04-11 00:01:57 +02:00
self.rect = self.image.get_rect(topleft=position_in_px)
2022-04-11 00:01:57 +02:00
self.team = team
2022-04-28 14:13:59 +02:00
self.max_hp = random.randint(7, 12)
self.attack = random.randint(4, 7)
2022-04-10 20:28:50 +02:00
self.defense = random.randint(1, 4)
self.points = 1
self.health_bar = HealthBar(screen, self.rect, current_hp=self.max_hp, max_hp=self.max_hp, calculate_xy=True, calculate_size=True)
2022-03-11 19:42:17 +01:00
2022-04-11 00:01:57 +02:00
def rotate_left(self):
2022-04-11 12:00:15 +02:00
self.direction = self.direction.left()
2022-04-11 00:01:57 +02:00
self.image = self.states[self.direction.value]
def update(self):
self.health_bar.update()
2022-04-11 00:01:57 +02:00
def rotate_right(self):
2022-04-11 12:00:15 +02:00
self.direction = self.direction.right()
2022-04-11 00:01:57 +02:00
self.image = self.states[self.direction.value]
2022-04-10 20:28:50 +02:00
def take_dmg(self, amount):
self.health_bar.take_dmg(amount)
def heal(self, amount):
self.health_bar.heal(amount)
def get_current_hp(self):
return self.health_bar.current_hp
def get_max_hp(self):
return self.health_bar.max_hp
2022-04-10 20:28:50 +02:00
def step_forward(self):
2022-04-11 00:01:57 +02:00
if self.direction.name == 'UP':
2022-04-11 12:00:15 +02:00
self.position = (self.position[0], self.position[1] - 1)
2022-04-11 00:01:57 +02:00
self.rect.y = self.rect.y - GRID_CELL_SIZE - 5
elif self.direction.name == 'RIGHT':
2022-04-11 12:00:15 +02:00
self.position = (self.position[0] + 1, self.position[1])
2022-04-11 00:01:57 +02:00
self.rect.x = self.rect.x + GRID_CELL_SIZE + 5
elif self.direction.name == 'DOWN':
2022-04-11 12:00:15 +02:00
self.position = (self.position[0], self.position[1] + 1)
2022-04-11 00:01:57 +02:00
self.rect.y = self.rect.y + GRID_CELL_SIZE + 5
elif self.direction.name == 'LEFT':
2022-04-11 12:00:15 +02:00
self.position = (self.position[0] - 1, self.position[1])
2022-04-11 00:01:57 +02:00
self.rect.x = self.rect.x - GRID_CELL_SIZE - 5
2022-04-12 20:21:29 +02:00
def team_alias(self) -> str:
return "k_b" if self.team == "blue" else "k_r"