Skip to content

Commit 9aaf505

Browse files
authored
Backport improvements from hatchling branch
2 parents cc3d988 + e76e66f commit 9aaf505

10 files changed

Lines changed: 75 additions & 65 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ keywords = [
3939
dependencies = [
4040
"pygame~=2.6.0",
4141
"typing_extensions>=4.12.2",
42+
"mypy_extensions>=1.0.0",
4243
"trio~=0.26.2",
4344
"cryptography>=43.0.0",
4445
"exceptiongroup; python_version < '3.11'",
@@ -53,7 +54,7 @@ version = {attr = "checkers.game.__version__"}
5354
"Bug Tracker" = "https://github.com/CoolCat467/Checkers/issues"
5455

5556
[project.scripts]
56-
checkers_game = "checkers.game:cli_run"
57+
checkers_game = "checkers:cli_run"
5758

5859
[tool.setuptools.package-data]
5960
checkers = ["py.typed", "data/*"]

src/checkers/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Programmed by CoolCat467
44

5-
# Copyright (C) 2023 CoolCat467
5+
# Copyright (C) 2023-2024 CoolCat467
66
#
77
# This program is free software: you can redistribute it and/or modify
88
# it under the terms of the GNU General Public License as published by
@@ -16,3 +16,9 @@
1616
#
1717
# You should have received a copy of the GNU General Public License
1818
# along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
20+
21+
from checkers.game import cli_run as cli_run
22+
23+
if __name__ == "__main__":
24+
cli_run()

src/checkers/base2d.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from checkers.vector import Vector2
3434

3535
if TYPE_CHECKING:
36-
from collections.abc import Callable, Generator, Iterable, Sequence
36+
from collections.abc import Callable, Iterable, Sequence
3737

3838

3939
def amol(
@@ -148,20 +148,6 @@ def get_colors(
148148
return colors
149149

150150

151-
def average_color(
152-
surface: pygame.surface.Surface,
153-
) -> Generator[int, None, None]:
154-
"""Return the average RGB value of a surface."""
155-
s_r, s_g, s_b = 0, 0, 0
156-
colors = get_colors(surface)
157-
for color in colors:
158-
r, g, b = color
159-
s_r += r
160-
s_g += g
161-
s_b += b
162-
return (int(x / len(colors)) for x in (s_r, s_g, s_b))
163-
164-
165151
def replace_with_color(
166152
surface: pygame.surface.Surface,
167153
color: tuple[int, int, int],
@@ -357,8 +343,8 @@ def __init__(
357343
self.value = 0
358344
self.max_value = int(states)
359345
self.anim = anim
360-
self.press_time: float = 1
361-
self.last_press: float = 0
346+
self.press_time: float = 1.0
347+
self.last_press: float = 0.0
362348
self.scan = int(max(get_surf_lens(self.anim)) / 2) + 2
363349

364350
keys = list(kwargs.keys())

src/checkers/client.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
write_position,
4747
)
4848

49+
if TYPE_CHECKING:
50+
from mypy_extensions import u8
51+
4952

5053
async def read_advertisements(
5154
timeout: int = 3, # noqa: ASYNC109
@@ -304,7 +307,7 @@ async def read_create_piece(self, event: Event[bytearray]) -> None:
304307
buffer = Buffer(event.data)
305308

306309
piece_pos = read_position(buffer)
307-
piece_type = buffer.read_value(StructFormat.UBYTE)
310+
piece_type: u8 = buffer.read_value(StructFormat.UBYTE)
308311

309312
await self.raise_event(
310313
Event("gameboard_create_piece", (piece_pos, piece_type)),
@@ -381,7 +384,7 @@ async def read_update_piece_animation(
381384
buffer = Buffer(event.data)
382385

383386
piece_pos = read_position(buffer)
384-
piece_type = buffer.read_value(StructFormat.UBYTE)
387+
piece_type: u8 = buffer.read_value(StructFormat.UBYTE)
385388

386389
await self.raise_event(
387390
Event("gameboard_update_piece_animation", (piece_pos, piece_type)),
@@ -415,7 +418,7 @@ async def read_game_over(self, event: Event[bytearray]) -> None:
415418
"""Read update_piece event from server."""
416419
buffer = Buffer(event.data)
417420

418-
winner = buffer.read_value(StructFormat.UBYTE)
421+
winner: u8 = buffer.read_value(StructFormat.UBYTE)
419422

420423
await self.raise_event(Event("game_winner", winner))
421424
self.running = False
@@ -430,7 +433,7 @@ async def read_action_complete(self, event: Event[bytearray]) -> None:
430433

431434
from_pos = read_position(buffer)
432435
to_pos = read_position(buffer)
433-
current_turn = buffer.read_value(StructFormat.UBYTE)
436+
current_turn: u8 = buffer.read_value(StructFormat.UBYTE)
434437

435438
await self.raise_event(
436439
Event("game_action_complete", (from_pos, to_pos, current_turn)),
@@ -441,7 +444,7 @@ async def read_initial_config(self, event: Event[bytearray]) -> None:
441444
buffer = Buffer(event.data)
442445

443446
board_size = read_position(buffer)
444-
current_turn = buffer.read_value(StructFormat.UBYTE)
447+
current_turn: u8 = buffer.read_value(StructFormat.UBYTE)
445448

446449
await self.raise_event(
447450
Event("game_initial_config", (board_size, current_turn)),
@@ -451,7 +454,7 @@ async def read_playing_as(self, event: Event[bytearray]) -> None:
451454
"""Read playing_as event from server."""
452455
buffer = Buffer(event.data)
453456

454-
playing_as = buffer.read_value(StructFormat.UBYTE)
457+
playing_as: u8 = buffer.read_value(StructFormat.UBYTE)
455458

456459
await self.raise_event(
457460
Event("game_playing_as", playing_as),

src/checkers/component.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
if TYPE_CHECKING:
3434
from collections.abc import Awaitable, Callable, Generator, Iterable
3535

36+
from mypy_extensions import u8
37+
3638
T = TypeVar("T")
3739

3840

@@ -45,7 +47,7 @@ def __init__(
4547
self,
4648
name: str,
4749
data: T,
48-
levels: int = 0,
50+
levels: u8 = 0,
4951
) -> None:
5052
"""Initialize event."""
5153
self.name = name

src/checkers/game.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
__title__ = "Checkers"
2323
__author__ = "CoolCat467"
2424
__license__ = "GNU General Public License Version 3"
25-
__version__ = "2.0.1"
25+
__version__ = "2.0.2"
2626

2727
# Note: Tile Ids are chess board tile titles, A1 to H8
2828
# A8 ... H8
@@ -34,9 +34,8 @@
3434
import contextlib
3535
import platform
3636
from collections import deque
37-
from os import path
3837
from pathlib import Path
39-
from typing import TYPE_CHECKING, Any, Final, TypeAlias, TypeVar
38+
from typing import TYPE_CHECKING, Any, Final, TypeVar
4039

4140
import pygame
4241
import trio
@@ -53,7 +52,7 @@
5352
Event,
5453
ExternalRaiseManager,
5554
)
56-
from checkers.network_shared import DEFAULT_PORT, find_ip
55+
from checkers.network_shared import DEFAULT_PORT, Pos, find_ip
5756
from checkers.objects import Button, OutlinedText
5857
from checkers.server import GameServer
5958
from checkers.sound import SoundData, play_sound as base_play_sound
@@ -105,11 +104,17 @@
105104

106105
T = TypeVar("T")
107106

108-
DATA_FOLDER: Final = Path(__file__).parent / "data"
107+
if globals().get("__file__") is None:
108+
import importlib
109109

110-
IS_WINDOWS: Final = platform.system() == "Windows"
110+
__file__ = str(
111+
Path(importlib.import_module("checkers.data").__path__[0]).parent
112+
/ "game.py",
113+
)
114+
115+
DATA_FOLDER: Final = Path(__file__).absolute().parent / "data"
111116

112-
Pos: TypeAlias = tuple[int, int]
117+
IS_WINDOWS: Final = platform.system() == "Windows"
113118

114119

115120
def render_text(
@@ -704,7 +709,7 @@ def generate_tile_images(self) -> None:
704709
outline_ident = outline.precalculate_outline(name, outline_color)
705710
image.add_image(f"{name}_outlined", outline_ident)
706711

707-
def get_tile_location(self, position: Pos) -> Vector2:
712+
def get_tile_location(self, position: tuple[int, int]) -> Vector2:
708713
"""Return the center point of a given tile position."""
709714
location = Vector2.from_iter(position) * self.tile_size
710715
center = self.tile_size // 2
@@ -773,7 +778,7 @@ def generate_board_image(self) -> Surface:
773778
### Blit the id of the tile at the tile's location
774779
##surf.blit(
775780
## render_text(
776-
## trio.Path(path.dirname(__file__), "data", "VeraSerif.ttf"),
781+
## DATA_FOLDER / "VeraSerif.ttf",
777782
## 20,
778783
## "".join(map(str, (x, y))),
779784
## GREEN
@@ -826,7 +831,7 @@ async def update_selected(self) -> None:
826831

827832
if not self.selected:
828833
movement: sprite.MovementComponent = self.get_component("movement")
829-
movement.speed = 0
834+
movement.speed = 0.0
830835

831836
async def click(self, event: Event[dict[str, int]]) -> None:
832837
"""Toggle selected."""
@@ -841,15 +846,18 @@ async def drag(self, event: Event[Any]) -> None:
841846
self.selected = True
842847
await self.update_selected()
843848
movement: sprite.MovementComponent = self.get_component("movement")
844-
movement.speed = 0
849+
movement.speed = 0.0
845850

846-
async def mouse_down(self, event: Event[dict[str, int | Pos]]) -> None:
851+
async def mouse_down(
852+
self,
853+
event: Event[dict[str, int | tuple[int, int]]],
854+
) -> None:
847855
"""Target click pos if selected."""
848856
if not self.selected:
849857
return
850858
if event.data["button"] == 1:
851859
movement: sprite.MovementComponent = self.get_component("movement")
852-
movement.speed = 200
860+
movement.speed = 200.0
853861
target: sprite.TargetingComponent = self.get_component("targeting")
854862
assert isinstance(event.data["pos"], tuple)
855863
target.destination = Vector2.from_iter(event.data["pos"])
@@ -944,7 +952,7 @@ class FPSCounter(objects.Text):
944952
def __init__(self) -> None:
945953
"""Initialize FPS counter."""
946954
font = pygame.font.Font(
947-
trio.Path(path.dirname(__file__), "data", "VeraSerif.ttf"),
955+
DATA_FOLDER / "VeraSerif.ttf",
948956
28,
949957
)
950958
super().__init__("fps", font)
@@ -1112,11 +1120,11 @@ async def entry_actions(self) -> None:
11121120
self.id = self.machine.new_group("title")
11131121

11141122
button_font = pygame.font.Font(
1115-
trio.Path(path.dirname(__file__), "data", "VeraSerif.ttf"),
1123+
DATA_FOLDER / "VeraSerif.ttf",
11161124
28,
11171125
)
11181126
title_font = pygame.font.Font(
1119-
trio.Path(path.dirname(__file__), "data", "VeraSerif.ttf"),
1127+
DATA_FOLDER / "VeraSerif.ttf",
11201128
56,
11211129
)
11221130

@@ -1266,7 +1274,7 @@ def __init__(self) -> None:
12661274
self.buttons: dict[tuple[str, int], int] = {}
12671275

12681276
self.font = pygame.font.Font(
1269-
trio.Path(path.dirname(__file__), "data", "VeraSerif.ttf"),
1277+
DATA_FOLDER / "VeraSerif.ttf",
12701278
28,
12711279
)
12721280

@@ -1412,7 +1420,7 @@ async def do_actions(self) -> None:
14121420
self.exit_data = (exit_status, message, True)
14131421

14141422
font = pygame.font.Font(
1415-
trio.Path(path.dirname(__file__), "data", "VeraSerif.ttf"),
1423+
DATA_FOLDER / "VeraSerif.ttf",
14161424
28,
14171425
)
14181426

@@ -1503,7 +1511,7 @@ async def async_run() -> None:
15031511
client = CheckersClient(event_manager)
15041512

15051513
background = pygame.image.load(
1506-
path.join(path.dirname(__file__), "data", "background.png"),
1514+
DATA_FOLDER / "background.png",
15071515
).convert()
15081516
client.clear(screen, background)
15091517

src/checkers/network.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@
3030
TYPE_CHECKING,
3131
Any,
3232
AnyStr,
33-
Generic,
3433
Literal,
3534
NoReturn,
36-
SupportsIndex,
37-
TypeAlias,
3835
)
3936

4037
import trio
@@ -51,23 +48,18 @@
5148
)
5249

5350
if TYPE_CHECKING:
54-
from collections.abc import Iterable
5551
from types import TracebackType
5652

5753
from typing_extensions import Self
5854

59-
BytesConvertable: TypeAlias = SupportsIndex | Iterable[SupportsIndex]
60-
else:
61-
BytesConvertable = Generic
62-
6355

6456
class NetworkTimeoutError(Exception):
6557
"""Network Timeout Error."""
6658

6759
__slots__ = ()
6860

6961

70-
class NetworkStreamNotConnectedError(RuntimeError):
62+
class NetworkStreamNotConnectedError(Exception):
7163
"""Network Stream Not Connected Error."""
7264

7365
__slots__ = ()

src/checkers/network_shared.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Programmed by CoolCat467
44

5-
# Copyright (C) 2023 CoolCat467
5+
# Copyright (C) 2023-2024 CoolCat467
66
#
77
# This program is free software: you can redistribute it and/or modify
88
# it under the terms of the GNU General Public License as published by
@@ -26,6 +26,7 @@
2626
from typing import TYPE_CHECKING, Final, NamedTuple, TypeAlias
2727

2828
import trio
29+
from mypy_extensions import u8
2930

3031
from .base_io import StructFormat
3132

@@ -37,7 +38,7 @@
3738

3839
DEFAULT_PORT: Final = 31613
3940

40-
Pos: TypeAlias = tuple[int, int]
41+
Pos: TypeAlias = tuple[u8, u8]
4142

4243

4344
class TickEventData(NamedTuple):
@@ -49,8 +50,8 @@ class TickEventData(NamedTuple):
4950

5051
def read_position(buffer: Buffer) -> Pos:
5152
"""Read a position tuple from buffer."""
52-
pos_x = buffer.read_value(StructFormat.UBYTE)
53-
pos_y = buffer.read_value(StructFormat.UBYTE)
53+
pos_x: u8 = buffer.read_value(StructFormat.UBYTE)
54+
pos_y: u8 = buffer.read_value(StructFormat.UBYTE)
5455

5556
return pos_x, pos_y
5657

src/checkers/sound.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def play_sound(
5252
"""Play sound with pygame."""
5353
sound_object = mixer.Sound(filename)
5454
sound_object.set_volume(sound_data.volume)
55-
seconds = sound_object.get_length()
55+
seconds: int | float = sound_object.get_length()
5656
if sound_data.maxtime > 0:
5757
seconds = sound_data.maxtime
5858
_channel = sound_object.play(

0 commit comments

Comments
 (0)