0

I am trying to give the end user a choice of how long the game will last, and zero lag, with radio buttons. In the code below, everything works as intended: the score increments, and there is no lag. Except for the timer. What am I doing wrong? N.B. I already have a version where the timer works but that has lag. lag == not user friendly.

import random
from tkinter import *

colours = [
    "Red",
    "Blue",
    "Green",
    "Pink",
    "Black",
    "Yellow",
    "Orange",
    "White",
    "Purple",
    "Brown",
]

root = Tk()

root.title("COLOUR GAME")

width = root.winfo_screenwidth()
height = root.winfo_screenheight()

root.geometry("%dx%d" % (width, height))
score = 0


timeleft = 30


def startGame(event):
    global timeleft
    timeleft = sel()
    if timeleft == sel():
        countdown()
    nextColour()


def nextColour():

    global score
    global timeleft
    timeleft = sel()

    if timeleft > 0:

        e.focus_set()

        if e.get().lower() == colours[1].lower():

            score += 1

        e.delete(0, END)

        random.shuffle(colours)

        label.config(fg=str(colours[1]), text=str(colours[0]))

        scoreLabel.config(text="Score: " + str(score))


def countdown():

    global timeleft
    timeleft = sel()

    if timeleft > 0:

        timeleft -= 1

        timeLabel.config(text="Time left: " + str(timeleft))

        timeLabel.after(1000, countdown)
    return True


def sel():
    global timeleft

    timeleft = str(var.get())
    timeLabel.config(text="Time left: " + str(timeleft))
    timeleft = var.get()
    return timeleft


var = IntVar()

r_1 = Radiobutton(
    root,
    text="1 minute",
    variable=var,
    value=60,
    font=("Arial", 20),
    command=sel,
)
r_1.grid(row=1, column=1, padx=20)

r_2 = Radiobutton(
    root,
    text="2 minutes",
    variable=var,
    value=120,
    font=("Arial", 20),
    command=sel,
)
r_2.grid(row=2, column=1, padx=20)


r_3 = Radiobutton(
    root,
    text="3 minutes",
    variable=var,
    value=180,
    font=("Arial", 20),
    command=sel,
)
r_3.grid(row=3, column=1, padx=20)

instructions = Label(
    root,
    text="Type in the colour " "of the words, and not the word text!",
    font=("Arial", 20),
    bg="yellow",
)
instructions.grid(row=0, column=0, pady=10)

scoreLabel = Label(
    root,
    text="Chose the length of the game\n then press enter to start",
    font=("Arial", 20),
    bg="yellow",
)
scoreLabel.grid(row=1, column=0, pady=10)

timeLabel = Label(
    root, text="Time left: " + str(var.get()), font=("Arial", 20), bg="yellow"
)

timeLabel.grid(row=2, column=0, pady=10)

label = Label(root, font=("Helvetica", 60))
label.grid(row=3, column=0, pady=10)

e = Entry(root, font=("Arial", 20))

root.bind("<Return>", startGame)
e.grid(row=4, column=0, pady=10)

e.focus_set()

root.mainloop()
  • Here is a nice countdown timer explainer that might help. https://stackoverflow.com/questions/10596988/making-a-countdown-timer-with-python-and-tkinter – Derek Oct 29 '22 at 06:56

0 Answers0