试图在屏幕上实现游戏

本文关键字:实现 游戏 屏幕 | 更新日期: 2023-09-27 18:29:26

基本上,我正在尝试在屏幕上实现游戏(以及稍后的暂停屏幕等)。我现在要做的只是用一个字符串重新绘制屏幕,上面写着游戏结束,我没有在屏幕上使用任何纹理。

此外,我很难获得键盘输入,所以当它确实说游戏结束时,我希望它能在按下某个键(例如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

试图在屏幕上实现游戏

我认为你说得对。像这样的东西怎么样:

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
            }
        }
    }