forked from suryanshsk/Python-Voice-Assistant-Suryanshsk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trivia_night.py
125 lines (106 loc) · 4.87 KB
/
trivia_night.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import tkinter as tk
import speech_recognition as sr
import pyttsx3
import requests
import random
class TriviaNightApp:
def __init__(self, root):
self.root = root
self.root.title("Trivia Night")
self.categories = self.fetch_categories()
self.difficulty_levels = ["easy", "medium", "hard"]
self.selected_category = None
self.selected_difficulty = None
self.trivia_questions = []
self.current_question_index = 0
self.engine = pyttsx3.init()
self.label = tk.Label(root, text="Welcome to Trivia Night! Choose your category and difficulty.", wraplength=300)
self.label.pack(pady=20)
self.category_label = tk.Label(root, text="Select a category:")
self.category_label.pack()
self.category_var = tk.StringVar(value=self.categories[0]["id"])
for category in self.categories:
tk.Radiobutton(root, text=category["name"], variable=self.category_var, value=category["id"]).pack(anchor=tk.W)
self.difficulty_label = tk.Label(root, text="Select difficulty:")
self.difficulty_label.pack()
self.difficulty_var = tk.StringVar(value=self.difficulty_levels[0])
for difficulty in self.difficulty_levels:
tk.Radiobutton(root, text=difficulty.capitalize(), variable=self.difficulty_var, value=difficulty).pack(anchor=tk.W)
self.start_button = tk.Button(root, text="Start Trivia", command=self.start_trivia)
self.start_button.pack(pady=10)
def fetch_categories(self):
try:
response = requests.get("https://opentdb.com/api_category.php")
return response.json()["trivia_categories"]
except Exception as e:
self.label.config(text="Failed to fetch categories.")
print(e)
return []
def start_trivia(self):
self.selected_category = self.category_var.get()
self.selected_difficulty = self.difficulty_var.get()
self.trivia_questions = self.fetch_questions(self.selected_category, self.selected_difficulty)
if self.trivia_questions:
random.shuffle(self.trivia_questions)
self.current_question_index = 0
self.show_question()
else:
self.label.config(text="No questions found for this category and difficulty.")
def fetch_questions(self, category_id, difficulty):
try:
response = requests.get(f"https://opentdb.com/api.php?amount=10&category={category_id}&difficulty={difficulty}&type=multiple")
data = response.json()
questions = []
for item in data["results"]:
questions.append({
"question": item["question"],
"correct_answer": item["correct_answer"],
"incorrect_answers": item["incorrect_answers"]
})
return questions
except Exception as e:
self.label.config(text="Failed to fetch questions.")
print(e)
return []
def show_question(self):
if self.current_question_index < len(self.trivia_questions):
question_data = self.trivia_questions[self.current_question_index]
question = question_data["question"]
self.label.config(text=question)
self.ask_question(question)
else:
self.end_game()
def ask_question(self, question):
self.engine.say(question)
self.engine.runAndWait()
def get_voice_input(self):
recognizer = sr.Recognizer()
with sr.Microphone() as source:
self.label.config(text="Listening...")
self.root.update() # Update the UI
audio = recognizer.listen(source)
try:
answer = recognizer.recognize_google(audio).lower()
self.label.config(text=f"You said: {answer}")
self.check_trivia_answer(answer)
except sr.UnknownValueError:
self.label.config(text="Sorry, I did not understand that.")
except sr.RequestError:
self.label.config(text="Could not request results from Google Speech Recognition service.")
def check_trivia_answer(self, user_answer):
correct_answer = self.trivia_questions[self.current_question_index]["correct_answer"].lower()
if user_answer == correct_answer:
self.label.config(text="Correct!")
else:
self.label.config(text=f"Incorrect! The correct answer was: {correct_answer}")
self.current_question_index += 1
self.show_question()
def end_game(self):
self.label.config(text="Thanks for playing Trivia Night! Goodbye!")
self.engine.say("Thanks for playing Trivia Night! Goodbye!")
self.engine.runAndWait()
self.root.quit()
if __name__ == "__main__":
root = tk.Tk()
app = TriviaNightApp(root)
root.mainloop()