Added highscore & settings saving

This commit is contained in:
theo@manjaro 2021-09-17 11:03:32 +02:00
parent 86c9a5fd0d
commit 3749029e80
5 changed files with 77 additions and 6 deletions

View File

@ -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)

View File

@ -43,18 +43,25 @@ class Game():
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
self.globals["cameray"] = 0
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()

View File

@ -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

View File

@ -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:

View File

@ -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}