diff options
author | Viljami Ilola <+@hix.fi> | 2024-03-30 13:04:39 +0200 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-03-30 13:04:39 +0200 |
commit | acfac65f17a274d6d7f73cb44ed9476032241134 (patch) | |
tree | 0ef93c0d81171a6be751e3edd8d4a029f1095fcc /src/sliceitoff | |
parent | 607c00bc57fb68572754404cb5da0e7f7a5618e5 (diff) |
lint, refactor, doc, repeat
Diffstat (limited to 'src/sliceitoff')
-rw-r--r-- | src/sliceitoff/game/gameplay.py | 7 | ||||
-rw-r--r-- | src/sliceitoff/game/initials.py | 3 | ||||
-rw-r--r-- | src/sliceitoff/game/level.py | 10 | ||||
-rw-r--r-- | src/sliceitoff/game/show.py | 4 | ||||
-rw-r--r-- | src/sliceitoff/hiscores/__init__.py | 1 | ||||
-rw-r--r-- | src/sliceitoff/hiscores/hiscores.py | 23 | ||||
-rw-r--r-- | src/sliceitoff/player/__init__.py | 3 | ||||
-rw-r--r-- | 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)<MAX_HIGHSCORES: self.table+=[(0,"") for _ in range(MAX_HIGHSCORES-len(self.table))] - + def add(self, score, initials): + """ Add new high score and reranks top """ self.table.append( (score, initials) ) self.table.sort(reverse=True) self.table = self.table[:MAX_HIGHSCORES] - + def high_enough(self, score): + """ Score is enough to make high scores """ return self.table[-1][0] < score - + def __del__(self): + """ On object deletion save current high scores to config file """ oldlines=[] - if os.path.isfile(self.config_filename): - 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, _ = line.split('=') if option != 'hiscore': oldlines.append(line) - - with open(self.config_filename, 'w') as config_file: + with open(self.config_filename, 'w', encoding="utf-8") as config_file: config_file.writelines(oldlines) for score, name in self.table: config_file.write(f"hiscore={score}!{name}\n") - + def __str__(self): text = ( " " diff --git a/src/sliceitoff/player/__init__.py b/src/sliceitoff/player/__init__.py index bf1fc91..12ca32c 100644 --- a/src/sliceitoff/player/__init__.py +++ b/src/sliceitoff/player/__init__.py @@ -1 +1,2 @@ -from .player import Player
\ No newline at end of file +""" player - The slicing tool on the screen """ +from .player import Player diff --git a/src/sliceitoff/player/player.py b/src/sliceitoff/player/player.py index 002594f..0103a81 100644 --- a/src/sliceitoff/player/player.py +++ b/src/sliceitoff/player/player.py @@ -1,14 +1,14 @@ +""" player.player - Player sprite group and actions """ import os import pygame -from random import randrange - from display import Scaling from text import Fonts DEBUG = os.getenv("DEBUG") class PlayerSprite(pygame.sprite.Sprite): + """ The slicing tool. There is 2 of these. Horizontal and vertical """ def __init__(self, image): super().__init__() self.image = image @@ -19,7 +19,8 @@ class PlayerSprite(pygame.sprite.Sprite): w, h = self.image.get_size() self.rect = self.image.get_rect().move(pos[0]-w//2,pos[1]-h//2) -class Player(pygame.sprite.LayeredUpdates): +class Player(pygame.sprite.Group): + """ The slicer. Special sprite group that only list 1 sprite """ def __init__(self): super().__init__() self.position = (0,0) @@ -34,17 +35,23 @@ class Player(pygame.sprite.LayeredUpdates): ch.fill( color, special_flags = pygame.BLEND_RGBA_MULT) image.blit(ch,(0,y)) image = pygame.transform.scale_by(image, 1_200 * Scaling.factor) - self.add(PlayerSprite(image), layer = 0) image = pygame.transform.rotate(image, 90) - self.add(PlayerSprite(image), layer = 1) + self.add(PlayerSprite(image)) + image = pygame.transform.rotate(image, 90) + self.add(PlayerSprite(image)) - def update(self, pos = None, direction = False, **kwargs): + def update(self, pos = None, direction = False): + """ Updates the position and direction """ if self.lazer: direction = False pos = None - super().update(pos = pos, **kwargs) if direction: self.direction = not self.direction - self.switch_layer(0,1) if pos: self.position = Scaling.scale_to_internal(pos) + for sprite in super().sprites(): + sprite.update(pos = pos) + + def sprites(self): + """ Only list sprite of current direction for draw """ + return [super().sprites()[self.direction]] |