From 90ca0d218e620953c37a2bc1e51390724ca36f96 Mon Sep 17 00:00:00 2001 From: "Jean A. Eckelberg" Date: Sat, 27 May 2023 01:14:39 -0500 Subject: [PATCH 1/5] Fix merge (#46) * Fix Merge --- game/controllers/master_controller.py | 5 +---- requirements.txt | 4 ++++ 2 files changed, 5 insertions(+), 4 deletions(-) create mode 100644 requirements.txt diff --git a/game/controllers/master_controller.py b/game/controllers/master_controller.py index 4bfb6da..154b6b9 100644 --- a/game/controllers/master_controller.py +++ b/game/controllers/master_controller.py @@ -118,10 +118,7 @@ def turn_logic(self, clients: list[Player], turn): self.movement_controller.handle_actions(client.actions[i], client, self.current_world_data["game_board"]) except IndexError: pass - try: - self.interact_controller.handle_actions(client.actions[i], client, self.current_world_data["game_board"]) - except IndexError: - pass + # checks event logic at the end of round # self.handle_events(clients) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7440582 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +tqdm~=4.65.0 +pygame~=2.3.0 +numpy~=1.24.2 +Pillow~=9.5.0 \ No newline at end of file From b34bc19c7216b99ed890499e25c4ea3fa2ff948a Mon Sep 17 00:00:00 2001 From: JeanAEckelberg Date: Sun, 25 Jun 2023 22:43:47 -0500 Subject: [PATCH 2/5] Added functions to calculate logic and a data structure to store it all --- Visualiser2/bytesprites/bytesprite.py | 1 + Visualiser2/main.py | 45 +++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/Visualiser2/bytesprites/bytesprite.py b/Visualiser2/bytesprites/bytesprite.py index 5a06095..daed57c 100644 --- a/Visualiser2/bytesprites/bytesprite.py +++ b/Visualiser2/bytesprites/bytesprite.py @@ -17,6 +17,7 @@ class ByteSprite(pyg.sprite.Sprite): __frame_index: int # Selects the sprite from the spritesheet to be used. Used for animation __config: Config = Config() + # make sure that all inherited classes constructors only take screen as an parameter def __init__(self, screen: pyg.Surface, filename: str, num_of_states: int, colorkey: pyg.Color | None = None, object_type: int = 0, layer: int = 0, top_left: Vector = Vector(0, 0)): # Add implementation here for selecting the sprite sheet to use diff --git a/Visualiser2/main.py b/Visualiser2/main.py index cc3d2de..2e90500 100644 --- a/Visualiser2/main.py +++ b/Visualiser2/main.py @@ -3,6 +3,7 @@ import pygame from Visualiser2.adapter import Adapter +from Visualiser2.bytesprites.bytesprite import ByteSprite from Visualiser2.config import Config from Visualiser2.utils.log_reader import logs_to_dict from game.utils.vector import Vector @@ -25,11 +26,12 @@ def __init__(self): self.simple_font = pygame.font.Font(None, 50) self.tick: int = 0 - self.bytesprites = pygame.sprite.Group() + self.bytesprite_templates = pygame.sprite.Group() + self.bytesprite_map: [[[ByteSprite]]] = list() def load(self): self.turn_logs = logs_to_dict() - self.bytesprites = self.adapter.populate_bytesprites() + self.bytesprite_templates = self.adapter.populate_bytesprites() def prerender(self): self.screen.fill(self.config.BACKGROUND_COLOR) @@ -38,16 +40,55 @@ def prerender(self): def render(self): if self.tick % self.config.NUMBER_OF_FRAMES_PER_TURN == 0: # NEXT TURN + self.recalc_animation(self.turn_logs[f'turn_{self.tick // self.config.NUMBER_OF_FRAMES_PER_TURN + 1:04d}']) self.adapter.recalc_animation( self.turn_logs[f'turn_{self.tick // self.config.NUMBER_OF_FRAMES_PER_TURN + 1:04d}']) else: # NEXT ANIMATION FRAME + self.continue_animation() self.adapter.continue_animation() self.adapter.render() pygame.display.flip() self.tick += 1 + def recalc_animation(self, turn_data: dict) -> None: + game_map: [[dict]] = turn_data['game_board']['game_map'] + row: list + for y, row in enumerate(game_map): + tile: dict + if len(self.bytesprite_map) < y + 1: + self.bytesprite_map.append(list()) + for x, tile in enumerate(row): + if len(self.bytesprite_map[y]) < x + 1: + self.bytesprite_map.append(list()) + temp_tile: dict | None = tile + z: int = 0 + while temp_tile is not None: + if len(self.bytesprite_map[y][x]) < z + 1: + self.bytesprite_map[y][x].append(None) + + if self.bytesprite_map[y][x][z] is None or self.bytesprite_map[y][x][z].object_type == temp_tile[ + 'object_type']: + sprite_class: ByteSprite | None = next(t for t in self.bytesprite_templates.sprites() if + isinstance(t, ByteSprite) and t.object_type == temp_tile[ + 'object_type']) + if sprite_class is None: + raise ValueError( + f'Must provide a bytesprite for each object type! Missing object_type: {temp_tile["object_type"]}') + + self.bytesprite_map[y][x][z] = sprite_class.__class__(self.screen) + + self.bytesprite_map[y][x][z].update(temp_tile, z, Vector(y=y, x=x)) + temp_tile = temp_tile.get('occupied_by') + z += 1 + + def continue_animation(self) -> None: + row: list + tile: list + sprite: ByteSprite + [[[sprite.set_image_and_render() for sprite in tile] for tile in row] for row in self.bytesprite_map] + def postrender(self): self.adapter.clean_up() self.clock.tick(self.config.FRAME_RATE) From 0063074749c95a8cc1ae31516e74758a037c632d Mon Sep 17 00:00:00 2001 From: JeanAEckelberg Date: Fri, 30 Jun 2023 21:01:22 -0500 Subject: [PATCH 3/5] added y index to tile logic --- Visualiser2/main.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Visualiser2/main.py b/Visualiser2/main.py index 2e90500..9c0e0d4 100644 --- a/Visualiser2/main.py +++ b/Visualiser2/main.py @@ -53,6 +53,11 @@ def render(self): self.tick += 1 def recalc_animation(self, turn_data: dict) -> None: + """ + + :param turn_data: + :return: None + """ game_map: [[dict]] = turn_data['game_board']['game_map'] row: list for y, row in enumerate(game_map): @@ -61,7 +66,7 @@ def recalc_animation(self, turn_data: dict) -> None: self.bytesprite_map.append(list()) for x, tile in enumerate(row): if len(self.bytesprite_map[y]) < x + 1: - self.bytesprite_map.append(list()) + self.bytesprite_map[y].append(list()) temp_tile: dict | None = tile z: int = 0 while temp_tile is not None: From 246cb88d063e3151e51aa3148d86d8b64d4e6736 Mon Sep 17 00:00:00 2001 From: JeanAEckelberg Date: Fri, 30 Jun 2023 21:20:54 -0500 Subject: [PATCH 4/5] removes unneeded sprites --- Visualiser2/main.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Visualiser2/main.py b/Visualiser2/main.py index 9c0e0d4..3204a36 100644 --- a/Visualiser2/main.py +++ b/Visualiser2/main.py @@ -73,7 +73,7 @@ def recalc_animation(self, turn_data: dict) -> None: if len(self.bytesprite_map[y][x]) < z + 1: self.bytesprite_map[y][x].append(None) - if self.bytesprite_map[y][x][z] is None or self.bytesprite_map[y][x][z].object_type == temp_tile[ + if self.bytesprite_map[y][x][z] is None or self.bytesprite_map[y][x][z].object_type != temp_tile[ 'object_type']: sprite_class: ByteSprite | None = next(t for t in self.bytesprite_templates.sprites() if isinstance(t, ByteSprite) and t.object_type == temp_tile[ @@ -88,6 +88,9 @@ def recalc_animation(self, turn_data: dict) -> None: temp_tile = temp_tile.get('occupied_by') z += 1 + while len(self.bytesprite_map[y][x]) > z: + self.bytesprite_map[y][x].pop() + def continue_animation(self) -> None: row: list tile: list From 6cea25cbb5edc22e07a5bbb3fbf9f998aa3e695a Mon Sep 17 00:00:00 2001 From: JeanAEckelberg Date: Fri, 30 Jun 2023 21:31:14 -0500 Subject: [PATCH 5/5] Comments --- Visualiser2/main.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Visualiser2/main.py b/Visualiser2/main.py index 3204a36..d0ff321 100644 --- a/Visualiser2/main.py +++ b/Visualiser2/main.py @@ -54,40 +54,52 @@ def render(self): def recalc_animation(self, turn_data: dict) -> None: """ - - :param turn_data: + Determine what bytesprites are needed at which location and calls logic to determine active spritesheet and render + :param turn_data: A dictionary of all the turn data for current turn :return: None """ game_map: [[dict]] = turn_data['game_board']['game_map'] + # Iterate on each row on the game map row: list for y, row in enumerate(game_map): - tile: dict + # Add rows to bytesprite_map if needed if len(self.bytesprite_map) < y + 1: self.bytesprite_map.append(list()) + # Iterate on each tile in the row + tile: dict for x, tile in enumerate(row): + # Add tiles to row if needed if len(self.bytesprite_map[y]) < x + 1: self.bytesprite_map[y].append(list()) + # Render layers on tile temp_tile: dict | None = tile z: int = 0 while temp_tile is not None: + # Add layers if needed if len(self.bytesprite_map[y][x]) < z + 1: self.bytesprite_map[y][x].append(None) + # Create or replace bytesprite at current tile on this current layer if self.bytesprite_map[y][x][z] is None or self.bytesprite_map[y][x][z].object_type != temp_tile[ 'object_type']: sprite_class: ByteSprite | None = next(t for t in self.bytesprite_templates.sprites() if isinstance(t, ByteSprite) and t.object_type == temp_tile[ 'object_type']) + # Check that a bytesprite template exists for current object type if sprite_class is None: raise ValueError( f'Must provide a bytesprite for each object type! Missing object_type: {temp_tile["object_type"]}') + # Instantiate a new bytesprite on current layer self.bytesprite_map[y][x][z] = sprite_class.__class__(self.screen) + # Call render logic on bytesprite self.bytesprite_map[y][x][z].update(temp_tile, z, Vector(y=y, x=x)) + # increase iteration temp_tile = temp_tile.get('occupied_by') z += 1 + # clean up additional layers while len(self.bytesprite_map[y][x]) > z: self.bytesprite_map[y][x].pop()