2017-04-16 156 views
1

我有一個GAME OVER畫面,當您的健康狀況消失時,此畫面正在運行。這個GAME OVER屏幕是通過show_go_screen執行的。我試圖將show_go_screen的方法複製到我的另一個屏幕的show_winning()函數中。當我回到遊戲中時,我通過設置你的健康恢復正常來操縱這個節目的獲獎功能。但是,一旦您進入YOU WIN屏幕並返回遊戲,您將無法再次進入YOU WIN屏幕。我已經在下面包含了我的播放器屏蔽代碼(它決定了何時有GAME OVER/YOUWIN事件),我還包括了show_go_screen(用於GAME OVER)和show_winning()方法的代碼(它是爲你贏得屏幕)。如果有人能告訴我如何最好地修改我的代碼以獲得所需的結果。如何在pygame中顯示GAME OVER畫面和YOU WIN畫面

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 

這裏是show_go_screen()和show_winning()方法

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 
+0

請勿發佈您的代碼;改爲創建[mcve]。它會讓這個問題對其他人更有利,因爲閱讀,理解,測試/驗證變得更容易,而且這也會讓那些試圖找出問題的人更容易。 –

回答

1

如果玩家或敵人有小於零的健康,設置self.playing = False其停止遊戲的while循環。然後在最外面的while循環中調用遊戲實例gshow_go_screen方法。

while g.running: 
    g.new() 
    g.show_go_screen() 

show_go_screen方法,的blit背景圖像和文本(I創建的BACKGROUND_IMG和全球字體對象),翻轉顯示,並開始與一個事件循環另一while循環在其中檢查用戶是否想要退出或繼續。如果用戶退出,則將self.running設置爲False,以便外部while循環也將停止,因爲其條件爲while g.running:。如果空間被按下,你可以從這個show_go_screen方法返回並且外部while循環調用g.new()什麼重置遊戲。

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) 

如果你想更復雜的場景/狀態,我建議使用finite state machine like this。它實際上只是一個場景管理員類,它允許您在遊戲的不同場景/狀態之間切換。