145 lines
6.1 KiB
Python
145 lines
6.1 KiB
Python
import random
|
|
|
|
import pygame
|
|
|
|
from algorithms.bfs import graphsearch, State
|
|
from common.constants import *
|
|
from common.helpers import castle_neighbors
|
|
from logic.knights_queue import KnightsQueue
|
|
from logic.spawner import Spawner
|
|
from models.castle import Castle
|
|
from models.knight import Knight
|
|
from models.monster import Monster
|
|
from models.tile import Tile
|
|
|
|
|
|
class Level:
|
|
def __init__(self, screen):
|
|
self.screen = screen
|
|
|
|
# sprite group setup
|
|
self.sprites = pygame.sprite.Group()
|
|
|
|
self.map = [[' ' for x in range(COLUMNS)] for y in range(ROWS)]
|
|
|
|
self.list_knights_blue = []
|
|
self.list_knights_red = []
|
|
self.list_monsters = []
|
|
self.list_castles = []
|
|
|
|
self.knights_queue = None
|
|
|
|
def create_map(self):
|
|
self.generate_map()
|
|
self.setup_base_tiles()
|
|
self.setup_objects()
|
|
self.knights_queue = KnightsQueue(self.list_knights_blue, self.list_knights_red)
|
|
|
|
def generate_map(self):
|
|
spawner = Spawner(self.map)
|
|
spawner.spawn_where_possible(['w' for x in range(NBR_OF_WATER)])
|
|
spawner.spawn_where_possible(['t' for x in range(NBR_OF_TREES)])
|
|
|
|
spawner.spawn_in_area(['k_b' for x in range(4)], LEFT_KNIGHTS_SPAWN_FIRST_ROW, LEFT_KNIGHTS_SPAWN_FIRST_COL,
|
|
KNIGHTS_SPAWN_WIDTH, KNIGHTS_SPAWN_HEIGHT)
|
|
spawner.spawn_in_area(['k_r' for x in range(4)], RIGHT_KNIGHTS_SPAWN_FIRST_ROW, RIGHT_KNIGHTS_SPAWN_FIRST_COL,
|
|
KNIGHTS_SPAWN_WIDTH, KNIGHTS_SPAWN_HEIGHT)
|
|
|
|
spawner.spawn_in_area(['c'], CASTLE_SPAWN_FIRST_ROW, CASTLE_SPAWN_FIRST_COL, CASTLE_SPAWN_WIDTH,
|
|
CASTLE_SPAWN_HEIGHT, 2)
|
|
|
|
spawner.spawn_where_possible(['m' for x in range(NBR_OF_MONSTERS)])
|
|
|
|
def setup_base_tiles(self):
|
|
textures = []
|
|
for texture_path in TILES:
|
|
converted_texture = pygame.image.load('resources/textures/' + texture_path).convert_alpha()
|
|
converted_texture = pygame.transform.scale(converted_texture, (40, 40))
|
|
textures.append((texture_path, converted_texture))
|
|
|
|
for row_index, row in enumerate(self.map):
|
|
for col_index, col in enumerate(row):
|
|
|
|
# add base tiles, e.g. water, tree, grass
|
|
if col == "w":
|
|
texture_index = 5
|
|
texture_surface = textures[texture_index][1]
|
|
Tile((col_index, row_index), texture_surface, self.sprites, 'w')
|
|
elif col == "t":
|
|
texture_index = 6
|
|
texture_surface = textures[texture_index][1]
|
|
Tile((col_index, row_index), texture_surface, self.sprites, 't')
|
|
else:
|
|
texture_index = random.randint(0, 4)
|
|
texture_surface = textures[texture_index][1]
|
|
Tile((col_index, row_index), texture_surface, self.sprites)
|
|
|
|
def setup_objects(self):
|
|
castle_count = 0 # TODO: find some smarter method to print castle
|
|
|
|
for row_index, row in enumerate(self.map):
|
|
print(row)
|
|
for col_index, col in enumerate(row):
|
|
|
|
# add objects, e.g. knights, monsters, castle
|
|
if col == "k_b":
|
|
knight = Knight((col_index, row_index), self.sprites, "blue")
|
|
self.map[row_index][col_index] = knight
|
|
self.list_knights_blue.append(knight)
|
|
elif col == "k_r":
|
|
knight = Knight((col_index, row_index), self.sprites, "red")
|
|
self.map[row_index][col_index] = knight
|
|
self.list_knights_red.append(knight)
|
|
elif col == "m":
|
|
monster = Monster((col_index, row_index), self.sprites)
|
|
self.map[row_index][col_index] = monster
|
|
self.list_monsters.append(monster)
|
|
elif col == "c":
|
|
castle_count += 1
|
|
if castle_count == 4:
|
|
castle = Castle((col_index, row_index), self.sprites)
|
|
self.map[row_index][col_index] = castle
|
|
self.list_castles.append(castle)
|
|
|
|
def handle_turn(self):
|
|
print("next turn")
|
|
current_knight = self.knights_queue.dequeue_knight()
|
|
knight_pos_x = current_knight.position[0]
|
|
knight_pos_y = current_knight.position[1]
|
|
state = State(knight_pos_y, knight_pos_x, current_knight.direction)
|
|
|
|
castle_cords = (self.list_castles[0].position[0], self.list_castles[0].position[1])
|
|
goal_list = castle_neighbors(self.map, castle_cords[0], castle_cords[1]) # list of castle neighbors
|
|
action_list = graphsearch(state, self.map, goal_list)
|
|
print(action_list)
|
|
|
|
if len(action_list) == 0:
|
|
return
|
|
|
|
next_action = action_list.pop(0)
|
|
if next_action == ACTION.get("rotate_left"):
|
|
current_knight.rotate_left()
|
|
elif next_action == ACTION.get("rotate_right"):
|
|
current_knight.rotate_right()
|
|
elif next_action == ACTION.get("go"):
|
|
current_knight.step_forward()
|
|
self.map[knight_pos_y][knight_pos_x] = ' '
|
|
|
|
# update knight on map
|
|
if current_knight.direction.name == 'UP':
|
|
self.map[knight_pos_y - 1][knight_pos_x] = current_knight
|
|
elif current_knight.direction.name == 'RIGHT':
|
|
self.map[knight_pos_y][knight_pos_x + 1] = current_knight
|
|
elif current_knight.direction.name == 'DOWN':
|
|
self.map[knight_pos_y + 1][knight_pos_x] = current_knight
|
|
elif current_knight.direction.name == 'LEFT':
|
|
self.map[knight_pos_y][knight_pos_x - 1] = current_knight
|
|
|
|
def update(self):
|
|
bg_width = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH
|
|
bg_height = (GRID_CELL_PADDING + GRID_CELL_SIZE) * ROWS + BORDER_WIDTH
|
|
pygame.draw.rect(self.screen, (255, 255, 255), pygame.Rect(5, 5, bg_width, bg_height), 0, BORDER_RADIUS)
|
|
|
|
# update and draw the game
|
|
self.sprites.draw(self.screen)
|