From abf80c1b0fc1ada22f2eaa50268d80ca34a2a6c8 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 01:31:08 -0500 Subject: [PATCH 01/22] general refactor/cleanup to get closer to PEP8 standards --- triple-dungeon/main.py | 19 +++++++++---------- triple-dungeon/map.py | 29 +++++++++++++++++++---------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/triple-dungeon/main.py b/triple-dungeon/main.py index f25021f..46b5d45 100644 --- a/triple-dungeon/main.py +++ b/triple-dungeon/main.py @@ -6,9 +6,9 @@ Holds the main game window, as well as manages basic functions for organizing th import arcade +from config import Config from map import Dungeon -from mobs import Player, Enemy -from config import Config, Sprites +from mobs import Player class Game(arcade.Window): @@ -31,11 +31,11 @@ class Game(arcade.Window): self.player = None # list to keep track of keypresses - self.prev_keypress = [] + self.prev_keypress = [] # Our physics engine self.physics_engine = None - + # Used to keep track of our scrolling self.view_bottom = 0 self.view_left = 0 @@ -58,17 +58,16 @@ class Game(arcade.Window): self.player.center_y = Config.SCREEN_HEIGHT / 2 self.player_list = self.player - # Create the dungeon dungeon = Dungeon() - + self.floor_list = dungeon.floor_list self.wall_list = dungeon.wall_list - + # Create monsters # This needs to be updated to comply with the new mobs.py code - #self.enemy_list.append(Enemy("resources/images/monsters/ghost/ghost1.png", 200, 200, 4).get_enemy()) - #self.enemy_list.append(Enemy("resources/images/monsters/frog/frog1.png", 200, 1000, 4).get_enemy()) + # self.enemy_list.append(Enemy("resources/images/monsters/ghost/ghost1.png", 200, 200, 4).get_enemy()) + # self.enemy_list.append(Enemy("resources/images/monsters/frog/frog1.png", 200, 1000, 4).get_enemy()) # Create the 'physics engine' self.physics_engine = arcade.PhysicsEngineSimple(self.player, self.wall_list) @@ -101,7 +100,7 @@ class Game(arcade.Window): self.player_list.change_x = Config.PLAYER_MOVEMENT_SPEED self.prev_keypress.append(key) elif key == 65307: - self.close() + self.close() def on_key_release(self, key, modifiers): """Called when the user releases a key. """ diff --git a/triple-dungeon/map.py b/triple-dungeon/map.py index be81f45..bbb2ce9 100644 --- a/triple-dungeon/map.py +++ b/triple-dungeon/map.py @@ -5,12 +5,14 @@ Pathfinding will also depend on objects here, and is thus integral to it's funct """ from __future__ import annotations -from config import Config + +import json import arcade -import json import numpy as np +from config import Config + class Dungeon(object): """ @@ -24,10 +26,13 @@ class Dungeon(object): :param level_count: The number of Active Levels that should be stored within the Dungeon. :param size: The diameter of the dungeon. Allows for a total of size^2 slots for levels. """ - # setup + + self.level_count = level_count + self.size = size + self.floor_list = arcade.SpriteList() self.wall_list = arcade.SpriteList() - level_size = 10 * Config.TILE_SCALING * Config.TILE_WIDTH + level_size = 10 * Config.TILE_SCALING * Config.TILE_WIDTH # get center level @@ -43,7 +48,7 @@ class Dungeon(object): room.load_file('resources/levels/map1/room.json') room.rotate_level(2) room.render() - room_floor, room_wall = room.get_lists() + room_floor, room_wall = room.floor_list, room.wall_list room_floor.move(level_size, 0) room_wall.move(level_size, 0) self.floor_list.extend(room_floor) @@ -53,7 +58,7 @@ class Dungeon(object): room = Level() room.load_file('resources/levels/map1/room.json') room.render() - room_floor, room_wall = room.get_lists() + room_floor, room_wall = room.floor_list, room.wall_list room_floor.move(-level_size, 0) room_wall.move(-level_size, 0) self.floor_list.extend(room_floor) @@ -112,10 +117,9 @@ class Level: with open(path) as file: level = json.load(file) + self.sprites = level['elements'] + self.level = level['structure'] - self.sprites = level['elements'] - self.level = level['structure'] - def render(self) -> None: """ Calls render on all sprites. @@ -126,7 +130,7 @@ class Level: # 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 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 = self.level[y][x] sprite = self.sprites[cur_tile] @@ -145,6 +149,11 @@ class Level: return self.floor_list, self.wall_list def rotate_level(self, times_rotated): + """ + Rotates the + :param times_rotated: + :return: + """ m = np.array(self.level) for i in range(0, times_rotated % 4): m = np.rot90(m) From 396fa7775d5a3fb9d571260a96088764d9dc9904 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 01:33:01 -0500 Subject: [PATCH 02/22] remove useless functions, basic reformat command, add dumb docstring --- triple-dungeon/map.py | 7 ------- triple-dungeon/mobs.py | 32 ++++++++++++++++++++------------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/triple-dungeon/map.py b/triple-dungeon/map.py index bbb2ce9..b9471c9 100644 --- a/triple-dungeon/map.py +++ b/triple-dungeon/map.py @@ -64,10 +64,6 @@ class Dungeon(object): self.floor_list.extend(room_floor) self.wall_list.extend(room_wall) - def add_level(self, sprit_list): - for x in sprit_list: - self.levels.append(x) - def render(self) -> None: """ Calls render on all level @@ -145,9 +141,6 @@ class Level: x = 0 y += 1 - def get_lists(self): - return self.floor_list, self.wall_list - def rotate_level(self, times_rotated): """ Rotates the diff --git a/triple-dungeon/mobs.py b/triple-dungeon/mobs.py index 5457745..b2e34ea 100644 --- a/triple-dungeon/mobs.py +++ b/triple-dungeon/mobs.py @@ -5,7 +5,7 @@ Organizes all classes related to Mobs, Entities, Enemies, Players and Items. import arcade -from config import Config, Sprites +from config import Config # Constants used to track if the player is facing left or right RIGHT_FACING = 0 @@ -14,10 +14,12 @@ FRONT_FACING = 2 UP_FACING = 3 DOWN_FACING = 4 + class Mob(arcade.Sprite): """ Represents a Mob. No defined behaviour, it has no intelligence. """ + def __init__(self, max_health=100, max_armor=0, *args, **kwargs) -> None: # Set up parent class super().__init__() @@ -42,6 +44,7 @@ class Player(Mob): Represents a Player. While this is a instance, there should only be one in the world at any given time. """ + def __init__(self, *args, **kwargs) -> None: super(Player, self).__init__(*args, **kwargs) @@ -57,8 +60,10 @@ class Player(Mob): # Load textures for running horizontally for i in range(6): - self.walking_textures.append([arcade.load_texture(f"{main_path}knight iso char_run left_{i}.png"),arcade.load_texture(f"{main_path}knight iso char_run left_{i}.png", mirrored=True)]) - + self.walking_textures.append([arcade.load_texture(f"{main_path}knight iso char_run left_{i}.png"), + arcade.load_texture(f"{main_path}knight iso char_run left_{i}.png", + mirrored=True)]) + # Load textures for running down for i in range(5): self.down_textures.append(arcade.load_texture(f"{main_path}knight iso char_run down_{i}.png")) @@ -66,8 +71,13 @@ class Player(Mob): # Load textures for running up for i in range(5): self.up_textures.append(arcade.load_texture(f"{main_path}knight iso char_run up_{i}.png")) - - def update_animation(self, delta_time: float = 1/60): + + def update_animation(self, delta_time: float = 1 / 60) -> None: + """ + Updates animations for the Player. + :param delta_time: No idea. + """ + # Figure out if we need to flip face left, right, up, or down if self.change_x > 0: self.character_face_direction = LEFT_FACING @@ -76,7 +86,6 @@ class Player(Mob): elif self.change_x == 0 and self.change_y == 0: self.character_face_direction = FRONT_FACING - # idle animation if self.change_x == 0 and self.change_y == 0: self.cur_texture += 1 @@ -85,7 +94,7 @@ class Player(Mob): self.texture = self.idle_textures[self.cur_texture // Config.IDLE_UPDATES_PER_FRAME] return - #walk up animation + # walk up animation if self.change_y > 0: self.cur_texture += 1 if self.cur_texture > 4 * Config.RUN_UPDATES_PER_FRAME: @@ -93,7 +102,7 @@ class Player(Mob): self.texture = self.up_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME] return - #walk down animation + # walk down animation if self.change_y < 0: self.cur_texture += 1 if self.cur_texture > 4 * Config.RUN_UPDATES_PER_FRAME: @@ -105,7 +114,8 @@ class Player(Mob): self.cur_texture += 1 if self.cur_texture > 5 * Config.RUN_UPDATES_PER_FRAME: self.cur_texture = 0 - self.texture = self.walking_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME][self.character_face_direction] + self.texture = self.walking_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME][ + self.character_face_direction] def tick(self): """ @@ -120,12 +130,10 @@ class Enemy(Mob): Represents an Enemy Mob. Will take basic offensive actions against Player objects. """ + def __init__(self, *args, **kwargs) -> None: super(Enemy, self).__init__(*args, **kwargs) - def get_enemy(self): - return self - def tick(self) -> None: """ A on_update function, the Enemy Mob should scan for the player, decide how to path to it, and From 290942aa0d7cf8b00c945259ba3a95405c23a50d Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 01:36:06 -0500 Subject: [PATCH 03/22] fixed last two check misses, added second line before Config class declaration, fixed extra space at end of comment --- triple-dungeon/config.py | 1 + triple-dungeon/main.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/triple-dungeon/config.py b/triple-dungeon/config.py index 1bae98e..9ed6819 100644 --- a/triple-dungeon/config.py +++ b/triple-dungeon/config.py @@ -9,6 +9,7 @@ BASE_PATH = os.path.dirname(os.path.abspath(__file__)) RESOURCES = os.path.join(BASE_PATH, "resources") IMAGES = os.path.join(RESOURCES, "images") + class Config(object): """ A simple class dedicated to loading, storing and organizing constants. diff --git a/triple-dungeon/main.py b/triple-dungeon/main.py index 46b5d45..193a117 100644 --- a/triple-dungeon/main.py +++ b/triple-dungeon/main.py @@ -51,7 +51,7 @@ class Game(arcade.Window): self.floor_list = arcade.SpriteList() self.enemy_list = arcade.SpriteList() - # Set up the player, specifically placing it at these coordinates. + # Set up the player, specifically placing it at these coordinates. self.player = Player() self.player.scale = 1 self.player.center_x = Config.SCREEN_WIDTH / 2 From 3d9a6d3eb41b80731a4a7a325b7acebc3c015388 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 01:44:21 -0500 Subject: [PATCH 04/22] create new enums class, move out constants from mobs.py --- triple-dungeon/config.py | 15 +++++++++++++++ triple-dungeon/mobs.py | 18 +++++------------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/triple-dungeon/config.py b/triple-dungeon/config.py index 9ed6819..5a92183 100644 --- a/triple-dungeon/config.py +++ b/triple-dungeon/config.py @@ -3,8 +3,11 @@ config.py Holds all constants used for setting up the game. May later hold functions for loading/saving configuration files. """ + import os +from enum import Enum + BASE_PATH = os.path.dirname(os.path.abspath(__file__)) RESOURCES = os.path.join(BASE_PATH, "resources") IMAGES = os.path.join(RESOURCES, "images") @@ -37,6 +40,18 @@ class Config(object): TOP_VIEWPORT_MARGIN = 100 +class Enums(Enum): + """ + A simple class used for tracking different simple + """ + + # Play Direction Enums + RIGHT_FACING + LEFT_FACING + FRONT_FACING + UP_FACING + DOWN_FACING + class Sprites(object): """ Simple class for holding sprite paths. diff --git a/triple-dungeon/mobs.py b/triple-dungeon/mobs.py index b2e34ea..283958e 100644 --- a/triple-dungeon/mobs.py +++ b/triple-dungeon/mobs.py @@ -5,15 +5,7 @@ Organizes all classes related to Mobs, Entities, Enemies, Players and Items. import arcade -from config import Config - -# Constants used to track if the player is facing left or right -RIGHT_FACING = 0 -LEFT_FACING = 1 -FRONT_FACING = 2 -UP_FACING = 3 -DOWN_FACING = 4 - +from config import Config, Enums class Mob(arcade.Sprite): """ @@ -51,7 +43,7 @@ class Player(Mob): main_path = "resources/images/character/knight/" # Default to face-front - self.character_face_direction = FRONT_FACING + self.character_face_direction = Enums.FRONT_FACING # Load textures for idle standing for i in range(4): @@ -80,11 +72,11 @@ class Player(Mob): # Figure out if we need to flip face left, right, up, or down if self.change_x > 0: - self.character_face_direction = LEFT_FACING + self.character_face_direction = Enums.LEFT_FACING elif self.change_x < 0: - self.character_face_direction = RIGHT_FACING + self.character_face_direction = Enums.RIGHT_FACING elif self.change_x == 0 and self.change_y == 0: - self.character_face_direction = FRONT_FACING + self.character_face_direction = Enums.FRONT_FACING # idle animation if self.change_x == 0 and self.change_y == 0: From d5cd8e8a664999b82a25fcf4e6aba9a47e4d3b8f Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 02:02:25 -0500 Subject: [PATCH 05/22] rename all knight animation files --- .../resources/images/character/knight/fix.py | 14 ++++++++++++++ .../{knight iso char_idle_0.png => idle_0.png} | Bin .../{knight iso char_idle_1.png => idle_1.png} | Bin .../{knight iso char_idle_2.png => idle_2.png} | Bin .../{knight iso char_idle_3.png => idle_3.png} | Bin ...ght iso char_run down_0.png => run_down_0.png} | Bin ...ght iso char_run down_1.png => run_down_1.png} | Bin ...ght iso char_run down_2.png => run_down_2.png} | Bin ...ght iso char_run down_3.png => run_down_3.png} | Bin ...ght iso char_run down_4.png => run_down_4.png} | Bin ...ght iso char_run left_0.png => run_left_0.png} | Bin ...ght iso char_run left_1.png => run_left_1.png} | Bin ...ght iso char_run left_2.png => run_left_2.png} | Bin ...ght iso char_run left_3.png => run_left_3.png} | Bin ...ght iso char_run left_4.png => run_left_4.png} | Bin ...ght iso char_run left_5.png => run_left_5.png} | Bin ...t iso char_run right_0.png => run_right_0.png} | Bin ...t iso char_run right_1.png => run_right_1.png} | Bin ...t iso char_run right_2.png => run_right_2.png} | Bin ...t iso char_run right_3.png => run_right_3.png} | Bin ...t iso char_run right_4.png => run_right_4.png} | Bin ...t iso char_run right_5.png => run_right_5.png} | Bin ...{knight iso char_run up_0.png => run_up_0.png} | Bin ...{knight iso char_run up_1.png => run_up_1.png} | Bin ...{knight iso char_run up_2.png => run_up_2.png} | Bin ...{knight iso char_run up_3.png => run_up_3.png} | Bin ...{knight iso char_run up_4.png => run_up_4.png} | Bin ...iso char_slice down_0.png => slice_down_0.png} | Bin ...iso char_slice down_1.png => slice_down_1.png} | Bin ...iso char_slice down_2.png => slice_down_2.png} | Bin ...iso char_slice left_0.png => slice_left_0.png} | Bin ...iso char_slice left_1.png => slice_left_1.png} | Bin ...iso char_slice left_2.png => slice_left_2.png} | Bin ...o char_slice right_0.png => slice_right_0.png} | Bin ...o char_slice right_1.png => slice_right_1.png} | Bin ...o char_slice right_2.png => slice_right_2.png} | Bin ...ght iso char_slice up_0.png => slice_up_0.png} | Bin ...ght iso char_slice up_1.png => slice_up_1.png} | Bin ...ght iso char_slice up_2.png => slice_up_2.png} | Bin 39 files changed, 14 insertions(+) create mode 100644 triple-dungeon/resources/images/character/knight/fix.py rename triple-dungeon/resources/images/character/knight/{knight iso char_idle_0.png => idle_0.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_idle_1.png => idle_1.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_idle_2.png => idle_2.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_idle_3.png => idle_3.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run down_0.png => run_down_0.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run down_1.png => run_down_1.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run down_2.png => run_down_2.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run down_3.png => run_down_3.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run down_4.png => run_down_4.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run left_0.png => run_left_0.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run left_1.png => run_left_1.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run left_2.png => run_left_2.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run left_3.png => run_left_3.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run left_4.png => run_left_4.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run left_5.png => run_left_5.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run right_0.png => run_right_0.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run right_1.png => run_right_1.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run right_2.png => run_right_2.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run right_3.png => run_right_3.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run right_4.png => run_right_4.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run right_5.png => run_right_5.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run up_0.png => run_up_0.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run up_1.png => run_up_1.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run up_2.png => run_up_2.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run up_3.png => run_up_3.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_run up_4.png => run_up_4.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_slice down_0.png => slice_down_0.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_slice down_1.png => slice_down_1.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_slice down_2.png => slice_down_2.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_slice left_0.png => slice_left_0.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_slice left_1.png => slice_left_1.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_slice left_2.png => slice_left_2.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_slice right_0.png => slice_right_0.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_slice right_1.png => slice_right_1.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_slice right_2.png => slice_right_2.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_slice up_0.png => slice_up_0.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_slice up_1.png => slice_up_1.png} (100%) rename triple-dungeon/resources/images/character/knight/{knight iso char_slice up_2.png => slice_up_2.png} (100%) diff --git a/triple-dungeon/resources/images/character/knight/fix.py b/triple-dungeon/resources/images/character/knight/fix.py new file mode 100644 index 0000000..b225d9a --- /dev/null +++ b/triple-dungeon/resources/images/character/knight/fix.py @@ -0,0 +1,14 @@ +import shutil +import os + +BASE = os.path.dirname(os.path.abspath(__file__)) +files = os.listdir(BASE) + +for file in filter(lambda filename: not filename.endswith(".py"), files): + before = file + after = file.replace("knight iso ", "") + after = after.replace("char_idle", "idle") + after = after.replace("char_run ", "run_") + after = after.replace("char_slice ", "slice_") + print(f'"{before}" -> "{after}"') + shutil.move(before, after) \ No newline at end of file diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_idle_0.png b/triple-dungeon/resources/images/character/knight/idle_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_idle_0.png rename to triple-dungeon/resources/images/character/knight/idle_0.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_idle_1.png b/triple-dungeon/resources/images/character/knight/idle_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_idle_1.png rename to triple-dungeon/resources/images/character/knight/idle_1.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_idle_2.png b/triple-dungeon/resources/images/character/knight/idle_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_idle_2.png rename to triple-dungeon/resources/images/character/knight/idle_2.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_idle_3.png b/triple-dungeon/resources/images/character/knight/idle_3.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_idle_3.png rename to triple-dungeon/resources/images/character/knight/idle_3.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run down_0.png b/triple-dungeon/resources/images/character/knight/run_down_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run down_0.png rename to triple-dungeon/resources/images/character/knight/run_down_0.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run down_1.png b/triple-dungeon/resources/images/character/knight/run_down_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run down_1.png rename to triple-dungeon/resources/images/character/knight/run_down_1.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run down_2.png b/triple-dungeon/resources/images/character/knight/run_down_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run down_2.png rename to triple-dungeon/resources/images/character/knight/run_down_2.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run down_3.png b/triple-dungeon/resources/images/character/knight/run_down_3.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run down_3.png rename to triple-dungeon/resources/images/character/knight/run_down_3.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run down_4.png b/triple-dungeon/resources/images/character/knight/run_down_4.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run down_4.png rename to triple-dungeon/resources/images/character/knight/run_down_4.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run left_0.png b/triple-dungeon/resources/images/character/knight/run_left_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run left_0.png rename to triple-dungeon/resources/images/character/knight/run_left_0.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run left_1.png b/triple-dungeon/resources/images/character/knight/run_left_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run left_1.png rename to triple-dungeon/resources/images/character/knight/run_left_1.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run left_2.png b/triple-dungeon/resources/images/character/knight/run_left_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run left_2.png rename to triple-dungeon/resources/images/character/knight/run_left_2.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run left_3.png b/triple-dungeon/resources/images/character/knight/run_left_3.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run left_3.png rename to triple-dungeon/resources/images/character/knight/run_left_3.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run left_4.png b/triple-dungeon/resources/images/character/knight/run_left_4.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run left_4.png rename to triple-dungeon/resources/images/character/knight/run_left_4.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run left_5.png b/triple-dungeon/resources/images/character/knight/run_left_5.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run left_5.png rename to triple-dungeon/resources/images/character/knight/run_left_5.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run right_0.png b/triple-dungeon/resources/images/character/knight/run_right_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run right_0.png rename to triple-dungeon/resources/images/character/knight/run_right_0.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run right_1.png b/triple-dungeon/resources/images/character/knight/run_right_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run right_1.png rename to triple-dungeon/resources/images/character/knight/run_right_1.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run right_2.png b/triple-dungeon/resources/images/character/knight/run_right_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run right_2.png rename to triple-dungeon/resources/images/character/knight/run_right_2.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run right_3.png b/triple-dungeon/resources/images/character/knight/run_right_3.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run right_3.png rename to triple-dungeon/resources/images/character/knight/run_right_3.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run right_4.png b/triple-dungeon/resources/images/character/knight/run_right_4.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run right_4.png rename to triple-dungeon/resources/images/character/knight/run_right_4.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run right_5.png b/triple-dungeon/resources/images/character/knight/run_right_5.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run right_5.png rename to triple-dungeon/resources/images/character/knight/run_right_5.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run up_0.png b/triple-dungeon/resources/images/character/knight/run_up_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run up_0.png rename to triple-dungeon/resources/images/character/knight/run_up_0.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run up_1.png b/triple-dungeon/resources/images/character/knight/run_up_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run up_1.png rename to triple-dungeon/resources/images/character/knight/run_up_1.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run up_2.png b/triple-dungeon/resources/images/character/knight/run_up_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run up_2.png rename to triple-dungeon/resources/images/character/knight/run_up_2.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run up_3.png b/triple-dungeon/resources/images/character/knight/run_up_3.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run up_3.png rename to triple-dungeon/resources/images/character/knight/run_up_3.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_run up_4.png b/triple-dungeon/resources/images/character/knight/run_up_4.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_run up_4.png rename to triple-dungeon/resources/images/character/knight/run_up_4.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_slice down_0.png b/triple-dungeon/resources/images/character/knight/slice_down_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_slice down_0.png rename to triple-dungeon/resources/images/character/knight/slice_down_0.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_slice down_1.png b/triple-dungeon/resources/images/character/knight/slice_down_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_slice down_1.png rename to triple-dungeon/resources/images/character/knight/slice_down_1.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_slice down_2.png b/triple-dungeon/resources/images/character/knight/slice_down_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_slice down_2.png rename to triple-dungeon/resources/images/character/knight/slice_down_2.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_slice left_0.png b/triple-dungeon/resources/images/character/knight/slice_left_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_slice left_0.png rename to triple-dungeon/resources/images/character/knight/slice_left_0.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_slice left_1.png b/triple-dungeon/resources/images/character/knight/slice_left_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_slice left_1.png rename to triple-dungeon/resources/images/character/knight/slice_left_1.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_slice left_2.png b/triple-dungeon/resources/images/character/knight/slice_left_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_slice left_2.png rename to triple-dungeon/resources/images/character/knight/slice_left_2.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_slice right_0.png b/triple-dungeon/resources/images/character/knight/slice_right_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_slice right_0.png rename to triple-dungeon/resources/images/character/knight/slice_right_0.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_slice right_1.png b/triple-dungeon/resources/images/character/knight/slice_right_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_slice right_1.png rename to triple-dungeon/resources/images/character/knight/slice_right_1.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_slice right_2.png b/triple-dungeon/resources/images/character/knight/slice_right_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_slice right_2.png rename to triple-dungeon/resources/images/character/knight/slice_right_2.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_slice up_0.png b/triple-dungeon/resources/images/character/knight/slice_up_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_slice up_0.png rename to triple-dungeon/resources/images/character/knight/slice_up_0.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_slice up_1.png b/triple-dungeon/resources/images/character/knight/slice_up_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_slice up_1.png rename to triple-dungeon/resources/images/character/knight/slice_up_1.png diff --git a/triple-dungeon/resources/images/character/knight/knight iso char_slice up_2.png b/triple-dungeon/resources/images/character/knight/slice_up_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/knight iso char_slice up_2.png rename to triple-dungeon/resources/images/character/knight/slice_up_2.png From 1b2f8dc431658123186e5c137093086bbe43bd31 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 02:02:54 -0500 Subject: [PATCH 06/22] delete fix.py --- .../resources/images/character/knight/fix.py | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 triple-dungeon/resources/images/character/knight/fix.py diff --git a/triple-dungeon/resources/images/character/knight/fix.py b/triple-dungeon/resources/images/character/knight/fix.py deleted file mode 100644 index b225d9a..0000000 --- a/triple-dungeon/resources/images/character/knight/fix.py +++ /dev/null @@ -1,14 +0,0 @@ -import shutil -import os - -BASE = os.path.dirname(os.path.abspath(__file__)) -files = os.listdir(BASE) - -for file in filter(lambda filename: not filename.endswith(".py"), files): - before = file - after = file.replace("knight iso ", "") - after = after.replace("char_idle", "idle") - after = after.replace("char_run ", "run_") - after = after.replace("char_slice ", "slice_") - print(f'"{before}" -> "{after}"') - shutil.move(before, after) \ No newline at end of file From 35513dce162bb782087097b177cf17df7a2bacca Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 02:03:15 -0500 Subject: [PATCH 07/22] implement new enums into animations file --- triple-dungeon/config.py | 10 +++++----- triple-dungeon/mobs.py | 13 ++++++------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/triple-dungeon/config.py b/triple-dungeon/config.py index 5a92183..a1a2aac 100644 --- a/triple-dungeon/config.py +++ b/triple-dungeon/config.py @@ -46,11 +46,11 @@ class Enums(Enum): """ # Play Direction Enums - RIGHT_FACING - LEFT_FACING - FRONT_FACING - UP_FACING - DOWN_FACING + RIGHT_FACING = 0 + LEFT_FACING = 1 + FRONT_FACING = 2 + UP_FACING = 3 + DOWN_FACING = 4 class Sprites(object): """ diff --git a/triple-dungeon/mobs.py b/triple-dungeon/mobs.py index 283958e..a6c3477 100644 --- a/triple-dungeon/mobs.py +++ b/triple-dungeon/mobs.py @@ -43,7 +43,7 @@ class Player(Mob): main_path = "resources/images/character/knight/" # Default to face-front - self.character_face_direction = Enums.FRONT_FACING + self.character_dir = Enums.FRONT_FACING # Load textures for idle standing for i in range(4): @@ -72,13 +72,13 @@ class Player(Mob): # Figure out if we need to flip face left, right, up, or down if self.change_x > 0: - self.character_face_direction = Enums.LEFT_FACING + self.character_dir = Enums.LEFT_FACING elif self.change_x < 0: - self.character_face_direction = Enums.RIGHT_FACING + self.character_dir = Enums.RIGHT_FACING elif self.change_x == 0 and self.change_y == 0: - self.character_face_direction = Enums.FRONT_FACING + self.character_dir = Enums.FRONT_FACING - # idle animation + # Idle Animation if self.change_x == 0 and self.change_y == 0: self.cur_texture += 1 if self.cur_texture > 3 * Config.IDLE_UPDATES_PER_FRAME: @@ -106,8 +106,7 @@ class Player(Mob): self.cur_texture += 1 if self.cur_texture > 5 * Config.RUN_UPDATES_PER_FRAME: self.cur_texture = 0 - self.texture = self.walking_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME][ - self.character_face_direction] + self.texture = self.walking_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME][self.character_dir.value] def tick(self): """ From 3378d916e5084491f42c5bc56e95cee859f971e3 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 02:19:19 -0500 Subject: [PATCH 08/22] create new sprites file for managing animations and static --- triple-dungeon/config.py | 3 ++- triple-dungeon/sprites.py | 41 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 triple-dungeon/sprites.py diff --git a/triple-dungeon/config.py b/triple-dungeon/config.py index a1a2aac..6fc426f 100644 --- a/triple-dungeon/config.py +++ b/triple-dungeon/config.py @@ -52,7 +52,8 @@ class Enums(Enum): UP_FACING = 3 DOWN_FACING = 4 -class Sprites(object): + +class SpritePaths(object): """ Simple class for holding sprite paths. """ diff --git a/triple-dungeon/sprites.py b/triple-dungeon/sprites.py new file mode 100644 index 0000000..eb77079 --- /dev/null +++ b/triple-dungeon/sprites.py @@ -0,0 +1,41 @@ +""" +sprites.py +A file dedicated to managing sprites and animations for characters. +""" +import re + + +class AnimationSet(object): + """ + A class that helps assist with grabbing new animations from a set. + """ + + def __init__(self, directory: str): + """ + Initializes the AnimationSet class by loading files and + + :param directory: A directory containing valid animation files in the correct format. + """ + pass + + +class PlayerAnimations(AnimationSet): + """ + A class dedicated to serving player animations. + Player animations must be served to the class in the correct format. + """ + + def __init__(self): + """ + Initializes the PlayerAnimations class. + """ + + super(PlayerAnimations, self).__init__() + + self.idles = self.getAnimations(re.compile(r'idle_\d+.png')) + self.down = self.getAnimations(re.compile(r'run_down_\d+.png')) + self.right = self.getAnimations(re.compile(r'run_right_\d+.png')) + self.up = self.getAnimations(re.compile(r'run_up_\d+.png')) + self.down = self.getAnimations(re.compile(r'run_left_\d+.png')) + + def __loadAnimations(self): \ No newline at end of file From b2a953c0e52d6e9b1e973626f61204a9062f1c37 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 02:30:21 -0500 Subject: [PATCH 09/22] implement getAnimations, correct regex patterns with capturing groups --- triple-dungeon/sprites.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/triple-dungeon/sprites.py b/triple-dungeon/sprites.py index eb77079..35ff8e8 100644 --- a/triple-dungeon/sprites.py +++ b/triple-dungeon/sprites.py @@ -2,6 +2,7 @@ sprites.py A file dedicated to managing sprites and animations for characters. """ +import os import re @@ -16,8 +17,17 @@ class AnimationSet(object): :param directory: A directory containing valid animation files in the correct format. """ - pass + self.animations = os.path.listdir(directory) + + def getAnimations(self, pattern: re.Pattern) -> iter: + # Finds all matching files + matches = [file for file in self.animations if re.match(pattern, file)] + # Sort in ascending order based on the connected animation index. Zero-indexing or not does not affect order. + matches.sort(key=lambda match : int(match.group(1))) + # Grab the filename and load it to the file directory + matches = list(map(lambda match : os.path.join(directory, match.group(0)), matches)) + return matches class PlayerAnimations(AnimationSet): """ @@ -32,10 +42,11 @@ class PlayerAnimations(AnimationSet): super(PlayerAnimations, self).__init__() - self.idles = self.getAnimations(re.compile(r'idle_\d+.png')) - self.down = self.getAnimations(re.compile(r'run_down_\d+.png')) - self.right = self.getAnimations(re.compile(r'run_right_\d+.png')) - self.up = self.getAnimations(re.compile(r'run_up_\d+.png')) - self.down = self.getAnimations(re.compile(r'run_left_\d+.png')) + # Grabs all animations needed. These are infinite iters, use next(iter) to grab the next animation. + self.idles = self.getAnimations(re.compile(r'idle_(\d+).png')) + self.down = self.getAnimations(re.compile(r'run_down_(\d+).png')) + self.right = self.getAnimations(re.compile(r'run_right_(\d+).png')) + self.up = self.getAnimations(re.compile(r'run_up_(\d+).png')) + self.down = self.getAnimations(re.compile(r'run_left_(\d+).png')) def __loadAnimations(self): \ No newline at end of file From 0d0a8f8b6ad9dc7c113625fc41d43a38cff6b030 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 02:35:58 -0500 Subject: [PATCH 10/22] add infinite cycle iter and arcade texture object loading, fix docstrings --- triple-dungeon/sprites.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/triple-dungeon/sprites.py b/triple-dungeon/sprites.py index 35ff8e8..24c1cc2 100644 --- a/triple-dungeon/sprites.py +++ b/triple-dungeon/sprites.py @@ -2,9 +2,13 @@ sprites.py A file dedicated to managing sprites and animations for characters. """ + +import arcade import os import re +from itertools import cycle + class AnimationSet(object): """ @@ -18,16 +22,25 @@ class AnimationSet(object): :param directory: A directory containing valid animation files in the correct format. """ - self.animations = os.path.listdir(directory) + self.directory = directory + self.animations = os.listdir(directory) def getAnimations(self, pattern: re.Pattern) -> iter: + """ + Loads all animations from the AnimationSet's directory that match the pattern. + The pattern must have 1 group that specifies the animation's index. + + :param pattern: A RegEx Pattern object. + :return: A infinite iterable looping through arcade.Texture objects. + """ + # Finds all matching files matches = [file for file in self.animations if re.match(pattern, file)] # Sort in ascending order based on the connected animation index. Zero-indexing or not does not affect order. matches.sort(key=lambda match : int(match.group(1))) # Grab the filename and load it to the file directory - matches = list(map(lambda match : os.path.join(directory, match.group(0)), matches)) - return matches + matches = list(map(lambda match : arcade.load_texture(os.path.join(self.directory, match.group(0))), matches)) + return cycle(matches) class PlayerAnimations(AnimationSet): """ From f457ad06432a980ea42a4f42a98117923fa31a8c Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 02:41:08 -0500 Subject: [PATCH 11/22] fix simple whitespace issues --- triple-dungeon/mobs.py | 1 + triple-dungeon/sprites.py | 11 +++++------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/triple-dungeon/mobs.py b/triple-dungeon/mobs.py index a6c3477..741bffe 100644 --- a/triple-dungeon/mobs.py +++ b/triple-dungeon/mobs.py @@ -7,6 +7,7 @@ import arcade from config import Config, Enums + class Mob(arcade.Sprite): """ Represents a Mob. No defined behaviour, it has no intelligence. diff --git a/triple-dungeon/sprites.py b/triple-dungeon/sprites.py index 24c1cc2..ea3301d 100644 --- a/triple-dungeon/sprites.py +++ b/triple-dungeon/sprites.py @@ -37,23 +37,24 @@ class AnimationSet(object): # Finds all matching files matches = [file for file in self.animations if re.match(pattern, file)] # Sort in ascending order based on the connected animation index. Zero-indexing or not does not affect order. - matches.sort(key=lambda match : int(match.group(1))) + matches.sort(key=lambda match: int(match.group(1))) # Grab the filename and load it to the file directory - matches = list(map(lambda match : arcade.load_texture(os.path.join(self.directory, match.group(0))), matches)) + matches = list(map(lambda match: arcade.load_texture(os.path.join(self.directory, match.group(0))), matches)) return cycle(matches) + class PlayerAnimations(AnimationSet): """ A class dedicated to serving player animations. Player animations must be served to the class in the correct format. """ - def __init__(self): + def __init__(self, directory: str): """ Initializes the PlayerAnimations class. """ - super(PlayerAnimations, self).__init__() + super(PlayerAnimations, self).__init__(directory) # Grabs all animations needed. These are infinite iters, use next(iter) to grab the next animation. self.idles = self.getAnimations(re.compile(r'idle_(\d+).png')) @@ -61,5 +62,3 @@ class PlayerAnimations(AnimationSet): self.right = self.getAnimations(re.compile(r'run_right_(\d+).png')) self.up = self.getAnimations(re.compile(r'run_up_(\d+).png')) self.down = self.getAnimations(re.compile(r'run_left_(\d+).png')) - - def __loadAnimations(self): \ No newline at end of file From 1e8f5b507ce9e4ad4a7da0da9a80a2f7767eeb61 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 02:53:29 -0500 Subject: [PATCH 12/22] add new knight sprite path, change folder "character" to "characters" --- triple-dungeon/config.py | 7 ++++++- .../{character => characters}/knight/idle_0.png | Bin .../{character => characters}/knight/idle_1.png | Bin .../{character => characters}/knight/idle_2.png | Bin .../{character => characters}/knight/idle_3.png | Bin .../{character => characters}/knight/run_down_0.png | Bin .../{character => characters}/knight/run_down_1.png | Bin .../{character => characters}/knight/run_down_2.png | Bin .../{character => characters}/knight/run_down_3.png | Bin .../{character => characters}/knight/run_down_4.png | Bin .../{character => characters}/knight/run_left_0.png | Bin .../{character => characters}/knight/run_left_1.png | Bin .../{character => characters}/knight/run_left_2.png | Bin .../{character => characters}/knight/run_left_3.png | Bin .../{character => characters}/knight/run_left_4.png | Bin .../{character => characters}/knight/run_left_5.png | Bin .../knight/run_right_0.png | Bin .../knight/run_right_1.png | Bin .../knight/run_right_2.png | Bin .../knight/run_right_3.png | Bin .../knight/run_right_4.png | Bin .../knight/run_right_5.png | Bin .../{character => characters}/knight/run_up_0.png | Bin .../{character => characters}/knight/run_up_1.png | Bin .../{character => characters}/knight/run_up_2.png | Bin .../{character => characters}/knight/run_up_3.png | Bin .../{character => characters}/knight/run_up_4.png | Bin .../knight/slice_down_0.png | Bin .../knight/slice_down_1.png | Bin .../knight/slice_down_2.png | Bin .../knight/slice_left_0.png | Bin .../knight/slice_left_1.png | Bin .../knight/slice_left_2.png | Bin .../knight/slice_right_0.png | Bin .../knight/slice_right_1.png | Bin .../knight/slice_right_2.png | Bin .../{character => characters}/knight/slice_up_0.png | Bin .../{character => characters}/knight/slice_up_1.png | Bin .../{character => characters}/knight/slice_up_2.png | Bin 39 files changed, 6 insertions(+), 1 deletion(-) rename triple-dungeon/resources/images/{character => characters}/knight/idle_0.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/idle_1.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/idle_2.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/idle_3.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_down_0.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_down_1.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_down_2.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_down_3.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_down_4.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_left_0.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_left_1.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_left_2.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_left_3.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_left_4.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_left_5.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_right_0.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_right_1.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_right_2.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_right_3.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_right_4.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_right_5.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_up_0.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_up_1.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_up_2.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_up_3.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/run_up_4.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/slice_down_0.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/slice_down_1.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/slice_down_2.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/slice_left_0.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/slice_left_1.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/slice_left_2.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/slice_right_0.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/slice_right_1.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/slice_right_2.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/slice_up_0.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/slice_up_1.png (100%) rename triple-dungeon/resources/images/{character => characters}/knight/slice_up_2.png (100%) diff --git a/triple-dungeon/config.py b/triple-dungeon/config.py index 6fc426f..0cab7fd 100644 --- a/triple-dungeon/config.py +++ b/triple-dungeon/config.py @@ -33,7 +33,7 @@ class Config(object): # Movement speed of player, in pixels per frame PLAYER_MOVEMENT_SPEED = 7 - # How many pixels to keep as a minimum margin between the character and the edge of the screen. + # How many pixels to keep as a minimum margin between the characters and the edge of the screen. LEFT_VIEWPORT_MARGIN = 250 RIGHT_VIEWPORT_MARGIN = 250 BOTTOM_VIEWPORT_MARGIN = 50 @@ -58,8 +58,13 @@ class SpritePaths(object): Simple class for holding sprite paths. """ + __CHARACTERS = os.path.join(IMAGES, "characters") __MONSTERS = os.path.join(IMAGES, "monsters") + # Single frame sprites SKELETON = os.path.join(__MONSTERS, "skeleton.png") GHOST = os.path.join(__MONSTERS, "ghost", "ghost1.png") FROG = os.path.join(__MONSTERS, "frog", "frog1.png") + + # Animated sprites + KNIGHT = os.path.join(__CHARACTERS, "knight") diff --git a/triple-dungeon/resources/images/character/knight/idle_0.png b/triple-dungeon/resources/images/characters/knight/idle_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/idle_0.png rename to triple-dungeon/resources/images/characters/knight/idle_0.png diff --git a/triple-dungeon/resources/images/character/knight/idle_1.png b/triple-dungeon/resources/images/characters/knight/idle_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/idle_1.png rename to triple-dungeon/resources/images/characters/knight/idle_1.png diff --git a/triple-dungeon/resources/images/character/knight/idle_2.png b/triple-dungeon/resources/images/characters/knight/idle_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/idle_2.png rename to triple-dungeon/resources/images/characters/knight/idle_2.png diff --git a/triple-dungeon/resources/images/character/knight/idle_3.png b/triple-dungeon/resources/images/characters/knight/idle_3.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/idle_3.png rename to triple-dungeon/resources/images/characters/knight/idle_3.png diff --git a/triple-dungeon/resources/images/character/knight/run_down_0.png b/triple-dungeon/resources/images/characters/knight/run_down_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_down_0.png rename to triple-dungeon/resources/images/characters/knight/run_down_0.png diff --git a/triple-dungeon/resources/images/character/knight/run_down_1.png b/triple-dungeon/resources/images/characters/knight/run_down_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_down_1.png rename to triple-dungeon/resources/images/characters/knight/run_down_1.png diff --git a/triple-dungeon/resources/images/character/knight/run_down_2.png b/triple-dungeon/resources/images/characters/knight/run_down_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_down_2.png rename to triple-dungeon/resources/images/characters/knight/run_down_2.png diff --git a/triple-dungeon/resources/images/character/knight/run_down_3.png b/triple-dungeon/resources/images/characters/knight/run_down_3.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_down_3.png rename to triple-dungeon/resources/images/characters/knight/run_down_3.png diff --git a/triple-dungeon/resources/images/character/knight/run_down_4.png b/triple-dungeon/resources/images/characters/knight/run_down_4.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_down_4.png rename to triple-dungeon/resources/images/characters/knight/run_down_4.png diff --git a/triple-dungeon/resources/images/character/knight/run_left_0.png b/triple-dungeon/resources/images/characters/knight/run_left_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_left_0.png rename to triple-dungeon/resources/images/characters/knight/run_left_0.png diff --git a/triple-dungeon/resources/images/character/knight/run_left_1.png b/triple-dungeon/resources/images/characters/knight/run_left_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_left_1.png rename to triple-dungeon/resources/images/characters/knight/run_left_1.png diff --git a/triple-dungeon/resources/images/character/knight/run_left_2.png b/triple-dungeon/resources/images/characters/knight/run_left_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_left_2.png rename to triple-dungeon/resources/images/characters/knight/run_left_2.png diff --git a/triple-dungeon/resources/images/character/knight/run_left_3.png b/triple-dungeon/resources/images/characters/knight/run_left_3.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_left_3.png rename to triple-dungeon/resources/images/characters/knight/run_left_3.png diff --git a/triple-dungeon/resources/images/character/knight/run_left_4.png b/triple-dungeon/resources/images/characters/knight/run_left_4.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_left_4.png rename to triple-dungeon/resources/images/characters/knight/run_left_4.png diff --git a/triple-dungeon/resources/images/character/knight/run_left_5.png b/triple-dungeon/resources/images/characters/knight/run_left_5.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_left_5.png rename to triple-dungeon/resources/images/characters/knight/run_left_5.png diff --git a/triple-dungeon/resources/images/character/knight/run_right_0.png b/triple-dungeon/resources/images/characters/knight/run_right_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_right_0.png rename to triple-dungeon/resources/images/characters/knight/run_right_0.png diff --git a/triple-dungeon/resources/images/character/knight/run_right_1.png b/triple-dungeon/resources/images/characters/knight/run_right_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_right_1.png rename to triple-dungeon/resources/images/characters/knight/run_right_1.png diff --git a/triple-dungeon/resources/images/character/knight/run_right_2.png b/triple-dungeon/resources/images/characters/knight/run_right_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_right_2.png rename to triple-dungeon/resources/images/characters/knight/run_right_2.png diff --git a/triple-dungeon/resources/images/character/knight/run_right_3.png b/triple-dungeon/resources/images/characters/knight/run_right_3.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_right_3.png rename to triple-dungeon/resources/images/characters/knight/run_right_3.png diff --git a/triple-dungeon/resources/images/character/knight/run_right_4.png b/triple-dungeon/resources/images/characters/knight/run_right_4.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_right_4.png rename to triple-dungeon/resources/images/characters/knight/run_right_4.png diff --git a/triple-dungeon/resources/images/character/knight/run_right_5.png b/triple-dungeon/resources/images/characters/knight/run_right_5.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_right_5.png rename to triple-dungeon/resources/images/characters/knight/run_right_5.png diff --git a/triple-dungeon/resources/images/character/knight/run_up_0.png b/triple-dungeon/resources/images/characters/knight/run_up_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_up_0.png rename to triple-dungeon/resources/images/characters/knight/run_up_0.png diff --git a/triple-dungeon/resources/images/character/knight/run_up_1.png b/triple-dungeon/resources/images/characters/knight/run_up_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_up_1.png rename to triple-dungeon/resources/images/characters/knight/run_up_1.png diff --git a/triple-dungeon/resources/images/character/knight/run_up_2.png b/triple-dungeon/resources/images/characters/knight/run_up_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_up_2.png rename to triple-dungeon/resources/images/characters/knight/run_up_2.png diff --git a/triple-dungeon/resources/images/character/knight/run_up_3.png b/triple-dungeon/resources/images/characters/knight/run_up_3.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_up_3.png rename to triple-dungeon/resources/images/characters/knight/run_up_3.png diff --git a/triple-dungeon/resources/images/character/knight/run_up_4.png b/triple-dungeon/resources/images/characters/knight/run_up_4.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/run_up_4.png rename to triple-dungeon/resources/images/characters/knight/run_up_4.png diff --git a/triple-dungeon/resources/images/character/knight/slice_down_0.png b/triple-dungeon/resources/images/characters/knight/slice_down_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/slice_down_0.png rename to triple-dungeon/resources/images/characters/knight/slice_down_0.png diff --git a/triple-dungeon/resources/images/character/knight/slice_down_1.png b/triple-dungeon/resources/images/characters/knight/slice_down_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/slice_down_1.png rename to triple-dungeon/resources/images/characters/knight/slice_down_1.png diff --git a/triple-dungeon/resources/images/character/knight/slice_down_2.png b/triple-dungeon/resources/images/characters/knight/slice_down_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/slice_down_2.png rename to triple-dungeon/resources/images/characters/knight/slice_down_2.png diff --git a/triple-dungeon/resources/images/character/knight/slice_left_0.png b/triple-dungeon/resources/images/characters/knight/slice_left_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/slice_left_0.png rename to triple-dungeon/resources/images/characters/knight/slice_left_0.png diff --git a/triple-dungeon/resources/images/character/knight/slice_left_1.png b/triple-dungeon/resources/images/characters/knight/slice_left_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/slice_left_1.png rename to triple-dungeon/resources/images/characters/knight/slice_left_1.png diff --git a/triple-dungeon/resources/images/character/knight/slice_left_2.png b/triple-dungeon/resources/images/characters/knight/slice_left_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/slice_left_2.png rename to triple-dungeon/resources/images/characters/knight/slice_left_2.png diff --git a/triple-dungeon/resources/images/character/knight/slice_right_0.png b/triple-dungeon/resources/images/characters/knight/slice_right_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/slice_right_0.png rename to triple-dungeon/resources/images/characters/knight/slice_right_0.png diff --git a/triple-dungeon/resources/images/character/knight/slice_right_1.png b/triple-dungeon/resources/images/characters/knight/slice_right_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/slice_right_1.png rename to triple-dungeon/resources/images/characters/knight/slice_right_1.png diff --git a/triple-dungeon/resources/images/character/knight/slice_right_2.png b/triple-dungeon/resources/images/characters/knight/slice_right_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/slice_right_2.png rename to triple-dungeon/resources/images/characters/knight/slice_right_2.png diff --git a/triple-dungeon/resources/images/character/knight/slice_up_0.png b/triple-dungeon/resources/images/characters/knight/slice_up_0.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/slice_up_0.png rename to triple-dungeon/resources/images/characters/knight/slice_up_0.png diff --git a/triple-dungeon/resources/images/character/knight/slice_up_1.png b/triple-dungeon/resources/images/characters/knight/slice_up_1.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/slice_up_1.png rename to triple-dungeon/resources/images/characters/knight/slice_up_1.png diff --git a/triple-dungeon/resources/images/character/knight/slice_up_2.png b/triple-dungeon/resources/images/characters/knight/slice_up_2.png similarity index 100% rename from triple-dungeon/resources/images/character/knight/slice_up_2.png rename to triple-dungeon/resources/images/characters/knight/slice_up_2.png From 424892cc2516bfc8ed7e7e4cce194bd1dfd28769 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 03:03:38 -0500 Subject: [PATCH 13/22] use modulus for animations in mobs.py, beginning to implement PlayerAnimations class, add more docstrings to PlayerAnimations, remove cycle --- triple-dungeon/mobs.py | 27 ++++++++++----------------- triple-dungeon/sprites.py | 12 ++++++++---- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/triple-dungeon/mobs.py b/triple-dungeon/mobs.py index 741bffe..375cab3 100644 --- a/triple-dungeon/mobs.py +++ b/triple-dungeon/mobs.py @@ -5,7 +5,8 @@ Organizes all classes related to Mobs, Entities, Enemies, Players and Items. import arcade -from config import Config, Enums +from config import Config, Enums, SpritePaths +from sprites import PlayerAnimations class Mob(arcade.Sprite): @@ -41,12 +42,12 @@ class Player(Mob): def __init__(self, *args, **kwargs) -> None: super(Player, self).__init__(*args, **kwargs) - main_path = "resources/images/character/knight/" - - # Default to face-front - self.character_dir = Enums.FRONT_FACING + self.animations = PlayerAnimations(SpritePaths.KNIGHT) + self.character_dir = Enums.FRONT_FACING # Face front by default. # Load textures for idle standing + self.idle_textures = self.animations.idles + self.walking_textures = self.animations. for i in range(4): texture = arcade.load_texture(f"{main_path}knight iso char_idle_{i}.png") self.idle_textures.append(texture) @@ -81,32 +82,24 @@ class Player(Mob): # Idle Animation if self.change_x == 0 and self.change_y == 0: - self.cur_texture += 1 - if self.cur_texture > 3 * Config.IDLE_UPDATES_PER_FRAME: - self.cur_texture = 0 + self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 3) self.texture = self.idle_textures[self.cur_texture // Config.IDLE_UPDATES_PER_FRAME] return # walk up animation if self.change_y > 0: - self.cur_texture += 1 - if self.cur_texture > 4 * Config.RUN_UPDATES_PER_FRAME: - self.cur_texture = 0 + self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 4) self.texture = self.up_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME] return # walk down animation if self.change_y < 0: - self.cur_texture += 1 - if self.cur_texture > 4 * Config.RUN_UPDATES_PER_FRAME: - self.cur_texture = 0 + self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 4) self.texture = self.down_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME] return # Walking left/right animation - self.cur_texture += 1 - if self.cur_texture > 5 * Config.RUN_UPDATES_PER_FRAME: - self.cur_texture = 0 + self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 5) self.texture = self.walking_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME][self.character_dir.value] def tick(self): diff --git a/triple-dungeon/sprites.py b/triple-dungeon/sprites.py index ea3301d..b47fc39 100644 --- a/triple-dungeon/sprites.py +++ b/triple-dungeon/sprites.py @@ -7,7 +7,7 @@ import arcade import os import re -from itertools import cycle +from typing import Pattern class AnimationSet(object): @@ -25,7 +25,7 @@ class AnimationSet(object): self.directory = directory self.animations = os.listdir(directory) - def getAnimations(self, pattern: re.Pattern) -> iter: + def getAnimations(self, pattern: Pattern) -> iter: """ Loads all animations from the AnimationSet's directory that match the pattern. The pattern must have 1 group that specifies the animation's index. @@ -40,20 +40,24 @@ class AnimationSet(object): matches.sort(key=lambda match: int(match.group(1))) # Grab the filename and load it to the file directory matches = list(map(lambda match: arcade.load_texture(os.path.join(self.directory, match.group(0))), matches)) - return cycle(matches) + return matches class PlayerAnimations(AnimationSet): """ A class dedicated to serving player animations. Player animations must be served to the class in the correct format. + + The correct format is: {action}[_{direction}]_{index}.png + action: [idle, run, slice] - The action being taken. + direction: [down, right, left, up] - The direction of the action, if applicable. Omit the underscore if not. + index: [0,) - The index of the animation. Index should be enumerated in ascending order. """ def __init__(self, directory: str): """ Initializes the PlayerAnimations class. """ - super(PlayerAnimations, self).__init__(directory) # Grabs all animations needed. These are infinite iters, use next(iter) to grab the next animation. From a5fb8347ee825825750b40a02928cf949d6f6eb0 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 03:09:53 -0500 Subject: [PATCH 14/22] switch to AnimationSet implementation for texture loading --- triple-dungeon/mobs.py | 98 +++++++++++++++++++++------------------ triple-dungeon/sprites.py | 8 ++-- 2 files changed, 58 insertions(+), 48 deletions(-) diff --git a/triple-dungeon/mobs.py b/triple-dungeon/mobs.py index 375cab3..2b4838e 100644 --- a/triple-dungeon/mobs.py +++ b/triple-dungeon/mobs.py @@ -45,26 +45,23 @@ class Player(Mob): self.animations = PlayerAnimations(SpritePaths.KNIGHT) self.character_dir = Enums.FRONT_FACING # Face front by default. - # Load textures for idle standing - self.idle_textures = self.animations.idles - self.walking_textures = self.animations. - for i in range(4): - texture = arcade.load_texture(f"{main_path}knight iso char_idle_{i}.png") - self.idle_textures.append(texture) - - # Load textures for running horizontally - for i in range(6): - self.walking_textures.append([arcade.load_texture(f"{main_path}knight iso char_run left_{i}.png"), - arcade.load_texture(f"{main_path}knight iso char_run left_{i}.png", - mirrored=True)]) - - # Load textures for running down - for i in range(5): - self.down_textures.append(arcade.load_texture(f"{main_path}knight iso char_run down_{i}.png")) - - # Load textures for running up - for i in range(5): - self.up_textures.append(arcade.load_texture(f"{main_path}knight iso char_run up_{i}.png")) + # for i in range(4): + # texture = arcade.load_texture(f"{main_path}knight iso char_idle_{i}.png") + # self.idle_textures.append(texture) + # + # # Load textures for running horizontally + # for i in range(6): + # self.walking_textures.append([arcade.load_texture(f"{main_path}knight iso char_run left_{i}.png"), + # arcade.load_texture(f"{main_path}knight iso char_run left_{i}.png", + # mirrored=True)]) + # + # # Load textures for running down + # for i in range(5): + # self.down_textures.append(arcade.load_texture(f"{main_path}knight iso char_run down_{i}.png")) + # + # # Load textures for running up + # for i in range(5): + # self.up_textures.append(arcade.load_texture(f"{main_path}knight iso char_run up_{i}.png")) def update_animation(self, delta_time: float = 1 / 60) -> None: """ @@ -73,34 +70,45 @@ class Player(Mob): """ # Figure out if we need to flip face left, right, up, or down - if self.change_x > 0: - self.character_dir = Enums.LEFT_FACING - elif self.change_x < 0: - self.character_dir = Enums.RIGHT_FACING - elif self.change_x == 0 and self.change_y == 0: - self.character_dir = Enums.FRONT_FACING + # if self.change_x > 0: + # self.character_dir = Enums.LEFT_FACING + # elif self.change_x < 0: + # self.character_dir = Enums.RIGHT_FACING + # elif self.change_x == 0 and self.change_y == 0: + # self.character_dir = Enums.FRONT_FACING + + if self.change_x == 0 and self.change_y == 0: + self.texture = next(self.animations.idles) + elif self.change_y > 0: # Up + self.texture = next(self.animations.up) + elif self.change_y < 0: # Down + self.texture = next(self.animations.down) + elif self.change_x > 0: # Left + self.texture = next(self.animations.right) + elif self.change_x < 0: # Right + self.texture = next(self.animations.left) # Idle Animation - if self.change_x == 0 and self.change_y == 0: - self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 3) - self.texture = self.idle_textures[self.cur_texture // Config.IDLE_UPDATES_PER_FRAME] - return + # if self.change_x == 0 and self.change_y == 0: + # self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 3) + # self.texture = self.idle_textures[self.cur_texture // Config.IDLE_UPDATES_PER_FRAME] + # return + # + # # walk up animation + # if self.change_y > 0: + # self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 4) + # self.texture = self.up_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME] + # return + # + # # walk down animation + # if self.change_y < 0: + # self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 4) + # self.texture = self.down_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME] + # return - # walk up animation - if self.change_y > 0: - self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 4) - self.texture = self.up_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME] - return - - # walk down animation - if self.change_y < 0: - self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 4) - self.texture = self.down_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME] - return - - # Walking left/right animation - self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 5) - self.texture = self.walking_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME][self.character_dir.value] + # # Walking left/right animation + # self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 5) + # self.texture = self.walking_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME][self.character_dir.value] def tick(self): """ diff --git a/triple-dungeon/sprites.py b/triple-dungeon/sprites.py index b47fc39..2b5f5f9 100644 --- a/triple-dungeon/sprites.py +++ b/triple-dungeon/sprites.py @@ -2,6 +2,7 @@ sprites.py A file dedicated to managing sprites and animations for characters. """ +from itertools import cycle import arcade import os @@ -35,12 +36,13 @@ class AnimationSet(object): """ # Finds all matching files - matches = [file for file in self.animations if re.match(pattern, file)] + matches = map(lambda file: re.match(pattern, file), self.animations) + matches = list(filter(lambda match: match is not None, matches)) # Sort in ascending order based on the connected animation index. Zero-indexing or not does not affect order. matches.sort(key=lambda match: int(match.group(1))) # Grab the filename and load it to the file directory matches = list(map(lambda match: arcade.load_texture(os.path.join(self.directory, match.group(0))), matches)) - return matches + return cycle(matches) class PlayerAnimations(AnimationSet): @@ -65,4 +67,4 @@ class PlayerAnimations(AnimationSet): self.down = self.getAnimations(re.compile(r'run_down_(\d+).png')) self.right = self.getAnimations(re.compile(r'run_right_(\d+).png')) self.up = self.getAnimations(re.compile(r'run_up_(\d+).png')) - self.down = self.getAnimations(re.compile(r'run_left_(\d+).png')) + self.left = self.getAnimations(re.compile(r'run_left_(\d+).png')) From fd64fc83c26257b276c61859d8deb61a2a17a184 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 03:10:19 -0500 Subject: [PATCH 15/22] remove all commented out code --- triple-dungeon/mobs.py | 49 ------------------------------------------ 1 file changed, 49 deletions(-) diff --git a/triple-dungeon/mobs.py b/triple-dungeon/mobs.py index 2b4838e..22f969c 100644 --- a/triple-dungeon/mobs.py +++ b/triple-dungeon/mobs.py @@ -43,25 +43,6 @@ class Player(Mob): super(Player, self).__init__(*args, **kwargs) self.animations = PlayerAnimations(SpritePaths.KNIGHT) - self.character_dir = Enums.FRONT_FACING # Face front by default. - - # for i in range(4): - # texture = arcade.load_texture(f"{main_path}knight iso char_idle_{i}.png") - # self.idle_textures.append(texture) - # - # # Load textures for running horizontally - # for i in range(6): - # self.walking_textures.append([arcade.load_texture(f"{main_path}knight iso char_run left_{i}.png"), - # arcade.load_texture(f"{main_path}knight iso char_run left_{i}.png", - # mirrored=True)]) - # - # # Load textures for running down - # for i in range(5): - # self.down_textures.append(arcade.load_texture(f"{main_path}knight iso char_run down_{i}.png")) - # - # # Load textures for running up - # for i in range(5): - # self.up_textures.append(arcade.load_texture(f"{main_path}knight iso char_run up_{i}.png")) def update_animation(self, delta_time: float = 1 / 60) -> None: """ @@ -69,14 +50,6 @@ class Player(Mob): :param delta_time: No idea. """ - # Figure out if we need to flip face left, right, up, or down - # if self.change_x > 0: - # self.character_dir = Enums.LEFT_FACING - # elif self.change_x < 0: - # self.character_dir = Enums.RIGHT_FACING - # elif self.change_x == 0 and self.change_y == 0: - # self.character_dir = Enums.FRONT_FACING - if self.change_x == 0 and self.change_y == 0: self.texture = next(self.animations.idles) elif self.change_y > 0: # Up @@ -88,28 +61,6 @@ class Player(Mob): elif self.change_x < 0: # Right self.texture = next(self.animations.left) - # Idle Animation - # if self.change_x == 0 and self.change_y == 0: - # self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 3) - # self.texture = self.idle_textures[self.cur_texture // Config.IDLE_UPDATES_PER_FRAME] - # return - # - # # walk up animation - # if self.change_y > 0: - # self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 4) - # self.texture = self.up_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME] - # return - # - # # walk down animation - # if self.change_y < 0: - # self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 4) - # self.texture = self.down_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME] - # return - - # # Walking left/right animation - # self.cur_texture = (self.cur_texture + 1) % (Config.RUN_UPDATES_PER_FRAME * 5) - # self.texture = self.walking_textures[self.cur_texture // Config.RUN_UPDATES_PER_FRAME][self.character_dir.value] - def tick(self): """ While Player objects do not have any AI (they are controlled by the user), From 8145c62090a98ef123686f04363441323cb9513f Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 03:19:16 -0500 Subject: [PATCH 16/22] change statements to use Enums with dictionary mapping --- triple-dungeon/mobs.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/triple-dungeon/mobs.py b/triple-dungeon/mobs.py index 22f969c..5d72d86 100644 --- a/triple-dungeon/mobs.py +++ b/triple-dungeon/mobs.py @@ -26,6 +26,16 @@ class Mob(arcade.Sprite): self.down_textures = [] self.cur_texture = 0 + + # Used for mapping directions to animations + self.map = { + Enums.IDLE : self.animations.idles, + Enums.UP : self.animations.up, + Enums.DOWN : self.animations.down, + Enums.RIGHT : self.animations.right, + Enums.LEFT : self.animations.left + } + def tick(self) -> None: """ A on_update function, the Mob should decide it's next actions here. @@ -43,6 +53,7 @@ class Player(Mob): super(Player, self).__init__(*args, **kwargs) self.animations = PlayerAnimations(SpritePaths.KNIGHT) + self.refreshIndex = 0 def update_animation(self, delta_time: float = 1 / 60) -> None: """ @@ -50,16 +61,21 @@ class Player(Mob): :param delta_time: No idea. """ - if self.change_x == 0 and self.change_y == 0: - self.texture = next(self.animations.idles) + self.refreshIndex = (self.refreshIndex + 1) % Config.RUN_UPDATES_PER_FRAME + + if self.change_x == 0 and self.change_y == 0: # Idle + dir = Enums.IDLE elif self.change_y > 0: # Up - self.texture = next(self.animations.up) + dir = Enums.UP elif self.change_y < 0: # Down - self.texture = next(self.animations.down) + dir = Enums.DOWN elif self.change_x > 0: # Left - self.texture = next(self.animations.right) + dir = Enums.LEFT elif self.change_x < 0: # Right - self.texture = next(self.animations.left) + dir = Enums.RIGHT + + if self.prev != dir or not wait: + self.texture = next(self.map[dir]) def tick(self): """ From e5c53fa7a04e6dbcfdd637e35b36edf82b1f4ec2 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 03:35:51 -0500 Subject: [PATCH 17/22] fix the enum mapping, fully implement the animations mapping, lower run updates per frame, fix hyperspeed animations --- triple-dungeon/config.py | 12 +++++------ triple-dungeon/mobs.py | 44 ++++++++++++++++++++++++---------------- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/triple-dungeon/config.py b/triple-dungeon/config.py index 0cab7fd..2b13ade 100644 --- a/triple-dungeon/config.py +++ b/triple-dungeon/config.py @@ -24,7 +24,7 @@ class Config(object): SCREEN_TITLE = "Triple Dungeon" TILE_WIDTH = 63 IDLE_UPDATES_PER_FRAME = 20 - RUN_UPDATES_PER_FRAME = 10 + RUN_UPDATES_PER_FRAME = 8 # Constants used to scale our sprites from their original size CHARACTER_SCALING = 1 @@ -46,11 +46,11 @@ class Enums(Enum): """ # Play Direction Enums - RIGHT_FACING = 0 - LEFT_FACING = 1 - FRONT_FACING = 2 - UP_FACING = 3 - DOWN_FACING = 4 + RIGHT = 0 + LEFT = 1 + UP = 2 + DOWN = 3 + IDLE = 4 class SpritePaths(object): diff --git a/triple-dungeon/mobs.py b/triple-dungeon/mobs.py index 5d72d86..9b9906b 100644 --- a/triple-dungeon/mobs.py +++ b/triple-dungeon/mobs.py @@ -26,16 +26,6 @@ class Mob(arcade.Sprite): self.down_textures = [] self.cur_texture = 0 - - # Used for mapping directions to animations - self.map = { - Enums.IDLE : self.animations.idles, - Enums.UP : self.animations.up, - Enums.DOWN : self.animations.down, - Enums.RIGHT : self.animations.right, - Enums.LEFT : self.animations.left - } - def tick(self) -> None: """ A on_update function, the Mob should decide it's next actions here. @@ -53,7 +43,18 @@ class Player(Mob): super(Player, self).__init__(*args, **kwargs) self.animations = PlayerAnimations(SpritePaths.KNIGHT) + # Used for mapping directions to animations + self.map = { + Enums.IDLE: self.animations.idles, + Enums.UP: self.animations.up, + Enums.DOWN: self.animations.down, + Enums.RIGHT: self.animations.right, + Enums.LEFT: self.animations.left + } + self.refreshIndex = 0 + self.prev = Enums.IDLE + self.texture = next(self.map[self.prev]) def update_animation(self, delta_time: float = 1 / 60) -> None: """ @@ -61,21 +62,28 @@ class Player(Mob): :param delta_time: No idea. """ + # Increase the refresh index according self.refreshIndex = (self.refreshIndex + 1) % Config.RUN_UPDATES_PER_FRAME - if self.change_x == 0 and self.change_y == 0: # Idle - dir = Enums.IDLE + # Logic to determine what direction we're in. + if self.change_x == 0 and self.change_y == 0: + cur = Enums.IDLE elif self.change_y > 0: # Up - dir = Enums.UP + cur = Enums.UP elif self.change_y < 0: # Down - dir = Enums.DOWN + cur = Enums.DOWN elif self.change_x > 0: # Left - dir = Enums.LEFT + cur = Enums.RIGHT elif self.change_x < 0: # Right - dir = Enums.RIGHT + cur = Enums.LEFT + else: # Idle + cur = Enums.IDLE - if self.prev != dir or not wait: - self.texture = next(self.map[dir]) + # If we're in a new direction or the refresh index has reset + if self.prev is not cur or self.refreshIndex == 0: + self.texture = next(self.map[cur]) + + self.prev = cur def tick(self): """ From 02812902ae9924fe1f8b05774ac48b6d423d8ff6 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 04:05:23 -0500 Subject: [PATCH 18/22] refactor load_file to load and create sprites copying render method --- triple-dungeon/map.py | 57 ++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/triple-dungeon/map.py b/triple-dungeon/map.py index b9471c9..71dba18 100644 --- a/triple-dungeon/map.py +++ b/triple-dungeon/map.py @@ -35,13 +35,17 @@ class Dungeon(object): level_size = 10 * Config.TILE_SCALING * Config.TILE_WIDTH # get center level + center = Level.load_file(1, 1, 'resources/levels/map1/center.json') + room_floor.move(level_size, 0) + room_wall.move(level_size, 0) + side = Level.load_file(2, 1, 'resources/levels/map1/room.json') + room_floor.move(level_size, 0) + room_wall.move(level_size, 0) center = Level() - center.load_file('resources/levels/map1/center.json') - center.render() - center_floor, center_wall = center.floor_list, center.wall_list - self.floor_list.extend(center_floor) - self.wall_list.extend(center_wall) + center.load_file('resources/levels/map1.center.json') + self.floor_list.extend(center.floor_list) + self.wall_list.extend(center.wall_list) # get a side room room = Level() @@ -49,8 +53,7 @@ class Dungeon(object): room.rotate_level(2) room.render() room_floor, room_wall = room.floor_list, room.wall_list - room_floor.move(level_size, 0) - room_wall.move(level_size, 0) + self.floor_list.extend(room_floor) self.wall_list.extend(room_wall) @@ -92,7 +95,10 @@ class Level: self.x, self.y = level_x, level_y self.sprites = [] - self.level = [] + self.structure = [] + + self.floorSprites = arcade.SpriteList() + self.wallSprites = arcade.SpriteList() # Tuples containing the Node positions of where walls, floor and entrances are. # All positions are generated based on the level's X and Y position, so that all points within @@ -101,20 +107,43 @@ class Level: self.wall_list = [] # self.entrances = [] - def load_file(self, path: str): + @staticmethod + def load_file(level_x: int, level_y:int, path: str) -> Level: """ Builds a Level from a given file path. :param path: Path to the Level file. :return: The new generated Level file. """ - self.floor_list = arcade.SpriteList() - self.wall_list = arcade.SpriteList() + level = Level(level_x, level_y) with open(path) as file: - level = json.load(file) - self.sprites = level['elements'] - self.level = level['structure'] + data = json.load(file) + # Loads elements and structure data from level file + level.sprites = data['elements'] + level.structure = data['structure'] + + level_scale = 10 * Config.TILE_SCALING * Config.TILE_WIDTH + tile_scale = Config.TILE_WIDTH * Config.TILE_SCALING + + # Places all of the tiles & sprites + for x in range(0, 10): + for y in range(0, 11): + tilePath = level.sprites[level.structure[x][y]] + sprite = arcade.Sprite(tilePath, Config.TILE_SCALING) + sprite.center_x, sprite.center_y = x * tile_scale, y * tile_scale + + if 'floor' in tilePath: + level.floorSprites.append(sprite) + elif 'wall' in tilePath: + level.wallSprites.append(sprite) + else: + print(f'Could not handle Tile: {tilePath}') + # Move everything into correct positions + level.floorSprites.move(level_scale * level_x, level_scale * level_y) + level.wallSprites.move(level_scale * level_x, level_scale * level_y) + + return level def render(self) -> None: """ From 9dcf6cf054e1c3adebc87314bbc09e7b6025ae64 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 04:14:53 -0500 Subject: [PATCH 19/22] make level rendering and dungeon rendering independent, remove complicated spritelist passing --- triple-dungeon/main.py | 14 +++++--------- triple-dungeon/map.py | 44 +++++++++++------------------------------- 2 files changed, 16 insertions(+), 42 deletions(-) diff --git a/triple-dungeon/main.py b/triple-dungeon/main.py index 193a117..6e90698 100644 --- a/triple-dungeon/main.py +++ b/triple-dungeon/main.py @@ -30,6 +30,8 @@ class Game(arcade.Window): # Separate variable that holds the player sprite self.player = None + self.dungeon = None + # list to keep track of keypresses self.prev_keypress = [] @@ -47,8 +49,6 @@ class Game(arcade.Window): # Create the Sprite lists self.player_list = arcade.SpriteList() - self.wall_list = arcade.SpriteList() - self.floor_list = arcade.SpriteList() self.enemy_list = arcade.SpriteList() # Set up the player, specifically placing it at these coordinates. @@ -59,10 +59,7 @@ class Game(arcade.Window): self.player_list = self.player # Create the dungeon - dungeon = Dungeon() - - self.floor_list = dungeon.floor_list - self.wall_list = dungeon.wall_list + self.dungeon = Dungeon() # Create monsters # This needs to be updated to comply with the new mobs.py code @@ -70,7 +67,7 @@ class Game(arcade.Window): # self.enemy_list.append(Enemy("resources/images/monsters/frog/frog1.png", 200, 1000, 4).get_enemy()) # Create the 'physics engine' - self.physics_engine = arcade.PhysicsEngineSimple(self.player, self.wall_list) + self.physics_engine = arcade.PhysicsEngineSimple(self.player, *(dungeon.getWalls())) def on_draw(self): """ Render the screen. """ @@ -79,8 +76,7 @@ class Game(arcade.Window): arcade.start_render() # Draw our sprites - self.floor_list.draw() - self.player_list.draw() + self.dungeon.render() self.enemy_list.draw() self.wall_list.draw() diff --git a/triple-dungeon/map.py b/triple-dungeon/map.py index 71dba18..9c36f08 100644 --- a/triple-dungeon/map.py +++ b/triple-dungeon/map.py @@ -32,40 +32,15 @@ class Dungeon(object): self.floor_list = arcade.SpriteList() self.wall_list = arcade.SpriteList() - level_size = 10 * Config.TILE_SCALING * Config.TILE_WIDTH - # get center level center = Level.load_file(1, 1, 'resources/levels/map1/center.json') - room_floor.move(level_size, 0) - room_wall.move(level_size, 0) side = Level.load_file(2, 1, 'resources/levels/map1/room.json') - room_floor.move(level_size, 0) - room_wall.move(level_size, 0) - center = Level() - center.load_file('resources/levels/map1.center.json') - self.floor_list.extend(center.floor_list) - self.wall_list.extend(center.wall_list) - - # get a side room - room = Level() - room.load_file('resources/levels/map1/room.json') - room.rotate_level(2) - room.render() - room_floor, room_wall = room.floor_list, room.wall_list - - self.floor_list.extend(room_floor) - self.wall_list.extend(room_wall) - - # get a side room - room = Level() - room.load_file('resources/levels/map1/room.json') - room.render() - room_floor, room_wall = room.floor_list, room.wall_list - room_floor.move(-level_size, 0) - room_wall.move(-level_size, 0) - self.floor_list.extend(room_floor) - self.wall_list.extend(room_wall) + self.levels = [ + [None, None, None], + [center, side, None], + [None, None, None] + ] def render(self) -> None: """ @@ -75,7 +50,9 @@ class Dungeon(object): for column in self.levels: for level in column: if level is not None: - level.render() + print('Rendering Level') + level.floorSprites.draw() + level.wallSprites.draw() class Level: @@ -108,7 +85,7 @@ class Level: # self.entrances = [] @staticmethod - def load_file(level_x: int, level_y:int, path: str) -> Level: + def load_file(level_x: int, level_y: int, path: str) -> Level: """ Builds a Level from a given file path. @@ -128,7 +105,7 @@ class Level: # Places all of the tiles & sprites for x in range(0, 10): - for y in range(0, 11): + for y in range(0, 10): tilePath = level.sprites[level.structure[x][y]] sprite = arcade.Sprite(tilePath, Config.TILE_SCALING) sprite.center_x, sprite.center_y = x * tile_scale, y * tile_scale @@ -139,6 +116,7 @@ class Level: level.wallSprites.append(sprite) else: print(f'Could not handle Tile: {tilePath}') + # Move everything into correct positions level.floorSprites.move(level_scale * level_x, level_scale * level_y) level.wallSprites.move(level_scale * level_x, level_scale * level_y) From 5cf2f5e7f7b261c4e0df33efb64b889bc3a4d9b5 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 04:36:32 -0500 Subject: [PATCH 20/22] add LEVEL_SIZE enum to Config, add position debug, add wall sprite list gatherer, add and use level position method, --- triple-dungeon/config.py | 3 +++ triple-dungeon/main.py | 6 ++++- triple-dungeon/map.py | 47 +++++++++++++++++----------------------- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/triple-dungeon/config.py b/triple-dungeon/config.py index 2b13ade..a291911 100644 --- a/triple-dungeon/config.py +++ b/triple-dungeon/config.py @@ -30,6 +30,9 @@ class Config(object): CHARACTER_SCALING = 1 TILE_SCALING = 2 + # The number of pixels across the level + LEVEL_SIZE = 10 * TILE_SCALING * TILE_WIDTH + # Movement speed of player, in pixels per frame PLAYER_MOVEMENT_SPEED = 7 diff --git a/triple-dungeon/main.py b/triple-dungeon/main.py index 6e90698..64c8e95 100644 --- a/triple-dungeon/main.py +++ b/triple-dungeon/main.py @@ -67,7 +67,7 @@ class Game(arcade.Window): # self.enemy_list.append(Enemy("resources/images/monsters/frog/frog1.png", 200, 1000, 4).get_enemy()) # Create the 'physics engine' - self.physics_engine = arcade.PhysicsEngineSimple(self.player, *(dungeon.getWalls())) + self.physics_engine = arcade.PhysicsEngineSimple(self.player, self.dungeon.getWalls()) def on_draw(self): """ Render the screen. """ @@ -75,8 +75,12 @@ class Game(arcade.Window): # Clear the screen to the background color arcade.start_render() + x, y = self.player.center_x, self.player.center_y + 100 + arcade.draw_text(f"({x}, {y})", x, y, arcade.color.WHITE, 15) + # Draw our sprites self.dungeon.render() + self.player_list.draw() self.enemy_list.draw() self.wall_list.draw() diff --git a/triple-dungeon/map.py b/triple-dungeon/map.py index 9c36f08..fe7c1a8 100644 --- a/triple-dungeon/map.py +++ b/triple-dungeon/map.py @@ -11,6 +11,8 @@ import json import arcade import numpy as np +from itertools import chain + from config import Config @@ -42,6 +44,16 @@ class Dungeon(object): [None, None, None] ] + def getWalls(self): + walls = arcade.SpriteList() + walls.extend( + list(chain.from_iterable( + chain.from_iterable([level.wallSprites for level in column if level is not None]) for column in + self.levels + )) + ) + return walls + def render(self) -> None: """ Calls render on all level @@ -50,7 +62,6 @@ class Dungeon(object): for column in self.levels: for level in column: if level is not None: - print('Rendering Level') level.floorSprites.draw() level.wallSprites.draw() @@ -82,13 +93,14 @@ class Level: # the dungeon can be mapped by a proper pathfinding system. self.floor_list = [] self.wall_list = [] - # self.entrances = [] @staticmethod def load_file(level_x: int, level_y: int, path: str) -> Level: """ Builds a Level from a given file path. + :param level_x: The level's X position within the Dungeon level matrix. + :param level_y: The level's Y position within the Dungeon level matrix. :param path: Path to the Level file. :return: The new generated Level file. """ @@ -100,7 +112,6 @@ class Level: level.sprites = data['elements'] level.structure = data['structure'] - level_scale = 10 * Config.TILE_SCALING * Config.TILE_WIDTH tile_scale = Config.TILE_WIDTH * Config.TILE_SCALING # Places all of the tiles & sprites @@ -118,35 +129,17 @@ class Level: print(f'Could not handle Tile: {tilePath}') # Move everything into correct positions - level.floorSprites.move(level_scale * level_x, level_scale * level_y) - level.wallSprites.move(level_scale * level_x, level_scale * level_y) + level.floorSprites.move(*level.center()) + level.wallSprites.move(*level.center()) return level - def render(self) -> None: + def center(self) -> tuple: """ - Calls render on all sprites. + Returns the pixel center of the level. + :return: A tuple containing the X and Y coordinates of the level's center """ - x = 0 - y = 0 - 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 = self.level[y][x] - sprite = self.sprites[cur_tile] - floor = arcade.Sprite(sprite, Config.TILE_SCALING) - floor.center_x = x_pos - floor.center_y = y_pos - if cur_tile == ' ': - self.floor_list.append(floor) - elif cur_tile == 'w': - self.wall_list.append(floor) - x += 1 - x = 0 - y += 1 + return self.x * Config.LEVEL_SIZE, self.y * Config.LEVEL_SIZE def rotate_level(self, times_rotated): """ From 6f1161ab752e597e45f2b41f503610c92f242c49 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 04:46:50 -0500 Subject: [PATCH 21/22] place character randomly in one of the Dungeon levels, remove player_list and just use player, add Dungeon.levelList property function, add in generated level loading --- triple-dungeon/main.py | 50 +++++++++++++++++++----------------------- triple-dungeon/map.py | 20 ++++++++++++----- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/triple-dungeon/main.py b/triple-dungeon/main.py index 64c8e95..dda90c7 100644 --- a/triple-dungeon/main.py +++ b/triple-dungeon/main.py @@ -3,6 +3,7 @@ main.py The main class used to load the game. Holds the main game window, as well as manages basic functions for organizing the game. """ +import random import arcade @@ -25,9 +26,6 @@ class Game(arcade.Window): self.wall_list = None self.floor_list = None self.enemy_list = None - self.player_list = None - - # Separate variable that holds the player sprite self.player = None self.dungeon = None @@ -48,23 +46,21 @@ class Game(arcade.Window): """ Set up the game here. Call this function to restart the game. """ # Create the Sprite lists - self.player_list = arcade.SpriteList() self.enemy_list = arcade.SpriteList() # Set up the player, specifically placing it at these coordinates. self.player = Player() self.player.scale = 1 - self.player.center_x = Config.SCREEN_WIDTH / 2 - self.player.center_y = Config.SCREEN_HEIGHT / 2 - self.player_list = self.player # Create the dungeon self.dungeon = Dungeon() + self.player.center_x, self.player.center_y = random.choice(self.dungeon.levelList).center() + # Create monsters # This needs to be updated to comply with the new mobs.py code - # self.enemy_list.append(Enemy("resources/images/monsters/ghost/ghost1.png", 200, 200, 4).get_enemy()) - # self.enemy_list.append(Enemy("resources/images/monsters/frog/frog1.png", 200, 1000, 4).get_enemy()) + # self.enemy_list.append(Enemy("resources/images/monsters/ghost/ghost1.png", 200, 200, 4)) + # self.enemy_list.append(Enemy("resources/images/monsters/frog/frog1.png", 200, 1000, 4)) # Create the 'physics engine' self.physics_engine = arcade.PhysicsEngineSimple(self.player, self.dungeon.getWalls()) @@ -80,7 +76,7 @@ class Game(arcade.Window): # Draw our sprites self.dungeon.render() - self.player_list.draw() + self.player.draw() self.enemy_list.draw() self.wall_list.draw() @@ -88,16 +84,16 @@ class Game(arcade.Window): """Called whenever a key is pressed. """ if key == arcade.key.UP or key == arcade.key.W: - self.player_list.change_y = Config.PLAYER_MOVEMENT_SPEED + self.player.change_y = Config.PLAYER_MOVEMENT_SPEED self.prev_keypress.append(key) elif key == arcade.key.DOWN or key == arcade.key.S: - self.player_list.change_y = -Config.PLAYER_MOVEMENT_SPEED + self.player.change_y = -Config.PLAYER_MOVEMENT_SPEED self.prev_keypress.append(key) elif key == arcade.key.LEFT or key == arcade.key.A: - self.player_list.change_x = -Config.PLAYER_MOVEMENT_SPEED + self.player.change_x = -Config.PLAYER_MOVEMENT_SPEED self.prev_keypress.append(key) elif key == arcade.key.RIGHT or key == arcade.key.D: - self.player_list.change_x = Config.PLAYER_MOVEMENT_SPEED + self.player.change_x = Config.PLAYER_MOVEMENT_SPEED self.prev_keypress.append(key) elif key == 65307: self.close() @@ -106,16 +102,16 @@ class Game(arcade.Window): """Called when the user releases a key. """ if key == arcade.key.UP or key == arcade.key.W: - self.player_list.change_y = 0 + self.player.change_y = 0 self.prev_keypress.remove(key) elif key == arcade.key.DOWN or key == arcade.key.S: - self.player_list.change_y = 0 + self.player.change_y = 0 self.prev_keypress.remove(key) elif key == arcade.key.LEFT or key == arcade.key.A: - self.player_list.change_x = 0 + self.player.change_x = 0 self.prev_keypress.remove(key) elif key == arcade.key.RIGHT or key == arcade.key.D: - self.player_list.change_x = 0 + self.player.change_x = 0 self.prev_keypress.remove(key) if self.prev_keypress: self.on_key_press(self.prev_keypress.pop(0), 0) @@ -126,29 +122,29 @@ class Game(arcade.Window): # Move the player with the physics engine self.physics_engine.update() - self.player_list.update_animation() + self.player.update_animation() changed = False # Track if we need to change the viewport # Below manages all scrolling mechanics # Scroll left left_boundary = self.view_left + Config.LEFT_VIEWPORT_MARGIN - if self.player_list.left < left_boundary: - self.view_left -= left_boundary - self.player_list.left + if self.player.left < left_boundary: + self.view_left -= left_boundary - self.player.left changed = True # Scroll right right_boundary = self.view_left + Config.SCREEN_WIDTH - Config.RIGHT_VIEWPORT_MARGIN - if self.player_list.right > right_boundary: - self.view_left += self.player_list.right - right_boundary + if self.player.right > right_boundary: + self.view_left += self.player.right - right_boundary changed = True # Scroll up top_boundary = self.view_bottom + Config.SCREEN_HEIGHT - Config.TOP_VIEWPORT_MARGIN - if self.player_list.top > top_boundary: - self.view_bottom += self.player_list.top - top_boundary + if self.player.top > top_boundary: + self.view_bottom += self.player.top - top_boundary changed = True # Scroll down bottom_boundary = self.view_bottom + Config.BOTTOM_VIEWPORT_MARGIN - if self.player_list.bottom < bottom_boundary: - self.view_bottom -= bottom_boundary - self.player_list.bottom + if self.player.bottom < bottom_boundary: + self.view_bottom -= bottom_boundary - self.player.bottom changed = True if changed: diff --git a/triple-dungeon/map.py b/triple-dungeon/map.py index fe7c1a8..7adea54 100644 --- a/triple-dungeon/map.py +++ b/triple-dungeon/map.py @@ -35,13 +35,12 @@ class Dungeon(object): self.floor_list = arcade.SpriteList() self.wall_list = arcade.SpriteList() - center = Level.load_file(1, 1, 'resources/levels/map1/center.json') - side = Level.load_file(2, 1, 'resources/levels/map1/room.json') + # center = Level.load_file(1, 1, 'resources/levels/map1/center.json') + # side = Level.load_file(2, 1, 'resources/levels/map1/room.json') + center = "resources/levels/map1/center.json" self.levels = [ - [None, None, None], - [center, side, None], - [None, None, None] + [Level.load_file(x, y, center) for y in range(size)] for x in range(size) ] def getWalls(self): @@ -65,6 +64,17 @@ class Dungeon(object): level.floorSprites.draw() level.wallSprites.draw() + @property + def levelList(self) -> list: + """ + Retrieves all Level objects from Dungeon intance. + :return: A list containing all Level objects. + """ + + return list(filter( + lambda level: level is not None, chain.from_iterable(self.levels) + )) + class Level: """ From a5fee04bdca0fd42d84e509179e3edca185db977 Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Apr 2020 04:52:07 -0500 Subject: [PATCH 22/22] attempt to fix position text, minor docstring corrections --- triple-dungeon/main.py | 10 +++++----- triple-dungeon/map.py | 11 +++++++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/triple-dungeon/main.py b/triple-dungeon/main.py index dda90c7..48bc7af 100644 --- a/triple-dungeon/main.py +++ b/triple-dungeon/main.py @@ -40,7 +40,7 @@ class Game(arcade.Window): self.view_bottom = 0 self.view_left = 0 - arcade.set_background_color(arcade.csscolor.BLACK) + arcade.set_background_color(arcade.color.BLACK) def setup(self): """ Set up the game here. Call this function to restart the game. """ @@ -53,7 +53,7 @@ class Game(arcade.Window): self.player.scale = 1 # Create the dungeon - self.dungeon = Dungeon() + self.dungeon = Dungeon(0, 3) self.player.center_x, self.player.center_y = random.choice(self.dungeon.levelList).center() @@ -71,15 +71,15 @@ class Game(arcade.Window): # Clear the screen to the background color arcade.start_render() - x, y = self.player.center_x, self.player.center_y + 100 - arcade.draw_text(f"({x}, {y})", x, y, arcade.color.WHITE, 15) - # Draw our sprites self.dungeon.render() self.player.draw() self.enemy_list.draw() self.wall_list.draw() + x, y = self.player.center_x, self.player.center_y + 100 + arcade.draw_text(f"({x}, {y})", x, y, arcade.color.WHITE, 15) + 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 7adea54..105d0e8 100644 --- a/triple-dungeon/map.py +++ b/triple-dungeon/map.py @@ -43,7 +43,14 @@ class Dungeon(object): [Level.load_file(x, y, center) for y in range(size)] for x in range(size) ] - def getWalls(self): + def getWalls(self) -> arcade.SpriteList: + """ + Simple one time function for getting all Wall sprites from all Levels. + Used by the physics engine during game startup. + + :return: A SpriteList containing all Sprites. + """ + walls = arcade.SpriteList() walls.extend( list(chain.from_iterable( @@ -67,7 +74,7 @@ class Dungeon(object): @property def levelList(self) -> list: """ - Retrieves all Level objects from Dungeon intance. + Retrieves all Level objects from Dungeon instance. :return: A list containing all Level objects. """