From 772e3bb47ab720f10a0c0f7e5312c5f9cb0d2f8e Mon Sep 17 00:00:00 2001 From: Faraphel Date: Tue, 21 Feb 2023 17:38:56 +0100 Subject: [PATCH] added a bomb functionality on the enemy game grid --- source/gui/scene/Game.py | 9 +++- source/gui/widget/grid/GameGridAlly.py | 8 ++-- source/gui/widget/grid/GameGridEnemy.py | 60 +++++++++++++++++++++++-- source/gui/widget/grid/abc/GameGrid.py | 2 - 4 files changed, 69 insertions(+), 10 deletions(-) diff --git a/source/gui/scene/Game.py b/source/gui/scene/Game.py index 986a752..f26b91c 100644 --- a/source/gui/scene/Game.py +++ b/source/gui/scene/Game.py @@ -22,6 +22,7 @@ class Game(Scene): self.batch_grid_line = pyglet.graphics.Batch() self.batch_grid_cursor = pyglet.graphics.Batch() self.batch_grid_boat = pyglet.graphics.Batch() + self.batch_grid_bomb = pyglet.graphics.Batch() self.background = self.add_widget( widget.Image, @@ -38,7 +39,8 @@ class Game(Scene): boats_length=[5, 5, 4, 3, 2], - style=texture.Grid.Style1, + grid_style=texture.Grid.Style1, + boat_style=texture.Grid.Boat.Style1, rows=8, columns=8, background_batch=self.batch_grid_background, @@ -52,12 +54,14 @@ class Game(Scene): x=lambda widget: widget.scene.window.width - 75 - widget.width, y=0.25, width=0.35, height=0.5, - style=texture.Grid.Style1, + grid_style=texture.Grid.Style1, + bomb_style=texture.Grid.Bomb.Style1, rows=8, columns=8, background_batch=self.batch_grid_background, line_batch=self.batch_grid_line, cursor_batch=self.batch_grid_cursor, + bomb_batch=self.batch_grid_bomb ) self.name_ally = self.add_widget( @@ -167,6 +171,7 @@ class Game(Scene): self.batch_input_background.draw() self.batch_grid_background.draw() self.batch_grid_boat.draw() + self.batch_grid_bomb.draw() self.batch_grid_line.draw() self.batch_grid_cursor.draw() diff --git a/source/gui/widget/grid/GameGridAlly.py b/source/gui/widget/grid/GameGridAlly.py index 6c22a6f..130b597 100644 --- a/source/gui/widget/grid/GameGridAlly.py +++ b/source/gui/widget/grid/GameGridAlly.py @@ -24,7 +24,8 @@ class GameGridAlly(GameGrid): rows: int, columns: int, - style: Type[Style], + grid_style: Type[Style], + boat_style: Type[Style], boats_length: list[int], preview_color: ColorRGB = (150, 255, 150), @@ -32,7 +33,7 @@ class GameGridAlly(GameGrid): **kwargs): self.cell_sprites: dict[Point2D, "Sprite"] = {} - super().__init__(scene, rows, columns, style, **kwargs) + super().__init__(scene, rows, columns, grid_style, **kwargs) self.boats_length = boats_length # the list of the size of the boats to place self.preview_color = preview_color @@ -41,6 +42,7 @@ class GameGridAlly(GameGrid): self.orientation: Orientation = Orientation.HORIZONTAL self._boat_kwargs = dict_filter_prefix("boat_", kwargs) + self.boat_style = boat_style 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))) @@ -94,7 +96,7 @@ class GameGridAlly(GameGrid): ) sprite = Sprite( - img=texture.Grid.Boat.Style1.get(form), + img=self.boat_style.get(form), **self._boat_kwargs ) sprite.rotation = rotation * 90 diff --git a/source/gui/widget/grid/GameGridEnemy.py b/source/gui/widget/grid/GameGridEnemy.py index be82a51..ef97a71 100644 --- a/source/gui/widget/grid/GameGridEnemy.py +++ b/source/gui/widget/grid/GameGridEnemy.py @@ -1,10 +1,64 @@ -from typing import Type +from typing import Type, TYPE_CHECKING +import pyglet + +from source.gui import texture from source.gui.texture.abc import Style from source.gui.widget.grid.abc import GameGrid +from source.gui.sprite import Sprite +from source.type import Point2D +from source.utils import dict_filter_prefix + +if TYPE_CHECKING: + from source.gui.scene.abc import Scene class GameGridEnemy(GameGrid): - def __init__(self, scene: "Scene", rows: int, columns: int, style: Type[Style], **kwargs): - super().__init__(scene, rows, columns, style, **kwargs) + def __init__(self, scene: "Scene", + rows: int, + columns: int, + + grid_style: Type[Style], + bomb_style: Type[Style], + + **kwargs): + self.cell_sprites: dict[Point2D, "Sprite"] = {} + + super().__init__(scene, rows, columns, grid_style, **kwargs) + + self._bomb_kwargs = dict_filter_prefix("bomb_", kwargs) + self.bomb_style = bomb_style + + self.add_listener("on_click_release", self.on_click_release) + + def _refresh_size(self): + super()._refresh_size() + + for (x, y), sprite in self.cell_sprites.items(): + sprite.x = self.x + (self.cell_width * x) + sprite.y = self.y + (self.cell_height * y) + sprite.width = self.cell_width + sprite.height = self.cell_height + + def place_bomb(self, cell: Point2D): + from random import randint + + self.cell_sprites[cell] = Sprite( + img=self.bomb_style.get("touched" if randint(0, 1) else "missed"), + **self._bomb_kwargs + ) + + self._refresh_size() + + 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) + + if button == pyglet.window.mouse.LEFT: + self.place_bomb(cell) + + def draw(self): + self.background.draw() + for sprite in self.cell_sprites.values(): sprite.draw() + self.cursor.draw() + for line in self.lines: line.draw() \ No newline at end of file diff --git a/source/gui/widget/grid/abc/GameGrid.py b/source/gui/widget/grid/abc/GameGrid.py index 512c4bd..d318f95 100644 --- a/source/gui/widget/grid/abc/GameGrid.py +++ b/source/gui/widget/grid/abc/GameGrid.py @@ -55,8 +55,6 @@ class GameGrid(BoxWidget): self.add_listener("on_hover_leave", lambda *_: self.hide_cursor()) self.add_listener("on_hover", self._refresh_cursor) - self.add_listener("on_click_release", lambda rel_x, rel_y, *_: print("click", self.get_cell_from_rel(rel_x, rel_y))) - self._refresh_size() def get_cell_from_rel(self, rel_x: int, rel_y: int) -> tuple[int, int]: