implemented JSON as data format for saveing files - UNSTABLE

JSON-implementation
Max Lange 8 years ago
parent c9f058e85c
commit 8e4ff45a3f

3
.gitignore vendored

@ -11,3 +11,6 @@
#empty folders
screenshots/*
assets/sprites/player/*
#symlinks in testing folder
src/tests/settings.py

@ -0,0 +1,11 @@
{
"screeny_current": 540,
"Fullscreen": false,
"debugscreen": false,
"volume": 0.5,
"player.pos.y": 0,
"screenx_current": 960,
"player.timeplay": 513,
"debugmode": false,
"player.pos.x": 0
}

@ -238,7 +238,9 @@ def pause():
settings.screeny_current / 2,
"Save Game")
if savename != "Exit":
settings.save(savename)
#TODO: this is ugly
saver = settings.data()
saver.save(savename)
settings.upd("get_saves")
if event == "Load Game":
savegame = savegames()

@ -8,6 +8,7 @@ import shutil
import sys
import traceback
import random
import json
def init():
@ -101,7 +102,7 @@ def init():
typeface = "monospace"
stdfont = pygame.font.SysFont(typeface, 15)
version = "0.3.3"
version = "0.3.3.1 dev"
up = False
down = False
left = False
@ -293,20 +294,15 @@ def toggle(var, option1, option2):
return var
class save():
class data():
def __init__(self, name):
def __init__(self):
"""create a new savegame"""
global saves
global current_game
pass
name = name.encode("utf-8")
self.name = name
def save(self, name):
upd("get_saves")
if len(saves) >= 50:
return False
name = name.encode("utf-8")
# removes invalid characters
if "/" in name:
@ -314,74 +310,65 @@ class save():
if "%" in name:
name = name.replace("%", "")
current_game = name
# handles the configparser object
self.config = SafeConfigParser()
self.config.read("./saves/" + name + ".ini")
if not os.path.isfile("./saves/" + name + ".ini"):
self.config.add_section("main")
# sets values
self.config.set("main", "fullscreen", str(fullscreen))
self.config.set("main", "screenx_current", str(screenx_current))
self.config.set("main", "screeny_current", str(screeny_current))
self.config.set("main", "debugscreen", str(debugscreen))
self.config.set("main", "debugmode", str(debugmode))
self.config.set("main", "skip", "True")
self.config.set("main", "posx", str(player.pos.x))
self.config.set("main", "posy", str(player.pos.y))
self.config.set("main", "volume", str(volume))
# and writes them
with open("./saves/" + name + ".ini", "w") as tmp:
self.config.write(tmp)
def load(name):
"""Load savegame"""
global fullscreen
global screenx_current
global screeny_current
global debugscreen
global debugmode
global config
global skip
global pos_x
global pos_y
global volume
global saves
global screen
upd("get_saves")
config = SafeConfigParser()
for a in saves:
if a == name.encode("utf-8"):
config.read("./saves/" + a + ".ini")
if not (saves == []):
# tries to load and returns values in terminal that couldnt be loaded
import ConfigParser
try:
from . import sounds
#lint:disable
fullscreen = config.getboolean("main", "fullscreen")
screenx_current = int(config.getfloat("main", "screenx_current"))
screeny_current = int(config.getfloat("main", "screeny_current"))
debugscreen = config.getboolean("main", "debugscreen")
debugmode = config.getboolean("main", "debugmode")
skip = config.getboolean("main", "skip")
pos_x = config.getfloat("main", "posy")
pos_y = config.getfloat("main", "posx")
sounds.music.volume = config.getfloat("main", "volume")
#lint:enable
except ConfigParser.NoOptionError as test:
print(("Saved game couldn't be loaded completly: " + str(test)))
except Exception:
print(("Unexpected error:", sys.exc_info()[0]))
print((traceback.format_exc()))
screen = pygame.display.set_mode((screenx_current, screeny_current))
data = {"Fullscreen": fullscreen,
"screenx_current": screenx_current,
"screeny_current": screeny_current,
"debugmode": debugmode,
"debugscreen": debugscreen,
"player.pos.x": player.pos.x,
"player.pos.y": player.pos.y,
"volume": volume,
"player.timeplay": player.timeplay
}
file_obj = open("./saves/" + name + ".json", "w")
json.dump(data, file_obj, indent=12)
def load(name):
"""Load savegame"""
global fullscreen
global screenx_current
global screeny_current
global debugscreen
global debugmode
global config
global skip
global pos_x
global pos_y
global volume
global saves
global screen
upd("get_saves")
config = SafeConfigParser()
for a in saves:
if a == name.encode("utf-8"):
config.read("./saves/" + a + ".ini")
if not (saves == []):
# tries to load and returns values in terminal that couldnt be loaded
import ConfigParser
try:
from . import sounds
#lint:disable
fullscreen = config.getboolean("main", "fullscreen")
screenx_current = int(config.getfloat("main", "screenx_current"))
screeny_current = int(config.getfloat("main", "screeny_current"))
debugscreen = config.getboolean("main", "debugscreen")
debugmode = config.getboolean("main", "debugmode")
skip = config.getboolean("main", "skip")
pos_x = config.getfloat("main", "posy")
pos_y = config.getfloat("main", "posx")
sounds.music.volume = config.getfloat("main", "volume")
#lint:enable
except ConfigParser.NoOptionError as test:
print(("Saved game couldn't be loaded completly: " + str(test)))
except Exception:
print(("Unexpected error:", sys.exc_info()[0]))
print((traceback.format_exc()))
screen = pygame.display.set_mode((screenx_current, screeny_current))
def quit():

@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
import json
data = {"Fullscreen": True, "pos_x": 0.3462345254}
data_json = json.dumps(data, indent=12)
print((data_json))
file_obj = open("test1.json", "rw+")
json.dump(data, file_obj)
file_obj.close()
file_obj = open("test1.json", "r")
read_data = json.load(file_obj)
print type(read_data["Fullscreen"])

@ -0,0 +1 @@
{"Fullscreen": true, "pos_x": 0.3462345254}
Loading…
Cancel
Save