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:
		| @@ -40,7 +40,10 @@ class Level: | |||||||
|         self.player.set_footstep_sound(self.footstepSound) |         self.player.set_footstep_sound(self.footstepSound) | ||||||
|  |  | ||||||
|         # Level intro message |         # 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) |         messagebox(levelIntro) | ||||||
|  |  | ||||||
|         # Handle level music |         # Handle level music | ||||||
|   | |||||||
| @@ -31,7 +31,7 @@ class Weapon: | |||||||
|         return cls( |         return cls( | ||||||
|             name="witch_broom", |             name="witch_broom", | ||||||
|             damage=3, |             damage=3, | ||||||
|             range=2.5, |             range=3, | ||||||
|             attackSound="player_broom_attack", |             attackSound="player_broom_attack", | ||||||
|             hitSound="player_broom_hit", |             hitSound="player_broom_hit", | ||||||
|             cooldown=500, |             cooldown=500, | ||||||
|   | |||||||
| @@ -60,6 +60,30 @@ class WickedQuest: | |||||||
|         except FileNotFoundError: |         except FileNotFoundError: | ||||||
|             return False |             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): |     def handle_input(self): | ||||||
|         """Process keyboard input for player actions.""" |         """Process keyboard input for player actions.""" | ||||||
|         keys = pygame.key.get_pressed() |         keys = pygame.key.get_pressed() | ||||||
| @@ -305,6 +329,14 @@ class WickedQuest: | |||||||
|                 exit_game() |                 exit_game() | ||||||
|             elif choice == "play": |             elif choice == "play": | ||||||
|                 self.currentGame = select_game(self.sounds) |                 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: |                 if self.currentGame: | ||||||
|                     self.player = None  # Reset player for new game |                     self.player = None  # Reset player for new game | ||||||
|                     self.gameStartTime = pygame.time.get_ticks() |                     self.gameStartTime = pygame.time.get_ticks() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user