merged master to decision_trees

This commit is contained in:
JakubR 2021-05-23 18:02:35 +02:00
commit 9b20be5248
30 changed files with 415 additions and 316 deletions

View File

@ -45,12 +45,32 @@ def get_estimated_cost(node: Node):
return abs(node.state.row - GOAL[0]) + abs(node.state.column - GOAL[1]) return abs(node.state.row - GOAL[0]) + abs(node.state.column - GOAL[1])
def goal_test(state: State): def tile_goal_test(state: State):
if (state.row, state.column) == GOAL: if (state.row, state.column) == GOAL:
return True return True
return False return False
def mine_goal_test(state: State):
if state.row == GOAL[0] and state.column == GOAL[1] - 1:
if state.direction == Direction.RIGHT:
return True
elif state.row == GOAL[0] and state.column == GOAL[1] + 1:
if state.direction == Direction.LEFT:
return True
elif state.row == GOAL[0] - 1 and state.column == GOAL[1]:
if state.direction == Direction.DOWN:
return True
elif state.row == GOAL[0] + 1 and state.column == GOAL[1]:
if state.direction == Direction.UP:
return True
return False
def get_successors(state: State, minefield: Minefield): def get_successors(state: State, minefield: Minefield):
successors = list() successors = list()
@ -73,6 +93,7 @@ def graphsearch(initial_state: State,
minefield: Minefield, minefield: Minefield,
fringe: List[Node] = None, fringe: List[Node] = None,
explored: List[Node] = None, explored: List[Node] = None,
target_type: str = "tile",
tox: int = None, tox: int = None,
toy: int = None): toy: int = None):
@ -86,6 +107,15 @@ def graphsearch(initial_state: State,
if tox is not None and toy is not None: if tox is not None and toy is not None:
GOAL = (tox, toy) GOAL = (tox, toy)
if target_type == "mine":
goal_test = mine_goal_test
else:
goal_test = tile_goal_test
if minefield.matrix[GOAL[0]][GOAL[1]].mine is not None and minefield.matrix[GOAL[0]][GOAL[1]].mine.active:
# TODO: cross-platform popup, move to separate function
ctypes.windll.user32.MessageBoxW(0, "Brak rozwiązania", "GAME OVER", 1)
return []
# fringe and explored initialization # fringe and explored initialization
if fringe is None: if fringe is None:
fringe = list() fringe = list()

0
assets/__init__.py Normal file
View File

262
assets/asset_constants.py Normal file
View File

@ -0,0 +1,262 @@
import pygame
import os
import project_constants as c
DIR_ASSETS = c.DIR_ASSETS
V_TILE_SIZE = c.V_TILE_SIZE
# ============== #
# === ASSETS === #
# ============== #
ASSET_BACKGROUND = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "numbered_grid.png")),
(650, 650)
)
ASSET_SAPPER = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "robot_sapper.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_WALL = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "brick_wall.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_CONCRETE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "concrete.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_MUD = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "mud.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_GRASS = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "grass.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_MINE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "mine.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_CHAINS = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chains.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_CHAIN_COUNTER = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_counter.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TIME_MINE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_mine.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
# ==================== #
# === TIME NUMBERS === #
# ==================== #
ASSET_NUMBER_TIME_0 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_0.png")),
(6, 16)
)
ASSET_NUMBER_TIME_1 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_1.png")),
(6, 16)
)
ASSET_NUMBER_TIME_2 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_2.png")),
(6, 16)
)
ASSET_NUMBER_TIME_3 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_3.png")),
(6, 16)
)
ASSET_NUMBER_TIME_4 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_4.png")),
(6, 16)
)
ASSET_NUMBER_TIME_5 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_5.png")),
(6, 16)
)
ASSET_NUMBER_TIME_6 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_6.png")),
(6, 16)
)
ASSET_NUMBER_TIME_7 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_7.png")),
(6, 16)
)
ASSET_NUMBER_TIME_8 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_8.png")),
(6, 16)
)
ASSET_NUMBER_TIME_9 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_9.png")),
(6, 16)
)
# ====================== #
# === CHAINS NUMBERS === #
# ====================== #
ASSET_NUMBER_CHAINS_0 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_0.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_1 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_1.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_2 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_2.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_3 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_3.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_4 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_4.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_5 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_5.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_6 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_6.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_7 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_7.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_8 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_8.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_9 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_9.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_10 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_10.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_11 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_11.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_12 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_12.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_13 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_13.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_14 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_14.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_15 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_15.png")),
(6, 12)
)
# ================== #
# === OLD ASSETS === #
# ================== #
ASSET_MINE_A = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "simple_assets/mine_a.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_MINE_B = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "simple_assets/mine_b.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_MINE_F = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "simple_assets/mine_f.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_MINE_K = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "simple_assets/mine_k.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_ORANGE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_orange.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_RED = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_red.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_BLUE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_blue.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_PURPLE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_purple.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_GREEN = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_green.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_YELLOW = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_yellow.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_WHITE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_white.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)

