From fcad1beff80fab7e69fa3ae1668540728ce949f0 Mon Sep 17 00:00:00 2001 From: Cameron Smart Date: Sat, 18 Apr 2020 18:12:16 -0700 Subject: [PATCH] load_file in map.py sets the tiles based on a json file --- triple-dungeon/config.py | 1 + triple-dungeon/main.py | 14 +++----- triple-dungeon/map.py | 34 ++++++++++++++++-- .../resources/images/tiles/wall_tile.png | Bin 0 -> 2391 bytes triple-dungeon/resources/levels/box.json | 19 ++++++++++ 5 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 triple-dungeon/resources/images/tiles/wall_tile.png create mode 100644 triple-dungeon/resources/levels/box.json diff --git a/triple-dungeon/config.py b/triple-dungeon/config.py index 933abaf..5366d99 100644 --- a/triple-dungeon/config.py +++ b/triple-dungeon/config.py @@ -14,6 +14,7 @@ class Config(object): SCREEN_WIDTH = 1000 SCREEN_HEIGHT = 650 SCREEN_TITLE = "Triple Dungeon" + TILE_WIDTH = 63 # Constants used to scale our sprites from their original size CHARACTER_SCALING = 1 diff --git a/triple-dungeon/main.py b/triple-dungeon/main.py index 0b13d56..8d80d0a 100644 --- a/triple-dungeon/main.py +++ b/triple-dungeon/main.py @@ -7,6 +7,7 @@ Holds the main game window, as well as manages basic functions for organizing th import arcade from config import Config +from map import Level class Game(arcade.Window): @@ -47,21 +48,15 @@ class Game(arcade.Window): self.player_list = arcade.SpriteList() # Set up the player, specifically placing it at these coordinates. - image_source = "images/monsters/skeleton.png" + image_source = "resources/images/monsters/skeleton.png" self.player_sprite = arcade.Sprite(image_source, Config.CHARACTER_SCALING) self.player_sprite.center_x = Config.SCREEN_WIDTH / 2 self.player_sprite.center_y = Config.SCREEN_HEIGHT / 2 self.player_sprite.scale = 4 self.player_list.append(self.player_sprite) - # Create the floor - # This shows using a loop to place multiple sprites horizontally and vertically - for y in range(0, 1250, 63 * Config.TILE_SCALING): - for x in range(0, 1250, 63 * Config.TILE_SCALING): - floor = arcade.Sprite("images/tiles/floor_tile.png", Config.TILE_SCALING) - floor.center_x = x - floor.center_y = y - self.floor_list.append(floor) + # Create the level + self.floor_list, self.wall_list = Level.load_file('resources/levels/box.json') # Create the 'physics engine' self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list) @@ -75,6 +70,7 @@ class Game(arcade.Window): # Draw our sprites self.floor_list.draw() self.player_list.draw() + self.wall_list.draw() def on_key_press(self, key, modifiers): """Called whenever a key is pressed. """ diff --git a/triple-dungeon/map.py b/triple-dungeon/map.py index beff019..ab6399f 100644 --- a/triple-dungeon/map.py +++ b/triple-dungeon/map.py @@ -5,9 +5,10 @@ Pathfinding will also depend on objects here, and is thus integral to it's funct """ from __future__ import annotations +from config import Config import arcade - +import json class Dungeon(object): """ @@ -69,7 +70,36 @@ class Level(object): :param path: Path to the Level file. :return: The new generated Level file. """ - pass + floor_list = arcade.SpriteList() + wall_list = arcade.SpriteList() + x = 0 + y = 0 + + with open(path) as file: + level = json.load(file) + elements = level['elements'] + structure = level['structure'] + + level_size = 10 * Config.TILE_SCALING * Config.TILE_WIDTH + + # Create the level + # This shows using a loop to place multiple sprites horizontally and vertically + for y_pos in range(0, level_size , 63 * Config.TILE_SCALING): + for x_pos in range(0, level_size, 63 * Config.TILE_SCALING): + cur_tile = structure[y][x] + sprite = elements[cur_tile] + floor = arcade.Sprite(sprite, Config.TILE_SCALING) + floor.center_x = x_pos + floor.center_y = y_pos + if(cur_tile == ' '): + floor_list.append(floor) + elif(cur_tile == 'w'): + wall_list.append(floor) + x += 1 + x = 0 + y += 1 + + return (floor_list, wall_list) def render(self) -> None: """ diff --git a/triple-dungeon/resources/images/tiles/wall_tile.png b/triple-dungeon/resources/images/tiles/wall_tile.png new file mode 100644 index 0000000000000000000000000000000000000000..dec66a05973156d9d35f1eacf7728c724f237452 GIT binary patch literal 2391 zcmV-d38?moP)+WSL7K?qy4@}ee!O6&GQWFz?(y%dU`Ts^h zW-%>XNX#1}Or2go(xR$0RO_8IY)o3dq4N;k(^Z7$?u9nfyK(QdbIQ~|efrp)|s^YsfW32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