I'm no pygame expert but I think CTRL + C
will only generate a KeyboardInterrupt
if the terminal window - not the pygame window - is focused. Since pygame captures all keystrokes while focused, you'll probably have to use pygame.key.get_mods()
and pygame.KMOD_CTRL
to capture CTRL
+ letter key.
Anyway, two while loops - one nested within the other - and a boolean seem to make a working pause function. This pauses and resumes on "p" and 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()