worked on bfs;
This commit is contained in:
parent
8196a5ec62
commit
57cdb87820
@ -30,10 +30,10 @@ def goal_test(state: State):
|
||||
def get_successors(state: State, map):
|
||||
successors = list()
|
||||
|
||||
state_left = State(state.row, state.column, state.direction.right())
|
||||
state_left = State(state.row, state.column, state.direction.left())
|
||||
successors.append((ACTION.get("rotate_left"), state_left))
|
||||
|
||||
state_right = State(state.row, state.column, state.direction.left())
|
||||
state_right = State(state.row, state.column, state.direction.right())
|
||||
successors.append((ACTION.get("rotate_right"), state_right))
|
||||
|
||||
target = go(state.row, state.column, state.direction)
|
||||
@ -95,8 +95,7 @@ def graphsearch(initial_state: State, map, fringe: List[Node] = None, explored:
|
||||
|
||||
# make sure not to fall into a cycle
|
||||
successor_state = (successor[1].row, successor[1].column, successor[1].direction)
|
||||
if successor_state not in fringe_states and \
|
||||
successor_state not in explored_states:
|
||||
if successor_state not in fringe_states and successor_state not in explored_states:
|
||||
# create new Node and add it at the end of fringe
|
||||
new_node = Node(state=successor[1],
|
||||
parent=element,
|
||||
@ -122,7 +121,6 @@ def go(row, column, direction):
|
||||
|
||||
|
||||
def is_valid_move(map, target_row, target_column):
|
||||
# TODO: check collisions with objects
|
||||
if 0 <= target_row < ROWS and 0 <= target_column < COLUMNS:
|
||||
return True
|
||||
|
||||
|
@ -47,11 +47,11 @@ class Direction(Enum):
|
||||
DOWN = 2
|
||||
LEFT = 3
|
||||
|
||||
def left(self):
|
||||
def right(self):
|
||||
v = (self.value + 1) % 4
|
||||
return Direction(v)
|
||||
|
||||
def right(self):
|
||||
def left(self):
|
||||
v = (self.value - 1) % 4
|
||||
return Direction(v)
|
||||
|
||||
|
@ -4,7 +4,6 @@ import pygame
|
||||
|
||||
from common.constants import *
|
||||
from common.helpers import print_numbers
|
||||
from logic.knights_queue import KnightsQueue
|
||||
from logic.level import Level
|
||||
from ui.logs import Logs
|
||||
from ui.screens.credits import Credits
|
||||
@ -59,7 +58,7 @@ class Game:
|
||||
running = False
|
||||
if event.key == 110: # clicked n letter on keyboard
|
||||
print_numbers_flag = not print_numbers_flag
|
||||
if event.type == NEXT_TURN: # is called every 't' milliseconds
|
||||
if event.type == NEXT_TURN: # is called every 'TURN_INTERVAL' milliseconds
|
||||
self.level.handle_turn()
|
||||
|
||||
stats.draw(self.screen)
|
||||
|
@ -63,15 +63,15 @@ class Level:
|
||||
if col == "w":
|
||||
texture_index = 5
|
||||
texture_surface = textures[texture_index][1]
|
||||
Tile((row_index, col_index), texture_surface, self.sprites, 'w')
|
||||
Tile((col_index, row_index), texture_surface, self.sprites, 'w')
|
||||
elif col == "t":
|
||||
texture_index = 6
|
||||
texture_surface = textures[texture_index][1]
|
||||
Tile((row_index, col_index), texture_surface, self.sprites, 't')
|
||||
Tile((col_index, row_index), texture_surface, self.sprites, 't')
|
||||
else:
|
||||
texture_index = random.randint(0, 4)
|
||||
texture_surface = textures[texture_index][1]
|
||||
Tile((row_index, col_index), texture_surface, self.sprites)
|
||||
Tile((col_index, row_index), texture_surface, self.sprites)
|
||||
|
||||
def setup_objects(self):
|
||||
castle_count = 0 # TODO: find some smarter method to print castle
|
||||
@ -82,52 +82,55 @@ class Level:
|
||||
|
||||
# add objects, e.g. knights, monsters, castle
|
||||
if col == "k_b":
|
||||
knight = Knight((row_index, col_index), self.sprites, "blue")
|
||||
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((row_index, col_index), self.sprites, "red")
|
||||
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((row_index, col_index), self.sprites)
|
||||
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((row_index, col_index), self.sprites)
|
||||
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.list_knights_red[0]
|
||||
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_x, knight_pos_y, current_knight.direction)
|
||||
state = State(knight_pos_y, knight_pos_x, current_knight.direction)
|
||||
action_list = graphsearch(state, self.map)
|
||||
print(action_list)
|
||||
|
||||
if len(action_list) == 0:
|
||||
return
|
||||
|
||||
if action_list[0] == ACTION.get("rotate_left"):
|
||||
next_action = action_list.pop(0)
|
||||
if next_action == ACTION.get("rotate_left"):
|
||||
current_knight.rotate_left()
|
||||
elif action_list[0] == ACTION.get("rotate_right"):
|
||||
elif next_action == ACTION.get("rotate_right"):
|
||||
current_knight.rotate_right()
|
||||
elif action_list[0] == ACTION.get("go"):
|
||||
elif next_action == ACTION.get("go"):
|
||||
current_knight.step_forward()
|
||||
self.map[knight_pos_x][knight_pos_y] = ' '
|
||||
self.map[knight_pos_y][knight_pos_x] = ' '
|
||||
|
||||
# update knight on map
|
||||
if current_knight.direction.name == 'UP':
|
||||
self.map[knight_pos_x][knight_pos_y - 1] = current_knight
|
||||
self.map[knight_pos_y - 1][knight_pos_x] = current_knight
|
||||
elif current_knight.direction.name == 'RIGHT':
|
||||
self.map[knight_pos_x + 1][knight_pos_y] = current_knight
|
||||
self.map[knight_pos_y][knight_pos_x + 1] = current_knight
|
||||
elif current_knight.direction.name == 'DOWN':
|
||||
self.map[knight_pos_x][knight_pos_y + 1] = current_knight
|
||||
self.map[knight_pos_y + 1][knight_pos_x] = current_knight
|
||||
elif current_knight.direction.name == 'LEFT':
|
||||
self.map[knight_pos_x - 1][knight_pos_y] = current_knight
|
||||
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
|
||||
|
@ -8,6 +8,6 @@ class Castle(pygame.sprite.Sprite):
|
||||
super().__init__(group)
|
||||
self.image = pygame.image.load("./resources/textures/castle.png").convert_alpha()
|
||||
self.image = pygame.transform.scale(self.image, (78, 78))
|
||||
position_in_px = (parse_cord(position[1]), parse_cord(position[0]))
|
||||
position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
|
||||
self.rect = self.image.get_rect(center=position_in_px)
|
||||
self.health = 80
|
||||
|
@ -26,7 +26,7 @@ class Knight(pygame.sprite.Sprite):
|
||||
|
||||
self.image = self.states[self.direction.value]
|
||||
self.position = position
|
||||
position_in_px = (parse_cord(position[1]), parse_cord(position[0]))
|
||||
position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
|
||||
self.rect = self.image.get_rect(topleft=position_in_px)
|
||||
|
||||
self.team = team
|
||||
@ -36,19 +36,23 @@ class Knight(pygame.sprite.Sprite):
|
||||
self.points = 1
|
||||
|
||||
def rotate_left(self):
|
||||
self.direction = self.direction.right()
|
||||
self.direction = self.direction.left()
|
||||
self.image = self.states[self.direction.value]
|
||||
|
||||
def rotate_right(self):
|
||||
self.direction = self.direction.left()
|
||||
self.direction = self.direction.right()
|
||||
self.image = self.states[self.direction.value]
|
||||
|
||||
def step_forward(self):
|
||||
if self.direction.name == 'UP':
|
||||
self.position = (self.position[0], self.position[1] - 1)
|
||||
self.rect.y = self.rect.y - GRID_CELL_SIZE - 5
|
||||
elif self.direction.name == 'RIGHT':
|
||||
self.position = (self.position[0] + 1, self.position[1])
|
||||
self.rect.x = self.rect.x + GRID_CELL_SIZE + 5
|
||||
elif self.direction.name == 'DOWN':
|
||||
self.position = (self.position[0], self.position[1] + 1)
|
||||
self.rect.y = self.rect.y + GRID_CELL_SIZE + 5
|
||||
elif self.direction.name == 'LEFT':
|
||||
self.position = (self.position[0] - 1, self.position[1])
|
||||
self.rect.x = self.rect.x - GRID_CELL_SIZE - 5
|
||||
|
@ -16,7 +16,7 @@ class Monster(pygame.sprite.Sprite):
|
||||
super().__init__(group)
|
||||
self.image = random.choice(monster_images)
|
||||
self.image = pygame.transform.scale(self.image, (40, 40))
|
||||
position_in_px = (parse_cord(position[1]), parse_cord(position[0]))
|
||||
position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
|
||||
self.rect = self.image.get_rect(topleft=position_in_px)
|
||||
|
||||
self.health = random.randrange(15, 25)
|
||||
|
@ -7,6 +7,6 @@ class Tile(pygame.sprite.Sprite):
|
||||
def __init__(self, position, image, group, tile_type=' '):
|
||||
super().__init__(group)
|
||||
self.image = image
|
||||
position_in_px = (parse_cord(position[1]), parse_cord(position[0]))
|
||||
position_in_px = (parse_cord(position[0]), parse_cord(position[1]))
|
||||
self.rect = self.image.get_rect(topleft=position_in_px)
|
||||
self.tile_type = tile_type
|
Loading…
Reference in New Issue
Block a user