Ability to save added, collect 200 bone dust and the game saves. Load a game from the load option in the main menu.

This commit is contained in:
Storm Dragon
2025-06-05 02:56:06 -04:00
parent d1519b13e2
commit 614d78166f
7 changed files with 439 additions and 5 deletions

View File

@@ -9,6 +9,7 @@ from src.level import Level
from src.object import Object
from src.player import Player
from src.game_selection import select_game, get_level_path
from src.save_manager import SaveManager
class WickedQuest:
@@ -22,6 +23,7 @@ class WickedQuest:
self.player = None
self.currentGame = None
self.runLock = False # Toggle behavior of the run keys
self.saveManager = SaveManager()
def load_level(self, levelNumber):
"""Load a level from its JSON file."""
@@ -84,6 +86,92 @@ class WickedQuest:
return errors
def load_game_menu(self):
"""Display load game menu with available saves"""
save_files = self.saveManager.get_save_files()
if not save_files:
messagebox("No save files found.")
return None
# Create menu options
options = []
for save_file in save_files:
options.append(save_file['display_name'])
options.append("Cancel")
# Show menu
currentIndex = 0
lastSpoken = -1
messagebox("Select a save file to load:")
while True:
if currentIndex != lastSpoken:
speak(options[currentIndex])
lastSpoken = currentIndex
event = pygame.event.wait()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return None
elif event.key in [pygame.K_DOWN, pygame.K_s] and currentIndex < len(options) - 1:
currentIndex += 1
try:
self.sounds['menu-move'].play()
except:
pass
elif event.key in [pygame.K_UP, pygame.K_w] and currentIndex > 0:
currentIndex -= 1
try:
self.sounds['menu-move'].play()
except:
pass
elif event.key == pygame.K_RETURN:
try:
self.sounds['menu-select'].play()
except:
pass
if currentIndex == len(options) - 1: # Cancel
return None
else:
return save_files[currentIndex]
pygame.event.clear()
def auto_save(self):
"""Automatically save the game if player has enough bone dust"""
if not self.player.can_save():
return False
# Automatically create save
try:
success, message = self.saveManager.create_save(
self.player,
self.currentLevel.levelId,
self.gameStartTime,
self.currentGame
)
if success:
try:
if 'save' in self.sounds:
play_sound(self.sounds['save'])
else:
print("Save sound not found in sounds dictionary")
except Exception as e:
print(f"Error playing save sound: {e}")
pass # Continue if save sound fails to play
else:
print(f"Save failed: {message}")
return success
except Exception as e:
print(f"Error during save: {e}")
return False
def handle_input(self):
"""Process keyboard input for player actions."""
keys = pygame.key.get_pressed()
@@ -127,7 +215,7 @@ class WickedQuest:
# Status queries
if keys[pygame.K_c]:
speak(f"{player.get_coins()} bone dust")
speak(f"{player.get_coins()} bone dust for extra lives, {player.get_save_bone_dust()} bone dust for saves")
if keys[pygame.K_h]:
speak(f"{player.get_health()} health of {player.get_max_health()}")
if keys[pygame.K_i]:
@@ -213,11 +301,11 @@ class WickedQuest:
cut_scene(self.sounds, "game_over")
display_text(report)
def game_loop(self):
def game_loop(self, startingLevelNum=1):
"""Main game loop handling updates and state changes."""
clock = pygame.time.Clock()
levelStartTime = pygame.time.get_ticks()
currentLevelNum = 1
currentLevelNum = startingLevelNum
while True:
currentTime = pygame.time.get_ticks()
@@ -288,6 +376,8 @@ class WickedQuest:
currentLevelNum += 1
if self.load_level(currentLevelNum):
# Auto save at the beginning of new level if conditions are met
self.auto_save()
levelStartTime = pygame.time.get_ticks() # Reset level timer for new level
continue
else:
@@ -319,10 +409,34 @@ class WickedQuest:
pass
while True:
choice = game_menu(self.sounds)
# Add load game option if saves exist
custom_options = []
if self.saveManager.has_saves():
custom_options.append("load_game")
choice = game_menu(self.sounds, None, *custom_options)
if choice == "exit":
exit_game()
elif choice == "load_game":
selected_save = self.load_game_menu()
if selected_save:
success, save_data = self.saveManager.load_save(selected_save['filepath'])
if success:
# Load the saved game
self.currentGame = save_data['game_state']['currentGame']
self.gameStartTime = save_data['game_state']['gameStartTime']
current_level = save_data['game_state']['currentLevel']
# Load the level
if self.load_level(current_level):
# Restore player state
self.saveManager.restore_player_state(self.player, save_data)
self.game_loop(current_level)
else:
messagebox("Failed to load saved level.")
else:
messagebox(f"Failed to load save: {save_data}")
elif choice == "play":
self.currentGame = select_game(self.sounds)
# Validate level files before starting