diff options
author | Viljami Ilola <+@hix.fi> | 2024-03-29 00:30:39 +0200 |
---|---|---|
committer | Viljami Ilola <+@hix.fi> | 2024-03-29 00:30:39 +0200 |
commit | 5c5df6e068b2957987cb51b34d6930769424f8ca (patch) | |
tree | 1f6ce6ff479ee787a3da6f51a9c337bcd24b841a /src | |
parent | 27c7e16e4bd808ce1176f18b7a78c02ff4fa88ee (diff) |
stats as object
Diffstat (limited to 'src')
-rw-r--r-- | src/sliceitoff/display/display.py | 12 | ||||
-rw-r--r-- | src/sliceitoff/enemies/enemies.py | 5 | ||||
-rw-r--r-- | src/sliceitoff/field/field.py | 11 | ||||
-rw-r--r-- | src/sliceitoff/game/game.py | 23 | ||||
-rw-r--r-- | src/sliceitoff/game/gameplay.py | 14 | ||||
-rw-r--r-- | src/sliceitoff/game/level.py | 23 | ||||
-rw-r--r-- | src/sliceitoff/screens/level.py | 4 | ||||
-rw-r--r-- | src/sliceitoff/screens/levelup.py | 15 | ||||
-rw-r--r-- | src/sliceitoff/stats/stats.py | 77 | ||||
-rw-r--r-- | src/sliceitoff/status/status.py | 21 |
10 files changed, 95 insertions, 110 deletions
diff --git a/src/sliceitoff/display/display.py b/src/sliceitoff/display/display.py index 2febdc2..bdb2514 100644 --- a/src/sliceitoff/display/display.py +++ b/src/sliceitoff/display/display.py @@ -1,9 +1,8 @@ import os import pygame -from stats import Stats - from .scaling import Scaling +from .colors import CGA_COLORS DEBUG = os.getenv("DEBUG") @@ -17,7 +16,7 @@ class Display(): pygame.FULLSCREEN | pygame.SCALED, vsync = 1 ) Scaling.update_scaling(self.screen.get_size()) - self.screen.fill(Stats.bordercolor) + #self.screen.fill(0) if DEBUG: print( "DISPLAY: \n" @@ -27,12 +26,11 @@ 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) + self.screen.fill(CGA_COLORS[4], rect=Scaling.active) for group in groups: group.draw(self.screen) - self.screen.fill(Stats.bordercolor, rect=Scaling.borders[0]) - self.screen.fill(Stats.bordercolor, rect=Scaling.borders[1]) + self.screen.fill(0, rect=Scaling.borders[0]) + self.screen.fill(0, rect=Scaling.borders[1]) pygame.display.flip() def __del__(self): diff --git a/src/sliceitoff/enemies/enemies.py b/src/sliceitoff/enemies/enemies.py index 658c1aa..797fe31 100644 --- a/src/sliceitoff/enemies/enemies.py +++ b/src/sliceitoff/enemies/enemies.py @@ -3,7 +3,6 @@ from random import randrange from display import Scaling from text import Fonts -from stats import Stats class EnemySprite(pygame.sprite.Sprite): def __init__(self): @@ -49,9 +48,9 @@ class EnemyBall(EnemySprite): class Enemies(pygame.sprite.Group): - def __init__(self, field = None): + def __init__(self, field = None, count = 0): super().__init__() - for _ in range(Stats.enemies): + for _ in range(count): enemy = EnemyBall() enemy.set_position( (randrange(0,300_000), randrange(0,200_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 82391d6..5769bfc 100644 --- a/src/sliceitoff/field/field.py +++ b/src/sliceitoff/field/field.py @@ -3,11 +3,9 @@ import pygame from random import randrange from display import Scaling, INTERNAL_WIDTH, INTERNAL_HEIGHT -from stats import Stats DEBUG = os.getenv("DEBUG") - class FieldSprite(pygame.sprite.Sprite): def __init__(self, area: tuple): super().__init__() @@ -67,19 +65,20 @@ class ExplodedField(FieldSprite): class Field(pygame.sprite.LayeredUpdates): initial_area = (320_000, 220_000) - def __init__(self): + def __init__(self, stats = None): super().__init__() self.add(FieldSprite( (0, 0, *__class__.initial_area) )) self.area_full = __class__.initial_area[0] * __class__.initial_area[1] + self.stats = stats def calculate_current_area(self): return sum( s.area[2]*s.area[3] for s in self.active_sprites() ) def update_stats(self): """ calculates remaining area and remaining percentage """ - Stats.percent = 100 * self.calculate_current_area() / self.area_full + self.stats.percent = 100 * self.calculate_current_area() / self.area_full if DEBUG: - print(f"FIELD: {Stats.percent}") + print(f"FIELD: {self.stats.percent}") def slice( self, @@ -152,4 +151,4 @@ class Field(pygame.sprite.LayeredUpdates): else: self.explode(field.area) field.remove(self) - Stats.field_count=len(self.active_sprites()) + self.stats.field_count=len(self.active_sprites()) diff --git a/src/sliceitoff/game/game.py b/src/sliceitoff/game/game.py index 146e34e..bcb04e7 100644 --- a/src/sliceitoff/game/game.py +++ b/src/sliceitoff/game/game.py @@ -21,12 +21,10 @@ class Game: pygame.init() self.clock = pygame.time.Clock() self.display = Display() - - pygame.mouse.set_visible(False) - - Fonts.load_fonts( Path(__file__).parent.parent.resolve() ) - + self.stats = None self.hiscores = HiScores() + Fonts.load_fonts( Path(__file__).parent.parent.resolve() ) + pygame.mouse.set_visible(False) def welcome(self): ws = Show(welcome_screen()) @@ -43,15 +41,14 @@ class Game: self.display.update( [his] ) def newgame(self): - Stats.new_game() - - while Stats.lives: - level = Level(display = self.display) + 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() - if Stats.lives: - Stats.level_up() + if self.stats.lives: + self.stats.level_up() def initials(self): initials = Initials() @@ -64,8 +61,8 @@ class Game: def run(self): self.welcome() self.newgame() - if self.hiscores.high_enough(Stats.score): - self.hiscores.add( Stats.score, self.initials()) + if self.hiscores.high_enough(self.stats.score): + self.hiscores.add( self.stats.score, self.initials()) self.show_highscores() diff --git a/src/sliceitoff/game/gameplay.py b/src/sliceitoff/game/gameplay.py index 028536c..ef708c4 100644 --- a/src/sliceitoff/game/gameplay.py +++ b/src/sliceitoff/game/gameplay.py @@ -1,8 +1,6 @@ """ 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__( @@ -10,11 +8,13 @@ class Gameplay: player = None, field = None, status = None, - enemies = None): + enemies = None, + stats = None): self.status = status self.player = player self.field = field self.enemies = enemies + self.stats = stats def fire(self): """ Lazer is fired. Actions to be taken. """ @@ -24,16 +24,16 @@ class Gameplay: 4_000) if not zap_sprite: return False - Stats.add_score(-500) + self.stats.add_score(-500) if pygame.sprite.spritecollideany(zap_sprite, self.enemies): - if Stats.lose_life(): + if self.stats.lose_life(): return True self.field.kill_if_not_colliding(self.enemies.sprites()) self.field.update_stats() - return Stats.percent < 20 + return self.stats.percent < 20 def quit(self): - Stats.lives = 0 + self.stats.lives = 0 return True def step(self): diff --git a/src/sliceitoff/game/level.py b/src/sliceitoff/game/level.py index ada29d2..a711b0d 100644 --- a/src/sliceitoff/game/level.py +++ b/src/sliceitoff/game/level.py @@ -5,7 +5,6 @@ from status import Status 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 @@ -14,23 +13,25 @@ from .show import Show class Level: """ One level that can be played """ - def __init__(self, display = None): + def __init__(self, display = None, stats = None): self.display = display - self.status = Status() - self.field = Field() - self.enemies = Enemies() + 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.player_single = pygame.sprite.Group() - self.level_info = Show(level_screen()) + self.level_info = Show(level_screen(stats.level)) self.ended = False - self.endscreen = pygame.sprite.Group() + self.endscreen = None self.gameplay = Gameplay( player = self.player, status = self.status, field = self.field, - enemies = self.enemies) + enemies = self.enemies, + stats = self.stats) self.obj_classes = [ self.status, self.field, @@ -55,12 +56,12 @@ class Level: else: self.player_single = pygame.sprite.GroupSingle( sprite = self.player.get_top_sprite()) - Stats.update_bonus(dt) + self.stats.update_bonus(dt) if self.gameplay.step(): self.player_single = pygame.sprite.Group() self.ended = True - if Stats.lives: - self.endscreen = Show(levelup_screen()) + if self.stats.lives: + self.endscreen = Show(levelup_screen(self.stats)) else: self.endscreen = Show(gameover_screen()) self.obj_classes.append(self.endscreen) diff --git a/src/sliceitoff/screens/level.py b/src/sliceitoff/screens/level.py index 4a63dc2..ea298ad 100644 --- a/src/sliceitoff/screens/level.py +++ b/src/sliceitoff/screens/level.py @@ -1,9 +1,9 @@ from text import TextPage from stats import Stats -def level_screen(): +def level_screen(level): return TextPage( - f"Level {Stats.level}!", + f"Level {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 9331020..ef48b7b 100644 --- a/src/sliceitoff/screens/levelup.py +++ b/src/sliceitoff/screens/levelup.py @@ -1,23 +1,22 @@ from text import TextPage -from stats import Stats -def levelup_screen(): +def levelup_screen(stats = None): ( total_bonus, level_bonus, life_bonus, time_bonus, area_bonus, - herd_bonus) = Stats.calc_bonus() + herd_bonus) = stats.calc_bonus() text = ( " LEVEL UP BONUS!\n" " ===============\n" "\n" - f"Level{Stats.level:3d}\xee\x12\xef{level_bonus:9d}pts\n" - f"Lives{Stats.lives:3d}\xec\x03\xef{life_bonus:9d}pts\n" - f"Time{int(Stats.bonus/1000):4d}\xed\x0e\xef{time_bonus:9d}pts\n" - f"Area{int(Stats.percent):4d}\xe9\xfe\xef{area_bonus:9d}pts\n" - f"Herd{Stats.enemies-Stats.field_count:4d}" + f"Level{stats.level:3d}\xee\x12\xef{level_bonus:9d}pts\n" + f"Lives{stats.lives:3d}\xec\x03\xef{life_bonus:9d}pts\n" + f"Time{int(stats.bonus/1000):4d}\xed\x0e\xef{time_bonus:9d}pts\n" + f"Area{int(stats.percent):4d}\xe9\xfe\xef{area_bonus:9d}pts\n" + f"Herd{stats.enemies-stats.field_count:4d}" f"\xea\x0f\xef{herd_bonus:9d}pts\n" f"\n" f"Bonus{total_bonus:13d}pts\n") diff --git a/src/sliceitoff/stats/stats.py b/src/sliceitoff/stats/stats.py index 79da2c9..2d63953 100644 --- a/src/sliceitoff/stats/stats.py +++ b/src/sliceitoff/stats/stats.py @@ -1,53 +1,40 @@ class Stats: - score = 0 - level = 0 - bonus = 0 - lives = 0 - enemies = 0 - percent = 0 - field_count = 0 - bgcolor = (64,0,0,255) - bordercolor = (0,0,0,255) + + def __init__(self): + self.score = 0 + self.level = 1 + self.bonus = 20_000 + self.lives = 3 + self.enemies = 3 + self.percent = 100 + self.field_count = 1 + self.bgcolor = (64,0,0,255) + self.bordercolor = (0,0,0,255) - @staticmethod - def new_game(): - __class__.score = 0 - __class__.level = 1 - __class__.bonus = 20_000 - __class__.lives = 3 - __class__.percent = 100 - __class__.field_count = 1 - __class__.enemies = 3 - - @staticmethod - def level_up(): - __class__.score += __class__.calc_bonus()[0] - __class__.bonus = 20_000 - __class__.percent = 100 - __class__.field_count = 1 - __class__.level += 1 - __class__.enemies = 3 + (__class__.level - 1) * 2 + def level_up(self): + self.score += self.calc_bonus()[0] + self.bonus = 20_000 + self.percent = 100 + self.field_count = 1 + self.level += 1 + self.enemies = 3 + (self.level - 1) * 2 - @staticmethod - def lose_life(): - __class__.lives -= 1 - return not __class__.lives + def lose_life(self): + self.lives -= 1 + return not self.lives - @staticmethod - def add_score(score_to_add): - __class__.score += score_to_add - __class__.score = max(0, __class__.score) + def add_score(self, score_to_add): + self.score += score_to_add + self.score = max(0, self.score) - @staticmethod - def calc_bonus(): - level = __class__.level * 3_000 - life = __class__.lives * 1_000 - time = __class__.level * __class__.bonus - area = __class__.level * int(max(20-__class__.percent,0) * 1_000) - herd = (__class__.enemies - __class__.field_count) * 1_000 + def calc_bonus(self): + level = self.level * 3_000 + life = self.lives * 1_000 + time = self.level * self.bonus + area = self.level * int(max(20-self.percent,0) * 1_000) + herd = (self.enemies - self.field_count) * 1_000 total = level + area + time + life + herd return total, level, life, time, area, herd - @staticmethod - def update_bonus(dt): - __class__.bonus = max(0, __class__.bonus - dt) + def update_bonus(self, dt): + self.bonus = max(0, self.bonus - dt) diff --git a/src/sliceitoff/status/status.py b/src/sliceitoff/status/status.py index ad3034c..bbe9a03 100644 --- a/src/sliceitoff/status/status.py +++ b/src/sliceitoff/status/status.py @@ -1,23 +1,28 @@ +""" status.status - The statusline bottom of screen showinf all stats """ import pygame -from stats import Stats from text import TextPage class Status(pygame.sprite.Group): - def __init__(self): + """ Statusline bottom of screen """ + def __init__(self, stats = None): super().__init__() + self.stats = stats self.old_srt = None def update(self, **kwargs): """ Rebuilds statusline if needed """ super().update(**kwargs) + percent = int(self.stats.percent) + if percent == 100: + percent = 99 score_str = ( - f"\xee\x12\xef{Stats.level:<2d}" - f"\xec\x03\xef{Stats.lives:<2d}" - f"\xed\x0e\xef{Stats.bonus // 1000:<3d}" - f"\xe9\xfe\xef{99 if Stats.percent == 100 else int(Stats.percent):<3d}" - f"\xea\x0f\xef{Stats.enemies-Stats.field_count:<3d}" - f"{Stats.score:08d}") + f"\xee\x12\xef{self.stats.level:<2d}" + f"\xec\x03\xef{self.stats.lives:<2d}" + f"\xed\x0e\xef{self.stats.bonus // 1000:<3d}" + f"\xe9\xfe\xef{percent:<3d}" + f"\xea\x0f\xef{self.stats.enemies-self.stats.field_count:<3d}" + f"{self.stats.score:08d}") if self.old_srt != score_str: self.empty() self.add( TextPage( |