diff --git a/agent.py b/agent.py new file mode 100644 index 0000000..a253098 --- /dev/null +++ b/agent.py @@ -0,0 +1,32 @@ +import pygame +import project_constants as const +import json_generator as js +import json + + # Class of our agent, initialization of it + # movment functions (those defiend by the 'go_' prefix are not meant to actually move our agent, they just return some values + # that are later used by another function called 'is_valid_move' (which is defined in Minefield)); + +class Agent: + def __init__(self, json_path): + with open(json_path) as json_data: + data = json.load(json_data) + self.x, self.y = data['agent_starting_position'].split(",") + self.position = [int(self.x), int(self.y)] + + def go_right(self): + + return self.position[0] + 1, self.position[1] + + def go_left(self): + + return self.position[0] -1, self.position[1] + + def go_up(self): + + return self.position[0], self.position[1] - 1 + + + def go_down(self): + + return self.position[0], self.position[1] + 1 \ No newline at end of file diff --git a/main.py b/main.py index 61af3bd..c6a5e4c 100644 --- a/main.py +++ b/main.py @@ -58,15 +58,22 @@ def main(): # Depending on what key we press, the agent will move in that direction # DISCRETION : The only keys that are available are arrow keys + # DISCRETION : is_valid_move is a new brand funcation that now plays a critical role in movement of our Agent (It is NOT just the "check up" function anymore) if keys[pygame.K_RIGHT]: - minefield.go_right() - elif keys[pygame.K_LEFT]: - minefield.go_left() - elif keys[pygame.K_UP]: - minefield.go_up() - elif keys[pygame.K_DOWN]: - minefield.go_down() + target_x, target_y = minefield.agent.go_right() + minefield.is_valid_move(target_x, target_y) + elif keys[pygame.K_LEFT]: + target_x, target_y = minefield.agent.go_left() + minefield.is_valid_move(target_x, target_y) + + elif keys[pygame.K_UP]: + target_x, target_y = minefield.agent.go_up() + minefield.is_valid_move(target_x, target_y) + + elif keys[pygame.K_DOWN]: + target_x, target_y = minefield.agent.go_down() + minefield.is_valid_move(target_x, target_y) if __name__ == "__main__": main() diff --git a/minefield.py b/minefield.py index 3db11f1..d78c841 100644 --- a/minefield.py +++ b/minefield.py @@ -1,9 +1,11 @@ import json - +import ctypes # +import agent as ag import project_constants as const import tile as tl from mines_models import standard_mine as sm + tile_asset_options = { "BLUE": const.ASSET_TILE_BLUE, "GREEN": const.ASSET_TILE_GREEN, @@ -35,7 +37,7 @@ class Minefield: def __init__(self, json_path): self.turn = 0 - # self.sapper = new Sapper Object (yet to be implemented) + self.agent = ag.Agent(const.MAP_RANDOM_10x10) # open JSON with minefield info with open(json_path) as json_data: @@ -64,10 +66,7 @@ class Minefield: self.matrix[x][y].color = tile_data["color"].upper() - # probably a temporary solution - # (depends on sapper's movement implementation) - sapper_x, sapper_y = data['agent_starting_position'].split(",") - self.sapper_position = (int(sapper_x), int(sapper_y)) + def draw(self, window): # iterate through tiles @@ -86,45 +85,29 @@ class Minefield: # current icons don't represent actual types, thus every mine has the same icon (temporary solution) window.blit(mine_asset_options['A'], tile_screen_coords) + # draw the sapper - sapper_screen_coords = calculate_screen_position(self.sapper_position[0], self.sapper_position[1]) + sapper_screen_coords = calculate_screen_position(self.agent.position[0], self.agent.position[1]) window.blit(const.ASSET_SAPPER, sapper_screen_coords) # ================ # # === MOVEMENT === # # ================ # - # check if sapper's destination is accessible + # check if sapper's destination is accessible if so then change position of the Agent + # If Agent comes upon a tile with a mine his starting position shall be reestablished def is_valid_move(self, target_x: int, target_y: int): if 0 <= target_x < const.V_GRID_HOR_TILES and \ - 0 <= target_y < const.V_GRID_VER_TILES and \ - self.matrix[target_x][target_y].mine is None: - return True - - return False - + 0 <= target_y < const.V_GRID_VER_TILES: + if self.matrix[target_x][target_y].mine is None: + self.agent.position[0] = target_x + self.agent.position[1] = target_y + else: + self.agent.position[0] = int(self.agent.x) + self.agent.position[1] = int(self.agent.y) + ctypes.windll.user32.MessageBoxW(0, "Znowu się nie udało", "GAME OVER", 1) + # This part of the pop up message is just a temporary solution + # Here are defined functions that move our agent. They are being called in main when certain key is pressed - def go_right(self): - target_x = self.sapper_position[0] + 1 - target_y = self.sapper_position[1] - if self.is_valid_move(target_x, target_y): - self.sapper_position = (target_x, target_y) - - def go_left(self): - target_x = self.sapper_position[0] - 1 - target_y = self.sapper_position[1] - if self.is_valid_move(target_x, target_y): - self.sapper_position = (target_x, target_y) - - def go_up(self): - target_x = self.sapper_position[0] - target_y = self.sapper_position[1] - 1 - if self.is_valid_move(target_x, target_y): - self.sapper_position = (target_x, target_y) - - def go_down(self): - target_x = self.sapper_position[0] - target_y = self.sapper_position[1] + 1 - if self.is_valid_move(target_x, target_y): - self.sapper_position = (target_x, target_y) + diff --git a/mines_models/tests.py b/mines_models/tests.py deleted file mode 100644 index 207ed83..0000000 --- a/mines_models/tests.py +++ /dev/null @@ -1,21 +0,0 @@ -import time_mine as tm -import chained_mine as cm -import standard_mine as sm - -mine1 = tm.TimeMine((1, 2), "time", 3) -mine2 = cm.ChainedMine((3, 4)) -mine3 = cm.ChainedMine((0, 6), "chained", mine2) -mine4 = sm.StandardMine((1, 2), "standard") - -mines = [mine1, mine2, mine3, mine4] - -for mine in mines: - print(mine.position) - print(mine.active) - -print(mine2.predecessor) -print(mine3.predecessor) - -mine4.disarm() - -mine5 = cm.ChainedMine()