Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Simon游戏:我被困在完成序列中_Python_Tkinter - Fatal编程技术网

Python Simon游戏:我被困在完成序列中

Python Simon游戏:我被困在完成序列中,python,tkinter,Python,Tkinter,我终于完成了我的西蒙游戏,但我对如何完成这个序列有些怀疑 编辑:按照你的要求,我已经编辑了我的文章。在这里,我将张贴我的代码,因为它是在这篇文章之前。这就是我的实际问题 1) 我不知道如何在每个回合后在序列中添加一个数字 2) 第一圈运行良好,但我不知道如何开始下一圈,我在检查序列后尝试再次调用该函数,但它不起作用 两件事的必要代码: from Tkinter import * import Tkinter import random base = Tkinter.Tk() fr = Tki

我终于完成了我的西蒙游戏,但我对如何完成这个序列有些怀疑

编辑:按照你的要求,我已经编辑了我的文章。在这里,我将张贴我的代码,因为它是在这篇文章之前。这就是我的实际问题

1) 我不知道如何在每个回合后在序列中添加一个数字

2) 第一圈运行良好,但我不知道如何开始下一圈,我在检查序列后尝试再次调用该函数,但它不起作用

两件事的必要代码:

from Tkinter import *
import Tkinter
import random

base = Tkinter.Tk()

fr = Tkinter.Frame(base, bg='black', width='238', height='250')

score = Tkinter.Label(base, bg='black', fg='white', text='Score:')
score.place(x = 30, y = 15)

s = 0

scoreNum = Tkinter.Label(base, bg='black', fg='white', text = s)
scoreNum.place(x = 70, y = 15)

clicks = []
color = 0

def yellowClick():

    yellow.configure(activebackground='yellow3')
    yellow.after(500, lambda: yellow.configure(activebackground='yellow'))

    global clicks
    global color

    color = 1
    clicks.append(color)

yellow = Tkinter.Button(base, bd='0', highlightthickness='0',
                       width='7', height='5', activebackground='yellow',  
                       bg='yellow3', command = yellowClick)

yellow.place(x = 30, y = 50)

def blueClick():

    blue.configure(activebackground='medium blue')
    blue.after(500, lambda: blue.configure(activebackground='blue'))

    global clicks
    global color

    color = 2
    clicks.append(color)

blue = Tkinter.Button(base, bd='0', highlightthickness='0',
                     width='7', height='5', activebackground='blue',
                     bg='medium blue', command = blueClick)

blue.place(x = 125, y = 50)

def redClick():

    red.configure(activebackground='red3')
    red.after(500, lambda: red.configure(activebackground='red'))

    global clicks
    global color 

    color = 3
    clicks.append(color)

red = Tkinter.Button(base, bd='0', highlightthickness='0',
                    width='7', height='5', activebackground='red',
                    bg = 'red3', command = redClick)    

red.place(x = 30, y = 145)

def greenClick():

    green.configure(activebackground='dark green')
    green.after(500, lambda: green.configure(activebackground='green4'))

    global clicks
    global color

    color = 4
    clicks.append(color)

green = Tkinter.Button(base, bd='0', highlightthickness='0',
                      width='7', height='5', activebackground='green4',
                      bg='dark green', command = greenClick)

green.place(x = 125, y = 145)   

def scoreUp():

    global s

    s = s + 1

    scoreNum.configure(text = s)


sequence = []

def checkSequence():

    global clicks
    global sequence

    if clicks == sequence:

        scoreUp()   

def showSequence():

    global sequence
    global clicks
    global check 

    r = random.randint(1, 4)

    if r == 1:

        yellow.configure(bg='yellow')
        yellow.after(1000, lambda: yellow.configure(bg='yellow3'))

        sequence.append(r)

        base.after(5000, checkSequence)

    elif r == 2:

        blue.configure(bg='blue')
        blue.after(1000, lambda: blue.configure(bg='medium blue'))  

        sequence.append(r)

        base.after(5000, checkSequence)

    elif r == 3:

        red.configure(bg='red')
        red.after(1000, lambda: red.configure(bg='red3'))   

        sequence.append(r)

        base.after(5000, checkSequence)

    elif r == 4:

        green.configure(bg='green4')
        green.after(1000, lambda: green.configure(bg='dark green')) 

        sequence.append(r)      

        base.after(5000, checkSequence)


base.after(2000, showSequence)

fr.pack()

base.resizable(False, False)
base.mainloop()

checkSequence函数是按时间激活的,因为我找不到其他方法来执行它。

下面是基于furas在注释中的解决方案的代码

