Attempt to make sure exit works even if there's a problem.
This commit is contained in:
		
							
								
								
									
										36
									
								
								menu.py
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								menu.py
									
									
									
									
									
								
							@@ -280,23 +280,38 @@ def donate():
 | 
				
			|||||||
    webbrowser.open('https://ko-fi.com/stormux')
 | 
					    webbrowser.open('https://ko-fi.com/stormux')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def exit_game(fade=0):
 | 
					def exit_game(fade=0):
 | 
				
			||||||
    """Clean up and exit the game.
 | 
					    """Clean up and exit the game properly.
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    Args:
 | 
					    Args:
 | 
				
			||||||
        fade (int): Milliseconds to fade out music before exiting.
 | 
					        fade (int): Milliseconds to fade out music before exiting.
 | 
				
			||||||
                      0 means stop immediately (default)
 | 
					                      0 means stop immediately (default)
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    # Get speech instance and check provider type
 | 
					    # Force clear any pending events to prevent hanging
 | 
				
			||||||
    speech = Speech.get_instance()
 | 
					    pygame.event.clear()
 | 
				
			||||||
    if speech.providerName == "speechd":
 | 
					    
 | 
				
			||||||
        speech.close()
 | 
					    # Stop all mixer channels first
 | 
				
			||||||
 | 
					    try:
 | 
				
			||||||
 | 
					        pygame.mixer.stop()
 | 
				
			||||||
 | 
					    except Exception as e:
 | 
				
			||||||
 | 
					        print(f"Warning: Could not stop mixer channels: {e}")
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # Get speech instance and handle all providers
 | 
				
			||||||
 | 
					    try:
 | 
				
			||||||
 | 
					        speech = Speech.get_instance()
 | 
				
			||||||
 | 
					        # Try to close speech regardless of provider type
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            speech.close()
 | 
				
			||||||
 | 
					        except Exception as e:
 | 
				
			||||||
 | 
					            print(f"Warning: Could not close speech: {e}")
 | 
				
			||||||
 | 
					    except Exception as e:
 | 
				
			||||||
 | 
					        print(f"Warning: Could not get speech instance: {e}")
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    # Handle music based on fade parameter
 | 
					    # Handle music based on fade parameter
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        if fade > 0:
 | 
					        if fade > 0 and pygame.mixer.music.get_busy():
 | 
				
			||||||
            pygame.mixer.music.fadeout(fade)
 | 
					            pygame.mixer.music.fadeout(fade)
 | 
				
			||||||
            # Brief pause to allow fade to start, but not complete
 | 
					            # Wait for fade to start but don't wait for full completion
 | 
				
			||||||
            pygame.time.wait(min(fade // 4, 200))  # Wait up to 200ms maximum
 | 
					            pygame.time.wait(min(250, fade))
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            pygame.mixer.music.stop()
 | 
					            pygame.mixer.music.stop()
 | 
				
			||||||
    except Exception as e:
 | 
					    except Exception as e:
 | 
				
			||||||
@@ -308,5 +323,6 @@ def exit_game(fade=0):
 | 
				
			|||||||
    except Exception as e:
 | 
					    except Exception as e:
 | 
				
			||||||
        print(f"Warning: Error during pygame.quit(): {e}")
 | 
					        print(f"Warning: Error during pygame.quit(): {e}")
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    # Exit the program
 | 
					    # Use os._exit for immediate termination
 | 
				
			||||||
    exit()
 | 
					    import os
 | 
				
			||||||
 | 
					    os._exit(0)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user