Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试在屏幕上实现游戏_C#_Xna_Logic - Fatal编程技术网

C# 尝试在屏幕上实现游戏

C# 尝试在屏幕上实现游戏,c#,xna,logic,C#,Xna,Logic,基本上,我正在尝试在屏幕上实现一个游戏(以及稍后的暂停屏幕等)。我现在要做的就是用一个字符串重新绘制屏幕,上面写着“游戏结束”,我没有在屏幕上使用任何纹理 另外,我在获取键盘输入时遇到困难,因此当它确实显示游戏结束时,我希望这样,当按下一个键(例如回车键)时,它可以有效地重新启动游戏,但是在XNA中,似乎当你按下菜单上的一个键时,处理速度过快,(我假设这是由于调用update和draw方法的次数造成的,例如每秒调用60次).所以说了这么多,我希望在屏幕上按下游戏键和游戏实际重新启动并重新绘制之间

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

另外,我在获取键盘输入时遇到困难,因此当它确实显示游戏结束时,我希望这样,当按下一个键(例如回车键)时,它可以有效地重新启动游戏,但是在XNA中,似乎当你按下菜单上的一个键时,处理速度过快,(我假设这是由于调用update和draw方法的次数造成的,例如每秒调用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;
      }
protected override void Draw(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
spriteBatch.Begin();

//如果(userPlayer.lifes我认为你说得对。像这样怎么样:

if (currentKeyboardState.IsKeyDown(Keys.Enter) && lastKeyboardState.IsKeyUp(Keys.Space))
{
    gameOverFlag = true;
    delayTime = DateTime.Now.Add( // Time to delay )
}
然后,在IngaMesscreen部分:

    if (userPlayer.Lives <= 0)
    {
        if (inGameScreenShowing == true)
        {
            inGameScreenShowing = false;
            gameOverScreenShowing = true;
            GameOverScreen(spriteBatch, gameTime);
        }
        else
        {
            if (delayTime > DateTime.Now)
            {
                // Hide screen and reset lives
            }
        }
    }
if(userPlayer.Lives DateTime.Now)
{
//隐藏屏幕和重置生命
}
}
}

哦……我刚刚意识到is键向上和is键向下正在检查两个不同的键……所以这是错误的……但是,当它检查同一个键时,它仍然不起作用……所以这不是主要错误:感谢您的快速回复……)。不幸的是,我收到一个错误,说我不能对操作数使用'>'运算符,因此两边的两个变量,因为它们分别是int类型和System.DateTime类型…DelayTime需要是DateTime