试图通过屏幕实现游戏

时间:2013-08-29 13:14:37

标签: c# xna logic

基本上我正在尝试通过屏幕实现游戏(以及暂停屏幕等等)。我现在正在做这个,只是通过重新绘制屏幕上的字符串说游戏结束而我不在游戏屏幕上使用任何纹理。

此外我很难获得键盘输入,所以当它说游戏结束时,我想要它,以便当按下一个键(例如ENTER键)使它重新启动游戏有效,但是使用XNA,它似乎当你按下菜单上的一个键时,处理过快发生,(我假设这是由于调用更新和绘制方法的次数...例如每秒60次)。所有这一切都说,我希望在屏幕上的游戏按键和游戏实际重新启动和重新绘制之间有一点延迟,所以希望我可以为其他菜单实现这个,所以我不会不得不继续问类似的问题,因此给你一个休息时间! :D lol

好的,所以这里是关于我的情况最相关的代码:

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

1 个答案:

答案 0 :(得分: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
            }
        }
    }