Python Forum
quiz game - problem with loading statements from file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
quiz game - problem with loading statements from file
#1
Hello, I'm trying to create a simple myth/fact quiz using tkinter. The game should be for two players - the player who presses the key first (the red team has the "R" key and the blue team has the "B" key) can answer (click) on the myth/fact. If he answers correctly, the score will be increased by 10... statements for which it will be argued whether they are myth or fact are from the file. Each line contains a new statement and also a correct answer (for example, one line from file looks like this: Going into space makes you weightless.=myth)
the problem is that I don't know how to load only one line at a time, wait until someone presses the r/b key, evaluate the answer and only then show the next question. It also dont show the score correctly.

I have tried to change it many times but I am still unsuccessful. Anyone have any ideas? I would be very grateful.


import tkinter
import random
import keyboard

root = tkinter.Tk()
root.title("Quiz Game")
        
canvas = tkinter.Canvas(root, width=1500, height=800, bg = "lightgreen")
canvas.pack()
        
x = 750
y = 400
def score(score_red,score_blue):
    canvas.create_text(x+500,y-230, text=score_blue, fill = "white", font = "Arial 30")
    canvas.create_text(x-400,y-230, text=score_red, fill = "white", font = "Arial 30")
    
def mythfact():
    canvas.create_text(x,y-250, text = "1", font = "Arial 40")
    def mytus():
        canvas.create_rectangle(x-700,y-50,x-10,y+200, fill = "pink")
        canvas.create_text(x-350,y+80, text = "MYTH", font = "Arial 60")
    def fakt():
        canvas.create_rectangle(x+10,y-50,x+700,y+200, fill = "pink")
        canvas.create_text(x+350,y+80, text = "FACT", font = "Arial 60")
    mytus()
    fakt()
mythfact()
def check():
    global score_red, score_blue
    f = open("fakt_mytus.txt", "r", encoding = "utf-8")
    for i,row in enumerate(f):
        g = row.split("=")
        statement = canvas.create_text(x,y-70, text = g[0], font = "Arial 30")
    def klik_1(suradnice):
        xx = suradnice.x
        yy = suradnice.y
        score(score_red,score_blue)
##        if keyboard.read_key() == "b" or "a":
        if x-700<xx<x-10 and y-50<yy<y+200:
            if (g[1]=="mytus"):
                right = canvas.create_text(x,y+250,text= "right", font = "Arial 60")
                canvas.update()
                canvas.after(1000)
                canvas.delete(right)
##                if keyboard.read_key() == "b":
##                    score_blue = score_blue + 10
##                if keyboard.read_key() == "r":
##                    score_red = score_red +10
            else:
                wrong = canvas.create_text(x,y, text = "wrong", font = "Arial 60")
                canvas.update()
                canvas.after(1000)
                canvas.delete(wrong)
        if x+10<xx<x+700 and y-50 <yy<y+200:
            if (g[1]=="fakt"):
                right = canvas.create_text(x,y,text="rigth", font = "Arial 60")
                canvas.update()
                canvas.after(1000)
                canvas.delete(right)
##                if keyboard.read_key() == "b":
##                    score_blue = score_blue + 10
##                if keyboard.read_key() == "r":
##                    score_red = score_red +10
            else:
                wrong = canvas.create_text(x,y,text = "wrong", font = "Arial 60")
                canvas.update()
                canvas.after(1000)
                canvas.delete(wrong)
    canvas.bind("<Button-1>", klik_1)
check()
score_red = 0
score_blue = 0

root.mainloop()

Attached Files

.txt   fakt_mytus.txt (Size: 340 bytes / Downloads: 87)
.py   1_kolo_anj.py (Size: 2.69 KB / Downloads: 99)
Reply
#2
Maybe I can offer some advice, which may or may not be endorsed...

I'm not seeing a f.close() in your script, but that aside, it may be better to use a file handler (which does not require the .close()) and also a .csv file, rather than to have the Q and A separated with a =.

Something like this:

import csv
with open("fakt_mytus.csv", mode='r', encoding='utf-8') as file:
    QandA = csv.reader(file)
    for q, a in QandA:
        # the rest of you code in this for loop
        print(q, a) # this is just for display and P.O.C
Just in case you don't get it, 'q' means 'question' and 'a' means 'answer' (as good as your English is, I can see it's not your first language).

You could have a variable object to count the number of 'reads' done, if you want that number, as you have with your enumerate() function.

I'd get that working as you want it to, then move on the the 'scores' problem, if I were you.

edit to add: it could also be better to have the QandA read as a function, which you call each time you need to, rather than (as is) a part of the check() function. I'm just 'spit balling' suggestions here while thinking about the logic flow.

Or maybe even read the entire file into an object (maybe a dictionary) from which you could then pull a 'q' and 'a' in some kind of a random order, rather than in a sequential way.
jefsummers likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
I think you are making this very difficult for yourself.

Instead of canvas and text you should use frames tkinter controls. I would make the score display and question a label and the myth and fact buttons.

Instead of using an event loop to occasionally check for a key press I would bind the key press to call a function.

Instead of looking for click events I would make myth and fact buttons and use button command callbacks.

This makes the logic fairly easy.

loop begin
ask question
wait for key press
wait for myth or fact button press
update score
repeat loop

But this being a GUI program, the waiting and the looping are event driven. There should be no sleep() statements or for or while loops.

When program starts, display the first question.
When a player key is pressed, lock out the keyboard and enable the Myth and Fact buttons
When a myth or fact button is pressed update the score, disable the Myth and Fact buttons, unlock the keyboard and display the next question.

I would use a variable to "lock" the keyboard. I would set the variable to None to indicate that no player has buzzed in. When a player hits their key, it calls a function. The function checks if the variable. If it is None, the function sets the variable to indicate what player buzzed in and enables the Myth and Fact buttons to provide feedback to the players. It might also update a label or some other display to indicate who buzzed in first. If the variable is not None, somebody already buzzed in and the keypress is ignored.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with my Black Jack game... JengaBenga 2 1,243 Sep-27-2022, 01:10 PM
Last Post: JengaBenga
  Using If Statements Instead of While Loop in Simple Game Program new_coder_231013 5 3,073 Dec-14-2021, 12:23 AM
Last Post: supuflounder
  Quoridor game. Problem: inserting player positions and walls in a checkerboard Roidesflammes 1 3,106 Aug-03-2021, 06:52 PM
Last Post: riii
  problem on creating a small game nayo43 5 2,698 Dec-13-2020, 01:03 PM
Last Post: jefsummers
  Running my game file yields a different error on a different computer system Bruizeh 0 1,449 Nov-10-2020, 03:15 AM
Last Post: Bruizeh
  Basic Quiz/Game searching1 10 5,567 Nov-18-2018, 03:58 AM
Last Post: ichabod801
  help setting questions and answers in quiz game? yoyoitsjess 3 3,612 May-10-2018, 07:35 AM
Last Post: buran
  Quiz Game Help (ASAP Please!) beginnercoder04 2 3,159 Apr-15-2018, 04:13 AM
Last Post: beginnercoder04

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020