diff options
author | Viljami Ilola <+@hix.fi> | 2024-03-28 22:28:45 +0200 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-03-28 22:28:45 +0200 |
commit | f894c2fd09ed17540c49cb4083573861ded76554 (patch) | |
tree | cedbe5bab1b5f91b7e3dfb70c17f5114034e700e | |
parent | f502b21183d307fcab9b353aa18609d15c3547f1 (diff) |
Show as sprite group with update
-rw-r--r-- | pyproject.toml | 2 | ||||
-rw-r--r-- | src/sliceitoff/display/display.py | 4 | ||||
-rw-r--r-- | src/sliceitoff/game/game.py | 8 | ||||
-rw-r--r-- | src/sliceitoff/game/initials.py | 2 | ||||
-rw-r--r-- | src/sliceitoff/game/level.py | 51 | ||||
-rw-r--r-- | src/sliceitoff/game/show.py | 33 | ||||
-rw-r--r-- | src/sliceitoff/hiscores/hiscores.py | 12 | ||||
-rw-r--r-- | src/sliceitoff/screens/hiscores.py | 6 | ||||
-rw-r--r-- | src/sliceitoff/text/text.py | 8 |
9 files changed, 73 insertions, 53 deletions
diff --git a/pyproject.toml b/pyproject.toml index 179995a..a3d7d9b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "sliceitoff" -version = "0.0-alpha" +version = "0.1-alpha" description = "" authors = ["Viljami Ilola <+@hix.fi>"] readme = "README.md" diff --git a/src/sliceitoff/display/display.py b/src/sliceitoff/display/display.py index 06b222c..c2a3e44 100644 --- a/src/sliceitoff/display/display.py +++ b/src/sliceitoff/display/display.py @@ -28,8 +28,10 @@ class Display(): def update(self, groups = None): """ Updates the screen: clear, blit gropus and flip """ self.screen.fill(Stats.bgcolor, rect=Scaling.active) + #(g.draw(self.screen) for g in groups if g) for group in groups: - group.draw(self.screen) + if group: + group.draw(self.screen) self.screen.fill(Stats.bordercolor, rect=Scaling.borders[0]) self.screen.fill(Stats.bordercolor, rect=Scaling.borders[1]) pygame.display.flip() diff --git a/src/sliceitoff/game/game.py b/src/sliceitoff/game/game.py index 0b67d2f..982466b 100644 --- a/src/sliceitoff/game/game.py +++ b/src/sliceitoff/game/game.py @@ -31,15 +31,15 @@ class Game: def welcome(self): ws = Show(welcome_screen()) dt = 0 - while ws.step(dt): - dt = self.clock.tick() + while ws.active: + ws.update(dt = self.clock.tick()) self.display.update( [ws] ) def show_highscores(self): his = Show(hiscores_screen(str(self.hiscores))) dt = 0 - while his.step(dt): - dt = self.clock.tick() + while his.active: + his.update(dt = self.clock.tick()) self.display.update( [his] ) def newgame(self): diff --git a/src/sliceitoff/game/initials.py b/src/sliceitoff/game/initials.py index 5b25195..a317dd9 100644 --- a/src/sliceitoff/game/initials.py +++ b/src/sliceitoff/game/initials.py @@ -38,7 +38,7 @@ class Initials(pygame.sprite.Group): self.name = self.name [:-1] elif pygame.key.name(event.key): self.name += pygame.key.name(event.key)[0].upper() - self.name = self.name[:8] + self.name = self.name[:3] self.empty() self.add(initials_screen(self.name)) return True diff --git a/src/sliceitoff/game/level.py b/src/sliceitoff/game/level.py index 15668a4..f0bb871 100644 --- a/src/sliceitoff/game/level.py +++ b/src/sliceitoff/game/level.py @@ -9,16 +9,24 @@ from stats import Stats from screens import levelup_screen, gameover_screen, level_screen from .gameplay import Gameplay from .anykey import anykey +from .show import Show class Level: """ One level that can be played """ def __init__(self, display = None): self.display = display + self.ended = False self.status = Status() self.field = Field() self.enemies = Enemies() self.player = Player() + self.player_single = pygame.sprite.Group() + + self.level_info = Show(level_screen()) + self.game_over = Show(gameover_screen(), active = False) + self.levelup = Show(levelup_screen(), active = False) + self.gameplay = Gameplay( player = self.player, status = self.status, @@ -27,37 +35,40 @@ class Level: self.obj_classes = [ self.status, self.field, - self.enemies] - self.level_info = level_screen() - self.level_over = None + self.enemies, + self.game_over, + self.levelup, + self.level_info] def step(self, dt): - for obj in self.obj_classes: - obj.update(dt = dt) - + self.status.update(dt = dt) + self.field.update(dt = dt) + self.enemies.update(dt = dt) self.enemies.hit_walls(self.field.active_sprites()) - objs = self.obj_classes[:] - if self.level_info: - if anykey(): - self.level_info = None + if self.level_info.active: + self.level_info.update(dt = dt) + + elif self.ended: + if self.game_over.active: + self.game_over.update(dt = dt) + elif self.levelup.active: + self.levelup.update(dt = dt) else: - objs += [self.level_info] - elif self.level_over: - objs += [self.level_over] - if anykey(): return False + else: - objs += [pygame.sprite.GroupSingle( - sprite = self.player.get_top_sprite())] + self.player_single = pygame.sprite.GroupSingle( + sprite = self.player.get_top_sprite()) Stats.update_bonus(dt) if self.gameplay.step(): + self.player_single = pygame.sprite.Group() + self.ended = True if Stats.lives: - self.level_over = levelup_screen() + self.levelup.active = True else: - self.level_over = gameover_screen() + self.game_over.active = True - self.display.update(objs) - + self.display.update(self.obj_classes+[self.player_single]) return True diff --git a/src/sliceitoff/game/show.py b/src/sliceitoff/game/show.py index 07146c3..018e555 100644 --- a/src/sliceitoff/game/show.py +++ b/src/sliceitoff/game/show.py @@ -4,24 +4,27 @@ from .anykey import anykey class Show(pygame.sprite.Group): """ To show some sprites and quit on any key """ - def __init__(self, sprites = []): + def __init__(self, sprites = None, active = True): super().__init__() self.add(sprites) + self.active = active + self.explode = False self.fadeout = 1_000 - self.timeout = 5_000 + self.timeout = 15_000 - def step(self, dt): - if self.fadeout < 0: - return False - - if self.timeout < 0: + def update(self, dt = 0, **kwargs): + super().update(dt = dt, explode = self.explode, **kwargs) + if self.fadeout <= 0: + self.active = False + elif self.timeout <= 0: if anykey(): - return False + self.fadeout = 0 self.fadeout -= dt - self.update(explode=dt) - return True - - if anykey(): - self.timeout = 0 - self.timeout -= dt - return True + self.explode = True + else: + if anykey(): + self.timeout = 0 + self.timeout -= dt + + def sprites(self): + return super().sprites() if self.active else None
\ No newline at end of file diff --git a/src/sliceitoff/hiscores/hiscores.py b/src/sliceitoff/hiscores/hiscores.py index 098f0e3..2725931 100644 --- a/src/sliceitoff/hiscores/hiscores.py +++ b/src/sliceitoff/hiscores/hiscores.py @@ -45,11 +45,15 @@ class HiScores: config_file.write(f"hiscore={score}!{name}\n") def __str__(self): - text = " HIGH SCORES!!\n\n" + text = ( + " " + "\xeeH\xecI\xedG\xe9H " + "\xeaS\xedC\xeeO\xebR\xe9E\xecS\xeb!\xed!\n\n") half = len(self.table)//2 for i in range(half): text += ( - f"{self.table[i][1]:<4s} {self.table[i][0]:08} " - f"{self.table[i+half][1]:<4s} " - f"{self.table[i+half][0]:08}\n") + f"\xed{self.table[i][1]:<3s} " + f"\xef{self.table[i][0]:07} " + f"\xed{self.table[i+half][1]:<3s} " + f"\xef{self.table[i+half][0]:07}\n") return text diff --git a/src/sliceitoff/screens/hiscores.py b/src/sliceitoff/screens/hiscores.py index 98bfd46..74a0ee2 100644 --- a/src/sliceitoff/screens/hiscores.py +++ b/src/sliceitoff/screens/hiscores.py @@ -4,6 +4,6 @@ def hiscores_screen(score_text): return TextPage( score_text, font = 'lcd', - size = (8_000, 16_000), - grid = (9_000, 14_000), - pos = (8_000, 0_000) ) + size = (12_000, 24_000), + grid = (12_000, 20_000), + pos = (12_000, 0_000) ) diff --git a/src/sliceitoff/text/text.py b/src/sliceitoff/text/text.py index 8c13004..da48f6c 100644 --- a/src/sliceitoff/text/text.py +++ b/src/sliceitoff/text/text.py @@ -15,11 +15,11 @@ class LetterSprite(pygame.sprite.Sprite): Scaling.factor * (1_000 - randrange(2_000)), Scaling.factor * (1_000 - randrange(2_000))) - def update(self, explode = 0, **kwargs): - if explode: + def update(self, dt = 0, explode = 0, **kwargs): + if explode and dt: self.rect = pygame.Rect( - self.rect.x + self.direction[0] * explode, - self.rect.y + self.direction[1] * explode, + self.rect.x + self.direction[0] * dt, + self.rect.y + self.direction[1] * dt, self.rect.w, self.rect.h) self.direction = ( |