From b802a63651cf4500988a57a53bea7dcddf76d441 Mon Sep 17 00:00:00 2001 From: Vlad <89295404+Virashu@users.noreply.github.com> Date: Thu, 15 Feb 2024 16:17:09 +0300 Subject: [PATCH] Types --- danmaku/database/database.py | 2 +- danmaku/ui/button.py | 21 ++++++++++++++------- danmaku/ui/game_end.py | 5 +++-- danmaku/ui/history.py | 2 +- danmaku/ui/menu.py | 2 ++ 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/danmaku/database/database.py b/danmaku/database/database.py index eadea02..e4e0594 100644 --- a/danmaku/database/database.py +++ b/danmaku/database/database.py @@ -94,7 +94,7 @@ def get_saved_game() -> dict: return game -def get_game_history() -> list: +def get_game_history() -> list[dict[str, int]]: """Get game history from database Returns list: [{"score", "level", "time"}] """ diff --git a/danmaku/ui/button.py b/danmaku/ui/button.py index 7b7c458..854628d 100644 --- a/danmaku/ui/button.py +++ b/danmaku/ui/button.py @@ -1,12 +1,13 @@ """Menu button class declaration.""" from dataclasses import dataclass +from typing import Any import pygame -import vgame +from vgame.graphics import Graphics, Sprite -class ClickableButton(vgame.graphics.Sprite): +class ClickableButton(Sprite): """Menu button class clickable by mouse""" def __init__( @@ -24,7 +25,7 @@ def __init__( self.text = text self.font_size = font_size - def draw(self, graphics: vgame.graphics.Graphics): + def draw(self, graphics: Graphics): graphics.rectangle(self.rect.topleft, self.rect.size, self.button_color) graphics.text(f" {self.text} ", self.rect.topleft, self.text_color) @@ -43,10 +44,10 @@ class Button: codename: str -class Cursor(vgame.graphics.Sprite): +class Cursor(Sprite): """Cursor class""" - def __init__(self, xy): + def __init__(self, xy: tuple[int, int]): super().__init__() self.x, self.y = xy self.width, self.height = 42, 40 @@ -61,12 +62,16 @@ def update(self, delta: int | float): int(self.height), ) - def draw(self, graphics: vgame.graphics.Graphics): + def draw(self, graphics: Graphics): graphics.draw_sprite(self) class SettingsValue: - def __init__(self, name: str, display_name: str, possible_values, value) -> None: + """An interactive value bar for settings""" + + def __init__( + self, name: str, display_name: str, possible_values: tuple[Any], value: Any + ) -> None: self.codename = name self.text = display_name self.possible_values = possible_values @@ -74,9 +79,11 @@ def __init__(self, name: str, display_name: str, possible_values, value) -> None self.selection_index = self.possible_values.index(self.value) def increase(self): + """Select next value""" self.selection_index = (self.selection_index + 1) % len(self.possible_values) self.value = self.possible_values[self.selection_index] def decrease(self): + """Select previous value""" self.selection_index = (self.selection_index - 1) % len(self.possible_values) self.value = self.possible_values[self.selection_index] diff --git a/danmaku/ui/game_end.py b/danmaku/ui/game_end.py index 581c011..0821ec9 100644 --- a/danmaku/ui/game_end.py +++ b/danmaku/ui/game_end.py @@ -18,14 +18,15 @@ def load(self): pygame.mixer.music.play() def set(self, text: str, background: tuple[int, int, int], music: str): + """Set end screen attributes""" self.text = text if "win" in text: self.delta_color = 2 else: self.delta_color = 0 self.background_color = background - self.text_color = list(background) - self.v = (255 - self.text_color[self.delta_color]) / 500 + self.text_color: list[int] = list(background) + self.v = (255 - self.text_color[self.delta_color]) // 500 self.music = music def update(self): diff --git a/danmaku/ui/history.py b/danmaku/ui/history.py index 3d981ef..fa63702 100644 --- a/danmaku/ui/history.py +++ b/danmaku/ui/history.py @@ -10,7 +10,7 @@ class History(vgame.Scene): def load(self): self.selection_index = 0 - self.history = get_game_history() + self.history: list[dict[str, int]] = get_game_history() self.record_count = len(self.history) def update(self): diff --git a/danmaku/ui/menu.py b/danmaku/ui/menu.py index acf7098..a0a79fa 100644 --- a/danmaku/ui/menu.py +++ b/danmaku/ui/menu.py @@ -64,6 +64,8 @@ def update(self): case "quit": # Maybe rework to quit through exit status pygame.event.post(pygame.event.Event(pygame.constants.QUIT)) + case _: + pass self.cursor.y = 100 + self.selection_index * 50 self.cursor.update(self.delta)