6a6179
6a6179

Reputation: 280

Trying to implement a game over screen

Basically i am trying to implement a game over screen (and a pause screen, etc later on). I am going about doing this at the moment just by re-drawing the screen with a string saying game over and im not using any textures for the game over screen.

Also im having difficulty with getting the keyboard input, so when it does say game over, i want it so that when a key is pressed (e.g. the ENTER key) make it restart the game in effect, however with XNA, it seems that when you press a key on a menu, it the processing happens too quickly, (im assuming this is due to the amount of times the update and draw methods are called... e.g. 60 times a second). So all of this being said, i would like to have a slight delay between the key being pressed on the game over screen and the game actually restarting and being re-drawn, so then hopefully i can implement this for other menu's, so i wont have to keep asking similar questions, therefore giving you people a break! :D lol

Ok, so here's the most relevant code regarding my situation:

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

I know some of the code in some of these methods may not make sense at all, e.g. the delay +=dt... i was trying to introduce a delay between when the key is pressed and when the gamestate is actually changed... also the lastKeyboardState = currentKeyboardState maybe should be somewhere else in the game over method but i had this in this method quite a while ago and have just left it there... :)

Thanks for the input (no pun intended) guys :D

Upvotes: 0

Views: 2379

Answers (1)

Paul Michaels
Paul Michaels

Reputation: 16705

I think you're on the right lines. How about something like this:

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

Then, in the InGameScreen section:

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

Upvotes: 1

Related Questions