diff options
author | Viljami Ilola <+@hix.fi> | 2024-03-18 18:08:20 +0200 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-03-18 18:08:20 +0200 |
commit | 78704848d4459907a456044615714b59d4593b2f (patch) | |
tree | d56c8422793ff1c5b6554146980760d264a6a0fe | |
parent | b219c4f736e6b98dd247064b362997576954f0ff (diff) |
stats, level_up, game_over
-rw-r--r-- | src/sliceitoff/__main__.py | 13 | ||||
-rw-r--r-- | src/sliceitoff/display/__init__.py | 2 | ||||
-rw-r--r-- | src/sliceitoff/display/display.py | 5 | ||||
-rw-r--r-- | src/sliceitoff/enemies/enemies.py | 7 | ||||
-rw-r--r-- | src/sliceitoff/field/field.py | 4 | ||||
-rw-r--r-- | src/sliceitoff/game/gameplay.py | 11 | ||||
-rw-r--r-- | src/sliceitoff/game/level.py | 13 | ||||
-rw-r--r-- | src/sliceitoff/stats/__init__.py | 1 | ||||
-rw-r--r-- | src/sliceitoff/stats/stats.py | 31 | ||||
-rw-r--r-- | src/sliceitoff/status/status.py | 28 |
10 files changed, 74 insertions, 41 deletions
diff --git a/src/sliceitoff/__main__.py b/src/sliceitoff/__main__.py index 17e7317..4e5a626 100644 --- a/src/sliceitoff/__main__.py +++ b/src/sliceitoff/__main__.py @@ -10,24 +10,29 @@ import pygame from display import Display from images import Images, Fonts from game import Level +from stats import Stats def sliceitoff(): """ The game - It all starts here """ pygame.init() + + pygame.mouse.set_visible(False) display = Display() current_path = Path(__file__).parent.resolve() Images.load_images( current_path ) Fonts.load_fonts( current_path ) - level1 = Level(display = display, level = 10, score = 10) - clock = pygame.time.Clock() dt = 0 + + Stats.new_game() - while level1.step(dt): - dt = clock.tick() + while Stats.lives: + level = Level(display = display) + while level.step(dt): + dt = clock.tick() pygame.quit() diff --git a/src/sliceitoff/display/__init__.py b/src/sliceitoff/display/__init__.py index 25b6f18..1decd21 100644 --- a/src/sliceitoff/display/__init__.py +++ b/src/sliceitoff/display/__init__.py @@ -1,5 +1,3 @@ from .scaling import Scaling from .display import Display from .static import INTERNAL_WIDTH, INTERNAL_HEIGHT - - diff --git a/src/sliceitoff/display/display.py b/src/sliceitoff/display/display.py index e4cc670..54b1586 100644 --- a/src/sliceitoff/display/display.py +++ b/src/sliceitoff/display/display.py @@ -1,7 +1,10 @@ import pygame +from stats import Stats + from .scaling import Scaling + class Display(): def __init__(self): pygame.display.init() @@ -14,7 +17,7 @@ class Display(): def update(self, groups = None): """ Updates the screen: clear, blit gropus and flip """ - self.screen.fill("magenta") + self.screen.fill(Stats.bgcolor) for group in groups: group.draw(self.screen) pygame.display.flip() diff --git a/src/sliceitoff/enemies/enemies.py b/src/sliceitoff/enemies/enemies.py index 6a9fda4..d9ba59a 100644 --- a/src/sliceitoff/enemies/enemies.py +++ b/src/sliceitoff/enemies/enemies.py @@ -3,6 +3,7 @@ from random import randrange from display import Scaling from images import Images +from stats import Stats class EnemySprite(pygame.sprite.Sprite): def __init__(self): @@ -46,15 +47,13 @@ class EnemyBall(EnemySprite): class Enemies(): - def __init__(self, field = None, level = 2): + def __init__(self, field = None): self.sprites = pygame.sprite.Group() - self.updated = True - self.level = level self.emerge_enemies() self.field = field def emerge_enemies(self): - for _ in range(self.level * 1): + for _ in range(3 + (Stats.level-1) * 2): enemy = EnemyBall() enemy.set_position( (randrange(0,380_000), randrange(0,260_000)) ) enemy.set_movement( (randrange(0,200)-100, randrange(0,200)-100) ) diff --git a/src/sliceitoff/field/field.py b/src/sliceitoff/field/field.py index 5881ed9..161cfcd 100644 --- a/src/sliceitoff/field/field.py +++ b/src/sliceitoff/field/field.py @@ -3,6 +3,8 @@ import os import pygame from display import Scaling, INTERNAL_WIDTH, INTERNAL_HEIGHT +from stats import Stats + DEBUG = os.getenv("DEBUG") class FieldSprite(pygame.sprite.Sprite): @@ -42,7 +44,7 @@ class Field(): def update_area(self): """ calculates remaining area and remaining percentage """ self.area_current = sum( s.area[2]*s.area[3] for s in self.sprites ) - self.percentage = 100 * self.area_current // self.area_full + Stats.percent = 100 * self.area_current / self.area_full if DEBUG: print( f"FIELD: {self.area_full}/{self.area_current}, " diff --git a/src/sliceitoff/game/gameplay.py b/src/sliceitoff/game/gameplay.py index a7224ca..33847fc 100644 --- a/src/sliceitoff/game/gameplay.py +++ b/src/sliceitoff/game/gameplay.py @@ -1,6 +1,8 @@ """ Reads user input and does actions when game play is on. """ import pygame +from stats import Stats + class Gameplay: """ Logic of the playfield """ def __init__(self, player = None, field = None, status = None): @@ -15,20 +17,23 @@ class Gameplay: case pygame.KEYDOWN: match event.key: case pygame.K_ESCAPE | pygame.K_q: + Stats.lives = 0 return True case pygame.K_SPACE: if self.player.fire_lazer(): return True case pygame.QUIT: + Stats.lives = 0 return True case pygame.MOUSEMOTION: self.player.set_position(pygame.mouse.get_pos()) case pygame.MOUSEBUTTONDOWN: self.player.set_position(pygame.mouse.get_pos()) if event.button == 1: - if ( - self.player.fire_lazer() - and self.status.lose_life() ): + if self.player.fire_lazer() and Stats.lose_life(): + return True + if Stats.percent < 10: + Stats.level_up() return True if event.button == 3: self.player.set_direction() diff --git a/src/sliceitoff/game/level.py b/src/sliceitoff/game/level.py index 28d9b44..fadbee3 100644 --- a/src/sliceitoff/game/level.py +++ b/src/sliceitoff/game/level.py @@ -3,18 +3,19 @@ from status import Status from player import Player from field import Field from enemies import Enemies -from game import Gameplay +from stats import Stats +from .gameplay import Gameplay class Level: """ One level that can be played """ - def __init__(self, level = None, score = None, display = None): + def __init__(self, display = None): self.display = display - self.status = Status(level = level) + self.status = Status() self.field = Field() - self.enemies = Enemies(field = self.field, level = level) + self.enemies = Enemies(field = self.field) self.player = Player(field = self.field, enemies = self.enemies) - self.gameplay = Gameplay(player = self.player, status = self.status ) + self.gameplay = Gameplay(player = self.player, status = self.status) self.obj_classes = ( self.status, self.field, @@ -25,6 +26,8 @@ class Level: for obj in self.obj_classes: obj.update(dt) + Stats.update_bonus(dt) + self.display.update( (obj.sprites for obj in self.obj_classes) ) if self.gameplay.step(): diff --git a/src/sliceitoff/stats/__init__.py b/src/sliceitoff/stats/__init__.py new file mode 100644 index 0000000..900594e --- /dev/null +++ b/src/sliceitoff/stats/__init__.py @@ -0,0 +1 @@ +from .stats import Stats diff --git a/src/sliceitoff/stats/stats.py b/src/sliceitoff/stats/stats.py new file mode 100644 index 0000000..de7b0c1 --- /dev/null +++ b/src/sliceitoff/stats/stats.py @@ -0,0 +1,31 @@ +class Stats: + score = 0 + level = 0 + bonus = 0 + lives = 0 + percent = 0 + bgcolor = (0,0,0,255) + + @staticmethod + def new_game(): + __class__.score = 0 + __class__.level = 1 + __class__.bonus = 20_000 + __class__.lives = 3 + __class__.percent = 100 + + @staticmethod + def level_up(): + __class__.score += __class__.level * 3_000 + __class__.score += __class__.level * __class__.bonus + __class__.bonus = 20_000 + __class__.level += 1 + + @staticmethod + def lose_life(): + __class__.lives -= 3 + return not __class__.lives + + @staticmethod + def update_bonus(dt): + __class__.bonus = max(0, __class__.bonus - dt) diff --git a/src/sliceitoff/status/status.py b/src/sliceitoff/status/status.py index f6517da..9b40955 100644 --- a/src/sliceitoff/status/status.py +++ b/src/sliceitoff/status/status.py @@ -3,6 +3,7 @@ import pygame from display import Scaling from images import Images, Fonts +from stats import Stats class LetterSprite(pygame.sprite.Sprite): def __init__(self, image, pos): @@ -23,37 +24,22 @@ class TextGroup(pygame.sprite.Group): self.add(LetterSprite(image, image_pos)) class Status(): - def __init__(self, level = 1): - self.score = 0 - self.bonus = 20_000 - self.lives = 3 - self.level = level + def __init__(self): self.sprites = pygame.sprite.Group() def update(self, dt): """ Update sprites basis of dt. dt = milliseconds from last update """ - self.bonus = max(0, self.bonus - dt) - score_str = ( "{:02d}\x12 {:02d}\xfe {:02d}\x03 " "{:02d}\x0e {:08d}\x0f").format( - self.level, - 99 if 100 == 100 else 99, - self.lives, - self.bonus // 1000, - self.score) + 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 lose_life(self): - """ Lose 1 life and return true if no lives left """ - self.lives -= 1 - return not self.lives - - def gain_life(self): - """ Gain 1 life """ - self.lives += 1 |