From 54d9133751083d09b78940b651eddf12d64b4542 Mon Sep 17 00:00:00 2001 From: s452645 Date: Fri, 12 Mar 2021 21:25:33 +0100 Subject: [PATCH] fully implemented random map generation --- main.py | 12 +- minefield.py | 80 +++-- project_constants.py | 7 + resources/minefields/firstmap.json | 451 +++++++++++++++++++++++++++++ 4 files changed, 524 insertions(+), 26 deletions(-) create mode 100644 resources/minefields/firstmap.json diff --git a/main.py b/main.py index 546ad52..ec91641 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,7 @@ from pyglet.gl import * # for blocky textures import project_constants import event_interpreter import minefield as mf +import json_generator @@ -19,11 +20,8 @@ def main(): glEnable(GL_TEXTURE_2D) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) - - # loading minefields # create an instance of Minefield, pass necessary data - minefield = mf.Minefield() - + minefield = mf.Minefield(project_constants.MAP_RANDOM_10x10) running = True while running: @@ -44,11 +42,7 @@ def main(): ) ) - # tiles - # TODO : call to tile blitting function goes here - - # mines - # TODO : call to a mine blitting function goes here + # draw tiles and mines minefield.draw(project_constants.SCREEN) pygame.display.update() diff --git a/minefield.py b/minefield.py index 9a90ca4..cb27cf5 100644 --- a/minefield.py +++ b/minefield.py @@ -1,39 +1,85 @@ import pygame +import json import project_constants as consts import tile as tl import mine as mn +tile_asset_options = { + "BLUE": consts.ASSET_TILE_BLUE, + "GREEN": consts.ASSET_TILE_GREEN, + "ORANGE": consts.ASSET_TILE_ORANGE, + "PURPLE": consts.ASSET_TILE_PURPLE, + "RED": consts.ASSET_TILE_RED, + "WHITE": consts.ASSET_TILE_WHITE, + "YELLOW": consts.ASSET_TILE_YELLOW +} + +mine_asset_options = { + 'A': consts.ASSET_MINE_A, + 'B': consts.ASSET_MINE_B, + 'F': consts.ASSET_MINE_F, + 'K': consts.ASSET_MINE_K +} + + +def calculate_screen_position(x, y): + coords = ( + consts.V_SCREEN_PADDING + consts.V_TILE_SIZE * x, + consts.V_SCREEN_PADDING + consts.V_TILE_SIZE * y, + ) + + return coords + class Minefield: - def __init__(self, minefield_data=None): + def __init__(self, json_path): - # create matrix of a desired size, fill it with empty tile objects - self.matrix = [[tl.Tile((i, j)) for i in range(consts.V_GRID_VER_TILES)] for j in range(consts.V_GRID_HOR_TILES)] + # open JSON with minefield info + with open(json_path) as json_data: + data = json.load(json_data) - # serialize JSON, create matrix + # create matrix of a desired size, fill it with default tile objects + self.matrix = [ + [ + tl.Tile((i, j)) for i in range(consts.V_GRID_VER_TILES) + ] for j in range(consts.V_GRID_HOR_TILES) + ] - # iterate through matrix fields + # iterate through tiles, set their colors and add mines for x in range(consts.V_GRID_HOR_TILES): for y in range(consts.V_GRID_VER_TILES): - # if there should be a mine, create one - mine = mn.Mine((x, y), 'f') - # change tile properties - self.matrix[x][y].update_color("green") - self.matrix[x][y].mine = mine + # load tile's data from json + tile_data = data[f"{x},{y}"] + + # create and add mine if there is one + if tile_data["mine"] is not None: + mine_type = tile_data["mine"]["mine_type"].upper() + mine = mn.Mine((x, y), mine_type) + self.matrix[x][y].mine = mine + + 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 for column in self.matrix: for tile in column: - pixel_coords = ( - consts.V_SCREEN_PADDING + consts.V_TILE_SIZE * tile.position[0], # x - consts.V_SCREEN_PADDING + consts.V_TILE_SIZE * tile.position[1] # y - ) - window.blit(tile.asset, pixel_coords) + # calculate tile position on the screen + tile_screen_coords = calculate_screen_position(tile.position[0], tile.position[1]) + # draw a tile + window.blit(tile_asset_options.get(tile.color), tile_screen_coords) + + # draw a mine on top if there is one if tile.mine is not None: - window.blit(tile.mine.asset, pixel_coords) - + window.blit(mine_asset_options.get(tile.mine.mine_type), tile_screen_coords) + # draw the sapper + sapper_screen_coords = calculate_screen_position(self.sapper_position[0], self.sapper_position[1]) + window.blit(consts.ASSET_SAPPER, sapper_screen_coords) \ No newline at end of file diff --git a/project_constants.py b/project_constants.py index 032af30..daaf234 100644 --- a/project_constants.py +++ b/project_constants.py @@ -6,6 +6,7 @@ import os # V a value like a string or an int # STRUCT a list or other structure of values # ASSET a png file (or other graphic format) +# MAP a JSON map file @@ -38,6 +39,12 @@ SCREEN = pygame.display.set_mode\ STRUCT_TILE_COLORS = ["blue", "green", "orange", "purple", "red", "white", "yellow"] STRUCT_MINE_TYPES = ['a', 'b', 'f', 'k'] +# ============== # +# ==== MAPS ==== # +# ============== # +MAP_RANDOM_10x10 = os.path.join("resources", "minefields", "firstmap.json") + + # ============== # # === ASSETS === # diff --git a/resources/minefields/firstmap.json b/resources/minefields/firstmap.json new file mode 100644 index 0000000..a2f2271 --- /dev/null +++ b/resources/minefields/firstmap.json @@ -0,0 +1,451 @@ +{ + "0,0": { + "color": "red", + "mine": null + }, + "0,1": { + "color": "yellow", + "mine": null + }, + "0,2": { + "color": "orange", + "mine": null + }, + "0,3": { + "color": "red", + "mine": null + }, + "0,4": { + "color": "red", + "mine": null + }, + "0,5": { + "color": "green", + "mine": null + }, + "0,6": { + "color": "red", + "mine": { + "mine_type": "a" + } + }, + "0,7": { + "color": "green", + "mine": null + }, + "0,8": { + "color": "yellow", + "mine": null + }, + "0,9": { + "color": "white", + "mine": { + "mine_type": "a" + } + }, + "1,0": { + "color": "green", + "mine": null + }, + "1,1": { + "color": "red", + "mine": null + }, + "1,2": { + "color": "blue", + "mine": { + "mine_type": "k" + } + }, + "1,3": { + "color": "green", + "mine": { + "mine_type": "b" + } + }, + "1,4": { + "color": "purple", + "mine": null + }, + "1,5": { + "color": "blue", + "mine": null + }, + "1,6": { + "color": "blue", + "mine": { + "mine_type": "a" + } + }, + "1,7": { + "color": "white", + "mine": null + }, + "1,8": { + "color": "orange", + "mine": null + }, + "1,9": { + "color": "blue", + "mine": null + }, + "2,0": { + "color": "yellow", + "mine": null + }, + "2,1": { + "color": "purple", + "mine": null + }, + "2,2": { + "color": "orange", + "mine": null + }, + "2,3": { + "color": "orange", + "mine": null + }, + "2,4": { + "color": "orange", + "mine": null + }, + "2,5": { + "color": "yellow", + "mine": { + "mine_type": "f" + } + }, + "2,6": { + "color": "orange", + "mine": null + }, + "2,7": { + "color": "purple", + "mine": { + "mine_type": "f" + } + }, + "2,8": { + "color": "yellow", + "mine": null + }, + "2,9": { + "color": "yellow", + "mine": null + }, + "3,0": { + "color": "purple", + "mine": null + }, + "3,1": { + "color": "orange", + "mine": { + "mine_type": "f" + } + }, + "3,2": { + "color": "green", + "mine": null + }, + "3,3": { + "color": "red", + "mine": null + }, + "3,4": { + "color": "blue", + "mine": null + }, + "3,5": { + "color": "yellow", + "mine": null + }, + "3,6": { + "color": "purple", + "mine": { + "mine_type": "b" + } + }, + "3,7": { + "color": "yellow", + "mine": null + }, + "3,8": { + "color": "white", + "mine": null + }, + "3,9": { + "color": "purple", + "mine": null + }, + "4,0": { + "color": "red", + "mine": null + }, + "4,1": { + "color": "white", + "mine": null + }, + "4,2": { + "color": "green", + "mine": null + }, + "4,3": { + "color": "yellow", + "mine": null + }, + "4,4": { + "color": "red", + "mine": null + }, + "4,5": { + "color": "purple", + "mine": { + "mine_type": "f" + } + }, + "4,6": { + "color": "purple", + "mine": null + }, + "4,7": { + "color": "red", + "mine": { + "mine_type": "k" + } + }, + "4,8": { + "color": "white", + "mine": null + }, + "4,9": { + "color": "green", + "mine": null + }, + "5,0": { + "color": "yellow", + "mine": null + }, + "5,1": { + "color": "white", + "mine": null + }, + "5,2": { + "color": "green", + "mine": { + "mine_type": "a" + } + }, + "5,3": { + "color": "red", + "mine": null + }, + "5,4": { + "color": "orange", + "mine": { + "mine_type": "a" + } + }, + "5,5": { + "color": "orange", + "mine": { + "mine_type": "k" + } + }, + "5,6": { + "color": "green", + "mine": null + }, + "5,7": { + "color": "blue", + "mine": null + }, + "5,8": { + "color": "red", + "mine": null + }, + "5,9": { + "color": "green", + "mine": null + }, + "6,0": { + "color": "yellow", + "mine": null + }, + "6,1": { + "color": "blue", + "mine": null + }, + "6,2": { + "color": "orange", + "mine": { + "mine_type": "f" + } + }, + "6,3": { + "color": "red", + "mine": null + }, + "6,4": { + "color": "yellow", + "mine": null + }, + "6,5": { + "color": "red", + "mine": null + }, + "6,6": { + "color": "green", + "mine": null + }, + "6,7": { + "color": "orange", + "mine": null + }, + "6,8": { + "color": "red", + "mine": null + }, + "6,9": { + "color": "blue", + "mine": null + }, + "7,0": { + "color": "white", + "mine": null + }, + "7,1": { + "color": "white", + "mine": { + "mine_type": "a" + } + }, + "7,2": { + "color": "red", + "mine": { + "mine_type": "k" + } + }, + "7,3": { + "color": "white", + "mine": { + "mine_type": "k" + } + }, + "7,4": { + "color": "red", + "mine": null + }, + "7,5": { + "color": "orange", + "mine": null + }, + "7,6": { + "color": "purple", + "mine": { + "mine_type": "k" + } + }, + "7,7": { + "color": "red", + "mine": null + }, + "7,8": { + "color": "red", + "mine": null + }, + "7,9": { + "color": "blue", + "mine": { + "mine_type": "f" + } + }, + "8,0": { + "color": "white", + "mine": null + }, + "8,1": { + "color": "yellow", + "mine": null + }, + "8,2": { + "color": "purple", + "mine": null + }, + "8,3": { + "color": "red", + "mine": null + }, + "8,4": { + "color": "orange", + "mine": null + }, + "8,5": { + "color": "red", + "mine": null + }, + "8,6": { + "color": "white", + "mine": null + }, + "8,7": { + "color": "orange", + "mine": null + }, + "8,8": { + "color": "orange", + "mine": null + }, + "8,9": { + "color": "purple", + "mine": null + }, + "9,0": { + "color": "blue", + "mine": null + }, + "9,1": { + "color": "orange", + "mine": null + }, + "9,2": { + "color": "green", + "mine": null + }, + "9,3": { + "color": "green", + "mine": null + }, + "9,4": { + "color": "green", + "mine": { + "mine_type": "a" + } + }, + "9,5": { + "color": "blue", + "mine": null + }, + "9,6": { + "color": "white", + "mine": { + "mine_type": "a" + } + }, + "9,7": { + "color": "orange", + "mine": { + "mine_type": "a" + } + }, + "9,8": { + "color": "blue", + "mine": { + "mine_type": "a" + } + }, + "9,9": { + "color": "orange", + "mine": null + }, + "agent_starting_position": "0,0" +} \ No newline at end of file