CDT-1D / qn_screen.py
qn_screen.py
Raw
from copy import deepcopy
from random import choice
from tkinter import *
from Questions import *
from initialise import *



# create a temp_list of data to remove elements so the original copy is not affected
def generate_temp_list():
    global temp_qn_list
    # getting a copy of the list that contains questions of the topic chosen
    temp_qn_list = deepcopy(topic_dict[chosen_topic[0]])


# randomly pick 1 qn(still packed in list) from a list of qns
def random_qn_generator():
    global qn_chosen
    qn_chosen = choice(temp_qn_list)
    while question_chosen.count(qn_chosen) > 0:
        temp_qn_list.remove(qn_chosen)
        qn_chosen = choice(temp_qn_list)
        question_chosen.append(qn_chosen)
        break

    else:
        question_chosen.append(qn_chosen)


# 4 ans will be randomly assigned positions to be displayed on 4 buttons
def random_ans_generator():
    # slice the tuple to just the 4 ans, excluding qn and "fact-check" ans
    qn_used = qn_chosen[1:5]
    global a
    a = choice(qn_used)
    qn_used.remove(a)
    global b
    b = choice(qn_used)
    qn_used.remove(b)
    global c
    c = choice(qn_used)
    qn_used.remove(c)
    global d
    d = choice(qn_used)


# question screen display
def call_question(root, stat):
    # creating a frame (a canvas that is within the screen that stores the widgets)
    frame = Frame(root)
    frame.tk.call('tk', 'scaling', 2)
    frame.grid()

    # to call a, b, c and d
    generate_temp_list()
    random_qn_generator()
    random_ans_generator()

    # label widgets
    question_label = Label(frame, text=qn_chosen[0])
    hp_label = Label(frame, text=f"HP: {stat['hp'][0]}")
    progress_label = Label(frame, text=f"Progress: {stat['progress'][0]}/10")
    points_label = Label(frame, text=f"Points: {stat['score'][0]}")

    # widgets positioning
    question_label.grid(row=2, column=2)
    hp_label.grid(row=0, column=0)
    progress_label.grid(row=0, column=2)
    points_label.grid(row=0, column=4)

    # create buttons for four options to be chosen as answer to question
    button1 = Button(frame, text=f"{a}", command=lambda: change_to_next(frame, a, stat, root))
    button2 = Button(frame, text=f"{b}", command=lambda: change_to_next(frame, b, stat, root))
    button3 = Button(frame, text=f"{c}", command=lambda: change_to_next(frame, c, stat, root))
    button4 = Button(frame, text=f"{d}", command=lambda: change_to_next(frame, d, stat, root))

    # buttons positioning
    button1.grid(row=3, column=1)
    button2.grid(row=3, column=3)
    button3.grid(row=5, column=1)
    button4.grid(row=5, column=3)

    # change call value of this screen (Prevent repeat call here)
    call_value_dict["question"] = False
    # print(call_value_dict)
    timer_time[0] = stat["time"][0]


# function to change frames
def change_to_next(frame, ans, stat, root):
    question_answer[0] = ans
    if timer_time[0] <= 0:
        question_answer[0] = "Time's up"

    # destroy current frame
    frame.grid_forget()

    # update game progress
    stat["progress"][0] += 1
    # check progress

    # when a boss question is answered correctly, the perk screen is called
    if chosen_topic[0][0:4] == "Boss" and question_answer[0] == qn_chosen[5]:
        call_value_dict["perks"] = True
        call_value_dict["topic"] = False

    # when 10 questions have been answered, the end screen is called
    elif stat["progress"][0] == 10:
        call_value_dict["end"] = True
        call_value_dict["topic"] = False
        result[0] = "Victory"

    # calls topic screen after question is answered
    else:
        call_value_dict["topic"] = True

    # change update value of this screen (updates this)
    update_value_dict["question"] = True


def update_question(stat):
    # create frame for "wrong" or "correct" ans
    # minus hp if answer is wrong
    if question_answer[0] != qn_chosen[5] or question_answer[0] == "Time's up":
        # print("health lost")
        stat["hp"][0] -= 1
        if boss_appearance[0] == "True":
            last_boss2[1] += 1

        # directs player to end screen if question is answered incorrectly and loses all hp
        if stat["hp"][0] == 0:
            call_value_dict["topic"] = False
            call_value_dict["end"] = True
            result[0] = "Game Over"

    # when question is answered correctly, points are added and stat for number of correct qns answered is updated
    else:
        stat["score"][0] += round((100 + 100 * round(timer_time[0], 2)) * stat["mult"][chosen_topic[0]], 1)
        stat["correct"][0] += 1

        if boss_appearance[0] == "True":
            last_boss2[0] += 1

    # stops timer and resets to zero while on this screen
    timer_time[0] = 0.0