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 pass
def update(self) -> None: def update(self) -> None:
"""Update the button.""" pass
def draw(self, surface: pygame.Surface, pos: tuple[float, float]) -> None: def draw(self, surface: pygame.Surface, pos: tuple[float, float]) -> None:
"""Draw the button on the button surface.""" """Draw the button on the button surface."""

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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