[英]Cannot make my pause function work in Pygame 3.4
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. 我正在制作Pong游戏,但是无论我做什么尝试,我都无法实现按CTRL + C时会停止的暂停系统。
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? 如何使游戏暂停,直到按CTRL + C为止?
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. 我不是pygame专家,但我认为
CTRL + C
仅在终端窗口(而非pygame窗口)聚焦时才会生成KeyboardInterrupt
。 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. 由于pygame在聚焦时会捕获所有击键,因此您可能必须使用
pygame.key.get_mods()
和pygame.KMOD_CTRL
来捕获CTRL
+字母键。
Anyway, two while loops - one nested within the other - and a boolean seem to make a working pause function. 无论如何,两个while循环-一个嵌套在另一个循环中-和一个布尔值似乎可以使工作暂停。 This pauses and resumes on "p" and
CTRL + C
: 这会暂停并在“ 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.