在pygame中暂停系统

时间:2014-05-16 19:24:15

标签: python time pygame

嗨我已经花了好几个小时试图在pygame中进行切换暂停,你按空格然后它会暂停然后再次按空格键以取消暂停,我没有运气任何人都可以帮助我?

  ############### 2nd attempt ############# 
 global timedelay

        timedelay = pygame.time.wait (0)
        pause = 0

        if event.type == KEYUP:
            if event.key == K_p  and pause == 0:
                timedelay = pygame.time.wait (9999)
                pause = 1
                if event.key == K_p  and pause == 0:
                    timedelay = pygame.time.wait (0)
                    pause = 0
        timedelay


        ############### 1st attempt #############       
        if event.type == KEYDOWN:   
            pause = 0
            if event.key == K_p and pause == 0:
                while 1: 
                    pause = 0
                    event = pygame.event.wait()
                    pause = 1
                    if event.type == KEYDOWN:
                        if event.key == K_p  and pause == 0:
                            print "woo!"
                            pause = 0
                            break

1 个答案:

答案 0 :(得分:0)

不要阻止整个主循环运行,只需跳过循环的状态更改部分。您仍然需要能够回复事件,否则您的应用就会没有响应"。你也想继续绘画,因为窗户可以移动并覆盖你的游戏,屏幕仍然需要刷新。所以:

game_over = False
paused = False
while not game_over:
    # handle events
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            paused = False
            if event.key == K_p:
                paused = True
        ...
    if not paused:
        # handle game state change
    # handle screen drawing
    pygame.display.flip()