From 7779e857e040733e04c95ab0b129693170df4db8 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Mon, 8 Nov 2021 22:17:14 +0100 Subject: Add type hints for screen package Signed-off-by: Olivier Gayot --- pycameltris/screens/InGame.py | 58 +++++++++++++++++++++---------------------- pycameltris/screens/Pause.py | 14 +++++------ pycameltris/screens/Screen.py | 4 +-- 3 files changed, 38 insertions(+), 38 deletions(-) (limited to 'pycameltris/screens') diff --git a/pycameltris/screens/InGame.py b/pycameltris/screens/InGame.py index 7363f5a..05c4b2d 100644 --- a/pycameltris/screens/InGame.py +++ b/pycameltris/screens/InGame.py @@ -2,13 +2,13 @@ import contextlib from functools import partial import random import sys -from typing import NoReturn +from typing import Callable, NoReturn, Optional import pygame from .Screen import Screen from ..piece import * -from ..controller import Input, KeyboardController, JoystickController +from ..controller import Input, Controller, KeyboardController, JoystickController from ..misc import Pause @@ -22,7 +22,7 @@ right_pane_canvas = pygame.Surface((300, 1000)) right_pane_canvas.fill((255, 255, 255)) class Player: - def __init__(self, controller, starting_level): + def __init__(self, controller: Controller, starting_level: int): self.controller = controller self.grid = [[None for _ in range(10)] for _ in range(20)] @@ -35,7 +35,7 @@ class Player: self.lines_burnt = 0 self.das = 0 - self.pressing_down_countdown = None + self.pressing_down_countdown: Optional[int] = None self.piece_drop_frames = 0 @@ -45,7 +45,7 @@ class Player: self.level_canvas = pygame.Surface((296, 50)) - def generate_piece(self): + def generate_piece(self) -> tuple[Piece, list[int]]: # We may want to make this a function outside the class piece = random.choice((TPiece, SPiece, IPiece, ZPiece, SquarePiece, LPiece, JPiece))() @@ -58,7 +58,7 @@ class Player: return (piece, [initial_y_position, initial_x_position]) - def burn_rows(self): + def burn_rows(self) -> int: rows_to_burn = list() for row in self.grid: @@ -71,7 +71,7 @@ class Player: return len(rows_to_burn) - def lock_piece(self): + def lock_piece(self) -> None: if self.has_collision(self.current_piece_position[0], self.current_piece_position[1]): raise WouldCollide() @@ -85,7 +85,7 @@ class Player: if count == 1: print("Single") - rate = 1 + rate = 1. elif count == 2: print("Double") rate = 2.5 @@ -94,9 +94,9 @@ class Player: rate = 7.5 elif count == 4: print("Tetris!") - rate = 30 + rate = 30. else: - rate = 0 + rate = 0. self.lines_burnt += count @@ -129,45 +129,45 @@ class Player: return False - def move_piece_down(self): + def move_piece_down(self) -> None: if not self.has_collision(self.current_piece_position[0] + 1, self.current_piece_position[1]): self.current_piece_position[0] += 1 else: raise WouldCollide() - def move_piece_up(self): + def move_piece_up(self) -> None: if not self.has_collision(self.current_piece_position[0] - 1, self.current_piece_position[1]): self.current_piece_position[0] -= 1 else: raise WouldCollide() - def move_piece_left(self): + def move_piece_left(self) -> None: if not self.has_collision(self.current_piece_position[0], self.current_piece_position[1] - 1): self.current_piece_position[1] -= 1 else: raise WouldCollide() - def move_piece_right(self): + def move_piece_right(self) -> None: if not self.has_collision(self.current_piece_position[0], self.current_piece_position[1] + 1): self.current_piece_position[1] += 1 else: raise WouldCollide() - def rotate_piece_counter_clockwise(self): + def rotate_piece_counter_clockwise(self) -> None: self.current_piece.rotate_counter_clockwise() if self.has_collision(self.current_piece_position[0], self.current_piece_position[1]): self.current_piece.rotate_clockwise() raise WouldCollide() - def rotate_piece_clockwise(self): + def rotate_piece_clockwise(self) -> None: self.current_piece.rotate_clockwise() if self.has_collision(self.current_piece_position[0], self.current_piece_position[1]): self.current_piece.rotate_counter_clockwise() raise WouldCollide() - def handle_input_pressed(self, event): + def handle_input_pressed(self, event: pygame.event.Event) -> None: if self.controller.get_input_down(event) == Input.QUIT: raise PlayerQuit() @@ -193,11 +193,11 @@ class Player: except WouldCollide: self.lock_piece() - def handle_input_released(self, event): + def handle_input_released(self, event: pygame.event.Event) -> None: if self.controller.get_input_up(event) == Input.MOVE_DOWN: self.pressing_down_countdown = None - def refresh_piece_preview_canvas(self): + def refresh_piece_preview_canvas(self) -> None: self.piece_preview_canvas.fill(black) non_empty_rows = list() @@ -220,7 +220,7 @@ class Player: if element is not None: self.piece_preview_canvas.blit(element, ((col_idx + x_offset) * 50 + 1, (row_idx + y_offset) * 50 + 1)) - def refresh_grid_canvas(self): + def refresh_grid_canvas(self) -> None: self.grid_canvas.fill(black) for row_idx, row in enumerate(self.grid): @@ -234,13 +234,13 @@ class Player: if element is not None: self.grid_canvas.blit(element, ((col_idx + self.current_piece_position[1]) * 50 + 1, (row_idx + self.current_piece_position[0]) * 50 + 1)) -def handle_input_pressed(instance, players, event): +def handle_input_pressed(instance, players: list[Player], event: pygame.event.Event) -> None: for player in players: if isinstance(player.controller, instance): player.handle_input_pressed(event) -def handle_input_released(instance, players, event): +def handle_input_released(instance, players: list[Player], event: pygame.event.Event) -> None: for player in players: if isinstance(player.controller, instance): player.handle_input_released(event) @@ -250,10 +250,10 @@ frames_per_gridcell = [48, 43, 38, 33, 28, 23, 18, 13, 8, 6, 5, 5, 5, 4, 4, 4, 3 class InGame(Screen): - def __init__(self, players, screen): - self.players = players - self.screen = screen - self.event_handler = dict() + def __init__(self, players: list[Player], screen: pygame.Surface): + self.players: list[Player] = players + self.screen: pygame.Surface = screen + self.event_handler: dict[int, Callable[[pygame.event.Event], None]] = {} def exit(_) -> NoReturn: sys.exit() @@ -264,7 +264,7 @@ class InGame(Screen): self.event_handler[pygame.JOYBUTTONDOWN] = partial(handle_input_pressed, JoystickController, self.players) self.event_handler[pygame.JOYBUTTONUP] = partial(handle_input_released, JoystickController, self.players) - def refresh_right_pane_canvas(self): + def refresh_right_pane_canvas(self) -> None: for player in self.players: player.level_canvas.fill(black) player.score_canvas.fill(black) @@ -280,14 +280,14 @@ class InGame(Screen): right_pane_canvas.blit(player.piece_preview_canvas, (50, 200)) - def refresh(self): + def refresh(self) -> None: for player in self.players: player.refresh_grid_canvas() self.screen.blit(player.grid_canvas, (0, 0)) self.refresh_right_pane_canvas() self.screen.blit(right_pane_canvas, (501, 0)) - def oneframe(self): + def oneframe(self) -> None: for player in self.players: player.piece_drop_frames += 1 diff --git a/pycameltris/screens/Pause.py b/pycameltris/screens/Pause.py index 1502be7..885b7f6 100644 --- a/pycameltris/screens/Pause.py +++ b/pycameltris/screens/Pause.py @@ -1,22 +1,22 @@ import contextlib import sys -from typing import NoReturn +from typing import Callable, NoReturn import pygame from .Screen import Screen -from ..controller import KeyboardController, JoystickController, Input +from ..controller import Controller, KeyboardController, JoystickController, Input from ..misc import UnPause -def handle_input_pressed(controller, event): +def handle_input_pressed(controller: Controller, event: pygame.event.Event): if controller.get_input_down(event) == Input.PAUSE: raise UnPause() class Pause(Screen): - def __init__(self, screen): - self.event_handler = dict() + def __init__(self, screen: pygame.Surface): + self.event_handler: dict[int, Callable[[pygame.event.Event], None]] = {} def exit(_) -> NoReturn: sys.exit() @@ -26,10 +26,10 @@ class Pause(Screen): self.event_handler[pygame.KEYDOWN] = lambda evt: handle_input_pressed(KeyboardController(pygame.key), evt) - def refresh(self): + def refresh(self) -> None: pass - def oneframe(self): + def oneframe(self) -> None: try: for event in pygame.event.get(): with contextlib.suppress(KeyError): diff --git a/pycameltris/screens/Screen.py b/pycameltris/screens/Screen.py index 01f80ab..419c1d2 100644 --- a/pycameltris/screens/Screen.py +++ b/pycameltris/screens/Screen.py @@ -3,10 +3,10 @@ import abc class Screen(abc.ABC): @abc.abstractmethod - def refresh(self): + def refresh(self) -> None: pass @abc.abstractmethod - def oneframe(self): + def oneframe(self) -> None: pass -- cgit v1.2.3