DEV Community

Cover image for Alternative to Game Screen Management - An Example
Adam K Dean
Adam K Dean

Posted on

1 1

Alternative to Game Screen Management - An Example

So just to give you an example of how clean you can make your code, let us look at a very simple menu screen, which simply asks for any key to be pressed. As you can see, it's -very- clean.

I also love #region's as well, best thing since line numbers.

For more information on my game Space Rocks - visit my newly designed (I'm working on it okay!) website at www.adamkdean.co.uk

(Update: game is no longer there, this post is here for history.)

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SpaceRocks
{
    class MenuScreen : IScreen
    {
        #region Variables
        private VariableService vars;

        private SpriteFont font;
        private Texture2D mainBackground;
        #endregion

        #region Constructor Methods
        public MenuScreen(Game game)
        {
            vars = ServiceExtensionMethods
                .GetService<variableservice>(game.Services);

            LoadContent();
        }

        private void LoadContent()
        {
            mainBackground = vars.Content.Load<texture2d>("Images/background");
            font = vars.Content.Load<spritefont>("Fonts/Menu");
        }
        #endregion

        #region Update Methods
        public void Update(GameTime gameTime)
        {
            UpdateInput();
        }

        private void UpdateInput()
        {
            if (Keyboard.GetState().GetPressedKeys().Length > 0)
                vars.CurrentScreen = new GameScreen(vars.Game);
        }
        #endregion

        #region Draw Methods
        public void Draw(GameTime gameTime)
        {
            vars.GraphicsDevice.Clear(Color.Black);

            vars.SpriteBatch.Begin();

            DrawBackground();
            DrawText();

            vars.SpriteBatch.End();
        }

        private void DrawBackground()
        {
            vars.SpriteBatch.Draw(mainBackground, Vector2.Zero, Color.White);
        }

        private void DrawText()
        {
            string text = "Press any key to begin..";
            Vector2 textSize = font.MeasureString(text);

            int x = vars.GraphicsDevice.Viewport.Width / 2 - (int)textSize.X / 2;
            int y = vars.GraphicsDevice.Viewport.Height / 2 - (int)textSize.Y / 2;

            vars.SpriteBatch.DrawString(font, text,
                new Vector2(x, y), Color.White);
        }
        #endregion
    }
}
Enter fullscreen mode Exit fullscreen mode

For anyone wondering why there are some tags at the end of certain code posts, it's because the awesome script I use cannot handle generics :(

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay