From a4794437d561a01c8bea12d59502e4cc72acfbea Mon Sep 17 00:00:00 2001 From: Viljami Ilola <+@hix.fi> Date: Sat, 27 Apr 2024 00:40:24 +0300 Subject: separate settings from hiscore --- src/sliceitoff/game/game.py | 12 +++--- src/sliceitoff/hiscores/__init__.py | 2 +- src/sliceitoff/hiscores/hiscores.py | 56 +++++++++---------------- src/sliceitoff/settings/__init__.py | 2 + src/sliceitoff/settings/settings.py | 82 +++++++++++++++++++++++++++++++++++++ src/sliceitoff/settings/static.py | 26 ++++++++++++ 6 files changed, 138 insertions(+), 42 deletions(-) create mode 100644 src/sliceitoff/settings/__init__.py create mode 100644 src/sliceitoff/settings/settings.py create mode 100644 src/sliceitoff/settings/static.py (limited to 'src') diff --git a/src/sliceitoff/game/game.py b/src/sliceitoff/game/game.py index 2ef377f..f85a544 100644 --- a/src/sliceitoff/game/game.py +++ b/src/sliceitoff/game/game.py @@ -6,13 +6,14 @@ from pathlib import Path import pygame +from sliceitoff.settings import settings from sliceitoff.display import Display from sliceitoff.stats import Stats from sliceitoff.screens import ( hiscores_screen, instructions1_screen, instructions2_screen) -from sliceitoff.hiscores import HiScores +from sliceitoff.hiscores import hi_scores from sliceitoff.text import fonts from sliceitoff.sfx import sfx @@ -32,7 +33,6 @@ class Game: self.clock = pygame.time.Clock() self.display = Display() self.stats = None - self.hiscores = HiScores() pygame.mouse.set_visible(False) def __del__(self): @@ -49,7 +49,7 @@ class Game: def show_highscores(self): """ displays highscores and waits a key """ sfx.music("pimpelipompeli") - his = Show(hiscores_screen(str(self.hiscores))) + his = Show(hiscores_screen(str(hi_scores))) while his.active: his.update(dt = self.clock.tick()) self.display.update( [his] ) @@ -94,6 +94,8 @@ class Game: while True: match self.mainmenu(): case MenuItems.QUIT: + hi_scores.save() + settings.save() break case MenuItems.HISCORES: self.show_highscores() @@ -103,6 +105,6 @@ class Game: self.settings() case MenuItems.NEWGAME: self.newgame() - if self.hiscores.high_enough(self.stats.score): - self.hiscores.add( self.stats.score, self.initials()) + if hi_scores.high_enough(self.stats.score): + hi_scores.add( self.stats.score, self.initials()) self.show_highscores() diff --git a/src/sliceitoff/hiscores/__init__.py b/src/sliceitoff/hiscores/__init__.py index fdad3e5..75ce884 100644 --- a/src/sliceitoff/hiscores/__init__.py +++ b/src/sliceitoff/hiscores/__init__.py @@ -1,2 +1,2 @@ """ highscores - High score storing and listing """ -from .hiscores import HiScores +from .hiscores import hi_scores diff --git a/src/sliceitoff/hiscores/hiscores.py b/src/sliceitoff/hiscores/hiscores.py index bcf41a0..d963605 100644 --- a/src/sliceitoff/hiscores/hiscores.py +++ b/src/sliceitoff/hiscores/hiscores.py @@ -1,32 +1,16 @@ """ hiscores.hiscores - high socres: loading, saving, converting to string """ -import os -from pathlib import Path -from .static import INITIAL_HIGHSCORES, MAX_HIGHSCORES +from sliceitoff.settings import settings +from .static import MAX_HIGHSCORES class HiScores: """ Keeps track of high scores """ - def __init__(self, filename = None): + def __init__(self): """ On creation load high scores from config file """ self.table=[] - if filename: - self.config_filename = filename - else: - if os.name == 'nt': - self.config_filename = (Path.home().resolve() - .joinpath('sliceitoff.cfg')) - else: - self.config_filename = (Path.home().resolve() - .joinpath('.config').joinpath('sliceitoffrc')) - if not self.config_filename.is_file(): - self.table=INITIAL_HIGHSCORES[:] - return - with open(self.config_filename, "r", encoding="utf-8") as config_file: - for line in config_file: - option, *value = line.split('=') - if option == 'hiscore' and value: - score, name = value[0].split('!') - self.add(int(score.strip()),name.strip()) + for value in settings.get_values("hiscore"): + score, name = value.split('!') + self.add(int(score.strip()),name.strip()) if len(self.table)