mirror of
https://github.com/kristoferssolo/Tetris.git
synced 2025-10-21 20:00:35 +00:00
feat(game): add rotation direction
style: change docstrings
This commit is contained in:
parent
b4157ef8b0
commit
1ead412528
@ -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:
|
||||
"""
|
||||
|
||||
@ -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]
|
||||
|
||||
@ -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):
|
||||
|
||||
@ -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",
|
||||
]
|
||||
|
||||
@ -8,6 +8,11 @@ class Direction(Enum):
|
||||
UP = -1
|
||||
|
||||
|
||||
class Rotation(Enum):
|
||||
CLOCKWISE = 90
|
||||
COUNTER_CLOCKWISE = -90
|
||||
|
||||
|
||||
class Field(Enum):
|
||||
EMPTY = None
|
||||
FILLED = "Block"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user