From bda12c3cc15d402e99cd885ad4834f599aad15be Mon Sep 17 00:00:00 2001 From: Faraphel Date: Tue, 21 Feb 2023 17:01:38 +0100 Subject: [PATCH] fixed a bug where the board inverted the rows and columns --- source/core/Board.py | 4 ++-- source/gui/scene/Game.py | 10 ++++++---- source/gui/widget/grid/GameGridAlly.py | 21 +++++++-------------- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/source/core/Board.py b/source/core/Board.py index d7b7ef0..745e04d 100644 --- a/source/core/Board.py +++ b/source/core/Board.py @@ -25,7 +25,7 @@ class Board: self._boats: dict[Boat, Point2D] = {} if boats is None else boats # position that have been shot by a bomb - self._bombs: np.array = np.ones((self._columns, self._rows), dtype=np.bool_) if bombs is None else bombs + self._bombs: np.array = np.ones((self._rows, self._columns), dtype=np.bool_) if bombs is None else bombs def __repr__(self) -> str: return f"<{self.__class__.__name__} width={self._columns}, height={self._rows}>" @@ -113,7 +113,7 @@ class Board: """ :return: the boat represented as a matrice """ - board = np.zeros((self._columns, self._rows), dtype=np.ushort) + board = np.zeros((self._rows, self._columns), dtype=np.ushort) for index, (boat, position) in enumerate(self._boats.items(), start=1): # Paste the boat into the board at the correct position. diff --git a/source/gui/scene/Game.py b/source/gui/scene/Game.py index 411f192..986a752 100644 --- a/source/gui/scene/Game.py +++ b/source/gui/scene/Game.py @@ -21,6 +21,7 @@ class Game(Scene): self.batch_grid_background = pyglet.graphics.Batch() self.batch_grid_line = pyglet.graphics.Batch() self.batch_grid_cursor = pyglet.graphics.Batch() + self.batch_grid_boat = pyglet.graphics.Batch() self.background = self.add_widget( widget.Image, @@ -43,6 +44,7 @@ class Game(Scene): background_batch=self.batch_grid_background, line_batch=self.batch_grid_line, cursor_batch=self.batch_grid_cursor, + boat_batch=self.batch_grid_boat, ) self.grid_enemy = self.add_widget( @@ -163,11 +165,11 @@ class Game(Scene): self.batch_button_background.draw() self.batch_input_background.draw() - # self.batch_grid_background.draw() - # self.batch_grid_line.draw() - # self.batch_grid_cursor.draw() + self.batch_grid_background.draw() + self.batch_grid_boat.draw() + self.batch_grid_line.draw() + self.batch_grid_cursor.draw() self.batch_label.draw() - self.grid_ally.draw() # DEBUG self.grid_enemy.draw() # DEBUG diff --git a/source/gui/widget/grid/GameGridAlly.py b/source/gui/widget/grid/GameGridAlly.py index 2a913e8..6c22a6f 100644 --- a/source/gui/widget/grid/GameGridAlly.py +++ b/source/gui/widget/grid/GameGridAlly.py @@ -40,9 +40,13 @@ class GameGridAlly(GameGrid): self.board = Board(rows=self.rows, columns=self.columns) self.orientation: Orientation = Orientation.HORIZONTAL - self._cell_kwargs = dict_filter_prefix("cell_", kwargs) + self._boat_kwargs = dict_filter_prefix("boat_", kwargs) + + self.add_listener("on_click_release", self.on_click_release) + self.add_listener("on_hover", lambda rel_x, rel_y: self.preview_boat(self.get_cell_from_rel(rel_x, rel_y))) # refresh + def _refresh_size(self): super()._refresh_size() @@ -91,7 +95,7 @@ class GameGridAlly(GameGrid): sprite = Sprite( img=texture.Grid.Boat.Style1.get(form), - **self._cell_kwargs + **self._boat_kwargs ) sprite.rotation = rotation * 90 @@ -136,10 +140,7 @@ class GameGridAlly(GameGrid): except InvalidBoatPosition: self.display_board(self.board) # if the boat can't be placed, ignore else: self.display_board(preview_board, preview=True) - def on_mouse_release(self, x: int, y: int, button: int, modifiers: int): - super().on_mouse_release(x, y, button, modifiers) - - rel_x, rel_y = x - self.x, y - self.y + def on_click_release(self, rel_x: int, rel_y: int, button: int, modifiers: int): cell = self.get_cell_from_rel(rel_x, rel_y) match button: @@ -150,14 +151,6 @@ class GameGridAlly(GameGrid): case pyglet.window.mouse.LEFT: self.place_boat(cell) - def on_mouse_motion(self, x: int, y: int, dx: int, dy: int): - super().on_mouse_motion(x, y, dx, dy) - - rel_x, rel_y = x - self.x, y - self.y - cell = self.get_cell_from_rel(rel_x, rel_y) - - self.preview_boat(cell) - def draw(self): self.background.draw() for sprite in self.cell_sprites.values(): sprite.draw()