WMICraft/models/knight.py

49 lines
1.6 KiB
Python
Raw Normal View History

2022-03-11 19:42:17 +01:00
import pygame.image
2022-03-24 12:54:22 +01:00
import random
2022-03-11 19:42:17 +01:00
2022-04-10 20:28:50 +02:00
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
2022-03-11 19:42:17 +01:00
class Knight(pygame.sprite.Sprite):
2022-04-10 20:28:50 +02:00
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)
2022-04-10 20:28:50 +02:00
self.defense = random.randint(1, 4)
self.points = 1
2022-03-11 19:42:17 +01:00
2022-04-10 20:28:50 +02:00
# 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