Created
March 1, 2022 12:47
-
-
Save brecert/039f0148a7ff1ac71cf325e2fc5f19cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tkinter as tk | |
from threading import Thread | |
import random | |
import time | |
# ablak létrehozása | |
window = tk.Tk() | |
window.geometry("450x450+500+250") | |
window.configure(bg="#fff") | |
frame = tk.Frame(window, bg="#000000") | |
buttons = [ | |
tk.Button(frame, text="One", bg="#ee572c", fg="#fff", activebackground="#ff0000"), | |
tk.Button(frame, text="Dos", bg="#47a04e", fg="#fff", activebackground="#00ff00"), | |
tk.Button(frame, text="San", bg="#0061a5", fg="#fff", activebackground="#0000ff"), | |
tk.Button(frame, text="Yon", bg="#ffb400", fg="#fff", activebackground="#ffff00") | |
] | |
buttons[0].grid(row=0, column=0, padx=5, pady=5, ipadx=16, ipady=16) | |
buttons[1].grid(row=0, column=2, padx=5, pady=5, ipadx=16, ipady=16) | |
buttons[2].grid(row=2, column=0, padx=5, pady=5, ipadx=16, ipady=16) | |
buttons[3].grid(row=2, column=2, padx=5, pady=5, ipadx=16, ipady=16) | |
class Game(Thread): | |
def __init__(self, buttons): | |
super(Game, self).__init__() | |
self.current = 0 | |
self.buttons = buttons | |
self.correct = [0] | |
for i, button in enumerate(self.buttons): | |
# take by value instead of reference | |
button.configure(command=lambda i=i: Thread(target=self.on_button, args=(i,)).start()) | |
def set_state(self, state): | |
for button in self.buttons: | |
button.configure(state=state) | |
def game_over(self): | |
self.set_state('disabled') | |
window.configure(bg="#fc0000") | |
# add more code | |
def next_correct(self): | |
if self.current >= len(self.correct)-1: | |
self.current = 0 | |
self.correct.append(random.randint(0, len(self.buttons)-1)) | |
self.play_correct() | |
else: | |
self.current += 1 | |
def highlight_button(self, i): | |
button = self.buttons[i] | |
bg = button.cget("background") | |
button.configure(bg=button.cget("activebackground")) | |
# play sound? | |
time.sleep(0.65) | |
button.configure(bg=bg) | |
def play_correct(self): | |
self.set_state('disabled') | |
for i in self.correct: | |
time.sleep(0.25) | |
self.highlight_button(i) | |
self.set_state('normal') | |
def on_button(self, i): | |
if i == self.correct[self.current]: | |
self.next_correct() | |
else: | |
self.game_over() | |
def run(self): | |
self.play_correct() | |
def start_game(): | |
start.destroy() | |
Game(buttons).start() | |
start = tk.Button(text="Start", bg="#fff", command=start_game) | |
start.pack() | |
frame.pack(expand=True, padx=16, pady=16) | |
window.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment