Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9eaff2d837 | ||
|
|
1fad510038 |
+20
-1
@@ -1,9 +1,28 @@
|
|||||||
###############################################
|
###############################################
|
||||||
Prompt to Kiro AI (Default):
|
Prompt to Kiro AI (Default):
|
||||||
|
|
||||||
|
from now on, I will mostly be asking questions about approach, so please do not perform any actions or code edits unless explicitly requested. also, please be less verbose - I want nice, quick answers that provide the necessary context. How can I check if a file exists/isreadable for my loadSave function?
|
||||||
|
|
||||||
Response:
|
Response:
|
||||||
|
|
||||||
|
Two clean options:
|
||||||
|
|
||||||
|
os.path.exists(path) — checks the file is there before opening.
|
||||||
|
|
||||||
|
python
|
||||||
|
|
||||||
|
import os
|
||||||
|
if os.path.exists(path):
|
||||||
|
# open and read
|
||||||
|
try/except — just attempt to open and catch the failure. This is the more Pythonic approach and also handles the "exists but unreadable/corrupt" case in one shot:
|
||||||
|
|
||||||
|
python
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(path) as f:
|
||||||
|
...
|
||||||
|
except (FileNotFoundError, IOError):
|
||||||
|
return defaultState()
|
||||||
|
For loadSave, the try/except is the better fit since you also want to catch bad JSON — wrap json.load in the same block and catch json.JSONDecodeError too, falling back to a fresh default state on any failure.
|
||||||
|
|
||||||
###############################################
|
###############################################
|
||||||
@@ -8,4 +8,9 @@ Description: The main AI agent in kiro, used for most if not all of the promptin
|
|||||||
###############################################
|
###############################################
|
||||||
Source: Google Search / Google Search AI
|
Source: Google Search / Google Search AI
|
||||||
Description: Used infrequently just for general questions regarding python or graphics, no specific usage (and google search AI is just kind of always there)
|
Description: Used infrequently just for general questions regarding python or graphics, no specific usage (and google search AI is just kind of always there)
|
||||||
|
###############################################
|
||||||
|
|
||||||
|
###############################################
|
||||||
|
Source: CMU Graphics Documentation (https://academy.cs.cmu.edu/cpcs-docs)
|
||||||
|
Description: Refferred to for implementation and MVC architecture along with how to interface with graphics
|
||||||
###############################################
|
###############################################
|
||||||
@@ -10,6 +10,7 @@
|
|||||||
# =============================================================================
|
# =============================================================================
|
||||||
|
|
||||||
from cmu_graphics import *
|
from cmu_graphics import *
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
@@ -44,19 +45,31 @@ def loadCreatures(path="creatures.tsv"):
|
|||||||
return creatures
|
return creatures
|
||||||
|
|
||||||
|
|
||||||
def loadSave(path):
|
def loadSave(path="save.json"):
|
||||||
# Load save.json and return the player's saved state (money, owned baits,
|
# 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
|
# 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
|
# 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.
|
# 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
|
# 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
|
# (after a sale, after a purchase, on quit) so progress persists between
|
||||||
# sessions. Keep the written structure identical to what loadSave expects.
|
# sessions. Keep the written structure identical to what loadSave expects.
|
||||||
pass
|
with open(path, "w") as f: #overwrite existing
|
||||||
|
f.write(json.dumps(saveState))
|
||||||
|
|
||||||
|
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
@@ -66,17 +79,52 @@ def writeSave(path, saveState):
|
|||||||
# No drawing and no event handling here - just state.
|
# No drawing and no event handling here - just state.
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
|
|
||||||
|
AVAILABLE_UPGRADES = [ # (name, bonus_type, bonus_modifier, price)
|
||||||
|
#power mods
|
||||||
|
("Smart Rod", "power", 2, 100),
|
||||||
|
("Clever Rod", "power", 7, 350),
|
||||||
|
("Intelligent Rod", "power", 10, 1500),
|
||||||
|
("Genius Rod", "power", 20, 20000)
|
||||||
|
]
|
||||||
|
|
||||||
def onAppStart(app):
|
def onAppStart(app):
|
||||||
# cmu_graphics entry point. Initialize ALL app.* state here:
|
# cmu_graphics entry point. Initialize ALL app.* state here:
|
||||||
# - window/layout constants (width, height, dock/water/grass regions)
|
# - window/layout constants (width, height, dock/water/grass regions)
|
||||||
# - app.creatures (from loadCreatures)
|
# - app.creatures (from loadCreatures)
|
||||||
# - app.save (from loadSave) and derived player values
|
# - app.save (from loadSave) and derived player values
|
||||||
# - app.scene (which screen is active: 'dock', 'shop', 'catch', ...)
|
|
||||||
# - fishing state (idle / waiting-for-bite / skill-check / result)
|
# - fishing state (idle / waiting-for-bite / skill-check / result)
|
||||||
# - skill-check state placeholders (cursor angle, bars, speed, result)
|
# - skill-check state placeholders (cursor angle, bars, speed, result)
|
||||||
# - app.stepsPerSecond for the game loop cadence
|
# - app.stepsPerSecond for the game loop cadence
|
||||||
# Delegates to the smaller setup helpers below to keep this readable.
|
# Delegates to the smaller setup helpers below to keep this readable.
|
||||||
pass
|
|
||||||
|
#DRAWING CONSTANTS
|
||||||
|
app.width = 600
|
||||||
|
app.height = 400
|
||||||
|
app.background = 'lightSkyBlue'
|
||||||
|
app.waterColor = 'midnightBlue'
|
||||||
|
app.grassColor = 'seaGreen'
|
||||||
|
app.waterHeight = app.height // 3
|
||||||
|
app.waterBottom = app.height = app.waterHeight
|
||||||
|
app.grassHeight = 20
|
||||||
|
app.grassBottom = app.height - app.waterHeight - app.grassHeight
|
||||||
|
|
||||||
|
#GAME STATE/SETUP
|
||||||
|
app.stepsPerSecond = 30
|
||||||
|
app.creatures = loadCreatures()
|
||||||
|
app.save = loadSave()
|
||||||
|
app.scene = "idle" # idle, waiting-for-bite, skill-check, result, shopping, menu
|
||||||
|
|
||||||
|
#SKILL CHECK
|
||||||
|
app.skillCheckAngle = 0
|
||||||
|
app.skillCheckSpeed = 1
|
||||||
|
app.skillCheckBars = []
|
||||||
|
app.skillCheckSpeed = 1
|
||||||
|
app.skillCheckFailed = False
|
||||||
|
|
||||||
|
#FISHING
|
||||||
|
app.fishingTimer = 0
|
||||||
|
app.hookedFish = None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def resetFishingState(app):
|
def resetFishingState(app):
|
||||||
@@ -102,6 +150,12 @@ def startSkillCheck(app, creature):
|
|||||||
# bar length/width. Also apply any owned upgrades that modify these values
|
# bar length/width. Also apply any owned upgrades that modify these values
|
||||||
# (e.g. slower cursor, wider bars, extra forgiveness). Stores the resulting
|
# (e.g. slower cursor, wider bars, extra forgiveness). Stores the resulting
|
||||||
# parameters on app for the controller/view to use.
|
# parameters on app for the controller/view to use.
|
||||||
|
app.scene = "skill-check"
|
||||||
|
difficulty = creature["difficulty"]
|
||||||
|
|
||||||
|
fishingPower = 1 + sum(upgrade[2] for upgrade in AVAILABLE_UPGRADES if (upgrade[0] in app.save["owned_upgrades"] and upgrade[1] == "power"))
|
||||||
|
|
||||||
|
app.skillCheckSpeed = difficulty / fishingPower
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user