问题
I have a GAME OVER screen that is functioning when your health is depleted. This GAME OVER screen is executed with the show_go_screen. I tried to just duplicate the method for the show_go_screen into my show_winning() function for the other screen. I rigged this show winning function by setting your health back to normal when going back to the game. However once you have gone to the YOU WIN screen and back to the game you cannot go to the YOU WIN screen again. I have included the code for my player shield below (which determine when there is a GAME OVER/YOUWIN event) I have also included the methods for the show_go_screen (which is for GAME OVER) and the code for show_winning() method (which is for the YOU WIN screen). If someone can tell me how to best modify my code to get the desired result.
if TrumpHits:
self.trump.shield -= 25
if self.trump.shield <= 0:
self.show_winning()
self.winning = True
if hits:
self.player.shield -= 20
if flyby:
self.player.shield -= 30
if self.player.shield <= 0:
self.playing = False
here are the show_go_screen() and the show_winning() methods
def show_go_screen(self):
# game over/continue
if not self.running:
return
bg = pg.image.load("GAMEOVERslimeCOVERAGE.png")
self.screen.blit(bg,(0,0))
self.draw_text("Press space bar to play again", 22, WHITE, WIDTH / 2, HEIGHT * 7 / 8)
HEIGHT / 2 + 40)
pg.display.flip()
waiting = True
while waiting:
self.clock.tick(FPS)
for event in pg.event.get():
if event.type == pg.QUIT:
waiting = False
self.running = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE:
waiting = False
self.PlayMusic()
def show_winning(self):
# game over/continue
if self.winning:
return
bg = pg.image.load("TRUMPyouwin3d.png")
self.screen.blit(bg,(0,0))
self.draw_text("Press any key to play again", 22, WHITE, WIDTH / 2, HEIGHT * 7 / 8)
pg.display.flip()
waiting = True
while waiting:
self.clock.tick(FPS)
for event in pg.event.get():
if event.type == pg.QUIT:
waiting = False
self.running = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE:
waiting = False
self.PlayMusic()
self.player.shield = 100
self.trump.shield = 100
def wait_for_key(self):
waiting = True
while waiting:
self.clock.tick(FPS)
for event in pg.event.get():
if event.type == pg.QUIT:
waiting = False
self.running = False
if event.type == pg.KEYUP:
waiting = False
回答1:
If the player or the enemy have less than zero health, set self.playing = False which stops the game's while loop. Then the show_go_screen method of the game instance g is called in the outermost while loop.
while g.running:
g.new()
g.show_go_screen()
In the show_go_screen method, blit the background image and the text (I created the BACKGROUND_IMG and the FONT object globally), flip the display and start another while loop with an event loop in which you check if the user wants to quit or continue. If the user quits, you set self.running to False so that the outer while loop will stop as well, because its condition is while g.running:. If space is pressed, you can just return from this show_go_screen method and the outer while loop calls g.new() what resets the game.
def show_go_screen(self):
self.screen.blit(BACKGROUND_IMG, (0, 0))
text_surface = FONT.render("Press space bar to play again", True, WHITE)
self.screen.blit(text_surface, (WIDTH / 2, HEIGHT * 7 / 8))
pg.display.flip()
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
self.running = False
done = True
if event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE:
done = True
self.clock.tick(FPS)
If you want to make more complex scenes/states, I recommend to use a finite state machine like this. It's actually just a scene manager class which allows you to switch between the different scenes/states of the game.
来源:https://stackoverflow.com/questions/43441996/how-to-display-game-over-screen-and-you-win-screen-in-pygame