Skip to content

Commit 9942017

Browse files
committed
added Quiz Game
1 parent cee1a59 commit 9942017

16 files changed

+153
-0
lines changed

Quiz Game/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
**Quiz Game**
2+
3+
**GOAL**
4+
The Goal of this game is to create a Quiz game.
5+
6+
**DESCRIPTION**
7+
This is a simple Quiz Game app, which fetchs questions from opentrivia and then tell user if given answer is correct, or wrong.
8+
9+
### LIBRARIES NEEDED
10+
11+
1. Tkinter: provides useful GUI functionalities.
12+
13+
**WHAT I HAD DONE**
14+
I have used HTML unescaping & created UI using Tkinter and built a Quiz Game using API from open Trivia Database that helps review API endpoints and parameters.
15+
16+
### DEMONSTRATION
17+
(https://drive.google.com/drive/folders/10w8ITuu04ra6uKS-rockJmNyM5K4uvVk?usp=share_link)
18+
![](Images/quizzler.gif)
19+
20+
### Sarthak Joleya ###
580 Bytes
Binary file not shown.
Binary file not shown.
1.79 KB
Binary file not shown.
4.61 KB
Binary file not shown.

Quiz Game/data.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import requests
2+
3+
parameters = {
4+
"amount": 10,
5+
"type": "boolean",
6+
}
7+
8+
response = requests.get("https://opentdb.com/api.php", params=parameters)
9+
response.raise_for_status()
10+
data = response.json()
11+
question_data = data["results"]
Loading
Loading
Loading

Quiz Game/images/quizzler.gif

2.65 MB
Loading

Quiz Game/main.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from question_model import Question
2+
from data import question_data
3+
from quiz_brain import QuizBrain
4+
from ui import QuizInterface
5+
6+
question_bank = []
7+
for question in question_data:
8+
question_text = question["question"]
9+
question_answer = question["correct_answer"]
10+
new_question = Question(question_text, question_answer)
11+
question_bank.append(new_question)
12+
13+
14+
quiz = QuizBrain(question_bank)
15+
quiz_ui = QuizInterface(quiz)

Quiz Game/question_model.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Question:
2+
3+
def __init__(self, q_text, q_answer):
4+
self.text = q_text
5+
self.answer = q_answer

Quiz Game/quiz_brain.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import html
2+
3+
4+
class QuizBrain:
5+
6+
def __init__(self, q_list):
7+
self.question_number = 0
8+
self.score = 0
9+
self.question_list = q_list
10+
self.current_question = None
11+
12+
def still_has_questions(self):
13+
return self.question_number < len(self.question_list)
14+
15+
def next_question(self):
16+
self.current_question = self.question_list[self.question_number]
17+
self.question_number += 1
18+
q_text = html.unescape(self.current_question.text)
19+
return f"Q.{self.question_number}: {q_text}"
20+
21+
def check_answer(self, user_answer):
22+
correct_answer = self.current_question.answer
23+
if user_answer.lower() == correct_answer.lower():
24+
self.score += 1
25+
return True
26+
else:
27+
return False
28+

Quiz Game/resources/false.png

2.8 KB
Loading

Quiz Game/resources/true.png

2.5 KB
Loading

Quiz Game/ui.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from tkinter import *
2+
from quiz_brain import QuizBrain
3+
4+
THEME_COLOR = "#375362"
5+
6+
7+
class QuizInterface:
8+
9+
def __init__(self, quiz_brain: QuizBrain):
10+
self.quiz = quiz_brain
11+
12+
self.window = Tk()
13+
self.window.title("Quiz Game")
14+
self.window.config(padx=20, pady=20, bg=THEME_COLOR)
15+
16+
self.score_label = Label(text="Score: 0", fg="white", bg=THEME_COLOR)
17+
self.score_label.config(font=("Times New Roman", 20))
18+
self.score_label.grid(row=0, column=1)
19+
20+
self.canvas = Canvas(width=300, height=250, bg="white")
21+
self.question_text = self.canvas.create_text(
22+
150,
23+
125,
24+
width=280,
25+
text="Some Question Text",
26+
fill=THEME_COLOR,
27+
font=("Arial", 20, "italic")
28+
)
29+
self.canvas.grid(row=1, column=0, columnspan=2, pady=50)
30+
31+
true_image = PhotoImage(file="resources/true.png")
32+
self.true_button = Button(image=true_image, highlightthickness=0, command=self.true_pressed)
33+
self.true_button.grid(row=2, column=0)
34+
35+
false_image = PhotoImage(file="resources/false.png")
36+
self.false_button = Button(image=false_image, highlightthickness=0, command=self.false_pressed)
37+
self.false_button.grid(row=2, column=1)
38+
39+
self.get_next_question()
40+
41+
self.window.mainloop()
42+
43+
def get_next_question(self):
44+
self.canvas.config(bg="white")
45+
if self.quiz.still_has_questions():
46+
self.score_label.config(text=f"Score: {self.quiz.score}")
47+
q_text = self.quiz.next_question()
48+
self.canvas.itemconfig(self.question_text, text=q_text)
49+
else:
50+
self.canvas.itemconfig(self.question_text, text="You've reached the end of the quiz.")
51+
self.true_button.config(state="disabled")
52+
self.false_button.config(state="disabled")
53+
54+
def true_pressed(self):
55+
self.give_feedback(self.quiz.check_answer("True"))
56+
57+
def false_pressed(self):
58+
is_right = self.quiz.check_answer("False")
59+
self.give_feedback(is_right)
60+
61+
def give_feedback(self, is_right):
62+
if is_right:
63+
self.canvas.config(bg="green")
64+
else:
65+
self.canvas.config(bg="red")
66+
self.window.after(1000, self.get_next_question)
67+
68+
69+
70+
71+
72+
73+
74+

0 commit comments

Comments
 (0)