I am making a Pong game but no matter what I try I just cannot implement a pause system that stops when CTRL + C is pressed.
I tried:
elif event.key == K_SPACE:
try:
hello = pygame.time.delay(1000)
except KeyboardInterrupt:
hello = pygame.time.delay(1)
And:
elif event.key == K_SPACE:
try:
pygame.time.get_ticks()
except KeyboardInterrupt:
pass
And:
elif event.key == K_SPACE:
try:
time.sleep(10)
except KeyboardInterrupt:
pass
How do I get my game to pause until CTRL + C is pressed?
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.