2013-08-29 152 views
0

基本上我試圖在屏幕上實現遊戲(以及稍後的暫停屏幕等)。我現在要做的只是通過重新繪製屏幕上的字符串說遊戲結束,即時消息不會在屏幕上使用任何紋理進行遊戲。我也希望它能夠在按下某個按鍵(例如ENTER鍵)時使它重新啓動遊戲,但是在使用XNA的情況下,看起來,當你按下菜單上的一個按鍵時,它的處理髮生得太快,(假設這是由於更新和繪製方法被調用的次數,例如每秒60次)。所以所有這一切都說,我想有一個輕微的延遲之間的關鍵是在屏幕上的遊戲被按下和遊戲實際上重新啓動和被重新繪製,所以然後希望我可以實現這個爲其他菜單的,所以我不會必須不斷提問類似的問題,因此給你一個休息的時間! :d笑試圖在屏幕上實現遊戲

好了,所以這裏是關於我的情況最相關的代碼:

protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     spriteBatch.Begin(); 

     //if (userPlayer.Lives <= 0) //if hascollided is used here, it may change from true to false and vice versa, therefore the game over screen wont stay displayed and will revert back to the other screen when there isnt a coliison. 

     if (inGameScreenShowing == true) 
     { 
      InGameScreen(spriteBatch, gameTime); 
     } 


     spriteBatch.DrawString(someText, "time?...: " + delay, new Vector2(screenWidth/2, screenHeight/6), Color.Pink); 
      spriteBatch.End(); 

      base.Draw(gameTime); 

    } 


public void InGameScreen(SpriteBatch spriteBatch, GameTime gameTime) 
    { 
     if (userPlayer.Lives <=0) 
     { 
      if (inGameScreenShowing == true) 
      { 
       inGameScreenShowing = false; 
       gameOverScreenShowing = true; 
       GameOverScreen(spriteBatch, gameTime); 
      } 
     } 

     if (gameOverScreenShowing == false) 
     { 
      userPlayer.Draw(spriteBatch); 
      computerPlayer.Draw(spriteBatch); 

      //spriteBatch.DrawString(someText, "Plyr&CompCrashed: " + playerCompHaveCollided, new Vector2(screenWidth - 300, 30), Color.Blue); 
      spriteBatch.DrawString(someText, "Plyr Pos: " + userPlayer.Position, new Vector2(30, 30), Color.Red); 
      spriteBatch.DrawString(someText, "Plyr Lives: " + userPlayer.Lives, new Vector2(screenWidth - 300, 30), Color.Orange); 
      spriteBatch.DrawString(someText, "Plyr Score: " + userPlayer.Score, new Vector2(screenWidth - 300, 60), Color.LightBlue); 
      spriteBatch.DrawString(someText, "Window Size: " + screenWidth + " , " + screenHeight, new Vector2(30, 60), Color.Purple); 
     } 
    } 


public void GameOverScreen(SpriteBatch spriteBatch, GameTime gameTime) 
    { 

     if (gameOverScreenShowing == true) 
     { 
      spriteBatch.DrawString(someText, "Game Over", new Vector2(screenWidth/2 - 30, screenHeight/2), Color.Yellow); 
     } 

     if (lastKeyboardState == null && currentKeyboardState == null) 
     { 
      lastKeyboardState = currentKeyboardState = Keyboard.GetState(); 
     } 
     else 
     { 
      if (currentKeyboardState.IsKeyDown(Keys.Enter) && lastKeyboardState.IsKeyUp(Keys.Space)) 
      { 
       delay += dt; 
       if (delay >= 1) 
       { 
        gameOverScreenShowing = false; 
        inGameScreenShowing = true; 
        userPlayer.Lives = 3; 
        this.Draw(gameTime); 
        //InGameScreen(spriteBatch, gameTime); 
       } 
      } 

     } 

      lastKeyboardState = currentKeyboardState; 
     } 

我知道有一些在其中一些方法中的代碼可能是沒有意義可言,例如延遲+ = dt ...我試圖介紹時間之間的按鍵被按下,當遊戲狀態實際上改變之間的延遲...也lastKeyboardState = currentKeyboardState也許應該在方法的其他地方在遊戲中,但我有這個在這種方法相當一段時間了,剛離開那裏... :)

感謝您的輸入(沒有雙關語意)人:d

+0

哦...我只是意識到的是關鍵起來,關鍵是上下正在檢查兩把不同的鑰匙......所以這是錯誤的......但是當它檢查相同的密鑰時它仍然不起作用......所以這不是主要錯誤:D – 6a6179

回答

1

我認爲你是在正確的線路。怎麼樣是這樣的:

if (currentKeyboardState.IsKeyDown(Keys.Enter) && lastKeyboardState.IsKeyUp(Keys.Space)) 
{ 
    gameOverFlag = true; 
    delayTime = DateTime.Now.Add(// Time to delay) 
} 

然後,在InGameScreen部分:

if (userPlayer.Lives <= 0) 
    { 
     if (inGameScreenShowing == true) 
     { 
      inGameScreenShowing = false; 
      gameOverScreenShowing = true; 
      GameOverScreen(spriteBatch, gameTime); 
     } 
     else 
     { 
      if (delayTime > DateTime.Now) 
      { 
       // Hide screen and reset lives 
      } 
     } 
    } 
+0

感謝您的快速回復...... :)。不幸的是,我得到一個錯誤,說我不能使用'>'操作符操作數,所以這兩個變量的任何一方,因爲它們分別是int類型和System.DateTime類型... – 6a6179

+0

DelayTime需要是DateTime –