西蒙遇到麻烦说游戏使用python和tkinter

时间:2018-06-14 23:30:30

标签: python-3.x tkinter

所以我编写了这款名为simon的经典记忆游戏。我有一切正常工作,直到计算机播放模式时的实际转弯部分,然后用户重复该模式。我有一个名为pattern.flash()的函数,它闪烁了模式。我还有一些功能可以启用和禁用允许用户单击方块的绑定。我需要帮助搞清楚如何真正完成游戏。这是代码:

from tkinter import *
import time
import random

global pattern_list
pattern_list = []
global user_list
user_list = []
comp_pattern_count = 0
user_pattern_count = []

def flash_blue_auto(index = 0):
    global comp_pattern_count
    flashing_colours = ["blue", "darkblue"]
    try:
        w.itemconfigure(blue_rectangle, fill=flashing_colours[index])
        window.after(100, flash_blue_auto, index + 1)

    except IndexError:
        comp_pattern_count += 1
        pass

def flash_red_auto(index = 0):
    global comp_pattern_count
    flashing_colours = ["red", "darkred"]
    try:
        w.itemconfigure(red_rectangle, fill=flashing_colours[index])
        window.after(100, flash_red_auto, index + 1)

    except IndexError:
        comp_pattern_count += 1
        pass

def flash_yellow_auto(index = 0):
    global comp_pattern_count
    flashing_colours = ["goldenrod", "darkgoldenrod"]

    try:
        w.itemconfigure(yellow_rectangle, fill=flashing_colours[index])
        window.after(100, flash_yellow_auto, index + 1)

    except IndexError:
        comp_pattern_count += 1
        pass

def flash_green_auto(index = 0):
    global comp_pattern_count
    flashing_colours = ["green", "darkgreen"]
    try:
        w.itemconfigure(green_rectangle, fill=flashing_colours[index])
        window.after(100, flash_green_auto, index + 1)

    except IndexError:
        comp_pattern_count += 1
        pass


def flash_blue(event, index = 0):
    global user_pattern_count
    flashing_colours = ["blue", "darkblue"]
    try:
        w.itemconfigure(blue_rectangle, fill=flashing_colours[index])
        window.after(100, flash_blue, "dummy", index + 1)
        user_list.append(1)

    except IndexError:
        pass

def flash_red(event, index = 0):
    global user_pattern_count
    flashing_colours = ["red", "darkred"]
    try:
        w.itemconfigure(red_rectangle, fill=flashing_colours[index])
        window.after(100, flash_red, "dummy", index + 1)
        user_list.append(2)

    except IndexError:
        pass

def flash_yellow(event, index = 0):
    global user_pattern_count
    flashing_colours = ["goldenrod", "darkgoldenrod"]
    try:
        w.itemconfigure(yellow_rectangle, fill=flashing_colours[index])
        window.after(100, flash_yellow, "dummy", index + 1)
        user_list.append(3)

    except IndexError:
        pass

def flash_green(event, index = 0):
    global user_pattern_count
    flashing_colours = ["green", "darkgreen"]
    try:
        w.itemconfigure(green_rectangle, fill=flashing_colours[index])
        window.after(100, flash_green, "dummy", index + 1)
        user_list.append(4)

    except IndexError:
        pass


def remove_click():
    w.tag_unbind(blue_rectangle, "<ButtonPress-1>")
    w.tag_unbind(red_rectangle, "<ButtonPress-1>")
    w.tag_unbind(yellow_rectangle, "<ButtonPress-1>")
    w.tag_unbind(green_rectangle, "<ButtonPress-1>")

def add_click():
    w.tag_bind(blue_rectangle, "<ButtonPress-1>", flash_blue)
    w.tag_bind(red_rectangle, "<ButtonPress-1>", flash_red)
    w.tag_bind(yellow_rectangle, "<ButtonPress-1>", flash_yellow)
    w.tag_bind(green_rectangle, "<ButtonPress-1>", flash_green)


def pattern_flash():
    pattern_list.append(random.randint(1, 4))

    for x in pattern_list:

        if x == 1:
            window.after(500, lambda: flash_blue_auto(index=0))
            window.update()
            time.sleep(0.5)

        elif x == 2:
            window.after(500, lambda: flash_red_auto(index=0))
            window.update()
            time.sleep(0.5)

        elif x == 3:
            window.after(500, lambda: flash_yellow_auto(index=0))
            window.update()
            time.sleep(0.5)

        else:
            window.after(500, lambda: flash_green_auto(index=0))
            window.update()
            time.sleep(0.5)




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="darkred")
yellow_rectangle = w.create_rectangle(483, 280, 683, 480, fill="darkgoldenrod")
green_rectangle = w.create_rectangle(683, 280, 883, 480, fill="darkgreen")

#This is the part where I get lost

pattern_flash()
add_click()

我想要闪烁模式,启用绑定,等待用户单击模式,然后当他们点击任何方块的次数与模式列表的长度相同时,绑定将被禁用。然后,如果他们得到正确的模式,程序就会循环播放,如果他们没有做好,程序就会停止。非常感谢任何帮助,很快!这项任务明天到期!

0 个答案:

没有答案