diff --git a/source/core/Board.py b/source/core/Board.py index 03ad3f3..2a408fa 100644 --- a/source/core/Board.py +++ b/source/core/Board.py @@ -3,7 +3,8 @@ import numpy as np from source.core.Boat import Boat from source.core.enums import Orientation, BombState from source.core.error import InvalidBoatPosition, PositionAlreadyShot, InvalidBombPosition -from source.utils import copy_array_offset, in_bbox +from source.type import Point2D +from source.utils import copy_array_offset class Board: @@ -13,7 +14,7 @@ class Board: self, width: int, height: int = None, - boats: dict[Boat, tuple[int, int]] = None, + boats: dict[Boat, Point2D] = None, bombs: np.array = None ) -> None: @@ -21,7 +22,7 @@ class Board: self._height: int = width if height is None else height # associate the boats to their position - self._boats: dict[Boat, tuple[int, int]] = {} 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 self._bombs: np.array = np.ones((self._width, self._height), dtype=np.bool_) if bombs is None else bombs @@ -32,7 +33,7 @@ class Board: def __str__(self) -> str: return str(self.get_matrice()) - def add_boat(self, boat: Boat, position: tuple[int, int]) -> None: + def add_boat(self, boat: Boat, position: Point2D) -> None: """ Add a boat to the board. Check before if the position is valid. :boat: the boat to add @@ -68,7 +69,7 @@ class Board: """ self._boats.pop(boat) - def bomb(self, position: tuple[int, int]) -> BombState: + def bomb(self, position: Point2D) -> BombState: """ Hit a position on the board :position: the position where to shoot diff --git a/source/core/error/InvalidBoatPosition.py b/source/core/error/InvalidBoatPosition.py index c93772a..14f5894 100644 --- a/source/core/error/InvalidBoatPosition.py +++ b/source/core/error/InvalidBoatPosition.py @@ -1,6 +1,7 @@ from source.core.Boat import Boat +from source.type import Point2D class InvalidBoatPosition(Exception): - def __init__(self, boat: Boat, position: tuple[int, int]): + def __init__(self, boat: Boat, position: Point2D): super().__init__(f"The boat {boat} can't be placed at {position}.") diff --git a/source/core/error/InvalidBombPosition.py b/source/core/error/InvalidBombPosition.py index e1454ef..c961274 100644 --- a/source/core/error/InvalidBombPosition.py +++ b/source/core/error/InvalidBombPosition.py @@ -1,3 +1,6 @@ +from source.type import Point2D + + class InvalidBombPosition(Exception): - def __init__(self, position: tuple[int, int]): + def __init__(self, position: Point2D): super().__init__(f"The bomb can't be placed at {position}.") diff --git a/source/core/error/PositionAlreadyShot.py b/source/core/error/PositionAlreadyShot.py index 140525b..2db497c 100644 --- a/source/core/error/PositionAlreadyShot.py +++ b/source/core/error/PositionAlreadyShot.py @@ -1,4 +1,7 @@ +from source.type import Point2D + + class PositionAlreadyShot(Exception): - def __init__(self, position: tuple[int, int]): + def __init__(self, position: Point2D): super().__init__(f"The position {position} have already been shot.") diff --git a/source/gui/scene/MainMenu.py b/source/gui/scene/MainMenu.py index b2eba66..cbf0fe3 100644 --- a/source/gui/scene/MainMenu.py +++ b/source/gui/scene/MainMenu.py @@ -1,5 +1,4 @@ from source.gui.scene.base import Scene -from source.gui.widget.Button import Button from typing import TYPE_CHECKING @@ -11,8 +10,6 @@ class MainMenuScene(Scene): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.add_widget(Button()) - def on_draw(self, window: "Window"): super().on_draw(window) diff --git a/source/gui/widget/Button.py b/source/gui/widget/Button.py index bf440bf..cd4ef66 100644 --- a/source/gui/widget/Button.py +++ b/source/gui/widget/Button.py @@ -4,6 +4,7 @@ import pyglet from source.gui.sprite import Sprite from source.gui.widget.base import Widget +from source.type import BBox from source.utils import in_bbox if TYPE_CHECKING: @@ -81,7 +82,7 @@ class Button(Widget): return self._hover_sprite if self._hovering else self._normal_sprite @property - def bbox(self) -> tuple[int, int, int, int]: + def bbox(self) -> BBox: return self.x, self.y, self.x + self.width, self.y + self.height # label getter and setter diff --git a/source/gui/window/Window.py b/source/gui/window/Window.py index bdfff5a..62f4e2c 100644 --- a/source/gui/window/Window.py +++ b/source/gui/window/Window.py @@ -3,7 +3,7 @@ from typing import Optional, Callable, TYPE_CHECKING import pyglet.window if TYPE_CHECKING: - from source.gui.scene import Scene + from source.gui.scene.base import Scene class Window(pyglet.window.Window): # NOQA - pycharm think pyglet window is abstract @@ -15,7 +15,7 @@ class Window(pyglet.window.Window): # NOQA - pycharm think pyglet window is abs putting everything in the window code. """ - def __init__(self, scenes: Optional["Scene"] = None, *args, **kwargs): + def __init__(self, scenes: Optional[list["Scene"]] = None, *args, **kwargs): super().__init__(*args, **kwargs) self._scenes: list["Scene"] = [] if scenes is not None: self.add_scene(*scenes) diff --git a/source/type.py b/source/type.py new file mode 100644 index 0000000..e72d125 --- /dev/null +++ b/source/type.py @@ -0,0 +1,2 @@ +Point2D = tuple[int, int] +BBox = tuple[int, int, int, int] diff --git a/source/utils/copy_array_offset.py b/source/utils/copy_array_offset.py index 89f609f..5f0ad46 100644 --- a/source/utils/copy_array_offset.py +++ b/source/utils/copy_array_offset.py @@ -1,7 +1,9 @@ import numpy as np +from source.type import Point2D -def copy_array_offset(src: np.array, dst: np.array, offset: tuple[int, int]) -> None: + +def copy_array_offset(src: np.array, dst: np.array, offset: Point2D) -> None: """ Copy a numpy array into another one with an offset :source: source array diff --git a/source/utils/in_bbox.py b/source/utils/in_bbox.py index 0f4e4d2..9eebf9d 100644 --- a/source/utils/in_bbox.py +++ b/source/utils/in_bbox.py @@ -1,4 +1,7 @@ -def in_bbox(point: tuple[int, int], bbox: tuple[int, int, int, int]) -> bool: +from source.type import Point2D, BBox + + +def in_bbox(point: Point2D, bbox: BBox) -> bool: """ Return true if a point is inside a bounding box :param point: the point to check