-
Notifications
You must be signed in to change notification settings - Fork 7
/
gamedata.py
32 lines (27 loc) · 1.28 KB
/
gamedata.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
""" This file holds all the important game data (saving and loading) of the game. """
import json
from os.path import exists as file_exists
from other.colors import print_green, print_yellow
class GameData:
"""
This class will offer loading and saving of game data for Zombie-Survival-Game.
"""
def __init__(self):
self.file = 'data.json'
self.file_exists = bool(file_exists(self.file))
def load_game(self):
"""loads and read data from data.json file"""
if self.file_exists:
with open(self.file, 'r') as user_data_file:
user_data = json.load(user_data_file)
print_green('Saved data has been loaded successfully!\n')
return user_data
else: # don't need to initialize values -- already initialized when class created
print_yellow('No saved data found...\n')
print_green('Starting a fresh game...\n')
def save_game(self, player_data: dict):
"""saves and writes to data.json file, if the file does not exist then this function will create a new save"""
if not self.file_exists:
print_yellow("No previous save! Creating a new save...\n")
with open(self.file, 'w') as user_data_file:
json.dump(player_data, user_data_file)