WMICraft/logic/level.py

145 lines
6.1 KiB
Python
Raw Normal View History

2022-04-10 20:28:50 +02:00
import random
import pygame
2022-04-11 00:01:57 +02:00
from algorithms.bfs import graphsearch, State
2022-04-10 20:28:50 +02:00
from common.constants import *
2022-04-11 13:13:03 +02:00
from common.helpers import castle_neighbors
2022-04-11 00:01:57 +02:00
from logic.knights_queue import KnightsQueue
2022-04-10 20:28:50 +02:00
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()
2022-04-11 00:01:57 +02:00
self.map = [[' ' for x in range(COLUMNS)] for y in range(ROWS)]
2022-04-10 20:28:50 +02:00
2022-04-11 00:01:57 +02:00
self.list_knights_blue = []
self.list_knights_red = []
self.list_monsters = []
self.list_castles = []
self.knights_queue = None
2022-04-10 20:28:50 +02:00
2022-04-11 00:01:57 +02:00
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)
2022-04-10 20:28:50 +02:00
spawner.spawn_where_possible(['w' for x in range(NBR_OF_WATER)])
spawner.spawn_where_possible(['t' for x in range(NBR_OF_TREES)])
2022-04-11 00:01:57 +02:00
spawner.spawn_in_area(['k_b' for x in range(4)], LEFT_KNIGHTS_SPAWN_FIRST_ROW, LEFT_KNIGHTS_SPAWN_FIRST_COL,
2022-04-10 20:28:50 +02:00
KNIGHTS_SPAWN_WIDTH, KNIGHTS_SPAWN_HEIGHT)
2022-04-11 00:01:57 +02:00
spawner.spawn_in_area(['k_r' for x in range(4)], RIGHT_KNIGHTS_SPAWN_FIRST_ROW, RIGHT_KNIGHTS_SPAWN_FIRST_COL,
2022-04-10 20:28:50 +02:00
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)])
2022-04-11 00:01:57 +02:00
def setup_base_tiles(self):
2022-04-10 20:28:50 +02:00
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))
2022-04-11 00:01:57 +02:00
for row_index, row in enumerate(self.map):
2022-04-10 20:28:50 +02:00
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]
2022-04-11 12:00:15 +02:00
Tile((col_index, row_index), texture_surface, self.sprites, 'w')
2022-04-10 20:28:50 +02:00
elif col == "t":
texture_index = 6
texture_surface = textures[texture_index][1]
2022-04-11 12:00:15 +02:00
Tile((col_index, row_index), texture_surface, self.sprites, 't')
2022-04-10 20:28:50 +02:00
else:
texture_index = random.randint(0, 4)
texture_surface = textures[texture_index][1]
2022-04-11 12:00:15 +02:00
Tile((col_index, row_index), texture_surface, self.sprites)
2022-04-11 00:01:57 +02:00
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):
2022-04-10 20:28:50 +02:00
# add objects, e.g. knights, monsters, castle
2022-04-11 00:01:57 +02:00
if col == "k_b":
2022-04-11 12:00:15 +02:00
knight = Knight((col_index, row_index), self.sprites, "blue")
2022-04-11 00:01:57 +02:00
self.map[row_index][col_index] = knight
self.list_knights_blue.append(knight)
elif col == "k_r":
2022-04-11 12:00:15 +02:00
knight = Knight((col_index, row_index), self.sprites, "red")
2022-04-11 00:01:57 +02:00
self.map[row_index][col_index] = knight
self.list_knights_red.append(knight)
2022-04-10 20:28:50 +02:00
elif col == "m":
2022-04-11 12:00:15 +02:00
monster = Monster((col_index, row_index), self.sprites)
2022-04-11 00:01:57 +02:00
self.map[row_index][col_index] = monster
self.list_monsters.append(monster)
2022-04-10 20:28:50 +02:00
elif col == "c":
castle_count += 1
if castle_count == 4:
2022-04-11 12:00:15 +02:00
castle = Castle((col_index, row_index), self.sprites)
2022-04-11 00:01:57 +02:00
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]
2022-04-11 12:00:15 +02:00
state = State(knight_pos_y, knight_pos_x, current_knight.direction)
2022-04-11 12:56:22 +02:00
castle_cords = (self.list_castles[0].position[0], self.list_castles[0].position[1])
2022-04-11 13:13:03 +02:00
goal_list = castle_neighbors(self.map, castle_cords[0], castle_cords[1]) # list of castle neighbors
action_list = graphsearch(state, self.map, goal_list)
2022-04-11 00:01:57 +02:00
print(action_list)
if len(action_list) == 0:
return
2022-04-11 12:00:15 +02:00
next_action = action_list.pop(0)
if next_action == ACTION.get("rotate_left"):
2022-04-11 00:01:57 +02:00
current_knight.rotate_left()
2022-04-11 12:00:15 +02:00
elif next_action == ACTION.get("rotate_right"):
2022-04-11 00:01:57 +02:00
current_knight.rotate_right()
2022-04-11 12:00:15 +02:00
elif next_action == ACTION.get("go"):
2022-04-11 00:01:57 +02:00
current_knight.step_forward()
2022-04-11 12:00:15 +02:00
self.map[knight_pos_y][knight_pos_x] = ' '
2022-04-11 00:01:57 +02:00
2022-04-11 12:00:15 +02:00
# update knight on map
2022-04-11 00:01:57 +02:00
if current_knight.direction.name == 'UP':
2022-04-11 12:00:15 +02:00
self.map[knight_pos_y - 1][knight_pos_x] = current_knight
2022-04-11 00:01:57 +02:00
elif current_knight.direction.name == 'RIGHT':
2022-04-11 12:00:15 +02:00
self.map[knight_pos_y][knight_pos_x + 1] = current_knight
2022-04-11 00:01:57 +02:00
elif current_knight.direction.name == 'DOWN':
2022-04-11 12:00:15 +02:00
self.map[knight_pos_y + 1][knight_pos_x] = current_knight
2022-04-11 00:01:57 +02:00
elif current_knight.direction.name == 'LEFT':
2022-04-11 12:00:15 +02:00
self.map[knight_pos_y][knight_pos_x - 1] = current_knight
2022-04-11 00:01:57 +02:00
def update(self):
2022-04-10 20:28:50 +02:00
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)