Skip to content

Commit

Permalink
Refactored code, improved error handling, optimized rendering, and ad…
Browse files Browse the repository at this point in the history
…ded concurrency.
  • Loading branch information
Ar7109 committed Jun 10, 2024
1 parent 5c679fe commit 4f600a9
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,18 @@


def perform_speech(prompt, villager):

global villager_text
global villager2_text
global villager3_text
global e_not_yet_pressed
global e_not_yet_pressed2

if villager==0:
villager_text=f"..."
elif villager==1:
villager2_text=f"..."
elif villager==2:
villager3_text=f"..."
if villager == 0:
villager_text = "..."
elif villager == 1:
villager2_text = "..."
elif villager == 2:
villager3_text = "..."

try:
# Gemini
Expand All @@ -95,34 +94,36 @@ def perform_speech(prompt, villager):

res_text = response.text

if "json" in res_text:
res_text = res_text.split("```")[1]
res_text = res_text.split("json")[1]

dialog = json.loads(res_text)["dialog"]
# Parse the JSON directly
response_json = json.loads(res_text)
dialog = response_json["dialog"]

print(dialog)

if villager==0:
villager_text=f"Villager 1 says: {dialog}"
elif villager==1:
villager2_text=f"Villager 2 says: {dialog}"
elif villager==2:
villager3_text=f"Villager 3 says: {dialog}"
if villager == 0:
villager_text = f"Villager 1 says: {dialog}"
elif villager == 1:
villager2_text = f"Villager 2 says: {dialog}"
elif villager == 2:
villager3_text = f"Villager 3 says: {dialog}"

# Pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[villager%2].id)
engine.setProperty('voice', voices[villager % 2].id)
print(dialog)
engine.say(dialog)
engine.runAndWait()
except Exception as e:
traceback.print_exc()
if villager==0:
traceback.print_exc()
if villager == 0:
e_not_yet_pressed = True
else:
e_not_yet_pressed = True






def fall(player_rect):
Expand Down Expand Up @@ -357,4 +358,4 @@ def divide_text(string):


pygame.quit()
sys.exit()
sys.exit()

0 comments on commit 4f600a9

Please sign in to comment.