added type hint for Point2D and BBox

This commit is contained in:
Faraphel 2023-01-10 20:33:13 +01:00
parent 31bb0c1d11
commit de9fcf78c0
10 changed files with 29 additions and 16 deletions

View file

@ -3,7 +3,8 @@ import numpy as np
from source.core.Boat import Boat from source.core.Boat import Boat
from source.core.enums import Orientation, BombState from source.core.enums import Orientation, BombState
from source.core.error import InvalidBoatPosition, PositionAlreadyShot, InvalidBombPosition 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: class Board:
@ -13,7 +14,7 @@ class Board:
self, self,
width: int, width: int,
height: int = None, height: int = None,
boats: dict[Boat, tuple[int, int]] = None, boats: dict[Boat, Point2D] = None,
bombs: np.array = None bombs: np.array = None
) -> None: ) -> None:
@ -21,7 +22,7 @@ class Board:
self._height: int = width if height is None else height self._height: int = width if height is None else height
# associate the boats to their position # 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 # 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 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: def __str__(self) -> str:
return str(self.get_matrice()) 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. Add a boat to the board. Check before if the position is valid.
:boat: the boat to add :boat: the boat to add
@ -68,7 +69,7 @@ class Board:
""" """
self._boats.pop(boat) self._boats.pop(boat)
def bomb(self, position: tuple[int, int]) -> BombState: def bomb(self, position: Point2D) -> BombState:
""" """
Hit a position on the board Hit a position on the board
:position: the position where to shoot :position: the position where to shoot

View file

@ -1,6 +1,7 @@
from source.core.Boat import Boat from source.core.Boat import Boat
from source.type import Point2D
class InvalidBoatPosition(Exception): 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}.") super().__init__(f"The boat {boat} can't be placed at {position}.")

View file

@ -1,3 +1,6 @@
from source.type import Point2D
class InvalidBombPosition(Exception): 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}.") super().__init__(f"The bomb can't be placed at {position}.")

View file

@ -1,4 +1,7 @@
from source.type import Point2D
class PositionAlreadyShot(Exception): 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.") super().__init__(f"The position {position} have already been shot.")

View file

@ -1,5 +1,4 @@
from source.gui.scene.base import Scene from source.gui.scene.base import Scene
from source.gui.widget.Button import Button
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
@ -11,8 +10,6 @@ class MainMenuScene(Scene):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.add_widget(Button())
def on_draw(self, window: "Window"): def on_draw(self, window: "Window"):
super().on_draw(window) super().on_draw(window)

View file

@ -4,6 +4,7 @@ import pyglet
from source.gui.sprite import Sprite from source.gui.sprite import Sprite
from source.gui.widget.base import Widget from source.gui.widget.base import Widget
from source.type import BBox
from source.utils import in_bbox from source.utils import in_bbox
if TYPE_CHECKING: if TYPE_CHECKING:
@ -81,7 +82,7 @@ class Button(Widget):
return self._hover_sprite if self._hovering else self._normal_sprite return self._hover_sprite if self._hovering else self._normal_sprite
@property @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 return self.x, self.y, self.x + self.width, self.y + self.height
# label getter and setter # label getter and setter

View file

@ -3,7 +3,7 @@ from typing import Optional, Callable, TYPE_CHECKING
import pyglet.window import pyglet.window
if TYPE_CHECKING: 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 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. 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) super().__init__(*args, **kwargs)
self._scenes: list["Scene"] = [] self._scenes: list["Scene"] = []
if scenes is not None: self.add_scene(*scenes) if scenes is not None: self.add_scene(*scenes)

2
source/type.py Normal file
View file

@ -0,0 +1,2 @@
Point2D = tuple[int, int]
BBox = tuple[int, int, int, int]

View file

@ -1,7 +1,9 @@
import numpy as np 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 Copy a numpy array into another one with an offset
:source: source array :source: source array

View file

@ -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 Return true if a point is inside a bounding box
:param point: the point to check :param point: the point to check