CDT-1D / topic_screen.py
topic_screen.py
Raw
import random
from tkinter import *
from initialise import *
from random import choice
from copy import deepcopy
from Questions import topic_list, boss_topic_list
from random import uniform
from char_screen import change_to_next
from Characters import *


# check boss
def boss_rng(stats):
    # unpack boss chance from stats
    correct_chance, wrong_chance = stats["bosschance"]
    correct_chance_lower, correct_chance_upper = correct_chance
    wrong_chance_lower, wrong_chance_upper = wrong_chance

    # calculate number of correct and wrong answers since last_boss
    correct_last_boss = last_boss[0]
    wrong_last_boss = last_boss[1]
    # calculate number of correct and wrong answers for boss questions
    correct_last_boss2 = last_boss2[0]
    wrong_last_boss2 = last_boss2[1]
    correct_boss = stats["correct"][0] - correct_last_boss - correct_last_boss2
    wrong_boss = stats["progress"][0] - stats["correct"][0] - wrong_last_boss - wrong_last_boss2 - 1

    # calculate rng
    rng = 0
    for i in range(correct_boss):
        rng += uniform(correct_chance_lower, correct_chance_upper)
    for i in range(wrong_boss):
        rng += uniform(wrong_chance_lower, wrong_chance_upper)
    # check boss appearance
    if rng > 3:  # create a 50-50 chance for appearance of boss qn after 3 correct or 2 correct + 2 wrong
        last_boss[0] += correct_boss
        last_boss[1] += wrong_boss
        return True
    else:
        return False


# randomly pick 3 topics to be displayed as options
def random_topic_generator(stat):
    global a
    a = choice(temp_topic_list)
    temp_topic_list.remove(a)
    global b
    b = choice(temp_topic_list)
    temp_topic_list.remove(b)
    if boss_rng(stat):
        global c
        c = choice(boss_topic_list)
        boss_appearance[0] = "True"
    else:
        c = choice(temp_topic_list)
        temp_topic_list.remove(c)
        boss_appearance[0] = "False"


# create a temp_list of data to remove elements so the original copy is not affected
def generate_temp_list():
    global temp_topic_list
    temp_topic_list = deepcopy(topic_list)


# call on multiplier values for a certain topic for a certain character
def check_multiplier(topic):
    if starting_char[0] == "Normie":
        if topic in normie_mult:
            return normie_mult[topic]
    if starting_char[0] == "Nerd":
        if topic in nerd_mult:
            return nerd_mult[topic]
    if starting_char[0] == "Jock":
        if topic in jock_mult:
            return jock_mult[topic]
    if starting_char[0] == "Boomer":
        if topic in boomer_mult:
            return boomer_mult[topic]
    if starting_char[0] == "Millenial":
        if topic in millenial_mult:
            return millenial_mult[topic]
    if starting_char[0] == "Teacher's Pet":
        if topic in teacherpet_mult:
            return teacherpet_mult[topic]


# topic screen display
def call_topic(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 and c
    generate_temp_list()
    random_topic_generator(stat)

    # label widgets
    topic_label = Label(frame, text="Choose your topic!")
    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
    topic_label.grid(row=0, column=2)
    hp_label.grid(row=1, column=0)
    progress_label.grid(row=1, column=2)
    points_label.grid(row=1, column=4)

    # create buttons for topics to be chosen with character's multiplier for that topic displayed under name of topic
    topic1 = Button(frame, text=f"{a} \n Multiplier: {check_multiplier(a)}", command=lambda: change_to_next(frame, a))
    topic2 = Button(frame, text=f"{b} \n Multiplier:{check_multiplier(b)}", command=lambda: change_to_next(frame, b))
    topic3 = Button(frame, text=f"{c} \n Multiplier: {check_multiplier(c)}", command=lambda: change_to_next(frame, c))

    # buttons positioning
    topic1.grid(row=2, column=0)
    topic2.grid(row=2, column=2)
    topic3.grid(row=2, column=4)

    # change call value of this screen (Prevent repeat call of topic_screen here)
    call_value_dict["topic"] = False


boss_triggered = False


# function to change frames
def change_to_next(frame, topic):
    # destroy current frame
    frame.grid_forget()
    # store what is clicked
    chosen_topic[0] = topic
    # change call value of next screen (Calls next screen)
    call_value_dict["question"] = True
    # change update value of this screen (updates this)
    update_value_dict["topic"] = True