refactor(game): move grid_pos to utils.py file

This commit is contained in:
Kristofers Solo 2023-12-29 23:56:47 +02:00
parent ef09979dba
commit d2d5f9485b
2 changed files with 10 additions and 8 deletions

View File

@ -8,12 +8,7 @@ from loguru import logger
from .colors import COLORS from .colors import COLORS
from .config import Config from .config import Config
from .utils import Direction from .utils import Direction, grid_pos
def _grid_pos(pos: int) -> int:
"""Return the position in the grid."""
return pos // Config.BLOCK_SIZE + 1
class Block(pygame.sprite.Sprite): class Block(pygame.sprite.Sprite):
@ -52,7 +47,7 @@ class Block(pygame.sprite.Sprite):
return return
self.rect.topleft = new_x, new_y self.rect.topleft = new_x, new_y
logger.debug(f"Moving block({id(self)}): {self.pos()} => ({_grid_pos(new_x)}, {_grid_pos(new_y)})") logger.debug(f"Moving block({id(self)}): {self.pos()} => ({grid_pos(new_x)}, {grid_pos(new_y)})")
def _calc_new_pos(self, direction: Direction) -> tuple[int, int]: def _calc_new_pos(self, direction: Direction) -> tuple[int, int]:
"""Calculate the new position of the block.""" """Calculate the new position of the block."""
@ -109,4 +104,4 @@ class Block(pygame.sprite.Sprite):
def pos(self) -> tuple[int, int]: def pos(self) -> tuple[int, int]:
"""Return the position of the block""" """Return the position of the block"""
return _grid_pos(self.rect.x), _grid_pos(self.rect.y) return grid_pos(self.rect.x), grid_pos(self.rect.y)

View File

@ -1,5 +1,12 @@
from enum import Enum from enum import Enum
from .config import Config
def grid_pos(pos: int) -> int:
"""Return the position in the grid."""
return pos // Config.BLOCK_SIZE + 1
class Direction(Enum): class Direction(Enum):
UP = (0, -1) UP = (0, -1)