def showSequence():

    global sequence    

    number_added = True
    r = random.randint(1, 4)

    if r == 1:
    elif r == 2:
    elif r == 3:
    elif r == 4:
    else:
        number_added = False

    return number_added # this will be True if a number was added, False if not
并调用该函数:

number_added = showSequence()
if number_added:
    # stop sequence here
你还说你不知道如何阻止它,试试这个:

import sys
sys.exit()

这将立即停止您当前使用的函数

我已经回答了这个问题,但您仍然有一些问题,下面是完整的代码。我提高了效率并解决了问题:)

这是工作,我已经测试过了。如果你有什么需要知道的,请发表评论

import Tkinter # you don't need to import Tkinter again, from ... import * moves all the functions from ... into your program
import random

base = Tkinter.Tk()

fr = Tkinter.Frame(base, bg='black', width='238', height='250')
fr.pack()

score = Tkinter.Label(base, bg='black', fg='white', text='Score:')
score.place(x = 30, y = 15)

scoreNum = Tkinter.Label(base, bg='black', fg='white', text = 0)
scoreNum.place(x = 70, y = 15)

global sequence
global clicks
global s
sequence = []
clicks = []
s = 0

yellow = Tkinter.Button(base, bd='0', highlightthickness='0',
                       width='7', height='5', activebackground='yellow',  
                       bg='yellow3', command = lambda *args: Click(yellow, "yellow", "yellow3", 1))


blue = Tkinter.Button(base, bd='0', highlightthickness='0',
                     width='7', height='5', activebackground='blue',
                     bg='medium blue', command = lambda *args: Click(blue, "blue", "medium blue", 2))

red = Tkinter.Button(base, bd='0', highlightthickness='0',
                    width='7', height='5', activebackground='red',
                    bg = 'red3', command = lambda *args: Click(red, "red", "red3", 3))    

green = Tkinter.Button(base, bd='0', highlightthickness='0',
                      width='7', height='5', activebackground='green4',
                      bg='dark green', command = lambda *args: Click(green, "green", "dark green", 4))

yellow.place(x = 30, y = 50)
blue.place(x = 125, y = 50)
red.place(x = 30, y = 145)
green.place(x = 125, y = 145)   


def Click(button, colour1, colour2, number): # these arguments change so that you don't have to copy the function 10 times
    global clicks

    yellow.configure(activebackground=colour2)
    yellow.after(500, lambda: yellow.configure(activebackground=colour1))

    clicks.append(number)
    checkSequence() # why not do this straight away?

def checkSequence():
    global clicks
    global sequence
    global s

    print("clicks:    "+str(clicks)) # debug
    print("sequence:  "+str(sequence))

    if clicks != sequence[:len(clicks)]: # if all their clicks so far e.g. [1, 2, 4, 3] DONT fit with the sequence e.g. [3, 2, 4, 3, 2, 1]
        print("           ...Incorrect!")
        s = 0
        scoreNum.configure(text = 0)
        sequence = []
        clicks = []
        base.after(1000, showSequence) # now move to the next value in the sequence

    elif clicks == sequence: # they have completed a sequence
        print("           ...Match!")
        s = s + 1
        scoreNum.configure(text = s)
        clicks = []
        base.after(1000, showSequence) # now move to the next value in the sequence

def showSequence():
    global sequence
    global clicks

    r = random.randint(1, 4)

    if r == 1:
        yellow.configure(bg='yellow')
        yellow.after(1000, lambda: yellow.configure(bg='yellow3'))

    elif r == 2:
        blue.configure(bg='blue')
        blue.after(1000, lambda: blue.configure(bg='medium blue'))  

    elif r == 3:
        red.configure(bg='red')
        red.after(1000, lambda: red.configure(bg='red3'))   

    elif r == 4:
        green.configure(bg='green')
        green.after(1000, lambda: green.configure(bg='dark green')) 

    sequence.append(r) # you don't need this in all the if statements, it always happens

base.after(100, showSequence)
base.resizable(False, False)
base.mainloop()

注意:我使用的是Python3.4.1,所以我将所有Tkinter都改为Tkinter

您所说的停止是什么意思?比如暂停并等待事件?是的,这就是我的意思
sequence==sequence+1
等于
0==1
,它总是假的。使用
True/False
变量来控制程序-即
number\u added=True
如果turn\u end==True:number\u added=False
谢谢,我会试试的。没问题:)如果你还需要什么,只需添加一条注释。记住,如果这对你有帮助,我不介意一个漂亮的、大的、绿色的勾号。是的,它对我有帮助。但我不知道如何在第一个转弯结束后开始下一个转弯。有什么想法吗?谢谢你的帮助。安德里亚,有用吗?如果我的答案正确的话!对不起,我正在测试它,是的,它很有效,非常感谢。