A splash screen and then your game screen: Zombie Santa Claus Level Programming

It’s so simple that you might now quite get it, there is a first screen, like a splash screen, then the main play screen appears.  You could add code to include a help screen, etc.  Or not.

In this case a splash screen appears for 3 seconds, total time, then another screen appears that has a molecule and fonts on it.  This represents a very simple way to have a splash screen with your game, but it only works with one screen.

If you are a little more advanced, you can see that it would be easy to create a class to handle some of the code in this program, it is not my goal to create class heavy programs that are difficult to understand.  I removed all “using” lines that were not being used, good practice would just keep them in as the compiler removes any that are not in use.

 

Make sure to scroll down to get to the zip file with the code ready to run!

  

Code Snippet

  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5.  
  6. namespace ZombieLevelGameManagement
  7. {
  8.     public class Game1 : Microsoft.Xna.Framework.Game
  9.     {
  10.         /********************************************************************
  11.          * Class variable area, variables placed here are available throughout
  12.          * the class
  13.          *******************************************************************/
  14.         /********************************************************************
  15.          * Object variables put in the code by the default template
  16.          *******************************************************************/
  17.         GraphicsDeviceManager graphics;
  18.         SpriteBatch spriteBatch;
  19.         /********************************************************************
  20.          * SpriteFont's added by author to load the spritefonts used in
  21.          * this lab (A type of object variable
  22.          *******************************************************************/
  23.         SpriteFont WeirdoFirstScreen;
  24.         SpriteFont SillyFontSecondScreen;
  25.         /********************************************************************
  26.          * Object variable for the image
  27.                  *******************************************************************/
  28.         Texture2D t_Molecule;
  29.         Rectangle r_Molecule;
  30.         /********************************************************************
  31.          * variables used for various storage jobs in the program
  32.          * all are initialized here, they could be initialized elsewhere in
  33.          * the program
  34.          *******************************************************************/
  35.         bool Splash = true, ScreenOne = false;
  36.         private int h_speed= 3, v_speed=-2;
  37.         private int MoleCuleStarting_x = 100, MoleCuleStarting_y = 100;
  38.         private int MoleculeWidth = 300, MoleculeHeight = 150;
  39.         /********************************************************************/
  40.         public Game1()
  41.         {
  42.             /********************************************************************
  43.              * Class constructor area
  44.              *******************************************************************/
  45.             graphics = new GraphicsDeviceManager(this);
  46.             Content.RootDirectory = "Content";
  47.  
  48.             /********************************************************************
  49.              *Frame rate is 30 fps by default for Windows Phone.
  50.              *******************************************************************/
  51.             TargetElapsedTime = TimeSpan.FromTicks(333333);
  52.             /********************************************************************
  53.              * Provides orientation support
  54.              *******************************************************************/
  55.             graphics.SupportedOrientations =
  56.                 DisplayOrientation.LandscapeLeft
  57.                 | DisplayOrientation.LandscapeRight
  58.                 |DisplayOrientation.Portrait;
  59.             /********************************************************************/
  60.         }
  61.         protected override void Initialize()
  62.         {
  63.             /********************************************************************
  64.              * You could initialize the booleans to determine which screen is active
  65.              * here, but I chose to declare and initialize the variables at the
  66.              * class level.
  67.              * The rectangle molecule is initialized here with
  68.              ********************************************************************/
  69.             r_Molecule= new Rectangle(MoleCuleStarting_x,
  70.                                       MoleCuleStarting_y,
  71.                                       MoleculeWidth,
  72.                                       MoleculeHeight);
  73.             /********************************************************************/
  74.             base.Initialize();
  75.         }
  76.         protected override void LoadContent()
  77.         {
  78.             /********************************************************************
  79.              * Entered by the default XNA Template
  80.              *******************************************************************/
  81.             spriteBatch = new SpriteBatch(GraphicsDevice);
  82.             /********************************************************************/
  83.             /************************************************************
  84.              * Loading the two fonts so that you can tell the difference
  85.              ***********************************************************/
  86.             WeirdoFirstScreen = Content.Load<SpriteFont>("WeirdoFont");
  87.             SillyFontSecondScreen = Content.Load<SpriteFont>("SillyFont");
  88.             /********************************************************************
  89.              * Load an image so you can see it doing something, but not much
  90.              * it must be that the molecule is at absolute zero.
  91.              *******************************************************************/
  92.             t_Molecule = Content.Load<Texture2D>("co2");
  93.         }
  94.         /********************************************************************/
  95.         protected override void Update(GameTime gameTime)
  96.         {
  97.             /********************************************************************
  98.              * For your game to pass the certification process you need to have
  99.              * the back key enabled. Leave this code in, even if it written
  100.              * for the XBox
  101.              *******************************************************************/
  102.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==
  103.                         ButtonState.Pressed)
  104.                     this.Exit();
  105.             /********************************************************************/
  106.  
  107.             /********************************************************************
  108.              * Check to see which screen is shown and then update the screen
  109.              * *****************************************************************/
  110.             if (Splash)
  111.             {
  112.                 SplashUpdate(gameTime);
  113.             }
  114.             else if (ScreenOne)
  115.             {
  116.                 ScreenOneUpdate();
  117.             }
  118.  
  119.             base.Update(gameTime);
  120.             /********************************************************************/
  121.         }
  122.          /*******************************************************************/
  123.         private void SplashUpdate(GameTime gameTime)
  124.         {
  125.             if (gameTime.TotalGameTime.TotalSeconds> 3)
  126.             {
  127.                 /********************************************************************
  128.                  * Reset the booleans that are storing the information about which
  129.                  * screen is in use.
  130.                 *******************************************************************/
  131.                 ScreenOne = true;
  132.                 Splash = false;
  133.                 return;
  134.             }
  135.         }
  136.         /********************************************************************/
  137.         private void ScreenOneUpdate()
  138.         {
  139.             /********************************************************************
  140.              * Call the method that will move the image, as you can see the
  141.              * Method was named in such a way you don't really need this level
  142.              * of comments.
  143.              *******************************************************************/
  144.             MoveImageScreenOne();
  145.         }
  146.         /********************************************************************/
  147.         protected override void Draw(GameTime gameTime)
  148.         {
  149.             GraphicsDevice.Clear(Color.CornflowerBlue);
  150.             /********************************************************************
  151.              * Begin drawing the SpriteBatches
  152.              *******************************************************************/
  153.             spriteBatch.Begin();
  154.             /********************************************************************
  155.              * Test for the screen type
  156.              *******************************************************************/
  157.             if (Splash)
  158.             {
  159.                 GraphicsDevice.Clear(Color.Black);
  160.                 /********************************************************************
  161.                  * Draw the splash screen and wait for 3 seconds, seems longer
  162.                  * All this does is present a line stating that you will wait for 3
  163.                  * seconds
  164.                  *******************************************************************/
  165.                 spriteBatch.DrawString(WeirdoFirstScreen,
  166.                                     "First Screen: Wait 3 Seconds",
  167.                                     new Vector2(150, 50),
  168.                                     Color.White);
  169.                 /********************************************************************/
  170.  
  171.             }
  172.             else if (ScreenOne)
  173.             {
  174.                 GraphicsDevice.Clear(Color.Orange);
  175.                 /********************************************************************
  176.                  * Draw the second screen using the second font with an
  177.                  * ugly orange hue for the background
  178.                 *******************************************************************/
  179.                 spriteBatch.DrawString(SillyFontSecondScreen,
  180.                                         "Second Screen",
  181.                                         new Vector2(10, 150),
  182.                                         Color.White);
  183.                 GraphicsDevice.Clear(Color.Orange);
  184.                 /********************************************************************/
  185.                 spriteBatch.DrawString(SillyFontSecondScreen,
  186.                                         "CO2 attacks",
  187.                                         new Vector2(10, 250),
  188.                                         Color.White);
  189.                 /********************************************************************
  190.                  * Draw the molecule, the position is updated with the
  191.                  * MoveImageScreenOne() method called from the Update method
  192.                  *******************************************************************/
  193.                 spriteBatch.Draw(t_Molecule,    //Texture
  194.                                 r_Molecule,     //molecule rectangle and includes the x,y
  195.                                 Color.White);   //Color
  196.                 /********************************************************************/
  197.             }
  198.             /********************************************************************
  199.              * Stop drawing the spriteBatch with the end
  200.              *******************************************************************/
  201.             spriteBatch.End();
  202.             base.Draw(gameTime);
  203.         }
  204.         /********************************************************************/
  205.         private void MoveImageScreenOne()
  206.         {
  207.             /********************************************************************
  208.              * Set the speed of the molecule to a constant speed
  209.              * Not a real life example where a molecule would have a lot of
  210.              * randomness
  211.              *******************************************************************/
  212.             r_Molecule.X += h_speed;
  213.             r_Molecule.Y += v_speed;
  214.             /********************************************************************
  215.              * Check for boundaries
  216.              *******************************************************************/
  217.             //BOTTOM
  218.             if (r_Molecule.Y >
  219.                 graphics.GraphicsDevice.Viewport.Height -
  220.                 r_Molecule.Height)
  221.             {    
  222.                 v_speed *= -1;
  223.             }
  224.             /********************************************************************/
  225.             //TOP
  226.             if (r_Molecule.Y < 0)
  227.             {
  228.                 v_speed *= -1;
  229.             }
  230.             /********************************************************************/
  231.             //LEFT
  232.             if (r_Molecule.X >
  233.                 graphics.GraphicsDevice.Viewport.Width - r_Molecule.Width)
  234.             {
  235.                 h_speed *= -1;
  236.             }
  237.             /********************************************************************/
  238.             //RIGHT
  239.             if (r_Molecule.X < 0)
  240.             {
  241.                 h_speed *= -1;
  242.             }
  243.             /********************************************************************/
  244.         }
  245.     }
  246. }

ZombieSantaClaus.zip