style: cleaned up code

This commit is contained in:
Kristofers Solo 2024-01-07 19:56:11 +02:00
parent fa31b192e7
commit 89fbf2e5ab
10 changed files with 31 additions and 20 deletions

View File

@ -37,7 +37,7 @@ class Button(BaseButton, BaseScreen, SceenElement, TextScreen):
pass
def update(self) -> None:
"""Update the button."""
pass
def draw(self, surface: pygame.Surface, pos: tuple[float, float]) -> None:
"""Draw the button on the button surface."""

View File

@ -1,11 +1,8 @@
from typing import Any
import pygame
from loguru import logger
from utils import CONFIG, Figure, GameMode
from game.sprites import Tetromino
from .base import BaseScreen
from .preview import Preview
from .score import Score
@ -16,7 +13,12 @@ class Game(BaseScreen):
"""
Game class.
Args:
mode: The game mode to start with.
Attributes:
game_mode: The game mode.
settings: The game settings.
display_surface: Pygame display surface.
clock: Pygame clock.
music: Pygame music.
@ -30,7 +32,6 @@ class Game(BaseScreen):
def __init__(self, game_mode: GameMode, settings: dict[str, Any]) -> None:
self.game_mode = game_mode
self.settings = settings
self.paused = False
self._initialize_game_components()
self._start_background_music()

View File

@ -11,6 +11,13 @@ from .game import Game
class Main(BaseScreen, SceenElement, TextScreen):
"""
Main class.
Args:
mode: The game mode to start with.
"""
def __init__(self, mode: GameMode) -> None:
logger.info("Initializing the game")
self._initialize_pygame()
@ -29,9 +36,11 @@ class Main(BaseScreen, SceenElement, TextScreen):
self._draw_text()
def update(self) -> None:
"""Update the display."""
pygame.display.update()
def handle_events(self) -> None:
"""Handle pygame events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.exit()
@ -47,10 +56,12 @@ class Main(BaseScreen, SceenElement, TextScreen):
button.on_hover(event)
def run(self) -> None:
"""Run the game loop continuously."""
while True:
self.run_game_loop()
def run_game_loop(self) -> None:
"""Run a single iteration of the game loop."""
if not self.game:
self.draw()
@ -62,17 +73,19 @@ class Main(BaseScreen, SceenElement, TextScreen):
self.update()
def exit(self) -> None:
"""Exit the game."""
"""Exit."""
logger.info("Exiting the game")
pygame.quit()
sys.exit()
def play(self) -> "Main":
"""Start a the game."""
self._draw_background()
self.game = Game(self.game_mode, self.settings)
return self
def _set_buttons(self) -> None:
"""Initialize and set up buttons."""
self.buttons: list[Button] = [
Button("Play", self.play),
Button("AI", None),
@ -86,12 +99,14 @@ class Main(BaseScreen, SceenElement, TextScreen):
pygame.display.set_caption(CONFIG.window.title)
def _draw_background(self) -> None:
"""Draw the background on the display surface."""
self.display_surface.fill(CONFIG.colors.bg)
def _draw_border(self) -> None:
pass
"""Draw a border (not implemented)."""
def _initialize_surface(self) -> None:
"""Initialize the display surface."""
self.display_surface = pygame.display.set_mode(CONFIG.window.size)
def _initialize_rect(self) -> None:
@ -99,7 +114,7 @@ class Main(BaseScreen, SceenElement, TextScreen):
self.rect = self.display_surface.get_rect(topright=(0, 0))
def _update_display_surface(self) -> None:
"""Do nothing. Not needed in this class."""
"""Update display surface (not implemented)."""
def _initialize_increment_height(self) -> None:
"""Initialize the increment height for positioning text elements/buttons."""

View File

@ -8,7 +8,7 @@ from utils import CONFIG, Direction, Figure, GameMode, Rotation
from game.sprites import Block, Tetromino
from game.timer import Timer, Timers
from .base import BaseScreen, SceenElement
from .base import BaseScreen
class Tetris(BaseScreen):
@ -18,12 +18,15 @@ class Tetris(BaseScreen):
Args:
get_next_figure: A function to get the next figure.
update_score: A function to update the score.
mode: The game mode to start with.
Attributes:
surface: Surface representing the game.
dispaly_surface: Surface representing the display.
rect: Rect representing the game surface.
sprites: Sprite group for managing blocks.
settings: The game settings.
game_mode: The game mode.
get_next_figure: A function to get the next figure.
update_score: A function to update the score.
grid_surface: Surface representing the grid.

View File

@ -2,8 +2,7 @@ from typing import Any, Optional
import numpy as np
import pygame
from loguru import logger
from utils import CONFIG, Rotation, Size
from utils import CONFIG, Rotation
class Block(pygame.sprite.Sprite):

View File

@ -2,8 +2,7 @@ from typing import Any, Callable, Optional
import numpy as np
import pygame
from loguru import logger
from utils import CONFIG, Direction, Figure, Rotation, Size
from utils import CONFIG, Direction, Figure, Rotation
from .block import Block

View File

@ -1,7 +1,5 @@
from abc import ABC, ABCMeta
from attrs import define
class Color(ABC, metaclass=ABCMeta):
bg: str

View File

@ -1,10 +1,8 @@
import random
from enum import Enum
from pathlib import Path
from typing import NamedTuple
import pygame
from attr import define
from pygame import Vector2 as Vec2
from .colors import TokyoNightNight

View File

@ -1,5 +1,5 @@
from pathlib import Path
from typing import Any, Optional
from typing import Any
import toml
from loguru import logger

View File

@ -1,7 +1,5 @@
from typing import NamedTuple, Union
from .enum import Direction
class Size(NamedTuple):
width: int | float