首页 > 解决方案 > 我怎样才能逐一检查列表中的每个项目,中间有延迟?

问题描述

我正在制作一个游戏,玩家会看到一个模式,并且必须重复它。如果玩家做对了,则游戏每次都会将 1 添加到序列中。

现在我正试图让游戏展示这种模式。他们设置它的方式是将游戏模式存储在列表中。1 表示左上角会亮 2 右上角等。每次玩家列表 == 到游戏列表时,都会在游戏列表中添加一个数字

def addlist():
    if playerpattern == gamepattern:
        gamepattern.append(random.randint(1, 4))

def idk():
    for number in gamepattern:
        if number == 1:
            Gamestate = 1
        if number == 2:
            Gamestate = 2
        if number == 3:
            Gamestate = 3
        if number == 4:
            Gamestate = 4
def show():
    if playerpattern == gamepattern:
            if Gamestate == 1:
                topleft.color = (255, 0, 0)
            else:
                topleft.color = (100, 0, 0)
            if Gamestate == 2:
                topright.color = (0, 0, 255)
            else:
                topright.color = (0, 0, 175)
            if Gamestate == 3:
                bottomleft.color = (0, 255, 0)
            else:
                bottomleft.color = (0, 175, 0)
            if Gamestate == 4:
                bottomright.color = (255, 255, 0)
            else:
                bottomright.color = (175, 175, 0)

playing = True
gamepattern = []
playerpattern = []

while playing:
    clock.tick(10)
    print(gamepattern)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            playing = False
        if event.type == MOUSEBUTTONDOWN:
            mouseclick()

    mousehover()
    addlist()
    show()
    draw()

pygame.quit()

这实际上不起作用,这只是我能想到的。我基本上需要一个从头到尾检查列表的函数,中间有一点时间。

感谢您的帮助

标签: pythonpython-3.xpygame

解决方案


听起来你正在重建旧的西蒙游戏。你在正确的轨道上。把需要做的事情分解成函数,然后从那里开始。您将需要以下功能:

  • 检查猜测是否正确(见下文)
  • 如果他们做对了,请添加到解决方案中。有条件地调用它...
  • 通过鼠标点击捕捉玩家的猜测
  • 如果他们弄错了,请重置所有内容...

然后你需要做的就是组成一个主循环,通过一些条件语句来完成动作。您可以在任何您认为合适的地方添加时间延迟。

In [1]: solution = [1, 1, 2, 2, 2, 3]                                           

In [2]: guess_1 =  [1, 1, 2, 2, 2, 3]                                           

In [3]: guess_2 =  [1, 1, 2, 2, 2, 4]                                           

In [4]: def check_answer(guess, soln): 
   ...:     if guess == soln: 
   ...:         return True 
   ...:     return False 
   ...:                                                                         

In [5]: check_answer(guess_1, solution)                                         
Out[5]: True

In [6]: check_answer(guess_2, solution)                                         
Out[6]: False

推荐阅读