diff --git a/src/game/block.py b/src/game/block.py index b465e3c..56c5b41 100644 --- a/src/game/block.py +++ b/src/game/block.py @@ -1,6 +1,6 @@ import numpy as np import pygame -from utils import CONFIG, Size +from utils import CONFIG, Rotation, Size class Block(pygame.sprite.Sprite): @@ -60,17 +60,18 @@ class Block(pygame.sprite.Sprite): """ return y >= CONFIG.game.rows or (y >= 0 and field[y, int(self.pos.x)]) - def rotate(self, pivot: pygame.Vector2) -> pygame.Vector2: + def rotate(self, pivot: pygame.Vector2, rotation: Rotation) -> pygame.Vector2: """ Rotates the block around a given pivot point. Args: pivot: The pivot point for rotation. + rotation: The rotation direction. Returns: The new position of the block after rotation. """ - return pivot + (self.pos - pivot).rotate(90) + return pivot + (self.pos - pivot).rotate(rotation.value) def _initialize_image(self, color: str) -> None: """ diff --git a/src/game/game.py b/src/game/game.py index 64e1b1c..2b1c1ff 100644 --- a/src/game/game.py +++ b/src/game/game.py @@ -2,7 +2,7 @@ from typing import Callable, Optional import numpy as np import pygame -from utils import CONFIG, Direction, Field, Figure +from utils import CONFIG, Direction, Field, Figure, Rotation from .block import Block from .log import log @@ -287,9 +287,14 @@ class Game: self.surface.fill(CONFIG.colors.bg_float) def _handle_movement_keys(self, keys: list[bool]) -> None: - """Handle movement keys [K_LEFT, K_RIGHT, K_a, K_d, K_h, K_l].""" - left_keys = keys[pygame.K_LEFT] or keys[pygame.K_a] or keys[pygame.K_h] - right_keys = keys[pygame.K_RIGHT] or keys[pygame.K_d] or keys[pygame.K_l] + """ + Handle movement keys. + + Move right [K_d, K_l]. + Move left [K_a, K_h]. + """ + right_keys = keys[pygame.K_d] or keys[pygame.K_l] + left_keys = keys[pygame.K_a] or keys[pygame.K_h] if not self.timers.horizontal.active: if left_keys: @@ -300,18 +305,32 @@ class Game: self.timers.horizontal.activate() def _handle_rotation_keys(self, keys: list[bool]) -> None: - """Handle rotation keys [K_r, K_UP, K_w, K_k].""" - rotate_keys = ( + """ + Handle rotation keys. + + Rotation clockwise [K_RIGHT, K_UP, K_r, K_w, K_k]. + Rotation counter-clockwise [K_LEFT, K_e, K_i]. + """ + clockwise_keys = ( keys[pygame.K_r] or keys[pygame.K_UP] or keys[pygame.K_w] or keys[pygame.K_k] + or keys[pygame.K_RIGHT] + ) + + counter_clockwise_keys = ( + keys[pygame.K_e] or keys[pygame.K_i] or keys[pygame.K_LEFT] ) if not self.timers.rotation.active: - if rotate_keys: + if clockwise_keys: self.tetromino.rotate() self.timers.rotation.activate() + if counter_clockwise_keys: + self.tetromino.rotate(Rotation.COUNTER_CLOCKWISE) + self.timers.rotation.activate() + def _handle_down_key(self, keys: list[bool]) -> None: """Handle the down key [K_DOWN, K_s, K_j].""" down_keys = keys[pygame.K_DOWN] or keys[pygame.K_s] or keys[pygame.K_j] diff --git a/src/game/tetromino.py b/src/game/tetromino.py index 13f10ef..f2cd75a 100644 --- a/src/game/tetromino.py +++ b/src/game/tetromino.py @@ -2,7 +2,7 @@ from typing import Callable, Optional import numpy as np import pygame -from utils import CONFIG, Direction, Figure, Size +from utils import CONFIG, Direction, Figure, Rotation, Size from .block import Block from .log import log @@ -66,11 +66,14 @@ class Tetromino: for block in self.blocks: block.pos.x += direction.value - def rotate(self) -> None: + def rotate(self, rotation: Rotation = Rotation.CLOCKWISE) -> None: """ Rotates the Tetromino clockwise. Does not rotate if the Tetromino is an O-shaped (square) figure. + + Args: + rotation: Rotation to perform (CLOCKWISE or COUNTER_CLOCKWISE). """ if self.figure == Figure.O: return @@ -78,7 +81,7 @@ class Tetromino: pivot: pygame.Vector2 = self.blocks[0].pos new_positions: list[pygame.Vector2] = [ - block.rotate(pivot) for block in self.blocks + block.rotate(pivot, rotation) for block in self.blocks ] if self._are_new_positions_valid(new_positions): diff --git a/src/utils/__init__.py b/src/utils/__init__.py index 954d5af..e93aab5 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -1,5 +1,5 @@ from .config import CONFIG -from .enum import Direction, Field +from .enum import Direction, Field, Rotation from .figure import Figure, FigureConfig from .log import log from .path import BASE_PATH @@ -14,4 +14,5 @@ __all__ = [ "FigureConfig", "Direction", "Field", + "Rotation", ] diff --git a/src/utils/enum.py b/src/utils/enum.py index bff8c2a..c9bad06 100644 --- a/src/utils/enum.py +++ b/src/utils/enum.py @@ -8,6 +8,11 @@ class Direction(Enum): UP = -1 +class Rotation(Enum): + CLOCKWISE = 90 + COUNTER_CLOCKWISE = -90 + + class Field(Enum): EMPTY = None FILLED = "Block"