Home > other >  Sequence memory game in Tkinter
Sequence memory game in Tkinter

Time:03-21

I'm new to coding and struggling on a project.

In my game I have a 3x3 square grid. Every-time I press the start button, a number from 1-9 is generated. The numbers are added to a list and the numbers correspond to the squares on the grid. So for example, when 1 is generated, it will flash the first button on the grid.

However as more numbers get generated, how would I make them flash one at a time in the order that is generated instead of at the same time?

Here is what I have so far.

sequenceRandom = random.randint(1,9)
    sequenceList.append(sequenceRandom)
    print (sequenceList)
    
    for i in range (0, len(sequenceList)):
        if sequenceList[i] == 1:
            root.after(200, lambda: button1.configure(background="light grey"))
            root.after(600, lambda: button1.configure(background="grey"))
        elif sequenceList[i] == 2:
            root.after(200, lambda: button2.configure(background="light grey"))
            root.after(400, lambda: button2.configure(background="grey"))
        elif sequenceList[i] == 3:
            root.after(200, lambda: button3.configure(background="light grey"))
            root.after(400, lambda: button3.configure(background="grey"))
        elif sequenceList[i] == 4:
            root.after(200, lambda: button4.configure(background="light grey"))
            root.after(400, lambda: button4.configure(background="grey"))
        elif sequenceList[i] == 5:
            root.after(200, lambda: button5.configure(background="light grey"))
            root.after(400, lambda: button5.configure(background="grey"))
        elif sequenceList[i] == 6:
            root.after(200, lambda: button6.configure(background="light grey"))
            root.after(400, lambda: button6.configure(background="grey"))
        elif sequenceList[i] == 7:
            root.after(200, lambda: button7.configure(background="light grey"))
            root.after(400, lambda: button7.configure(background="grey"))
        elif sequenceList[i] == 8:
            root.after(200, lambda: button8.configure(background="light grey"))
            root.after(400, lambda: button8.configure(background="grey"))
        elif sequenceList[i] == 9:
            root.after(200, lambda: button9.configure(background="light grey"))
            root.after(400, lambda: button9.configure(background="grey"))
        

CodePudding user response:

I will give you answer. You can fix it by a simple way. You should use one variable.

convertTime = -600
for i in range (0, len(sequenceList)):
    convertTime  = 600
    if sequenceList[i] == 1:
        root.after(convertTime   200 , lambda: button1.configure(background="light grey"))
        root.after(convertTime   400, lambda: button1.configure(background="grey"))
    elif sequenceList[i] == 2:
        root.after(convertTime   200, lambda: button2.configure(background="light grey"))
        root.after(convertTime   400, lambda: button2.configure(background="grey"))
    ...

repeat elif instruction

  • Related