fixed a bug where the board inverted the rows and columns

This commit is contained in:
Faraphel 2023-02-21 17:01:38 +01:00
parent 6eb8d2452d
commit bda12c3cc1
3 changed files with 15 additions and 20 deletions

View file

@ -25,7 +25,7 @@ class Board:
self._boats: dict[Boat, Point2D] = {} if boats is None else boats self._boats: dict[Boat, Point2D] = {} if boats is None else boats
# position that have been shot by a bomb # 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: def __repr__(self) -> str:
return f"<{self.__class__.__name__} width={self._columns}, height={self._rows}>" return f"<{self.__class__.__name__} width={self._columns}, height={self._rows}>"
@ -113,7 +113,7 @@ class Board:
""" """
:return: the boat represented as a matrice :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): for index, (boat, position) in enumerate(self._boats.items(), start=1):
# Paste the boat into the board at the correct position. # Paste the boat into the board at the correct position.

View file

@ -21,6 +21,7 @@ class Game(Scene):
self.batch_grid_background = pyglet.graphics.Batch() self.batch_grid_background = pyglet.graphics.Batch()
self.batch_grid_line = pyglet.graphics.Batch() self.batch_grid_line = pyglet.graphics.Batch()
self.batch_grid_cursor = pyglet.graphics.Batch() self.batch_grid_cursor = pyglet.graphics.Batch()
self.batch_grid_boat = pyglet.graphics.Batch()
self.background = self.add_widget( self.background = self.add_widget(
widget.Image, widget.Image,
@ -43,6 +44,7 @@ class Game(Scene):
background_batch=self.batch_grid_background, background_batch=self.batch_grid_background,
line_batch=self.batch_grid_line, line_batch=self.batch_grid_line,
cursor_batch=self.batch_grid_cursor, cursor_batch=self.batch_grid_cursor,
boat_batch=self.batch_grid_boat,
) )
self.grid_enemy = self.add_widget( self.grid_enemy = self.add_widget(
@ -163,11 +165,11 @@ class Game(Scene):
self.batch_button_background.draw() self.batch_button_background.draw()
self.batch_input_background.draw() self.batch_input_background.draw()
# self.batch_grid_background.draw() self.batch_grid_background.draw()
# self.batch_grid_line.draw() self.batch_grid_boat.draw()
# self.batch_grid_cursor.draw() self.batch_grid_line.draw()
self.batch_grid_cursor.draw()
self.batch_label.draw() self.batch_label.draw()
self.grid_ally.draw() # DEBUG
self.grid_enemy.draw() # DEBUG self.grid_enemy.draw() # DEBUG

View file

@ -40,9 +40,13 @@ class GameGridAlly(GameGrid):
self.board = Board(rows=self.rows, columns=self.columns) self.board = Board(rows=self.rows, columns=self.columns)
self.orientation: Orientation = Orientation.HORIZONTAL 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 # refresh
def _refresh_size(self): def _refresh_size(self):
super()._refresh_size() super()._refresh_size()
@ -91,7 +95,7 @@ class GameGridAlly(GameGrid):
sprite = Sprite( sprite = Sprite(
img=texture.Grid.Boat.Style1.get(form), img=texture.Grid.Boat.Style1.get(form),
**self._cell_kwargs **self._boat_kwargs
) )
sprite.rotation = rotation * 90 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 except InvalidBoatPosition: self.display_board(self.board) # if the boat can't be placed, ignore
else: self.display_board(preview_board, preview=True) else: self.display_board(preview_board, preview=True)
def on_mouse_release(self, x: int, y: int, button: int, modifiers: int): def on_click_release(self, rel_x: int, rel_y: int, button: int, modifiers: int):
super().on_mouse_release(x, y, button, modifiers)
rel_x, rel_y = x - self.x, y - self.y
cell = self.get_cell_from_rel(rel_x, rel_y) cell = self.get_cell_from_rel(rel_x, rel_y)
match button: match button:
@ -150,14 +151,6 @@ class GameGridAlly(GameGrid):
case pyglet.window.mouse.LEFT: case pyglet.window.mouse.LEFT:
self.place_boat(cell) 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): def draw(self):
self.background.draw() self.background.draw()
for sprite in self.cell_sprites.values(): sprite.draw() for sprite in self.cell_sprites.values(): sprite.draw()