From 3749029e80b5409e0cc55caf1b57df7be1eb040c Mon Sep 17 00:00:00 2001 From: "theo@manjaro" Date: Fri, 17 Sep 2021 11:03:32 +0200 Subject: [PATCH] Added highscore & settings saving --- gamedata/definitions.py | 63 ++++++++++++++++++++++++++++- gamedata/game.py | 15 +++++-- gamedata/objects/gameover.py | 2 + gamedata/objects/ingame/manager.py | 2 +- gamedata/objects/menu/optionmenu.py | 1 + 5 files changed, 77 insertions(+), 6 deletions(-) diff --git a/gamedata/definitions.py b/gamedata/definitions.py index 6c2184c..6359bb8 100644 --- a/gamedata/definitions.py +++ b/gamedata/definitions.py @@ -1,5 +1,5 @@ from random import * -import sys,os,math,shutil,json +import sys,os,configparser def drawcenter(game,surface,posx,posy): # Draw a surface in its center @@ -68,3 +68,64 @@ class Timer(): self.timer+=amount self.maxcount+=amount +def get_save_dir(dirname): + path = "" + p = sys.platform + if p=="linux": + path = os.environ["HOME"]+"/.local/share/"+dirname + if p=="win32": + path = os.getenv('APPDATA')+"\\"+dirname + + # Create folder if non-existant + try: + os.mkdir(path) + except: + pass + + return path + +def loadscore(dirname,filename="highscore.ini"): + filepath = os.path.join(dirname,filename) + highscore = 0 + try: + config = configparser.ConfigParser() + config.read(filepath) + if "highscore" in config.sections(): + highscore = int(config["highscore"].get("score",0)) + except: + # Save file corrupted, reset it + os.remove(filepath) + + return highscore + +def loadsettings(dirname,filename="settings.ini"): + filepath = os.path.join(dirname,filename) + sfx = 100 + bgm = 100 + try: + config = configparser.ConfigParser() + config.read(filepath) + if "settings" in config.sections(): + bgm = int(config["settings"].get("bgm",100)) + sfx = int(config["settings"].get("sfx",100)) + except: + # Save file corrupted, reset it + os.remove(filepath) + return bgm,sfx + +def savescore(dirname,highscore=0,filename="highscore.ini"): + filepath = os.path.join(dirname,filename) + config = configparser.ConfigParser() + config["highscore"] = {"score":highscore} + + with open(filepath,'w') as configfile: + config.write(configfile) + +def savesettings(dirname,bgm=1,sfx=1,filename="settings.ini"): + filepath = os.path.join(dirname,filename) + config = configparser.ConfigParser() + bgm,sfx = round(bgm*100),round(sfx*100) + config["settings"] = {"bgm":bgm,"sfx":sfx} + + with open(filepath,'w') as configfile: + config.write(configfile) diff --git a/gamedata/game.py b/gamedata/game.py index e6c44b6..ee4df3d 100644 --- a/gamedata/game.py +++ b/gamedata/game.py @@ -42,6 +42,14 @@ class Game(): self.math = math self.elapsedtime = 0 + + self.datadir = lib.get_save_dir("pinmikpanik") + self.dataname = "highscore.ini" + self.settingsname = "settings.ini" + + highscore = lib.loadscore(self.datadir,self.dataname) + bgm,sfx = lib.loadsettings(self.datadir,self.settingsname) + bgm,sfx = bgm/100,sfx/100 self.globals = {} # Un dico pour ranger toute les valeurs globales, pour communiquer entre objets par exemples self.globals["camerax"] = 0 @@ -49,12 +57,11 @@ class Game(): self.globals["scamerax"] = 3 self.globals["scameray"] = 0 self.globals["debug"] = False - self.globals["highscore"] = 0 + self.globals["highscore"] = highscore self.scaleCamera() - settings = {"sfx":1,"bgm":1} - self.globals["bgmvolume"] = settings["bgm"] - self.globals["sfxvolume"] = settings["sfx"] + self.globals["bgmvolume"] = bgm + self.globals["sfxvolume"] = sfx self.reinit_volumes() diff --git a/gamedata/objects/gameover.py b/gamedata/objects/gameover.py index bb776eb..39782ac 100644 --- a/gamedata/objects/gameover.py +++ b/gamedata/objects/gameover.py @@ -20,6 +20,8 @@ class GameOver(BaseObject): game.globals["highscore"] = game.globals["score"] self.highscore = True + self.game.lib.savescore(self.game.datadir,self.game.globals["highscore"],self.game.dataname) + self.displayscore = 0 self.launched = False diff --git a/gamedata/objects/ingame/manager.py b/gamedata/objects/ingame/manager.py index a517818..95376c1 100644 --- a/gamedata/objects/ingame/manager.py +++ b/gamedata/objects/ingame/manager.py @@ -52,7 +52,7 @@ class Manager(BaseObject): else: # Game over if self.endtimer.tick(self.game.dt): - self.game.globals["score"] = self.score + self.game.globals["score"] = int(self.score) self.game.scenes.gameover(self.game) if self.invincible: diff --git a/gamedata/objects/menu/optionmenu.py b/gamedata/objects/menu/optionmenu.py index f6921e8..0471373 100644 --- a/gamedata/objects/menu/optionmenu.py +++ b/gamedata/objects/menu/optionmenu.py @@ -5,6 +5,7 @@ class OptionMenu(menu.Menu): super().__init__(x,y,game,w,h) # initialise l'objet de base avec les bons arguments # Les dicos des boutons def fnRetour(self,game): + game.lib.savesettings(game.datadir,bgm=game.globals["bgmvolume"],sfx=game.globals["sfxvolume"],filename=game.settingsname) game.scene = game.scenes.main btnRetour= {"name":"Retour","function": fnRetour}