add saveState management

This commit is contained in:
2026-10-07 00:03:06 -04:00
parent 9d14bc56b2
commit 1fad510038
2 changed files with 37 additions and 5 deletions
+17 -4
View File
@@ -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))
# =============================================================================