Dodanie fukncji rozbrajania min w klasie minefield.

This commit is contained in:
Paulina Szyszko 2021-05-20 15:01:09 +02:00
parent 7fe9e0958c
commit 9755ddc96d
8 changed files with 57 additions and 9 deletions

View File

@ -7,6 +7,8 @@ import project_constants as const
import minefield as mf import minefield as mf
from mine_models.time_mine import TimeMine from mine_models.time_mine import TimeMine
import searching_algorithms.a_star as a_star import searching_algorithms.a_star as a_star
from display_assets import blit_graphics from display_assets import blit_graphics
@ -68,6 +70,7 @@ def main():
# drawing map so black screen doesn't appear # drawing map so black screen doesn't appear
blit_graphics(minefield) blit_graphics(minefield)
while running: while running:
# ============== # # ============== #
@ -234,6 +237,10 @@ def main():
# is_game_over = False # is_game_over = False
minefield.disarm_mine(1,1)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -11,4 +11,6 @@ class ChainedMine(Mine):
super().__init__(position, active) super().__init__(position, active)
def disarm(self): def disarm(self):
if (self.predecessor.active == False):
super().disarm()
pass pass

View File

@ -16,4 +16,4 @@ class Mine(ABC):
@abstractmethod @abstractmethod
def disarm(self): def disarm(self):
pass self.active = False

View File

@ -7,4 +7,4 @@ class StandardMine(Mine):
super().__init__(position, active) super().__init__(position, active)
def disarm(self): def disarm(self):
pass super().disarm()

View File

@ -9,4 +9,4 @@ class TimeMine(Mine):
super().__init__(position, active) super().__init__(position, active)
def disarm(self): def disarm(self):
pass super().disarm()

View File

@ -4,6 +4,15 @@ import tile as tl
from mine_models.time_mine import TimeMine from mine_models.time_mine import TimeMine
import json_generator as jg import json_generator as jg
import pygame
import time
from display_assets import blit_graphics
from project_constants import HIGHLIGHT, INPUT_ROW, INPUT_COLUMN, RANDOM_BUTTON, OK_BUTTON
from ui.ui_components_list import UiComponentsList
class Minefield: class Minefield:
def __init__(self, json_path): def __init__(self, json_path):
@ -51,10 +60,37 @@ class Minefield:
# check if sapper's destination is accessible # check if sapper's destination is accessible
# If Agent comes upon a tile with a mine his starting position shall be reestablished # If Agent comes upon a tile with a mine his starting position shall be reestablished
def is_valid_move(self, target_row: int, target_column: int): @staticmethod
def is_valid_move(target_row: int, target_column: int):
if 0 <= target_row < const.V_GRID_VER_TILES \ if 0 <= target_row < const.V_GRID_VER_TILES \
and 0 <= target_column < const.V_GRID_HOR_TILES \ and 0 <= target_column < const.V_GRID_HOR_TILES:
and self.matrix[target_row][target_column].mine is None:
return True return True
return False return False
def disarm_mine(self, x, y):
tile = self.matrix[x][y]
mine = tile.mine
mine.disarm()
# This for loop is temporary solution for graphics to refresh. It works weird but it will be changed eventually.
for i in range(3):
time.sleep(1)
self.next_turn()
# FPS control
pygame.time.Clock().tick(const.V_FPS)
delta_time = pygame.time.Clock().get_time() / 1000
action_delta_time = delta_time / const.ACTION_INTERVAL
# graphics (from display_assets)
blit_graphics(self)
const.SCREEN.blit(HIGHLIGHT, const.get_tile_coordinates((x, y)))
self.agent.update_and_draw(const.SCREEN, 0, self)
# drawing ui components so they don't "disappear"
UiComponentsList([INPUT_ROW, INPUT_COLUMN, RANDOM_BUTTON, OK_BUTTON]).draw_all(const.SCREEN,
pygame.mouse.get_pos())
# updating graphics
pygame.display.flip()

View File

@ -22,7 +22,7 @@ V_NAME_OF_WINDOW = "MineFusion TM"
DIR_ASSETS = os.path.join("resources", "assets") DIR_ASSETS = os.path.join("resources", "assets")
V_FPS = 60 V_FPS = 60
ACTION_INTERVAL = 0.3 # interval between two actions in seconds ACTION_INTERVAL = 0.6 # interval between two actions in seconds
V_TILE_SIZE = 60 V_TILE_SIZE = 60
V_GRID_VER_TILES = 10 # vertical (number of rows) V_GRID_VER_TILES = 10 # vertical (number of rows)
@ -84,6 +84,7 @@ class Terrain(Enum):
CONCRETE = 2 CONCRETE = 2
GRASS = 5 GRASS = 5
MUD = 8 MUD = 8
MINE = 500
# =============== # # =============== #

View File

@ -6,7 +6,9 @@ from project_constants import Terrain
# It is used in Tile.cost (giving the value to the tile) # It is used in Tile.cost (giving the value to the tile)
def assume_cost(terrain_type): def assume_cost(terrain_type, mine):
if mine is not None:
return Terrain.MINE
if terrain_type == "CONCRETE": if terrain_type == "CONCRETE":
return Terrain.CONCRETE return Terrain.CONCRETE
elif terrain_type == "GRASS": elif terrain_type == "GRASS":
@ -19,6 +21,6 @@ class Tile:
def __init__(self, position, terrain_type=None, mine=None): def __init__(self, position, terrain_type=None, mine=None):
self.position = position self.position = position
self.terrain_type = terrain_type self.terrain_type = terrain_type
self.cost = assume_cost(terrain_type) self.cost = assume_cost(terrain_type, mine)
# mine is an instance of Mine class # mine is an instance of Mine class
self.mine = mine self.mine = mine