delete 'mines_models/tests.py'

This commit is contained in:
Bartłomiej Grochowski 2021-03-26 16:36:21 +01:00 committed by Jakub Danilewicz
parent 1ccb1277fc
commit 4cf9d57fa0
4 changed files with 66 additions and 65 deletions

32
agent.py Normal file
View File

@ -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

21
main.py
View File

@ -58,15 +58,22 @@ def main():
# Depending on what key we press, the agent will move in that direction # Depending on what key we press, the agent will move in that direction
# DISCRETION : The only keys that are available are arrow keys # 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]: if keys[pygame.K_RIGHT]:
minefield.go_right() target_x, target_y = minefield.agent.go_right()
elif keys[pygame.K_LEFT]: minefield.is_valid_move(target_x, target_y)
minefield.go_left()
elif keys[pygame.K_UP]:
minefield.go_up()
elif keys[pygame.K_DOWN]:
minefield.go_down()
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__": if __name__ == "__main__":
main() main()

View File

@ -1,9 +1,11 @@
import json import json
import ctypes #
import agent as ag
import project_constants as const import project_constants as const
import tile as tl import tile as tl
from mines_models import standard_mine as sm from mines_models import standard_mine as sm
tile_asset_options = { tile_asset_options = {
"BLUE": const.ASSET_TILE_BLUE, "BLUE": const.ASSET_TILE_BLUE,
"GREEN": const.ASSET_TILE_GREEN, "GREEN": const.ASSET_TILE_GREEN,
@ -35,7 +37,7 @@ class Minefield:
def __init__(self, json_path): def __init__(self, json_path):
self.turn = 0 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 # open JSON with minefield info
with open(json_path) as json_data: with open(json_path) as json_data:
@ -64,10 +66,7 @@ class Minefield:
self.matrix[x][y].color = tile_data["color"].upper() 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): def draw(self, window):
# iterate through tiles # 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) # 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) window.blit(mine_asset_options['A'], tile_screen_coords)
# draw the sapper # 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) window.blit(const.ASSET_SAPPER, sapper_screen_coords)
# ================ # # ================ #
# === MOVEMENT === # # === 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): def is_valid_move(self, target_x: int, target_y: int):
if 0 <= target_x < const.V_GRID_HOR_TILES and \ if 0 <= target_x < const.V_GRID_HOR_TILES and \
0 <= target_y < const.V_GRID_VER_TILES and \ 0 <= target_y < const.V_GRID_VER_TILES:
self.matrix[target_x][target_y].mine is None: if self.matrix[target_x][target_y].mine is None:
return True self.agent.position[0] = target_x
self.agent.position[1] = target_y
return False 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 # 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)

View File

@ -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()