renamed data_handler.py to data.py, renamed DataHandler class to SaveData
This commit is contained in:
parent
509efa7e23
commit
f56f775541
@ -2,12 +2,15 @@ import pocket_friends
|
||||
import json
|
||||
|
||||
|
||||
class DataHandler:
|
||||
class SaveData:
|
||||
"""
|
||||
Class that handles the hardware attributes and save files.
|
||||
Class that represents the save data of the game.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Constructs the object with all starting values.
|
||||
"""
|
||||
# Attributes that are saved to a file to recover upon startup.
|
||||
self.attributes = {
|
||||
'version': pocket_friends.__version__,
|
||||
@ -28,7 +31,7 @@ class DataHandler:
|
||||
|
||||
def write_save(self):
|
||||
"""
|
||||
Writes attributes of class to "save.json" file.
|
||||
Writes attributes of the save object to "save.json" file.
|
||||
"""
|
||||
with open(save_dir + '/save.json', 'w') as save_file:
|
||||
json.dump(self.attributes, save_file)
|
||||
@ -36,7 +39,8 @@ class DataHandler:
|
||||
|
||||
def read_save(self):
|
||||
"""
|
||||
Reads from "save.json" and inserts into attributes dictionary. Creates file if it does not exist.
|
||||
Reads from "save.json" and inserts into the save object's attributes dictionary. Creates file if it does not
|
||||
exist.
|
||||
"""
|
||||
# Open up the save file and read it into self.attributes.
|
||||
try:
|
||||
@ -47,21 +51,3 @@ class DataHandler:
|
||||
# If there is no save file, write one with the defaults.
|
||||
except FileNotFoundError:
|
||||
self.write_save()
|
||||
|
||||
def update(self):
|
||||
"""
|
||||
Run the game logic.
|
||||
"""
|
||||
self.frames_passed += 1
|
||||
# Run logic of the game every second.
|
||||
if self.frames_passed >= game_fps:
|
||||
|
||||
# Add one to the age of the bloop.
|
||||
self.attributes['age'] += 1
|
||||
|
||||
# Save the data when the age of the bloop is a multiple of 10.
|
||||
if self.attributes['age'] % 10 == 0:
|
||||
self.write_save()
|
||||
|
||||
# Reset frame counter
|
||||
self.frames_passed = 0
|
Loading…
Reference in New Issue
Block a user