0

So I'm coding this game called simon says which is this classic colour memory game. The algorithm is obviously not done yet, but I just don't know how I can get the square to flash. I'm just testing it with the only the blue square.

from tkinter import *
import random
import time

def click():
        lightblue_rectangle = w.create_rectangle(483, 480, 683, 680, fill="blue")
        window.after(500, click)
        blue_rectangle = w.create_rectangle(483, 480, 683, 680, fill="darkblue")



window = Tk()

w = Canvas(window, width=1366, height=766)
w.configure(background = "black")
w.pack()


blue_rectangle = w.create_rectangle(483, 480, 683, 680, fill="darkblue")
red_rectangle = w.create_rectangle(683, 480, 883, 680, fill="red")
yellow_rectangle = w.create_rectangle(483, 280, 683, 480, fill="yellow")
green_rectangle = w.create_rectangle(683, 280, 883, 480, fill="green")

w.tag_bind(blue_rectangle, "<ButtonPress-1>", click)

I get this error that says: click() takes 0 positional arguments but 1 was given. What I am trying to do is make the square flash. I can take care of the random pattern later. I just need help with making the square flash.

1 Answers1

2

The following will flash the bottom right square upon clicking:

Edited to answer new requests:

flashes only once.
uses lightblue i/o yellow.
w.find_withtag(tag) returns the canvas item reference index that alows the canvas to identify which item to act on.
dummy is a placeholder dummy_variable that fills in for event - it does nothing else.

import tkinter as tk


def flash(event, idx=0):
    print(idx)
    flashing_colors = ['lightblue', 'darkblue']
    try:
        w.itemconfigure(w.find_withtag('blue_rectangle'), fill=flashing_colors[idx])
        window.after(100, flash, 'dummy', idx + 1)
    except IndexError:
        pass


if __name__ == '__main__':

    window = tk.Tk()

    w = tk.Canvas(window, width=1366, height=766)
    w.configure(background="black")
    w.pack()

    blue_rectangle = w.create_rectangle(483, 480, 683, 680, fill="darkblue", tags=('blue_rectangle',))
    red_rectangle = w.create_rectangle(683, 480, 883, 680, fill="red")
    yellow_rectangle = w.create_rectangle(483, 280, 683, 480, fill="yellow")
    green_rectangle = w.create_rectangle(683, 280, 883, 480, fill="green")

    w.bind("<ButtonPress-1>", flash)

    window.mainloop()
Community
  • 1
  • 1
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • Hey thanks for the answer. I have a few questions. I only want the rectangle to flash once, and I just want to flash a lighter colour, not yellow. So if blue was clicked, it would flash light blue. How would I go about doing this? Also what does find_withtag and 'dummy' do? – Parthav Patel Jun 14 '18 at 02:20
  • I edited the answer to address your new questions and requests. – Reblochon Masque Jun 14 '18 at 02:32
  • Thanks so much for the answer. Now, if you dont mind I have one more question. im creating the random computer generated pattern. The pattern will flash colours 1 by 1. And every round it will add 1 more random colour. How do I get the program to flash a colour? Not on a click. – Parthav Patel Jun 14 '18 at 14:10
  • You will need to bind the flashing trigger to an event/output produced by your program. You should maybe stare at it a little bit, and if you cannot figure it out, then ask a new question. In the meantime, if my answer helped you, you should accept it. – Reblochon Masque Jun 14 '18 at 14:20