Validate all level data before starting game. No more getting to level 10 before realizing there's an error in the json syntax. Notify players when the level is locked with a boss level message.

This commit is contained in:
Storm Dragon
2025-02-26 02:40:47 -05:00
parent 1b0a6eba83
commit 6ef69a33e6
3 changed files with 37 additions and 2 deletions

View File

@@ -40,7 +40,10 @@ class Level:
self.player.set_footstep_sound(self.footstepSound)
# Level intro message
levelIntro = f"Level {levelData['level_id']}, {levelData['name']}. {levelData['description']}"
levelIntro = f"Level {levelData['level_id']}, {levelData['name']}. "
if self.isLocked:
levelIntro += "This is a boss level. You must defeat all enemies before you can advance. "
levelIntro += levelData['description']
messagebox(levelIntro)
# Handle level music

View File

@@ -31,7 +31,7 @@ class Weapon:
return cls(
name="witch_broom",
damage=3,
range=2.5,
range=3,
attackSound="player_broom_attack",
hitSound="player_broom_hit",
cooldown=500,

View File

@@ -60,6 +60,30 @@ class WickedQuest:
except FileNotFoundError:
return False
def validate_levels(self):
"""Check if level files have valid JSON."""
errors = []
# Check levels from 1 until no more files are found
levelNumber = 1
while True:
levelPath = get_level_path(self.currentGame, levelNumber)
if not os.path.exists(levelPath):
break
try:
with open(levelPath, 'r') as f:
# This will raise an exception if JSON is invalid
json.load(f)
except json.JSONDecodeError as e:
errors.append(f"Level {levelNumber}: Invalid JSON format - {str(e)}")
except Exception as e:
errors.append(f"Level {levelNumber}: Error reading file - {str(e)}")
levelNumber += 1
return errors
def handle_input(self):
"""Process keyboard input for player actions."""
keys = pygame.key.get_pressed()
@@ -305,6 +329,14 @@ class WickedQuest:
exit_game()
elif choice == "play":
self.currentGame = select_game(self.sounds)
# Validate level files before starting
errors = self.validate_levels()
if errors:
errorLines = ["Level files contain errors:"]
errorLines.extend(errors)
errorLines.append("\nPlease fix these errors before playing.")
display_text(errorLines)
continue
if self.currentGame:
self.player = None # Reset player for new game
self.gameStartTime = pygame.time.get_ticks()