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.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

View file

@ -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}.")

View file

@ -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}.")

View file

@ -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.")

View file

@ -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)

View file

@ -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

View file

@ -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)

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
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

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
:param point: the point to check