View File

@ -1,10 +1,11 @@
import pygame import pygame
import project_constants as const import project_constants as const
from assets import asset_constants as asset
from mines.mine_models.standard_mine import StandardMine from objects.mines.mine_models.standard_mine import StandardMine
from mines.mine_models.chained_mine import ChainedMine from objects.mines.mine_models.chained_mine import ChainedMine
from mines.mine_models.time_mine import TimeMine from objects.mines.mine_models.time_mine import TimeMine
# ================================= # # ================================= #
@ -12,15 +13,15 @@ from mines.mine_models.time_mine import TimeMine
# ================================= # # ================================= #
tile_asset_options = { tile_asset_options = {
"MUD": const.ASSET_MUD, "MUD": asset.ASSET_MUD,
"GRASS": const.ASSET_GRASS, "GRASS": asset.ASSET_GRASS,
"CONCRETE": const.ASSET_CONCRETE "CONCRETE": asset.ASSET_CONCRETE
} }
mine_asset_options = { mine_asset_options = {
"MINE": const.ASSET_MINE, "MINE": asset.ASSET_MINE,
"CHAINS": const.ASSET_CHAINS, "CHAINS": asset.ASSET_CHAINS,
"TIME_MINE": const.ASSET_TIME_MINE, "TIME_MINE": asset.ASSET_TIME_MINE,
} }
@ -33,7 +34,7 @@ def blit_graphics(minefield):
# background grid (fills frame with white, blits grid) # background grid (fills frame with white, blits grid)
const.SCREEN.fill((255, 255, 255)) const.SCREEN.fill((255, 255, 255))
const.SCREEN.blit( const.SCREEN.blit(
const.ASSET_BACKGROUND, asset.ASSET_BACKGROUND,
( (
const.V_SCREEN_PADDING, const.V_SCREEN_PADDING,
const.V_SCREEN_PADDING const.V_SCREEN_PADDING
@ -112,22 +113,22 @@ def display_sapper(coords, direction):
if direction == const.Direction.UP: if direction == const.Direction.UP:
const.SCREEN.blit( const.SCREEN.blit(
const.ASSET_SAPPER, asset.ASSET_SAPPER,
sapper_screen_coords sapper_screen_coords
) )
elif direction == const.Direction.RIGHT: elif direction == const.Direction.RIGHT:
const.SCREEN.blit( const.SCREEN.blit(
pygame.transform.rotate(const.ASSET_SAPPER, 270), pygame.transform.rotate(asset.ASSET_SAPPER, 270),
sapper_screen_coords sapper_screen_coords
) )
elif direction == const.Direction.DOWN: elif direction == const.Direction.DOWN:
const.SCREEN.blit( const.SCREEN.blit(
pygame.transform.rotate(const.ASSET_SAPPER, 180), pygame.transform.rotate(asset.ASSET_SAPPER, 180),
sapper_screen_coords sapper_screen_coords
) )
elif direction == const.Direction.LEFT: elif direction == const.Direction.LEFT:
const.SCREEN.blit( const.SCREEN.blit(
pygame.transform.rotate(const.ASSET_SAPPER, 90), pygame.transform.rotate(asset.ASSET_SAPPER, 90),
sapper_screen_coords sapper_screen_coords
) )
@ -139,21 +140,21 @@ def display_sapper(coords, direction):
def display_concrete(coords): def display_concrete(coords):
const.SCREEN.blit( const.SCREEN.blit(
const.ASSET_CONCRETE, asset.ASSET_CONCRETE,
calculate_screen_position(coords) calculate_screen_position(coords)
) )
def display_mud(coords): def display_mud(coords):
const.SCREEN.blit( const.SCREEN.blit(
const.ASSET_MUD, asset.ASSET_MUD,
calculate_screen_position(coords) calculate_screen_position(coords)
) )
def display_grass(coords): def display_grass(coords):
const.SCREEN.blit( const.SCREEN.blit(
const.ASSET_GRASS, asset.ASSET_GRASS,
calculate_screen_position(coords) calculate_screen_position(coords)
) )
@ -165,7 +166,7 @@ def display_grass(coords):
def display_mine(coords): def display_mine(coords):
const.SCREEN.blit( const.SCREEN.blit(
const.ASSET_MINE, asset.ASSET_MINE,
calculate_screen_position(coords) calculate_screen_position(coords)
) )
@ -173,35 +174,35 @@ def display_mine(coords):
def display_chained_mine(coords, parent_coords=None): def display_chained_mine(coords, parent_coords=None):
def display_number(which_coord, number): def display_number(which_coord, number):
number_asset = const.ASSET_NUMBER_CHAINS_0 number_asset = asset.ASSET_NUMBER_CHAINS_0
if number == 1: if number == 1:
number_asset = const.ASSET_NUMBER_CHAINS_1 number_asset = asset.ASSET_NUMBER_CHAINS_1
elif number == 3: elif number == 3:
number_asset = const.ASSET_NUMBER_CHAINS_3 number_asset = asset.ASSET_NUMBER_CHAINS_3
elif number == 4: elif number == 4:
number_asset = const.ASSET_NUMBER_CHAINS_4 number_asset = asset.ASSET_NUMBER_CHAINS_4
elif number == 5: elif number == 5:
number_asset = const.ASSET_NUMBER_CHAINS_5 number_asset = asset.ASSET_NUMBER_CHAINS_5
elif number == 6: elif number == 6:
number_asset = const.ASSET_NUMBER_CHAINS_6 number_asset = asset.ASSET_NUMBER_CHAINS_6
elif number == 7: elif number == 7:
number_asset = const.ASSET_NUMBER_CHAINS_7 number_asset = asset.ASSET_NUMBER_CHAINS_7
elif number == 8: elif number == 8:
number_asset = const.ASSET_NUMBER_CHAINS_8 number_asset = asset.ASSET_NUMBER_CHAINS_8
elif number == 9: elif number == 9:
number_asset = const.ASSET_NUMBER_CHAINS_9 number_asset = asset.ASSET_NUMBER_CHAINS_9
elif number == 10: elif number == 10:
number_asset = const.ASSET_NUMBER_CHAINS_10 number_asset = asset.ASSET_NUMBER_CHAINS_10
elif number == 11: elif number == 11:
number_asset = const.ASSET_NUMBER_CHAINS_11 number_asset = asset.ASSET_NUMBER_CHAINS_11
elif number == 12: elif number == 12:
number_asset = const.ASSET_NUMBER_CHAINS_12 number_asset = asset.ASSET_NUMBER_CHAINS_12
elif number == 13: elif number == 13:
number_asset = const.ASSET_NUMBER_CHAINS_13 number_asset = asset.ASSET_NUMBER_CHAINS_13
elif number == 14: elif number == 14:
number_asset = const.ASSET_NUMBER_CHAINS_14 number_asset = asset.ASSET_NUMBER_CHAINS_14
elif number == 15: elif number == 15:
number_asset = const.ASSET_NUMBER_CHAINS_15 number_asset = asset.ASSET_NUMBER_CHAINS_15
mine_coords = calculate_screen_position(coords) mine_coords = calculate_screen_position(coords)
number_coords = mine_coords number_coords = mine_coords
@ -218,11 +219,17 @@ def display_chained_mine(coords, parent_coords=None):
display_mine(coords) display_mine(coords)
const.SCREEN.blit( const.SCREEN.blit(
const.ASSET_CHAINS, asset.ASSET_CHAINS,
calculate_screen_position(coords) calculate_screen_position(coords)
) )
if parent_coords is not None: if parent_coords is not None:
const.SCREEN.blit(
asset.ASSET_CHAIN_COUNTER,
calculate_screen_position(coords)
)
display_number(const.Coords.X, parent_coords[0]) display_number(const.Coords.X, parent_coords[0])
display_number(const.Coords.Y, parent_coords[1]) display_number(const.Coords.Y, parent_coords[1])
@ -231,25 +238,25 @@ def display_time_mine(coords, time):
def display_number(which_digit, number): def display_number(which_digit, number):
number_asset = const.ASSET_NUMBER_TIME_0 number_asset = asset.ASSET_NUMBER_TIME_0
if number == 1: if number == 1:
number_asset = const.ASSET_NUMBER_TIME_1 number_asset = asset.ASSET_NUMBER_TIME_1
elif number == 2: elif number == 2:
number_asset = const.ASSET_NUMBER_TIME_2 number_asset = asset.ASSET_NUMBER_TIME_2
elif number == 3: elif number == 3:
number_asset = const.ASSET_NUMBER_TIME_3 number_asset = asset.ASSET_NUMBER_TIME_3
elif number == 4: elif number == 4:
number_asset = const.ASSET_NUMBER_TIME_4 number_asset = asset.ASSET_NUMBER_TIME_4
elif number == 5: elif number == 5:
number_asset = const.ASSET_NUMBER_TIME_5 number_asset = asset.ASSET_NUMBER_TIME_5
elif number == 6: elif number == 6:
number_asset = const.ASSET_NUMBER_TIME_6 number_asset = asset.ASSET_NUMBER_TIME_6
elif number == 7: elif number == 7:
number_asset = const.ASSET_NUMBER_TIME_7 number_asset = asset.ASSET_NUMBER_TIME_7
elif number == 8: elif number == 8:
number_asset = const.ASSET_NUMBER_TIME_8 number_asset = asset.ASSET_NUMBER_TIME_8
elif number == 9: elif number == 9:
number_asset = const.ASSET_NUMBER_TIME_9 number_asset = asset.ASSET_NUMBER_TIME_9
mine_coords = calculate_screen_position(coords) mine_coords = calculate_screen_position(coords)
number_coords = mine_coords number_coords = mine_coords
@ -264,7 +271,7 @@ def display_time_mine(coords, time):
) )
const.SCREEN.blit( const.SCREEN.blit(
const.ASSET_TIME_MINE, asset.ASSET_TIME_MINE,
calculate_screen_position(coords) calculate_screen_position(coords)
) )

23
game.py
View File

@ -1,13 +1,13 @@
from random import randint from random import choice, randint
import project_constants as const import project_constants as const
from display_assets import blit_graphics from assets.display_assets import blit_graphics
from algorithms.search import a_star from algorithms.search import a_star
from minefield import Minefield from minefield import Minefield
from mines.mine_models.time_mine import TimeMine from objects.mines.mine_models.time_mine import TimeMine
from ui.ui_components_manager import UiComponentsManager from ui.ui_components_manager import UiComponentsManager
from ui.text_box import TextBox from ui.text_box import TextBox
@ -112,8 +112,20 @@ class Game:
def get_turn_number(self): def get_turn_number(self):
return self.turn return self.turn
# draws a random mine to disarm (in auto mode)
def set_random_mine_as_target(self):
self.goal = choice(self.minefield.get_active_mines()).position
# display new destination
self.input_box_row.set_texts(user_input=str(self.goal[0]))
self.input_box_column.set_texts(user_input=str(self.goal[1]))
# prevents highlighting input_box_row,
# couldn't find any better solution w/o major Game class changes
self.input_box_row.set_is_selected(False)
# gets action sequence for agent # gets action sequence for agent
def get_action_sequence(self): def get_action_sequence(self, target_type: str = "tile"):
return a_star.graphsearch( return a_star.graphsearch(
initial_state=a_star.State( initial_state=a_star.State(
row=self.agent.row, row=self.agent.row,
@ -121,6 +133,7 @@ class Game:
direction=self.agent.direction direction=self.agent.direction
), ),
minefield=self.minefield, minefield=self.minefield,
target_type=target_type,
tox=self.goal[0], tox=self.goal[0],
toy=self.goal[1] toy=self.goal[1]
) )
@ -187,7 +200,7 @@ class Game:
self.minefield = self.initial_minefield.__copy__() self.minefield = self.initial_minefield.__copy__()
self.agent = self.initial_minefield.agent self.agent = self.initial_minefield.agent
# initializes ui components and assigns them to attributes # initializes old ui components and assigns them to attributes
def initialize_gui_components(self): def initialize_gui_components(self):
# calculating in game gui coordinates # calculating in game gui coordinates
gui_width = const.V_SIDE_MENU_WIDTH gui_width = const.V_SIDE_MENU_WIDTH

View File

@ -4,12 +4,12 @@ import random
import project_constants as const import project_constants as const
# import tile class # import tile class
from tile import Tile from objects.tile import Tile
# import mine models # import mine models
from mines.mine_models.standard_mine import StandardMine from objects.mines.mine_models.standard_mine import StandardMine
from mines.mine_models.time_mine import TimeMine from objects.mines.mine_models.time_mine import TimeMine
from mines.mine_models.chained_mine import ChainedMine from objects.mines.mine_models.chained_mine import ChainedMine
class JsonGenerator: class JsonGenerator:

23
main.py
View File

@ -1,7 +1,6 @@
# libraries # libraries
import pygame import pygame
from pyglet.gl import * # for blocky textures from pyglet.gl import * # for blocky textures
# other files of this project # other files of this project
from game import Game from game import Game
import project_constants as const import project_constants as const
@ -25,6 +24,7 @@ def main():
# setting flags for program # setting flags for program
running = True running = True
in_menu = True in_menu = True
auto = False
is_game_over = False is_game_over = False
# create and initialize_gui_components game instance # create and initialize_gui_components game instance
@ -37,7 +37,7 @@ def main():
# ==== MENU ==== # # ==== MENU ==== #
# ============== # # ============== #
while running and in_menu: while running and in_menu and not auto:
events = pygame.event.get() events = pygame.event.get()
# checking if game should stop running # checking if game should stop running
@ -58,12 +58,24 @@ def main():
# if ok button is clicked then leave menu section # if ok button is clicked then leave menu section
in_menu = not game.button_ok.is_clicked(pygame.mouse.get_pos(), events) in_menu = not game.button_ok.is_clicked(pygame.mouse.get_pos(), events)
# if auto button is clicked then leave menu and start auto-mode
auto = game.button_auto.is_clicked(pygame.mouse.get_pos(), events)
# ========================== # # ========================== #
# ==== BEFORE GAME LOOP ==== # # ==== BEFORE GAME LOOP ==== #
# ========================== # # ========================== #
# initializing action_sequence variable
action_sequence = None
# getting action sequence for agent # getting action sequence for agent
action_sequence = game.get_action_sequence() if auto and running:
in_menu = False
game.set_random_mine_as_target()
action_sequence = game.get_action_sequence("mine")
elif running:
action_sequence = game.get_action_sequence("tile")
# initializing game attributes before the game loop # initializing game attributes before the game loop
game.initialize_before_game_loop() game.initialize_before_game_loop()
@ -87,6 +99,11 @@ def main():
# drawing minefield and agent instances # drawing minefield and agent instances
game.draw_minefield() game.draw_minefield()
# handling auto button (clicking quits auto-mode)
if auto:
game.button_auto.set_flags(is_active=True)
auto = not game.button_auto.is_clicked(pygame.mouse.get_pos(), events)
# drawing inactive gui components so they don't "disappear" # drawing inactive gui components so they don't "disappear"
game.run_in_game_menu_overlay(pygame.mouse.get_pos(), events) game.run_in_game_menu_overlay(pygame.mouse.get_pos(), events)
game.set_is_active_flag_for_all_in_game_gui_components(False) game.set_is_active_flag_for_all_in_game_gui_components(False)

View File

@ -1,7 +1,6 @@
import agent as ag
import project_constants as const import project_constants as const
import tile as tl from objects import tile as tl, agent as ag
from mines.mine_models.time_mine import TimeMine from objects.mines.mine_models.time_mine import TimeMine
import json_generator as jg import json_generator as jg
@ -46,6 +45,22 @@ class Minefield:
if mine is not None and isinstance(mine, TimeMine): if mine is not None and isinstance(mine, TimeMine):
mine.timer = max(0, mine.starting_time - int(self.turn / 4)) mine.timer = max(0, mine.starting_time - int(self.turn / 4))
def get_active_mines(self):
mines = list()
for row in range(const.V_GRID_VER_TILES):
for column in range(const.V_GRID_VER_TILES):
mine = self.matrix[row][column].mine
if mine is not None and mine.active:
# do not add mines with predecessors
if mine.type == 'chained' and mine.predecessor is not None:
continue
mines.append(mine)
return mines
def disarm_mine(self, x, y): def disarm_mine(self, x, y):
tile = self.matrix[x][y] tile = self.matrix[x][y]
mine = tile.mine mine = tile.mine
@ -56,7 +71,6 @@ 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
@staticmethod @staticmethod
def is_valid_move(target_row: int, target_column: int): 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 \

0
objects/__init__.py Normal file
View File

View File

@ -1,4 +1,5 @@
import project_constants as const import project_constants as const
from assets import asset_constants as asset
import json import json
from pygame import transform from pygame import transform
@ -33,10 +34,10 @@ class Agent:
coord_x, coord_y = self.on_screen_coordinates[0], self.on_screen_coordinates[1] coord_x, coord_y = self.on_screen_coordinates[0], self.on_screen_coordinates[1]
tl_dimension = const.V_TILE_SIZE / 2 tl_dimension = const.V_TILE_SIZE / 2
rotating_rect = transform.rotate(const.ASSET_SAPPER, self.rotation_angle).get_rect() rotating_rect = transform.rotate(asset.ASSET_SAPPER, self.rotation_angle).get_rect()
rotating_rect.center = (coord_x + tl_dimension, coord_y + tl_dimension) rotating_rect.center = (coord_x + tl_dimension, coord_y + tl_dimension)
window.blit(transform.rotate(const.ASSET_SAPPER, self.rotation_angle), rotating_rect) window.blit(transform.rotate(asset.ASSET_SAPPER, self.rotation_angle), rotating_rect)
def update(self, delta_time, minefield): def update(self, delta_time, minefield):
self.new_action = self.going_forward + self.rotating_left * 2 + self.rotating_right * 4 self.new_action = self.going_forward + self.rotating_left * 2 + self.rotating_right * 4

View File

View File

View File

@ -1,11 +1,9 @@
import json import json
import mines.disarming.mine_parameters as param import objects.mines.disarming.mine_parameters as param
import os import os
from random import choice
# this module is self contained, used to generate a json file # this module is self contained, used to generate a json file
DIR_DATA = os.path.join("../../resources", "data") DIR_DATA = os.path.join("../../../resources", "data")
# just to show, how mine parameters works # just to show, how mine parameters works

View File

View File

@ -3,7 +3,7 @@ from .mine import Mine
class StandardMine(Mine): class StandardMine(Mine):
def __init__(self, position, active=True): def __init__(self, position, active=True):
self.mine_type = "standard" self.type = "standard"
super().__init__(position, active) super().__init__(position, active)
def disarm(self): def disarm(self):

View File

@ -10,7 +10,7 @@ from ui.input_box import InputBox
# STRUCT a list or other structure of values # STRUCT a list or other structure of values
# FUNC a function # FUNC a function
# OBJ a classes instance # OBJ a classes instance
# ASSET a png file (or other graphic format) # ASSET a png file (or other graphic format) (moved to asset_constants)
# MAP a JSON map file # MAP a JSON map file
@ -19,9 +19,10 @@ from ui.input_box import InputBox
# ================= # # ================= #
V_NAME_OF_WINDOW = "MineFusion TM" V_NAME_OF_WINDOW = "MineFusion TM"
DIR_ASSETS = os.path.join("resources", "assets")
V_FPS = 60 V_FPS = 60
DIR_ASSETS = os.path.join("resources", "assets")
TURN_INTERVAL = 0.3 # interval between two turns in seconds TURN_INTERVAL = 0.3 # interval between two turns in seconds
V_TILE_SIZE = 60 V_TILE_SIZE = 60
@ -149,7 +150,7 @@ def get_tile_coordinates(position):
# initializing pygame modules so all instances can be constructed properly # initializing pygame modules so all instances can be constructed properly
pygame.init() pygame.init()
# creating ui components used in menu # creating old ui components used in menu
HIGHLIGHT = pygame.transform.scale( HIGHLIGHT = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_white.png")), pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_white.png")),
(V_TILE_SIZE, V_TILE_SIZE) (V_TILE_SIZE, V_TILE_SIZE)
@ -211,248 +212,4 @@ OK_BUTTON = Button(
MAP_RANDOM_10x10 = os.path.join("resources", "minefields", "fourthmap.json") MAP_RANDOM_10x10 = os.path.join("resources", "minefields", "fourthmap.json")
# ============== #
# === ASSETS === #
# ============== #
ASSET_BACKGROUND = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "numbered_grid.png")),
(650, 650)
)
ASSET_SAPPER = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "robot_sapper.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_WALL = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "brick_wall.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_CONCRETE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "concrete.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_MUD = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "mud.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_GRASS = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "grass.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_MINE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "mine.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_CHAINS = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chains.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TIME_MINE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_mine.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
# ==================== #
# === TIME NUMBERS === #
# ==================== #
ASSET_NUMBER_TIME_0 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_0.png")),
(6, 16)
)
ASSET_NUMBER_TIME_1 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_1.png")),
(6, 16)
)
ASSET_NUMBER_TIME_2 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_2.png")),
(6, 16)
)
ASSET_NUMBER_TIME_3 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_3.png")),
(6, 16)
)
ASSET_NUMBER_TIME_4 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_4.png")),
(6, 16)
)
ASSET_NUMBER_TIME_5 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_5.png")),
(6, 16)
)
ASSET_NUMBER_TIME_6 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_6.png")),
(6, 16)
)
ASSET_NUMBER_TIME_7 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_7.png")),
(6, 16)
)
ASSET_NUMBER_TIME_8 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_8.png")),
(6, 16)
)
ASSET_NUMBER_TIME_9 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "time_numbers/time_number_9.png")),
(6, 16)
)
# ====================== #
# === CHAINS NUMBERS === #
# ====================== #
ASSET_NUMBER_CHAINS_0 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_0.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_1 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_1.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_2 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_2.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_3 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_3.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_4 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_4.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_5 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_5.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_6 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_6.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_7 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_7.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_8 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_8.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_9 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_9.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_10 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_10.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_11 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_11.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_12 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_12.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_13 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_13.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_14 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_14.png")),
(6, 12)
)
ASSET_NUMBER_CHAINS_15 = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "chain_numbers/chain_number_15.png")),
(6, 12)
)
# ================== #
# === OLD ASSETS === #
# ================== #
ASSET_MINE_A = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "simple_assets/mine_a.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_MINE_B = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "simple_assets/mine_b.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_MINE_F = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "simple_assets/mine_f.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_MINE_K = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "simple_assets/mine_k.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_ORANGE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_orange.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_RED = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_red.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_BLUE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_blue.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_PURPLE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_purple.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_GREEN = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_green.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_YELLOW = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_yellow.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)
ASSET_TILE_WHITE = pygame.transform.scale(
pygame.image.load(os.path.join(DIR_ASSETS, "old_tiles/tile_white.png")),
(V_TILE_SIZE, V_TILE_SIZE)
)

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 325 B

After

Width:  |  Height:  |  Size: 248 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 196 B

After

Width:  |  Height:  |  Size: 196 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 325 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 196 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 301 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 301 B

After

Width:  |  Height:  |  Size: 296 B

0
ui/__init__.py Normal file
View File