-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress.py
52 lines (46 loc) · 1.59 KB
/
progress.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Progress saving
from fileIO import readFile, writeFile
class Progress(dict):
attributes = ["score", "time"]
path = "savefile.txt"
@staticmethod
def load():
# Load from save file
progress = {}
for line in readFile(Progress.path).splitlines():
line = line.split(",")
level = line[0]
line = line[1:]
attributes = {}
for i, item in enumerate(Progress.attributes):
if line[i] == "None":
attributes[item] = None
elif line[i].isdecimal():
attributes[item] = int(line[i])
else:
attributes[item] = line[i]
progress[level] = attributes
return progress
def __init__(self, levels, game):
super().__init__(Progress.load())
self.game = game
# Fill in missing levels from save file
for level in levels:
if level not in self:
self[level] = dict(zip(Progress.attributes,
[None]*len(Progress.attributes)))
def save(self):
contents = ""
for level in self:
line = level
for item in Progress.attributes:
line += "," + str(self[level][item])
contents += line + "\n"
writeFile(Progress.path, contents)
def clear(self):
for level in self:
self[level] = dict(zip(Progress.attributes,
[None]*len(Progress.attributes)))
self.save()
self.game.initLevelMenu()
self.game.menu = self.game.levelMenu