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: 93)
.py   1_kolo_anj.py (Size: 2.69 KB / Downloads: 112)
Reply


Messages In This Thread
quiz game - problem with loading statements from file - by lapiduch - Apr-20-2023, 01:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with my Black Jack game... JengaBenga 2 1,297 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,188 Dec-14-2021, 12:23 AM
Last Post: supuflounder
  Quoridor game. Problem: inserting player positions and walls in a checkerboard Roidesflammes 1 3,162 Aug-03-2021, 06:52 PM
Last Post: riii
  problem on creating a small game nayo43 5 2,758 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,483 Nov-10-2020, 03:15 AM
Last Post: Bruizeh
  Basic Quiz/Game searching1 10 5,694 Nov-18-2018, 03:58 AM
Last Post: ichabod801
  help setting questions and answers in quiz game? yoyoitsjess 3 3,667 May-10-2018, 07:35 AM
Last Post: buran
  Quiz Game Help (ASAP Please!) beginnercoder04 2 3,211 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