refactor(game): use Direction enum

This commit is contained in:
Kristofers Solo 2024-01-04 02:59:50 +02:00
parent 86d7899e1f
commit 684f5ba196
5 changed files with 18 additions and 10 deletions

View File

@ -1,5 +1,5 @@
import pygame import pygame
from utils import CONFIG, Figure from utils import CONFIG, Direction, Figure
from .log import log from .log import log
from .tetromino import Tetromino from .tetromino import Tetromino
@ -57,10 +57,10 @@ class Game:
self.tetromino.move_down() self.tetromino.move_down()
def move_left(self) -> None: def move_left(self) -> None:
self.tetromino.move_left() self.tetromino.move_horizontal(Direction.LEFT)
def move_right(self) -> None: def move_right(self) -> None:
self.tetromino.move_right() self.tetromino.move_horizontal(Direction.RIGHT)
def _create_grid_surface(self) -> None: def _create_grid_surface(self) -> None:
self.grid_surface = self.surface.copy() self.grid_surface = self.surface.copy()
@ -100,5 +100,5 @@ class Game:
) )
def _timer_update(self) -> None: def _timer_update(self) -> None:
for timer in self.timers.values(): for timer in self.timers:
timer.update() timer.update()

View File

@ -1,7 +1,7 @@
from typing import Optional from typing import Optional
import pygame import pygame
from utils import CONFIG, Figure, FigureConfig, Size from utils import CONFIG, Direction, Figure, FigureConfig, Size
from .block import Block from .block import Block
@ -23,10 +23,10 @@ class Tetromino:
for block in self.blocks: for block in self.blocks:
block.pos.y += 1 block.pos.y += 1
def move_left(self) -> None: def move_horizontal(self, direction: Direction) -> None:
for block in self.blocks: for block in self.blocks:
block.pos.x -= 1 block.pos.x += direction.value
def move_right(self) -> None: def next_move_horizontal_collide(self, block: Block, direction: Direction) -> None:
for block in self.blocks: for block in self.blocks:
block.pos.x += 1 block.pos.x += direction.value

View File

@ -1,4 +1,5 @@
from .config import CONFIG from .config import CONFIG
from .direction import Direction
from .figure import Figure, FigureConfig from .figure import Figure, FigureConfig
from .log import log from .log import log
from .path import BASE_PATH from .path import BASE_PATH
@ -11,4 +12,5 @@ __all__ = [
"Size", "Size",
"Figure", "Figure",
"FigureConfig", "FigureConfig",
"Direction",
] ]

View File

@ -18,7 +18,7 @@ class Game:
size: Size = Size(columns * cell.width, rows * cell.width) size: Size = Size(columns * cell.width, rows * cell.width)
pos: Vec2 = Vec2(padding, padding) pos: Vec2 = Vec2(padding, padding)
offset: Vec2 = Vec2(columns // 2, -1) offset: Vec2 = Vec2(columns // 2, -1)
initial_speed: int = 200 initial_speed: int = 400
movment_delay: int = 200 movment_delay: int = 200
rotation_delay: int = 200 rotation_delay: int = 200

6
src/utils/direction.py Normal file
View File

@ -0,0 +1,6 @@
from enum import Enum
class Direction(Enum):
LEFT = -1
RIGHT = 1