Skip to content

Instantly share code, notes, and snippets.

@wishrohitv
Created December 8, 2023 15:53
Show Gist options
  • Save wishrohitv/39b0e95e219659caae055c9571614ba8 to your computer and use it in GitHub Desktop.
Save wishrohitv/39b0e95e219659caae055c9571614ba8 to your computer and use it in GitHub Desktop.
kivy clock implementation
class Quiz_Game(Screen):
def on_enter(self, *args):
# user's option selection choice
self.users_selected_opt = []
# game increment questions
self.number_of_question_list = []
# currently unused
self.answers = []
# question index
self.qnr = 0
# timer
self.timer_countdown = 10
# game score
self.total_wrong_answer = 0
self.total_right_answer = 0
with open("qus_set.json") as r:
self.question = json.load(r)
# appending question number of question to a list
for i in self.question:
self.number_of_question_list.append(i)
# starting game on screen opening
self.next()
# next question button for changing question for user
def next(self):
# game score
self.game_score()
# starting timer
self.timer_count()
# checking if number question in list and preventing crash
if len(self.number_of_question_list) > self.qnr:
# question increment
self.qnr += 1
# getting question overview from json through list index
ii = self.number_of_question_list[self.qnr - 1]
# showing question number to gui game
self.ids.qn.text = f"Q{self.qnr}"
# showing questions and options on screen
self.ids.questions.text = self.question.get(ii)["question"]
self.ids.q1.text = self.question.get(ii)["options"]["1"]
self.ids.q2.text = self.question.get(ii)["options"]["2"]
self.ids.q3.text = self.question.get(ii)["options"]["3"]
self.ids.q4.text = self.question.get(ii)["options"]["4"]
# after changing to next question making selected button back to normal
self.ids.q1.state = "normal"
self.ids.q2.state = "normal"
self.ids.q3.state = "normal"
self.ids.q4.state = "normal"
self.ids.q1.background_color = "#A2FFD1"
self.ids.q2.background_color = "#A2FFD1"
self.ids.q3.background_color = "#A2FFD1"
self.ids.q4.background_color = "#A2FFD1"
# decrement question
self.prev_ = self.qnr
def display_answers(self, qn, *args):
list_index = int(qn[1:])
ans = self.question.get(qn.lower())["answer"]
# print(ans)
usr_ans = self.users_selected_opt[-1][qn]["opt"]
usr_ans_state = self.users_selected_opt[-1][qn]["state"]
# print(usr_ans)
match usr_ans[0]:
case "opt1":
if usr_ans_state:
if ans == usr_ans[1]:
self.ids.q1.background_color = "green"
self.total_right_answer += 1
else:
self.ids.q1.background_color = "red"
self.total_wrong_answer += 1
case "opt2":
if usr_ans_state:
if ans == usr_ans[1]:
self.ids.q2.background_color = "green"
self.total_right_answer += 1
else:
self.ids.q2.background_color = "red"
self.total_wrong_answer += 1
case "opt3":
if usr_ans_state:
if ans == usr_ans[1]:
self.ids.q3.background_color = "green"
self.total_right_answer += 1
else:
self.ids.q3.background_color = "red"
self.total_wrong_answer += 1
case "opt4":
if usr_ans_state:
if ans == usr_ans[1]:
self.ids.q4.background_color = "green"
self.total_right_answer += 1
else:
self.ids.q4.background_color = "red"
self.total_wrong_answer += 1
# previous button
def prev(self):
self.prev_ -= 1
# print(self.prev_, "testing")
if self.prev_ >= 0:
ii = self.number_of_question_list[self.prev_]
self.ids.qn.text = f"Q{self.prev_}"
self.ids.questions.text = self.question.get(ii)["question"]
self.ids.q1.text = self.question.get(ii)["options"]["1"]
self.ids.q2.text = self.question.get(ii)["options"]["2"]
self.ids.q3.text = self.question.get(ii)["options"]["3"]
self.ids.q4.text = self.question.get(ii)["options"]["4"]
# making question state back to normal
self.ids.q1.state = "normal"
self.ids.q2.state = "normal"
self.ids.q3.state = "normal"
self.ids.q4.state = "normal"
self.ids.q1.background_color = "#A2FFD1"
self.ids.q2.background_color = "#A2FFD1"
self.ids.q3.background_color = "#A2FFD1"
self.ids.q4.background_color = "#A2FFD1"
# showing questions red or green
def selected_que(self, qn, ans_dict, value):
# checking only this function work on next question not on previous
if self.qnr == int(qn[1:]):
self.users_selected_opt.append(ans_dict)
Clock.schedule_once(partial(self.display_answers, qn), .6)
# print(ans_dict)
# game timer
def timer_count(self):
self.looper = Clock.schedule_interval(self.timer_logic, 1)
def timer_logic(self, *args):
self.timer_countdown -= 1
if self.timer_countdown > -1:
mins, secs = divmod(self.timer_countdown, 60)
timers = '{:02d}:{:02d}'.format(mins, secs)
self.ids.timer.text = timers
# print(f"hii, {self.timer_countdown}")
if self.timer_countdown < 5:
self.anim = Animation(color=(0, 0, 0, 0), duration=0.1) + Animation(color=(0, 0, 0, 0), duration=.1)
self.anim += Animation(color=(1, 0, 0, 1), duration=0.1) + Animation(color=(1, 0, 0, 1), duration=.1)
self.anim.repeat = False
self.anim.start(self.ids.timer)
elif self.timer_countdown == 0:
self.anim.stop(self.ids.timer)
else:
self.looper.cancel()
self.timer_countdown = 10
print("time up!")
def timer_stop_start(self):
self.looper.cancel()
def game_score(self):
self.ids.correct_ans.text = f"Your Correct Answer\n {self.total_right_answer}"
self.ids.wrong_ans.text = f"Your Wrong Answer\n {self.total_wrong_answer}"
self.ids.total_score.text = f"Your Total Score\n {self.total_right_answer - self.total_wrong_answer}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment