如何通过按下按钮 'r' 来重新启动我的程序?
How to restart my program with a button press 'r'?
好的,我已经在 Python 3.7.4 和 Pygame 1.9.6 中创建了 Space Invaders。我已经让一切正常工作,没有错误或任何东西。我什至让暂停按钮工作得很好。就在屏幕显示 'GAME OVER' 文本表示你输了的时候。我想在文本消失后弹出一个 window 要求再次播放。但我只是不知道从哪里开始。我查看了 How do I restart a program based on user input? 寻求帮助,但我不明白在哪里实施它或在哪里查看。这是我在 Pygame.
中创建的第一个真正的 project/game
代码:
paused = False
running = True
while running:
# RGB = Red, Green, Blue
screen.fill((0, 0, 0))
# Background Image
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p: # Pausing
paused = True
if event.key == pygame.K_u: # Unpausing
paused = False
if not paused:
'''The rest of the code, with the movement key presses, etc.'''
由于 'while running loop' 以上的所有内容都在不断加载或用于数据检索,例如图像、图像应位于的坐标、背景音乐等。所以我希望它像 pausing/unpausing 当我按下说 'r' 时的部分代码。我想在不退出的情况下重播该程序。所以我会非常感谢人们的帮助。谢谢大家
您必须在对象中创建函数或方法来重置数据并在每场比赛前使用它。您可以为此使用外部 while
循环。
# executed only once
pygame.init()
screen = ...
# .. load data ...
player = ...
enemy = ...
gameing = True
while gameing:
# reset data before every game
player.reset()
enemy.reset()
score = 0
paused = False
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT: # exit game
running = False
gameing = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r: # restart game (but don't exit)
running = False
#gameing = True
if event.key == pygame.K_x: # exit game and program
running = False
gameing = False
if game_over: # restart game (but don't exit)
running = False
#gameing = True
您也可以按照评论中提到的@Starbuck5 组织它 - 但是您应该使用 running
退出程序而不是在游戏结束时退出游戏
def reset():
global score, paused, running
player.reset()
enemy.reset()
score = 0
paused = False
running = True
# executed only once
pygame.init()
screen = ...
# .. load data ...
player = ...
enemy = ...
# (re)set data before first game
reset()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT: # exit game
running = False
gameing = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r: # restart game (but don't exit)
#running = False # DON'T DO THIS
reset()
if event.key == pygame.K_x: # exit game and program
running = False
if game_over: # restart game (but don't exit)
#running = False # DON'T DO THIS
reset()
这需要对每个必须重置的变量使用 global
,这样当您在 类 中有代码时它会更有用,您可以使用 self.
而不是 global
好的,我已经在 Python 3.7.4 和 Pygame 1.9.6 中创建了 Space Invaders。我已经让一切正常工作,没有错误或任何东西。我什至让暂停按钮工作得很好。就在屏幕显示 'GAME OVER' 文本表示你输了的时候。我想在文本消失后弹出一个 window 要求再次播放。但我只是不知道从哪里开始。我查看了 How do I restart a program based on user input? 寻求帮助,但我不明白在哪里实施它或在哪里查看。这是我在 Pygame.
中创建的第一个真正的 project/game代码:
paused = False
running = True
while running:
# RGB = Red, Green, Blue
screen.fill((0, 0, 0))
# Background Image
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p: # Pausing
paused = True
if event.key == pygame.K_u: # Unpausing
paused = False
if not paused:
'''The rest of the code, with the movement key presses, etc.'''
由于 'while running loop' 以上的所有内容都在不断加载或用于数据检索,例如图像、图像应位于的坐标、背景音乐等。所以我希望它像 pausing/unpausing 当我按下说 'r' 时的部分代码。我想在不退出的情况下重播该程序。所以我会非常感谢人们的帮助。谢谢大家
您必须在对象中创建函数或方法来重置数据并在每场比赛前使用它。您可以为此使用外部 while
循环。
# executed only once
pygame.init()
screen = ...
# .. load data ...
player = ...
enemy = ...
gameing = True
while gameing:
# reset data before every game
player.reset()
enemy.reset()
score = 0
paused = False
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT: # exit game
running = False
gameing = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r: # restart game (but don't exit)
running = False
#gameing = True
if event.key == pygame.K_x: # exit game and program
running = False
gameing = False
if game_over: # restart game (but don't exit)
running = False
#gameing = True
您也可以按照评论中提到的@Starbuck5 组织它 - 但是您应该使用 running
退出程序而不是在游戏结束时退出游戏
def reset():
global score, paused, running
player.reset()
enemy.reset()
score = 0
paused = False
running = True
# executed only once
pygame.init()
screen = ...
# .. load data ...
player = ...
enemy = ...
# (re)set data before first game
reset()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT: # exit game
running = False
gameing = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r: # restart game (but don't exit)
#running = False # DON'T DO THIS
reset()
if event.key == pygame.K_x: # exit game and program
running = False
if game_over: # restart game (but don't exit)
#running = False # DON'T DO THIS
reset()
这需要对每个必须重置的变量使用 global
,这样当您在 类 中有代码时它会更有用,您可以使用 self.
而不是 global