From cd128f5bc4fae39fb7e77683758d437f64eb4203 Mon Sep 17 00:00:00 2001 From: Viljami Ilola <+@hix.fi> Date: Sun, 24 Mar 2024 01:22:39 +0200 Subject: gameover, level x and level up screens --- src/sliceitoff/game/anykey.py | 7 +++++++ src/sliceitoff/game/gameplay.py | 14 +++++-------- src/sliceitoff/game/level.py | 46 ++++++++++++++++++++++++++--------------- src/sliceitoff/game/show.py | 17 +++++---------- 4 files changed, 46 insertions(+), 38 deletions(-) create mode 100644 src/sliceitoff/game/anykey.py (limited to 'src/sliceitoff/game') diff --git a/src/sliceitoff/game/anykey.py b/src/sliceitoff/game/anykey.py new file mode 100644 index 0000000..6fd48a2 --- /dev/null +++ b/src/sliceitoff/game/anykey.py @@ -0,0 +1,7 @@ +import pygame + +def anykey(): + for event in pygame.event.get(): + if event.type in (pygame.MOUSEBUTTONDOWN, pygame.KEYDOWN, pygame.QUIT): + return True + return False diff --git a/src/sliceitoff/game/gameplay.py b/src/sliceitoff/game/gameplay.py index 12daeaf..6869cbb 100644 --- a/src/sliceitoff/game/gameplay.py +++ b/src/sliceitoff/game/gameplay.py @@ -17,18 +17,14 @@ class Gameplay: self.enemies = enemies def fire(self): - hitbox = self.field.slice( + """ Lazer is fired. Actions to be taken. """ + zap_sprite = self.field.slice( self.player.position, self.player.direction, 4_500) - hit = False - if hitbox is not None: - for enemy in self.enemies.sprites(): - if hitbox.colliderect(enemy.rect): - hit = True - break - - if hit: + if not zap_sprite: + return False + if pygame.sprite.spritecollideany(zap_sprite, self.enemies): if Stats.lose_life(): return True self.field.kill_if_not_colliding(self.enemies.sprites()) diff --git a/src/sliceitoff/game/level.py b/src/sliceitoff/game/level.py index 25c34b9..15668a4 100644 --- a/src/sliceitoff/game/level.py +++ b/src/sliceitoff/game/level.py @@ -6,7 +6,9 @@ from player import Player from field import Field from enemies import Enemies from stats import Stats +from screens import levelup_screen, gameover_screen, level_screen from .gameplay import Gameplay +from .anykey import anykey class Level: @@ -22,30 +24,40 @@ class Level: status = self.status, field = self.field, enemies = self.enemies) - self.obj_classes = ( + self.obj_classes = [ self.status, self.field, - self.enemies, - self.player) + self.enemies] + self.level_info = level_screen() + self.level_over = None def step(self, dt): for obj in self.obj_classes: obj.update(dt = dt) self.enemies.hit_walls(self.field.active_sprites()) - - Stats.update_bonus(dt) - - self.display.update( ( - self.status, - self.field, - self.enemies, - pygame.sprite.GroupSingle( - sprite = self.player.get_top_sprite()))) - - - if self.gameplay.step(): - return False - + objs = self.obj_classes[:] + if self.level_info: + if anykey(): + self.level_info = None + 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())] + Stats.update_bonus(dt) + if self.gameplay.step(): + if Stats.lives: + self.level_over = levelup_screen() + else: + self.level_over = gameover_screen() + + self.display.update(objs) + + return True diff --git a/src/sliceitoff/game/show.py b/src/sliceitoff/game/show.py index c3fe436..50ed0ba 100644 --- a/src/sliceitoff/game/show.py +++ b/src/sliceitoff/game/show.py @@ -1,5 +1,6 @@ """ Reads user input and does actions when game play is on. """ import pygame +from .anykey import anykey class Show: """ To show some sprites and quit on any key """ @@ -13,21 +14,13 @@ class Show: return False if self.timeout < 0: - for event in pygame.event.get(): - if event.type in ( - pygame.MOUSEBUTTONDOWN, - pygame.KEYDOWN, - pygame.QUIT): - return False + if anykey(): + return False self.fadeout -= dt self.sprites.update(explode=dt) return True - for event in pygame.event.get(): - if event.type in ( - pygame.MOUSEBUTTONDOWN, - pygame.KEYDOWN, - pygame.QUIT): - self.timeout = 0 + if anykey(): + self.timeout = 0 self.timeout -= dt return True -- cgit v1.2.3