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

image

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

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. /******************************************************
  4. * The using GameStateManagement refers to the namespace
  5. * that the files in the ScreenManager uses
  6. *****************************************************/
  7. using GameStateManagement;
  8.  
  9.  
  10. namespace SimpleGameManagement
  11. {
  12.     /*********************************************
  13.      * The public class is derived from the class
  14.      * GameScreen in the namespace GameStateManagement.
  15.      ********************************************/
  16.     public class GamePlayScreen : GameScreen
  17.     {
  18.  
  19.         /// <summary>
  20.         /// The Dracula image uses the texture t_Dracula
  21.         /// </summary>
  22.         Texture2D t_Darcula;
  23.  
  24.         /// <summary>
  25.         /// The location for the Dracula is determined by the 2D vector v_Dracula
  26.         /// </summary>
  27.         Vector2 v_Darcula;
  28.         
  29.         /**********************************************
  30.          * The GameScreen class defines some core game
  31.          * functionality matching the three game states:
  32.          *      LoadContent, Update, and Draw.
  33.          * In the method below content is loaded
  34.          *********************************************/
  35.         public override void LoadContent()
  36.         {
  37.             LoadAssets();
  38.             base.LoadContent();
  39.         }
  40.  
  41.         /**********************************************
  42.          * LoadAssets method, this method loads the
  43.          * gameplay screen?s resources and initialize
  44.          * some of its variables.  You use this to keep
  45.          * the LoadContent from getting messy, it helps
  46.          * with management of your code.         *
  47.          **********************************************/
  48.         public void LoadAssets()
  49.         {
  50.             t_Darcula = Load<Texture2D>("Dracula");
  51.             v_Darcula = new Vector2(100, 100);
  52.         }
  53.         /***********************************************
  54.          * Override the Draw method so that the gameplay
  55.          * screen will be able to draw itself onto the
  56.          * screen
  57.          ***********************************************/
  58.         public override void Draw(GameTime gameTime)
  59.         {
  60.             float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
  61.  
  62.             /*********************************************
  63.              * The ScreenManager is one of the classes
  64.              * that you included to help with game
  65.              * management.  The
  66.              * */
  67.             ScreenManager.SpriteBatch.Begin();
  68.  
  69.             DrawStuff();
  70.  
  71.             ScreenManager.SpriteBatch.End();
  72.  
  73.             
  74.             base.Draw(gameTime);
  75.         }
  76.  
  77.         /***********************************************
  78.          * The DrawStuff method (sometimes referred to as
  79.          * a helper method).  The DrawStuff could
  80.          * have more things going on.  In this case
  81.          * the DrawStuff method draws the various
  82.          * background elements and keeps your Draw method
  83.          * neat and clean.
  84.          * ********************************************/
  85.         public void DrawStuff()
  86.         {
  87.             ScreenManager.Game.GraphicsDevice.Clear(Color.Yellow);
  88.             ScreenManager.SpriteBatch.Draw(t_Darcula, v_Darcula, Color.White);
  89.         }
  90.     }
  91. }

 

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

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Audio;
  3. using Microsoft.Xna.Framework.Content;
  4. using Microsoft.Xna.Framework.GamerServices;
  5. using Microsoft.Xna.Framework.Graphics;
  6. using Microsoft.Xna.Framework.Input;
  7. using Microsoft.Xna.Framework.Input.Touch;
  8. using Microsoft.Xna.Framework.Media;
  9. using GameStateManagement;
  10.  
  11. namespace SimpleGameManagement
  12. {
  13.     /// <summary>
  14.     /// This is the main type for your game
  15.     /// </summary>
  16.     public class SimpleGame : Game
  17.     {
  18.         GraphicsDeviceManager graphics;
  19.         /************************************************
  20.          * The ScreenManage object is from the
  21.          * GameStateManagement
  22.          ************************************************/
  23.         ScreenManager screenManager;
  24.  
  25.         /************************************************
  26.          * The SimpleGame contains all of the code needed
  27.          * and instantiates the ScreenManager within this
  28.          * class.  The screenManager (lower case 's') adds
  29.          * a new GamePlayScreen.
  30.          ************************************************/
  31.         public SimpleGame()
  32.         {
  33.             graphics = new GraphicsDeviceManager(this);
  34.             Content.RootDirectory = "Content";
  35.  
  36.             screenManager = new ScreenManager(this);
  37.             Components.Add(screenManager);
  38.             graphics.IsFullScreen = true;
  39.             screenManager.AddScreen(new GamePlayScreen(), null);
  40.         }
  41.     }
  42. }

You can use any PNG for your image, the ugly spud that I used was a clipart from Office.