import pygame.image import random from common.constants import GRID_CELL_SIZE 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 class Knight(pygame.sprite.Sprite): def __init__(self, position, group): super().__init__(group) self.direction = 2 self.states = load_knight_textures() self.image = self.states[self.direction] self.rect = self.image.get_rect(topleft=position) self.health = random.randint(7, 12) self.attack = random.randint(4, 7) self.defense = random.randint(1, 4) self.points = 1 # direction arg = -1 to rotate left # direction arg = 1 to rotate right def rotate(self, direction): self.direction = (self.direction + direction) % 4 self.image = self.states[self.direction] def step_forward(self): if self.direction == 0: self.rect.y = self.rect.y - GRID_CELL_SIZE elif self.direction == 1: self.rect.x = self.rect.x + GRID_CELL_SIZE elif self.direction == 2: self.rect.y = self.rect.y + GRID_CELL_SIZE else: self.rect.x = self.rect.x - GRID_CELL_SIZE