changed coordinates naming convention to (row, column)

This commit is contained in:
s452645 2021-04-11 19:04:06 +02:00
parent a90cc61586
commit 3f5d27e414
4 changed files with 43 additions and 43 deletions

View File

@ -11,22 +11,21 @@ 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)]
self.row, self.column = data['agent_starting_position'].split(",")
self.position = [int(self.row), int(self.column)]
def go_right(self):
return self.position[0] + 1, self.position[1]
return self.position[0], self.position[1] + 1
def go_left(self):
return self.position[0] -1, self.position[1]
return self.position[0], self.position[1] - 1
def go_up(self):
return self.position[0], self.position[1] - 1
return self.position[0] - 1, self.position[1]
def go_down(self):
return self.position[0], self.position[1] + 1
return self.position[0] + 1, self.position[1]

17
main.py
View File

@ -60,20 +60,21 @@ def main():
# 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]:
target_x, target_y = minefield.agent.go_right()
minefield.is_valid_move(target_x, target_y)
target_row, target_column = minefield.agent.go_right()
minefield.is_valid_move(target_row, target_column)
elif keys[pygame.K_LEFT]:
target_x, target_y = minefield.agent.go_left()
minefield.is_valid_move(target_x, target_y)
target_row, target_column = minefield.agent.go_left()
minefield.is_valid_move(target_row, target_column)
elif keys[pygame.K_UP]:
target_x, target_y = minefield.agent.go_up()
minefield.is_valid_move(target_x, target_y)
target_row, target_column = minefield.agent.go_up()
minefield.is_valid_move(target_row, target_column)
elif keys[pygame.K_DOWN]:
target_x, target_y = minefield.agent.go_down()
minefield.is_valid_move(target_x, target_y)
target_row, target_column = minefield.agent.go_down()
minefield.is_valid_move(target_row, target_column)
if __name__ == "__main__":
main()

View File

@ -26,10 +26,10 @@ mine_asset_options = {
}
def calculate_screen_position(x, y):
def calculate_screen_position(row, column):
coords = (
const.V_SCREEN_PADDING + const.V_TILE_SIZE * x,
const.V_SCREEN_PADDING + const.V_TILE_SIZE * y,
const.V_SCREEN_PADDING + const.V_TILE_SIZE * column,
const.V_SCREEN_PADDING + const.V_TILE_SIZE * row,
)
return coords
@ -48,28 +48,28 @@ class Minefield:
# create matrix of a desired size, fill it with default tile objects
self.matrix = [
[
tl.Tile((x, y)) for y in range(const.V_GRID_VER_TILES)
] for x in range(const.V_GRID_HOR_TILES)
tl.Tile((row, column)) for column in range(const.V_GRID_HOR_TILES)
] for row in range(const.V_GRID_VER_TILES)
]
# iterate through tiles, set their colors and add mines
for x in range(const.V_GRID_HOR_TILES):
for y in range(const.V_GRID_VER_TILES):
for row in range(const.V_GRID_VER_TILES):
for column in range(const.V_GRID_HOR_TILES):
# load tile's data from json
tile_data = data[f"{x},{y}"]
tile_data = data[f"{row},{column}"]
# if there is a mine, create & assign new Mine object (type recognition included)
if tile_data["mine"] is not None:
mine = self._create_mine(tile_data["mine"], x, y)
self.matrix[x][y].mine = mine
mine = self._create_mine(tile_data["mine"], row, column)
self.matrix[row][column].mine = mine
self.matrix[x][y].color = tile_data["color"].upper()
self.matrix[row][column].color = tile_data["color"].upper()
def draw(self, window):
# iterate through tiles
for raw in self.matrix:
for tile in raw:
for row in self.matrix:
for tile in row:
# calculate tile position on the screen
tile_screen_coords = calculate_screen_position(tile.position[0], tile.position[1])
@ -93,26 +93,26 @@ class Minefield:
# 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:
if self.matrix[target_x][target_y].mine is None:
self.agent.position[0] = target_x
self.agent.position[1] = target_y
def is_valid_move(self, target_row: int, target_column: int):
if 0 <= target_row < const.V_GRID_VER_TILES and \
0 <= target_column < const.V_GRID_HOR_TILES:
if self.matrix[target_row][target_column].mine is None:
self.agent.position[0] = target_row
self.agent.position[1] = target_column
else:
self.agent.position[0] = int(self.agent.x)
self.agent.position[1] = int(self.agent.y)
self.agent.position[0] = int(self.agent.row)
self.agent.position[1] = int(self.agent.column)
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
# distinguishes new mine's type and creates appropriate object
def _create_mine(self, mine_data, x, y):
def _create_mine(self, mine_data, row, column):
mine_type = mine_data["mine_type"]
# TIME MINE
if mine_type == "time":
timer = mine_data["timer"]
mine = tm.TimeMine((x, y), int(timer))
mine = tm.TimeMine((row, column), int(timer))
# CHAINED MINE
elif mine_type == "chained":
@ -121,14 +121,14 @@ class Minefield:
x, y = map(int, mine_data["predecessor"].split(','))
# get predecessor object and assign it to the new mine
predecessor = self.matrix[x][y].mine
mine = cm.ChainedMine((x, y), predecessor)
predecessor = self.matrix[row][column].mine
mine = cm.ChainedMine((row, column), predecessor)
else:
mine = cm.ChainedMine((x, y))
mine = cm.ChainedMine((row, column))
# STANDARD MINE
else:
mine = sm.StandardMine((x, y))
mine = sm.StandardMine((row, column))
return mine

View File

@ -17,7 +17,7 @@ ASSETS_DIR = os.path.join("resources", "assets")
V_FPS = 60
V_TILE_SIZE = 60
V_GRID_VER_TILES = V_GRID_HOR_TILES = 10 # vertical, horizontal
V_GRID_VER_TILES = V_GRID_HOR_TILES = 10 # vertical (number of rows), horizontal (number of columns)
V_SCREEN_PADDING = 10
V_MINEFIELD_HEIGHT = V_TILE_SIZE * V_GRID_VER_TILES
V_MINEFIELD_WIDTH = V_TILE_SIZE * V_GRID_HOR_TILES