diff options
author | Viljami Ilola <+@hix.fi> | 2024-03-30 13:58:02 +0200 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-03-30 13:58:02 +0200 |
commit | 5874005e58d0f26b4393845b3b18160658ece160 (patch) | |
tree | 428f315dbd99e3f77677107586cac493d000cac7 /src/sliceitoff | |
parent | acfac65f17a274d6d7f73cb44ed9476032241134 (diff) |
refactor game.level
Diffstat (limited to 'src/sliceitoff')
-rw-r--r-- | src/sliceitoff/game/__init__.py | 1 | ||||
-rw-r--r-- | src/sliceitoff/game/game.py | 25 | ||||
-rw-r--r-- | src/sliceitoff/game/level.py | 38 | ||||
-rw-r--r-- | src/sliceitoff/game/show.py | 3 | ||||
-rw-r--r-- | src/sliceitoff/player/player.py | 9 |
5 files changed, 36 insertions, 40 deletions
diff --git a/src/sliceitoff/game/__init__.py b/src/sliceitoff/game/__init__.py index a2771db..7785623 100644 --- a/src/sliceitoff/game/__init__.py +++ b/src/sliceitoff/game/__init__.py @@ -1 +1,2 @@ +""" game - All the game logic and user inputs """ from .game import Game diff --git a/src/sliceitoff/game/game.py b/src/sliceitoff/game/game.py index bcb04e7..b7d5212 100644 --- a/src/sliceitoff/game/game.py +++ b/src/sliceitoff/game/game.py @@ -17,6 +17,7 @@ from .show import Show from .initials import Initials class Game: + """ This is the whole game. """ def __init__(self): pygame.init() self.clock = pygame.time.Clock() @@ -26,46 +27,46 @@ class Game: Fonts.load_fonts( Path(__file__).parent.parent.resolve() ) pygame.mouse.set_visible(False) + def __del__(self): + pygame.quit() + def welcome(self): + """ displays instruction and waits a key """ ws = Show(welcome_screen()) - dt = 0 while ws.active: ws.update(dt = self.clock.tick()) self.display.update( [ws] ) def show_highscores(self): + """ displays highscores and waits a key """ his = Show(hiscores_screen(str(self.hiscores))) - dt = 0 while his.active: his.update(dt = self.clock.tick()) self.display.update( [his] ) def newgame(self): + """ new game, new score, runs through levels till game over """ self.stats = Stats() while self.stats.lives: - level = Level(display = self.display, stats = self.stats) - dt = 0 - while level.step(dt): - dt = self.clock.tick() + level = Level(stats = self.stats) + while level.active: + level.update(dt = self.clock.tick()) + self.display.update( [level] ) if self.stats.lives: self.stats.level_up() def initials(self): + """ asks for initials in case of high enough score """ initials = Initials() - dt = 0 while initials.active: initials.update(dt = self.clock.tick()) self.display.update([initials]) return initials.name def run(self): + """ This is the main loop of the game (not loop at the moment) """ self.welcome() self.newgame() if self.hiscores.high_enough(self.stats.score): self.hiscores.add( self.stats.score, self.initials()) self.show_highscores() - - - def __del__(self): - pygame.quit() - diff --git a/src/sliceitoff/game/level.py b/src/sliceitoff/game/level.py index 0742213..085cbc9 100644 --- a/src/sliceitoff/game/level.py +++ b/src/sliceitoff/game/level.py @@ -1,28 +1,26 @@ +""" game.level - This is what runs invidual levels """ import pygame -from display import Display from status import Status from player import Player from field import Field from enemies import Enemies from screens import levelup_screen, gameover_screen, level_screen from .gameplay import Gameplay -from .anykey import anykey from .show import Show - -class Level: +class Level(pygame.sprite.Group): """ One level that can be played """ - def __init__(self, display = None, stats = None): - self.display = display + def __init__(self, stats = None): + super().__init__() self.stats = stats self.status = Status(stats = self.stats) self.field = Field(stats = self.stats) self.enemies = Enemies(count = self.stats.enemies) self.player = Player() self.level_info = Show(level_screen(stats.level)) - self.ended = False + self.active = True self.endscreen = None self.gameplay = Gameplay( @@ -30,28 +28,27 @@ class Level: field = self.field, enemies = self.enemies, stats = self.stats) - self.obj_classes = [ - self.status, - self.field, - self.enemies, - self.level_info, - self.player] - - def step(self, dt): + + def update(self, dt = 0): + """ Updates groups, calls gameplay and adds sprites for drawing """ + self.empty() self.status.update(dt = dt) self.field.update(dt = dt) - self.enemies.update(dt = dt, field_rects = self.field.active_rects() ) + self.enemies.update(dt = dt, field_rects = self.field.active_rects()) + self.add(self.status, self.field, self.enemies) if self.level_info.active: self.level_info.update(dt = dt) + self.add(self.level_info) elif self.ended: if self.endscreen.active: self.endscreen.update(dt = dt) + self.add(self.endscreen) else: - return False - + self.active = False else: + self.add(self.player) self.stats.update_bonus(dt) if self.gameplay.step(): self.ended = True @@ -59,8 +56,3 @@ class Level: self.endscreen = Show(levelup_screen(self.stats)) else: self.endscreen = Show(gameover_screen()) - self.obj_classes.append(self.endscreen) - - self.display.update(self.obj_classes) - - return True diff --git a/src/sliceitoff/game/show.py b/src/sliceitoff/game/show.py index 94e2540..0fb6e05 100644 --- a/src/sliceitoff/game/show.py +++ b/src/sliceitoff/game/show.py @@ -14,7 +14,6 @@ class Show(pygame.sprite.Group): def update(self, dt = 0, **kwargs): """ First timeout then fadeout and then inactivity """ - super().update(dt = dt, explode = self.explode, **kwargs) if self.fadeout <= 0: self.active = False elif self.timeout <= 0: @@ -26,6 +25,8 @@ class Show(pygame.sprite.Group): if anykey(): self.timeout = 0 self.timeout -= dt + for sprite in self.sprites(): + sprite.update(dt = dt, explode = self.explode, **kwargs) def sprites(self): """ Return sprites only when active """ diff --git a/src/sliceitoff/player/player.py b/src/sliceitoff/player/player.py index 0103a81..72c5b2f 100644 --- a/src/sliceitoff/player/player.py +++ b/src/sliceitoff/player/player.py @@ -13,8 +13,9 @@ class PlayerSprite(pygame.sprite.Sprite): super().__init__() self.image = image self.rect = self.image.get_rect() - - def update(self, pos = None, **kwargs): + + def update(self, pos = None): + """ Sets sprite center at given position """ if pos: w, h = self.image.get_size() self.rect = self.image.get_rect().move(pos[0]-w//2,pos[1]-h//2) @@ -49,8 +50,8 @@ class Player(pygame.sprite.Group): self.direction = not self.direction if pos: self.position = Scaling.scale_to_internal(pos) - for sprite in super().sprites(): - sprite.update(pos = pos) + for sprite in super().sprites(): + sprite.update(pos = pos) def sprites(self): """ Only list sprite of current direction for draw """ |