From acfac65f17a274d6d7f73cb44ed9476032241134 Mon Sep 17 00:00:00 2001 From: Viljami Ilola <+@hix.fi> Date: Sat, 30 Mar 2024 13:04:39 +0200 Subject: lint, refactor, doc, repeat --- src/sliceitoff/game/gameplay.py | 7 +++---- src/sliceitoff/game/initials.py | 3 ++- src/sliceitoff/game/level.py | 10 +++------- src/sliceitoff/game/show.py | 4 ++-- src/sliceitoff/hiscores/__init__.py | 1 + src/sliceitoff/hiscores/hiscores.py | 23 ++++++++++++++--------- src/sliceitoff/player/__init__.py | 3 ++- src/sliceitoff/player/player.py | 23 +++++++++++++++-------- 8 files changed, 42 insertions(+), 32 deletions(-) diff --git a/src/sliceitoff/game/gameplay.py b/src/sliceitoff/game/gameplay.py index ef708c4..5a14767 100644 --- a/src/sliceitoff/game/gameplay.py +++ b/src/sliceitoff/game/gameplay.py @@ -7,15 +7,13 @@ class Gameplay: self, player = None, field = None, - status = None, enemies = None, stats = None): - self.status = status self.player = player self.field = field self.enemies = enemies self.stats = stats - + def fire(self): """ Lazer is fired. Actions to be taken. """ zap_sprite = self.field.slice( @@ -33,9 +31,10 @@ class Gameplay: return self.stats.percent < 20 def quit(self): + """ Lose lives so no leveling up """ self.stats.lives = 0 return True - + def step(self): """ Processes events for the step (frame) """ for event in pygame.event.get(): diff --git a/src/sliceitoff/game/initials.py b/src/sliceitoff/game/initials.py index 49db3d6..7811afa 100644 --- a/src/sliceitoff/game/initials.py +++ b/src/sliceitoff/game/initials.py @@ -14,8 +14,9 @@ class Initials(pygame.sprite.Group): self.explode = False self.active = True self.fadeout = 1_000 - + def update(self, dt = 0, **kwargs): + """ Does it all. Reads keyboard and updates screen """ if not self.active: return diff --git a/src/sliceitoff/game/level.py b/src/sliceitoff/game/level.py index 26df345..0742213 100644 --- a/src/sliceitoff/game/level.py +++ b/src/sliceitoff/game/level.py @@ -20,7 +20,6 @@ class Level: self.field = Field(stats = self.stats) self.enemies = Enemies(count = self.stats.enemies) self.player = Player() - self.player_single = pygame.sprite.Group() self.level_info = Show(level_screen(stats.level)) self.ended = False @@ -28,7 +27,6 @@ class Level: self.gameplay = Gameplay( player = self.player, - status = self.status, field = self.field, enemies = self.enemies, stats = self.stats) @@ -36,7 +34,8 @@ class Level: self.status, self.field, self.enemies, - self.level_info] + self.level_info, + self.player] def step(self, dt): self.status.update(dt = dt) @@ -53,11 +52,8 @@ class Level: return False else: - self.player_single = pygame.sprite.GroupSingle( - sprite = self.player.get_top_sprite()) self.stats.update_bonus(dt) if self.gameplay.step(): - self.player_single = pygame.sprite.Group() self.ended = True if self.stats.lives: self.endscreen = Show(levelup_screen(self.stats)) @@ -65,6 +61,6 @@ class Level: self.endscreen = Show(gameover_screen()) self.obj_classes.append(self.endscreen) - self.display.update(self.obj_classes+[self.player_single]) + self.display.update(self.obj_classes) return True diff --git a/src/sliceitoff/game/show.py b/src/sliceitoff/game/show.py index b9b684b..94e2540 100644 --- a/src/sliceitoff/game/show.py +++ b/src/sliceitoff/game/show.py @@ -11,7 +11,7 @@ class Show(pygame.sprite.Group): self.explode = False self.fadeout = 1_000 self.timeout = 15_000 - + def update(self, dt = 0, **kwargs): """ First timeout then fadeout and then inactivity """ super().update(dt = dt, explode = self.explode, **kwargs) @@ -29,4 +29,4 @@ class Show(pygame.sprite.Group): def sprites(self): """ Return sprites only when active """ - return super().sprites() if self.active else [] \ No newline at end of file + return super().sprites() if self.active else [] diff --git a/src/sliceitoff/hiscores/__init__.py b/src/sliceitoff/hiscores/__init__.py index f3d7811..fdad3e5 100644 --- a/src/sliceitoff/hiscores/__init__.py +++ b/src/sliceitoff/hiscores/__init__.py @@ -1 +1,2 @@ +""" highscores - High score storing and listing """ from .hiscores import HiScores diff --git a/src/sliceitoff/hiscores/hiscores.py b/src/sliceitoff/hiscores/hiscores.py index 2725931..1b3b87f 100644 --- a/src/sliceitoff/hiscores/hiscores.py +++ b/src/sliceitoff/hiscores/hiscores.py @@ -1,9 +1,12 @@ +""" hiscores.hiscores - high socres: loading, saving, converting to string""" import os MAX_HIGHSCORES = 20 class HiScores: + """ Keeps track of high scores """ def __init__(self): + """ On creation load high scores from config file """ self.table=[] self.config_filename = os.path.join( os.getenv('HOME'), @@ -12,7 +15,7 @@ class HiScores: if not os.path.isfile(self.config_filename): self.table=[(0,"") for _ in range(MAX_HIGHSCORES)] return - with open(self.config_filename, "r") as config_file: + with open(self.config_filename, "r", encoding="utf-8") as config_file: for line in config_file: option, value = line.split('=') if option == 'hiscore': @@ -20,30 +23,32 @@ class HiScores: self.add(int(score.strip()),name.strip()) if len(self.table)