我为游戏编写了一个代码,您在其中滑动15个图块以按顺序对其进行设置。在游戏结束时,我希望屏幕仅等待5秒钟才能关闭(也希望稍后再提示)。
游戏结束时,它首先暂停,然后显示最后一个动作,然后关闭。显示屏是否有滞后。如何使它完成显示,然后等待。我尝试阅读类似的帖子,但听不懂。
这是相关的代码。
> def run_game(self):
"""Start the main loop for the game"""
self._shuffle_grid()
self._show_board()
game_over = False
while True:
change = self._check_events()
if change:
self._show_board()
game_over = self.end_of_game()
if game_over:
pygame.time.wait(5000)
sys.exit()
def _check_events(self):
# Watch for keyboard and mouse events
change = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
key = "U"
elif event.key == pygame.K_DOWN:
key = "D"
elif event.key == pygame.K_LEFT:
key = "L"
elif event.key == pygame.K_RIGHT:
key = "R"
if key == "U" or "D" or "L" or "R":
change = self.key_check(key)
return(change)
答案 0 :(得分:1)
我实际上尝试了这个并且奏效了。我仍然不知道如何显示消息,然后等待5秒钟。正在努力。
def celebrate(self):
""" does end of game thing"""
game_clock = pygame.time.Clock()
time_passed = pygame.time.get_ticks()
self._show_board()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
sys.exit()
if pygame.time.get_ticks() > time_passed + 5000:
sys.exit()
def run_game(self):
"""Start the main loop for the game"""
self._shuffle_grid()
self._show_board()
game_over = False
while True:
change = self._check_events()
if change:
game_over = self.end_of_game()
if not game_over:
self._show_board()
else:
self.celebrate()