Skip to content

Commit 1f2a973

Browse files
Initial Commit
0 parents  commit 1f2a973

File tree

9 files changed

+137
-0
lines changed

9 files changed

+137
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/venv
2+
.git
3+
.idea
4+
__pycache__
5+
.DS_Store

data.py

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

images/false.png

2.8 KB
Loading

images/true.png

2.5 KB
Loading

main.py

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

question_model.py

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

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, question_list):
7+
self.question_list = question_list
8+
self.question_no = 0
9+
self.score = 0
10+
self.current_question = None
11+
12+
def ask_question(self):
13+
self.current_question = self.question_list[self.question_no]
14+
self.question_no += 1
15+
q_text = html.unescape(self.current_question.text)
16+
return f"Q.{self.question_no}: {q_text} \nTrue or False?"
17+
18+
def check_answer(self, user_answer: str) -> bool:
19+
if user_answer.lower() == self.current_question.answer.lower():
20+
self.score += 1
21+
return True
22+
return False
23+
24+
def still_has_questions(self):
25+
return self.question_no < len(self.question_list)
26+
27+
def get_score(self):
28+
return self.score

readme.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Quiz Game
2+
3+
A simple quiz game in Python that asks you questions with True or False answers and keeps track of your score. This app uses the [tkinter GUI](https://docs.python.org/3/library/tkinter.html) for the UI and the [Open Trivia Database](https://opentdb.com/) for the questions.
4+
5+
### External Libraries
6+
1. [requests](https://requests.readthedocs.io/en/latest/)

ui.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
14+
self.window.title("Quiz")
15+
self.window.config(bg=THEME_COLOR, pady=20, padx=20)
16+
17+
self.score_board = Label(text=f"Score: {0}", font=("Arial", 16, "bold"), fg="white", highlightthickness=0, bg=THEME_COLOR)
18+
self.score_board.grid(row=0, column=0, columnspan=2)
19+
20+
self.canvas = Canvas(height=250, width=300, bg="white", highlightthickness=0)
21+
self.question_text = self.canvas.create_text(150, 125, text="Hey there", fill=THEME_COLOR, font=("Arial", 20, "italic"), width=280)
22+
self.canvas.grid(row=1, column=0, columnspan=2, pady=50)
23+
24+
true_img = PhotoImage(file="images/true.png")
25+
self.true_button = Button(image=true_img, highlightthickness=0, highlightbackground=THEME_COLOR, command=lambda: self.on_press("true"))
26+
self.true_button.grid(row=2, column=0)
27+
28+
false_img = PhotoImage(file="images/false.png")
29+
self.false_button = Button(image=false_img, highlightthickness=0, highlightbackground=THEME_COLOR, command=lambda: self.on_press("false"))
30+
self.false_button.grid(row=2, column=1)
31+
32+
self.ask_question()
33+
34+
self.window.mainloop()
35+
36+
def ask_question(self):
37+
self.canvas.config(bg="white")
38+
if self.quiz.still_has_questions():
39+
q_text = self.quiz.ask_question()
40+
self.canvas.itemconfig(self.question_text, text=q_text)
41+
else:
42+
self.canvas.itemconfig(self.question_text, text=f"You've reached the end of the quiz. Your final score is {self.quiz.get_score()}.")
43+
self.true_button.config(state="disabled")
44+
self.false_button.config(state="disabled")
45+
46+
def on_press(self, user_answer):
47+
timer = None
48+
if timer is not None:
49+
self.window.after_cancel(timer)
50+
51+
is_right = self.quiz.check_answer(user_answer)
52+
if is_right:
53+
self.update_score_board()
54+
self.give_feedback(is_right)
55+
56+
timer = self.window.after(1000, self.ask_question)
57+
58+
def update_score_board(self):
59+
self.score_board.config(text=f"Score: {self.quiz.get_score()}")
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")

0 commit comments

Comments
 (0)