如何使列表中的每个项目都发生延迟?

时间:2019-06-15 05:12:58

标签: python python-3.x pygame

我正在尝试制作一种记忆游戏,其中有四种颜色,并且您必须匹配模式,同时程序不断向列表中添加+1。

现在我无法弄清楚如何使游戏显示图案。然后等待玩家输入结束游戏或为图案添加其他颜色。

这就是我能想到的:

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

def listcheck():
    if playerpattern == pattern:
        for i in pattern:
            if i == 1:
                topleft.color = (255, 0, 0)
            else:
                topleft.color = (100, 0, 0)
            if i == 2:
                topright.color = (0, 0, 255)
            else:
                topright.color = (0, 0, 175)
            if i == 2:
                bottomleft.color = (0, 255, 0)
            else:
                bottomleft.color = (0, 175, 0)
            if i ==4:
                bottomright.color = (255, 255, 0)
            else:
                bottomright.color = (175, 175, 0)

pattern = []
playerpattern = []
playing = True

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

    mousehover()
    addlist()
    listcheck()
    draw()

pygame.quit()

基本上,当游戏开始时,我希望游戏点亮模式[0](4个正方形之一)。

然后当播放器正确播放时,显示pattern [0],然后延迟显示pattern [1],然后重复直到结束。 感谢您的帮助

1 个答案:

答案 0 :(得分:1)

pygame具有计时器功能pygame.time.set_timer()

定义事件ID(7是任意数字,您可以定义此数字)

myEventID = pygame.USEREVENT+7

以一定的间隔时间启动计时器,例如1秒(1000毫秒)

pygame.time.set_timer(myEventID, 1000) # 1000 milliseconds interval

在时间跨度每次到期时获取事件通知:

for event in pygame.event.get():

    if event.type == myEventID:
        # [...]

通过将0传递给毫秒参数来停止计时器:

pygame.time.set_timer(myEventID, 0)

添加一个游戏状态变量,该变量指示游戏的当前状态,并在计时器中修改此变量。状态是整数,具有特定含义,例如

gameState = 0
while playing: 

    for event in pygame.event.get():

        if event.type == myEventID:
            # change the game state - this is just an example
            if gameState == 0:
                gameState = 1 


    if gameState == 0:

        # do default

    elif gameState == 1:

        # do something else

    elif gameState == 2:

       # do something completely different

    else:

       # another case