Attempt to fix traceback on game exit with some older games.

This commit is contained in:
Storm Dragon
2025-03-14 23:10:14 -04:00
parent 2ad22ff1ae
commit be6dfdf53a
9 changed files with 483 additions and 457 deletions

View File

@ -28,19 +28,19 @@ class Speech:
def __init__(self):
"""Initialize speech system with available provider."""
# Handle speech delays so we don't get stuttering
self.last_spoken = {"text": None, "time": 0}
self.speech_delay = 250 # ms
self.lastSpoken = {"text": None, "time": 0}
self.speechDelay = 250 # ms
# Try to initialize a speech provider
self.provider = None
self.provider_name = None
self.providerName = None
# Try speechd first
try:
import speechd
self.spd = speechd.Client()
self.provider = self.spd
self.provider_name = "speechd"
self.providerName = "speechd"
return
except ImportError:
pass
@ -50,7 +50,7 @@ class Speech:
import accessible_output2.outputs.auto
self.ao2 = accessible_output2.outputs.auto.Auto()
self.provider = self.ao2
self.provider_name = "accessible_output2"
self.providerName = "accessible_output2"
return
except ImportError:
pass
@ -68,23 +68,23 @@ class Speech:
if not self.provider:
return
current_time = pygame.time.get_ticks()
currentTime = pygame.time.get_ticks()
# Check if this is the same text within the delay window
if (self.last_spoken["text"] == text and
current_time - self.last_spoken["time"] < self.speech_delay):
if (self.lastSpoken["text"] == text and
currentTime - self.lastSpoken["time"] < self.speechDelay):
return
# Update last spoken tracking
self.last_spoken["text"] = text
self.last_spoken["time"] = current_time
self.lastSpoken["text"] = text
self.lastSpoken["time"] = currentTime
# Proceed with speech
if self.provider_name == "speechd":
if self.providerName == "speechd":
if interrupt:
self.spd.cancel()
self.spd.say(text)
elif self.provider_name == "accessible_output2":
elif self.providerName == "accessible_output2":
self.ao2.speak(text, interrupt=interrupt)
# Display the text on screen
@ -94,29 +94,29 @@ class Speech:
font = pygame.font.Font(None, 36)
# Wrap the text
max_width = screen.get_width() - 40 # Leave a 20-pixel margin on each side
wrapped_text = textwrap.wrap(text, width=max_width // font.size('A')[0])
maxWidth = screen.get_width() - 40 # Leave a 20-pixel margin on each side
wrappedText = textwrap.wrap(text, width=maxWidth // font.size('A')[0])
# Render each line
text_surfaces = [font.render(line, True, (255, 255, 255)) for line in wrapped_text]
textSurfaces = [font.render(line, True, (255, 255, 255)) for line in wrappedText]
screen.fill((0, 0, 0)) # Clear screen with black
# Calculate total height of text block
total_height = sum(surface.get_height() for surface in text_surfaces)
totalHeight = sum(surface.get_height() for surface in textSurfaces)
# Start y-position (centered vertically)
current_y = (screen.get_height() - total_height) // 2
currentY = (screen.get_height() - totalHeight) // 2
# Blit each line of text
for surface in text_surfaces:
text_rect = surface.get_rect(center=(screen.get_width() // 2, current_y + surface.get_height() // 2))
screen.blit(surface, text_rect)
current_y += surface.get_height()
for surface in textSurfaces:
textRect = surface.get_rect(center=(screen.get_width() // 2, currentY + surface.get_height() // 2))
screen.blit(surface, textRect)
currentY += surface.get_height()
pygame.display.flip()
def close(self):
"""Clean up speech resources."""
if self.provider_name == "speechd":
if self.providerName == "speechd":
self.spd.close()
# Global instance for backward compatibility
_speech_instance = None
_speechInstance = None
def speak(text, interrupt=True):
"""Speak text using the global speech instance.
@ -125,10 +125,10 @@ def speak(text, interrupt=True):
text (str): Text to speak and display
interrupt (bool): Whether to interrupt current speech (default: True)
"""
global _speech_instance
if _speech_instance is None:
_speech_instance = Speech.get_instance()
_speech_instance.speak(text, interrupt)
global _speechInstance
if _speechInstance is None:
_speechInstance = Speech.get_instance()
_speechInstance.speak(text, interrupt)
def messagebox(text):
"""Display a simple message box with text.