trivia.py

Created by ccsio

Created on September 24, 2023

2.07 KB

A simple trivia game. You need to install the trivia_data.py module as well. You can create your own questions in that file, following the format.


from kandinsky import draw_string as ds, fill_rect as fr, color
from ion import keydown as kd, KEY_ONE, KEY_TWO, KEY_THREE, KEY_FOUR
from random import randint
from time import sleep
try:
    from trivia_data import data
except:
    raise Exception("cant find 'trivia_data' module")
white = color(248, 252, 248)
red = color(248,0,0)
score,R,W = 0,0,0
opts = [True]*4
def new(dat):
    data.remove(dat)
    qst,a = dat[0],dat[1]
    words = qst.split(" ")
    l1,l2 = "",""
    for i in range(len(words)):
        if len(l1) + len(words[i]) <= 31:
            l1 += words[i] + " "
        else:
            for j in range(i,len(words)):
                l2 += words[j] + " "
            break
    fr(0,20,320,220,white)
    ds(l1,int(160-5*len(l1)),20)
    ds(l2,int(160-5*len(l2)),37)
    for i,j,l in list(zip(a,(60,77,94,111), range(1,5))):
        ds(str(l)+")"+i,0,j)
    return dat[2]
def stats():
    fr(0,0,320,18,white)
    ds("W:"+str(W)+"  R:"+str(R),2,0)
    ds(str(score),315-10*len(str(score)),0)
def r_ans(p):
    global R; R += 1
    global score; score += p
    global opts; opts = [True]*4
    stats()
    points=10
    if len(data)==0:
        fr(0,18,320,240,white)
        ds("You Won!",120,93)
        while True:
            if kd(KEY_ONE):
                raise Exception("")
    else:
        return new(data[randint(0,len(data)-1)]), points
def w_ans(idx,p):
    global W; W += 1
    stats()
    p -= 3
    if p < 0:
        p = 0
    opts[idx-1] = False
    fr(0,60+17*(idx-1),320,17,white)
    ds(str(idx)+") Wrong Answer!",0,60+17*(idx-1),red)
    return p
stats()
cr = new(data[randint(0,len(data)-1)])
points = 0
while True:
    if (kd(KEY_ONE) and cr == 1) or (kd(KEY_TWO) and cr == 2) or (kd(KEY_THREE) and cr == 3) or (kd(KEY_FOUR) and cr == 4):
        cr,points = r_ans(points)
        sleep(0.2)
    elif kd(KEY_ONE) and opts[0]:
        points = w_ans(1,points)
    elif kd(KEY_TWO) and opts[1]:
        points = w_ans(2,points)
    elif kd(KEY_THREE) and opts[2]:
        points = w_ans(3,points)
    elif kd(KEY_FOUR) and opts[3]:
        points = w_ans(4,points)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.