0

我正在制作一个乒乓球游戏,但无论我尝试什么,我都无法实现一个暂停系统,当按下 CTRL + C 时它会停止。

我试过了:

elif event.key == K_SPACE:
    try:
        hello = pygame.time.delay(1000) 
    except KeyboardInterrupt:
        hello = pygame.time.delay(1)

和:

elif event.key == K_SPACE:
    try:
        pygame.time.get_ticks()
    except KeyboardInterrupt:
        pass

和:

elif event.key == K_SPACE:
    try:
        time.sleep(10)
    except KeyboardInterrupt:
        pass

如何让我的游戏暂停直到按下 CTRL + C?

4

1 回答 1

0

我不是 pygame 专家,但我认为只有在终端窗口(而不是 pygame 窗口)被聚焦CTRL + C时才会生成。KeyboardInterrupt由于 pygame 在专注时会捕获所有击键,因此您可能必须使用pygame.key.get_mods()andpygame.KMOD_CTRL来捕获CTRL+ 字母键。

无论如何,两个 while 循环(一个嵌套在另一个循环中)和一个布尔值似乎构成了一个有效的暂停功能。这会在 "p" 和 上暂停和恢复CTRL + C

import pygame

def main():
    pygame.init()
    WIDTH=100
    HEIGHT=100
    SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
    CLOCK = pygame.time.Clock()
    FPS = 10
    running = True
    # outer loop
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_p:
                    # resume
                    running = True
                if event.key == pygame.K_c:
                    if pygame.key.get_mods() & pygame.KMOD_CTRL:
                        # ctrl + z
                        running = True
        print "paused"
        CLOCK.tick(FPS)
        # game loop
        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_p:
                        # pause
                        running = False
                    if event.key == pygame.K_c:
                        if pygame.key.get_mods() & pygame.KMOD_CTRL:
                            # ctrl + z
                            running = False
            # rest of game code
            print "running"
            CLOCK.tick(FPS)
main()
于 2016-04-01T01:01:44.647 回答