pocket-friends/pocket_friends/game_files/io/data.py

56 lines
1.6 KiB
Python
Raw Normal View History

2023-05-11 11:51:51 -04:00
import pocket_friends
import json
2023-05-11 15:28:21 -04:00
class SaveData:
2023-05-12 11:46:23 -04:00
"""
Class used to read and write save data for the game
2023-05-11 11:51:51 -04:00
2023-05-12 11:46:23 -04:00
Attributes:
attributes (dict): Dictionary containing all the attributes to read and write from a save file.
"""
2023-05-11 11:51:51 -04:00
def __init__(self):
"""
Constructs the object with all starting values.
"""
2023-05-11 11:51:51 -04:00
# Attributes that are saved to a file to recover upon startup.
self.attributes = {
'version': pocket_friends.__version__,
'time_elapsed': 0,
'bloop': '',
'age': 0,
'health': 0,
'hunger': 0,
'happiness': 0,
'care_counter': 0,
'missed_care': 0,
'adult': 0,
'evolution_stage': '',
}
# Frame counter
self.frames_passed = 0
def write_save(self):
"""
Writes attributes of the save object to "save.json" file.
2023-05-11 11:51:51 -04:00
"""
with open(save_dir + '/save.json', 'w') as save_file:
json.dump(self.attributes, save_file)
save_file.close()
def read_save(self):
"""
Reads from "save.json" and inserts into the save object's attributes dictionary. Creates file if it does not
exist.
2023-05-11 11:51:51 -04:00
"""
# Open up the save file and read it into self.attributes.
try:
with open(save_dir + '/save.json', 'r') as save_file:
self.attributes = json.load(save_file)
save_file.close()
# If there is no save file, write one with the defaults.
except FileNotFoundError:
self.write_save()