added a bomb functionality on the enemy game grid
This commit is contained in:
parent
bda12c3cc1
commit
772e3bb47a
4 changed files with 69 additions and 10 deletions
|
@ -22,6 +22,7 @@ class Game(Scene):
|
||||||
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.batch_grid_boat = pyglet.graphics.Batch()
|
||||||
|
self.batch_grid_bomb = pyglet.graphics.Batch()
|
||||||
|
|
||||||
self.background = self.add_widget(
|
self.background = self.add_widget(
|
||||||
widget.Image,
|
widget.Image,
|
||||||
|
@ -38,7 +39,8 @@ class Game(Scene):
|
||||||
|
|
||||||
boats_length=[5, 5, 4, 3, 2],
|
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,
|
rows=8, columns=8,
|
||||||
|
|
||||||
background_batch=self.batch_grid_background,
|
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,
|
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,
|
rows=8, columns=8,
|
||||||
|
|
||||||
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,
|
||||||
|
bomb_batch=self.batch_grid_bomb
|
||||||
)
|
)
|
||||||
|
|
||||||
self.name_ally = self.add_widget(
|
self.name_ally = self.add_widget(
|
||||||
|
@ -167,6 +171,7 @@ class Game(Scene):
|
||||||
self.batch_input_background.draw()
|
self.batch_input_background.draw()
|
||||||
self.batch_grid_background.draw()
|
self.batch_grid_background.draw()
|
||||||
self.batch_grid_boat.draw()
|
self.batch_grid_boat.draw()
|
||||||
|
self.batch_grid_bomb.draw()
|
||||||
self.batch_grid_line.draw()
|
self.batch_grid_line.draw()
|
||||||
self.batch_grid_cursor.draw()
|
self.batch_grid_cursor.draw()
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,8 @@ class GameGridAlly(GameGrid):
|
||||||
rows: int,
|
rows: int,
|
||||||
columns: int,
|
columns: int,
|
||||||
|
|
||||||
style: Type[Style],
|
grid_style: Type[Style],
|
||||||
|
boat_style: Type[Style],
|
||||||
|
|
||||||
boats_length: list[int],
|
boats_length: list[int],
|
||||||
preview_color: ColorRGB = (150, 255, 150),
|
preview_color: ColorRGB = (150, 255, 150),
|
||||||
|
@ -32,7 +33,7 @@ class GameGridAlly(GameGrid):
|
||||||
**kwargs):
|
**kwargs):
|
||||||
self.cell_sprites: dict[Point2D, "Sprite"] = {}
|
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.boats_length = boats_length # the list of the size of the boats to place
|
||||||
self.preview_color = preview_color
|
self.preview_color = preview_color
|
||||||
|
@ -41,6 +42,7 @@ class GameGridAlly(GameGrid):
|
||||||
self.orientation: Orientation = Orientation.HORIZONTAL
|
self.orientation: Orientation = Orientation.HORIZONTAL
|
||||||
|
|
||||||
self._boat_kwargs = dict_filter_prefix("boat_", kwargs)
|
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_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)))
|
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(
|
sprite = Sprite(
|
||||||
img=texture.Grid.Boat.Style1.get(form),
|
img=self.boat_style.get(form),
|
||||||
**self._boat_kwargs
|
**self._boat_kwargs
|
||||||
)
|
)
|
||||||
sprite.rotation = rotation * 90
|
sprite.rotation = rotation * 90
|
||||||
|
|
|
@ -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.texture.abc import Style
|
||||||
from source.gui.widget.grid.abc import GameGrid
|
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):
|
class GameGridEnemy(GameGrid):
|
||||||
def __init__(self, scene: "Scene", rows: int, columns: int, style: Type[Style], **kwargs):
|
def __init__(self, scene: "Scene",
|
||||||
super().__init__(scene, rows, columns, style, **kwargs)
|
|
||||||
|
|
||||||
|
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()
|
|
@ -55,8 +55,6 @@ class GameGrid(BoxWidget):
|
||||||
self.add_listener("on_hover_leave", lambda *_: self.hide_cursor())
|
self.add_listener("on_hover_leave", lambda *_: self.hide_cursor())
|
||||||
self.add_listener("on_hover", self._refresh_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()
|
self._refresh_size()
|
||||||
|
|
||||||
def get_cell_from_rel(self, rel_x: int, rel_y: int) -> tuple[int, int]:
|
def get_cell_from_rel(self, rel_x: int, rel_y: int) -> tuple[int, int]:
|
||||||
|
|
Loading…
Reference in a new issue