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/__main__.py | 1 - src/sliceitoff/assets/fonts.lst | 4 ++++ src/sliceitoff/field/field.py | 11 ++++----- src/sliceitoff/game/anykey.py | 7 ++++++ src/sliceitoff/game/gameplay.py | 14 +++++------- src/sliceitoff/game/level.py | 46 ++++++++++++++++++++++++-------------- src/sliceitoff/game/show.py | 17 +++++--------- src/sliceitoff/player/player.py | 20 +++++++++-------- src/sliceitoff/screens/__init__.py | 5 ++++- src/sliceitoff/screens/gameover.py | 8 +++++++ src/sliceitoff/screens/level.py | 9 ++++++++ src/sliceitoff/screens/levelup.py | 42 ++++++++++++++-------------------- src/sliceitoff/stats/stats.py | 1 + 13 files changed, 106 insertions(+), 79 deletions(-) create mode 100644 src/sliceitoff/game/anykey.py create mode 100644 src/sliceitoff/screens/gameover.py create mode 100644 src/sliceitoff/screens/level.py diff --git a/src/sliceitoff/__main__.py b/src/sliceitoff/__main__.py index 7d21c7d..2ca02f7 100644 --- a/src/sliceitoff/__main__.py +++ b/src/sliceitoff/__main__.py @@ -34,7 +34,6 @@ def sliceitoff(): dt = clock.tick() display.update( [welcome.sprites] ) - Stats.new_game() diff --git a/src/sliceitoff/assets/fonts.lst b/src/sliceitoff/assets/fonts.lst index f5b4bd3..4a57f92 100644 --- a/src/sliceitoff/assets/fonts.lst +++ b/src/sliceitoff/assets/fonts.lst @@ -1,4 +1,8 @@ lcd assets gnufonts UTIL GNUFONTS LCD.FNT deco assets gnufonts UTIL GNUFONTS DECO.FNT 8x8 assets gnufonts UTIL GNUFONTS 8X8.FNT +8x14 assets gnufonts UTIL GNUFONTS 8X14.FNT computer assets gnufonts UTIL GNUFONTS COMPUTER.FNT +breeze assets gnufonts UTIL GNUFONTS BREEZE.FNT +scott assets gnufonts UTIL GNUFONTS SCOTT.FNT +hearst assets gnufonts UTIL GNUFONTS HEARST.FNT \ No newline at end of file diff --git a/src/sliceitoff/field/field.py b/src/sliceitoff/field/field.py index 134501c..f1a2086 100644 --- a/src/sliceitoff/field/field.py +++ b/src/sliceitoff/field/field.py @@ -128,8 +128,9 @@ class Field(pygame.sprite.LayeredUpdates): area = ax, y2, aw, y3-y2 self.explode(area) - self.add(SliceSprite(area)) - return Scaling.area_to_rect( area ) + zap_spite = SliceSprite(area) + self.add(zap_spite) + return zap_spite def active_sprites(self): """ Returns all sprites that are not dead """ @@ -137,9 +138,9 @@ class Field(pygame.sprite.LayeredUpdates): def explode(self, area): sx, sy, w, h = area - for x in range(int(sx),int(sx+w),4_000): - for y in range(int(sy),int(sy+h),3_000): - self.add(ExplodedField((x,y,3_000,3_000))) + for x in range(int(sx),int(sx+w),8_000): + for y in range(int(sy),int(sy+h),8_000): + self.add(ExplodedField((x,y,4_000,4_000))) def kill_if_not_colliding(self, sprites): 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 diff --git a/src/sliceitoff/player/player.py b/src/sliceitoff/player/player.py index de49083..c062f1c 100644 --- a/src/sliceitoff/player/player.py +++ b/src/sliceitoff/player/player.py @@ -12,7 +12,7 @@ class PlayerSprite(pygame.sprite.Sprite): self.image = image self.rect = self.image.get_rect() - def update(self, pos = None): + def update(self, pos = None, **kwargs): if pos: w, h = self.image.get_size() self.rect = self.image.get_rect().move(pos[0]-w//2,pos[1]-h//2) @@ -28,11 +28,13 @@ class Player(pygame.sprite.LayeredUpdates): image = pygame.transform.rotate(image, 90) self.add(PlayerSprite(image), layer = 1) - def update(self, pos = None, direction = False, dt = 0): - if not self.lazer: - if direction: - self.direction = not self.direction - self.switch_layer(0,1) - if pos: - super().update(pos = pos) - self.position = Scaling.scale_to_internal(pos) + def update(self, pos = None, direction = False, **kwargs): + 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) diff --git a/src/sliceitoff/screens/__init__.py b/src/sliceitoff/screens/__init__.py index 89294bd..ad49657 100644 --- a/src/sliceitoff/screens/__init__.py +++ b/src/sliceitoff/screens/__init__.py @@ -1,2 +1,5 @@ -#from .levelup import LevelUp from .welcome import welcome_screen +from .levelup import levelup_screen +from .gameover import gameover_screen +from .level import level_screen + diff --git a/src/sliceitoff/screens/gameover.py b/src/sliceitoff/screens/gameover.py new file mode 100644 index 0000000..33bda18 --- /dev/null +++ b/src/sliceitoff/screens/gameover.py @@ -0,0 +1,8 @@ +from text import TextPage + +def gameover_screen(): + return TextPage( + "Game Over!", + font = '8x8', + size = (24_000, 24_000), + pos = (48_000, 108_000) ) diff --git a/src/sliceitoff/screens/level.py b/src/sliceitoff/screens/level.py new file mode 100644 index 0000000..4a63dc2 --- /dev/null +++ b/src/sliceitoff/screens/level.py @@ -0,0 +1,9 @@ +from text import TextPage +from stats import Stats + +def level_screen(): + return TextPage( + f"Level {Stats.level}!", + font = '8x8', + size = (24_000, 24_000), + pos = (48_000, 108_000) ) diff --git a/src/sliceitoff/screens/levelup.py b/src/sliceitoff/screens/levelup.py index 178160c..0a0468b 100644 --- a/src/sliceitoff/screens/levelup.py +++ b/src/sliceitoff/screens/levelup.py @@ -1,27 +1,19 @@ -import os -import pygame - -from display import Scaling -from images import Images, Fonts +from text import TextPage from stats import Stats -class Status(): - def __init__(self): - self.sprites = pygame.sprite.Group() - - def update(self, dt): - """ Update sprites basis of dt. dt = milliseconds from last update """ - - score_str = ( - "{:02d}\x12 {:02d}\xfe {:02d}\x03 " - "{:02d}\x0e {:08d}\x0f").format( - Stats.level, - 99 if Stats.percent == 100 else int(Stats.percent), - Stats.lives, - Stats.bonus // 1000, - Stats.score) - self.sprites = TextGroup( - score_str, - (0, 282_000), - size = 16_000, - font = '8x8') +def levelup_screen(): + text = ( + " LEVEL UP BONUS!\n" + " ===============\n" + "\n" + f"Level{Stats.level*3000:7d}pts\n" + f"Time{Stats.bonus:8d}pts\n" + f"\n" + f"Total{Stats.level*3000+Stats.bonus:7d}pts") + + return TextPage( + text, + font = 'hearst', + size = (12_000, 24_000), + grid = (14_000, 24_000), + pos = (12_000, 6_000) ) diff --git a/src/sliceitoff/stats/stats.py b/src/sliceitoff/stats/stats.py index 22345d7..80cd347 100644 --- a/src/sliceitoff/stats/stats.py +++ b/src/sliceitoff/stats/stats.py @@ -20,6 +20,7 @@ class Stats: __class__.score += __class__.level * 3_000 __class__.score += __class__.level * __class__.bonus __class__.bonus = 20_000 + __class__.percent = 100 __class__.level += 1 @staticmethod -- cgit v1.2.3