add saveState management
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
# =============================================================================
|
||||
|
||||
from cmu_graphics import *
|
||||
import json
|
||||
|
||||
|
||||
# =============================================================================
|
||||
@@ -44,19 +45,31 @@ def loadCreatures(path="creatures.tsv"):
|
||||
return creatures
|
||||
|
||||
|
||||
def loadSave(path):
|
||||
def loadSave(path="save.json"):
|
||||
# Load save.json and return the player's saved state (money, owned baits,
|
||||
# owned upgrades, current bait, caught log, etc.). If the file is missing
|
||||
# or unreadable, return a fresh default-state dict so a first-time player
|
||||
# starts cleanly. This is the only place that reads the save file.
|
||||
pass
|
||||
defaultState = {
|
||||
"cash": 0,
|
||||
"owned_baits": [],
|
||||
"owned_upgrades": [],
|
||||
"current_bait": "worm",
|
||||
"catch_log": []
|
||||
}
|
||||
try:
|
||||
with open(path, "r") as f:
|
||||
return json.load(f)
|
||||
except (FileNotFoundError, IOError):
|
||||
return defaultState
|
||||
|
||||
|
||||
def writeSave(path, saveState):
|
||||
def writeSave(saveState, path="save.json"):
|
||||
# Serialize the current player state to save.json. Call this at key moments
|
||||
# (after a sale, after a purchase, on quit) so progress persists between
|
||||
# sessions. Keep the written structure identical to what loadSave expects.
|
||||
pass
|
||||
with open(path, "w") as f: #overwrite existing
|
||||
f.write(json.dumps(saveState))
|
||||
|
||||
|
||||
# =============================================================================
|
||||
|
||||
Reference in New Issue
Block a user