Simply learning Game Management: Using the Game State
If you read my blog on a regular basis, I deeply appreciate you as a reader, thank you for returning. I am depressed somewhat about the events of the day, which I won’t mention, but you know what I am talking about. Anyway….
In a class that I teach at Cal State Dominguez Hills, my unfortunate students have to suffer with my blathering for hours on end. But it does require that I prepare from time to time for the class and I have been using the Catapult tutorial because I had been told that it is a “beginner” tutorial. I never worked through it till today, and I suddenly realized that my students were very patient with me, so thank you students. I need to break this down, because 50 pages of anything, unless it is like one sentence per page, is too long. So until I get bored with this whole idea or I work through everything I am going to work through the catapult tutorial.
So first, to get the assets and code that I will use, download the catapult app project from :
Make sure to right click on the zip file to insure that the file is unblocked, by clicking on the Unblock button. If you don’t see the unblock button then that is ok.
The Catapult tutorial is an excellent tutorial, but like a lot of tutorials the tutorial states that you can complete it in 90 minutes. Right, that is if you have no questions and aren’t thinking about what you are doing. If you are trying to figure out how, what and why, then it takes quite a bit longer.
I will generally assume that you will be able to create an XNA game project. If not you can start with the referenced tutorial and then switch to this when it gets complicated.
Now create a project that is named “SimpleGameManagement”
We will use the following files:
Make sure your solution explorer looks like the Solution Explorer as shown
Once you have added the Game Management files, we will rework the project files.
For the Windows Phone you do not need the Project.cs file, so delete it to clean things up.
Change Game1.cs to SimpleGame.cs and select that it is ok to change the project.
Create a new class named SimpleGameManagement and paste the code below, replacing the default code: The comments are included to explain the code, but if you are doing code for industry, you should keep your comments short and pithy. Don’t use the “flower” approach that I use, why? Because you will have to maintain them and that takes time. There is more code and explanation below the code shown below:
Code Snippet
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- /******************************************************
- * The using GameStateManagement refers to the namespace
- * that the files in the ScreenManager uses
- *****************************************************/
- using GameStateManagement;
- namespace SimpleGameManagement
- {
- /*********************************************
- * The public class is derived from the class
- * GameScreen in the namespace GameStateManagement.
- ********************************************/
- public class GamePlayScreen : GameScreen
- {
- /// <summary>
- /// The Dracula image uses the texture t_Dracula
- /// </summary>
- Texture2D t_Darcula;
- /// <summary>
- /// The location for the Dracula is determined by the 2D vector v_Dracula
- /// </summary>
- Vector2 v_Darcula;
- /**********************************************
- * The GameScreen class defines some core game
- * functionality matching the three game states:
- * LoadContent, Update, and Draw.
- * In the method below content is loaded
- *********************************************/
- public override void LoadContent()
- {
- LoadAssets();
- base.LoadContent();
- }
- /**********************************************
- * LoadAssets method, this method loads the
- * gameplay screen?s resources and initialize
- * some of its variables. You use this to keep
- * the LoadContent from getting messy, it helps
- * with management of your code. *
- **********************************************/
- public void LoadAssets()
- {
- t_Darcula = Load<Texture2D>("Dracula");
- v_Darcula = new Vector2(100, 100);
- }
- /***********************************************
- * Override the Draw method so that the gameplay
- * screen will be able to draw itself onto the
- * screen
- ***********************************************/
- public override void Draw(GameTime gameTime)
- {
- float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
- /*********************************************
- * The ScreenManager is one of the classes
- * that you included to help with game
- * management. The
- * */
- ScreenManager.SpriteBatch.Begin();
- DrawStuff();
- ScreenManager.SpriteBatch.End();
- base.Draw(gameTime);
- }
- /***********************************************
- * The DrawStuff method (sometimes referred to as
- * a helper method). The DrawStuff could
- * have more things going on. In this case
- * the DrawStuff method draws the various
- * background elements and keeps your Draw method
- * neat and clean.
- * ********************************************/
- public void DrawStuff()
- {
- ScreenManager.Game.GraphicsDevice.Clear(Color.Yellow);
- ScreenManager.SpriteBatch.Draw(t_Darcula, v_Darcula, Color.White);
- }
- }
- }
In the original game class module replace the code with the following, you most likely notice that the Initialize, LoadContent, Draw and Update methods are replaced with the ScreenManager object in the GameStateManagement namespace.
Code Snippet
- 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.Input.Touch;
- using Microsoft.Xna.Framework.Media;
- using GameStateManagement;
- namespace SimpleGameManagement
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class SimpleGame : Game
- {
- GraphicsDeviceManager graphics;
- /************************************************
- * The ScreenManage object is from the
- * GameStateManagement
- ************************************************/
- ScreenManager screenManager;
- /************************************************
- * The SimpleGame contains all of the code needed
- * and instantiates the ScreenManager within this
- * class. The screenManager (lower case 's') adds
- * a new GamePlayScreen.
- ************************************************/
- public SimpleGame()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- screenManager = new ScreenManager(this);
- Components.Add(screenManager);
- graphics.IsFullScreen = true;
- screenManager.AddScreen(new GamePlayScreen(), null);
- }
- }
- }
You can use any PNG for your image, the ugly spud that I used was a clipart from Office.