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
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace ZombieLevelGameManagement
- {
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- /********************************************************************
- * Class variable area, variables placed here are available throughout
- * the class
- *******************************************************************/
- /********************************************************************
- * Object variables put in the code by the default template
- *******************************************************************/
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- /********************************************************************
- * SpriteFont's added by author to load the spritefonts used in
- * this lab (A type of object variable
- *******************************************************************/
- SpriteFont WeirdoFirstScreen;
- SpriteFont SillyFontSecondScreen;
- /********************************************************************
- * Object variable for the image
- *******************************************************************/
- Texture2D t_Molecule;
- Rectangle r_Molecule;
- /********************************************************************
- * variables used for various storage jobs in the program
- * all are initialized here, they could be initialized elsewhere in
- * the program
- *******************************************************************/
- bool Splash = true, ScreenOne = false;
- private int h_speed= 3, v_speed=-2;
- private int MoleCuleStarting_x = 100, MoleCuleStarting_y = 100;
- private int MoleculeWidth = 300, MoleculeHeight = 150;
- /********************************************************************/
- public Game1()
- {
- /********************************************************************
- * Class constructor area
- *******************************************************************/
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- /********************************************************************
- *Frame rate is 30 fps by default for Windows Phone.
- *******************************************************************/
- TargetElapsedTime = TimeSpan.FromTicks(333333);
- /********************************************************************
- * Provides orientation support
- *******************************************************************/
- graphics.SupportedOrientations =
- DisplayOrientation.LandscapeLeft
- | DisplayOrientation.LandscapeRight
- |DisplayOrientation.Portrait;
- /********************************************************************/
- }
- protected override void Initialize()
- {
- /********************************************************************
- * You could initialize the booleans to determine which screen is active
- * here, but I chose to declare and initialize the variables at the
- * class level.
- * The rectangle molecule is initialized here with
- ********************************************************************/
- r_Molecule= new Rectangle(MoleCuleStarting_x,
- MoleCuleStarting_y,
- MoleculeWidth,
- MoleculeHeight);
- /********************************************************************/
- base.Initialize();
- }
- protected override void LoadContent()
- {
- /********************************************************************
- * Entered by the default XNA Template
- *******************************************************************/
- spriteBatch = new SpriteBatch(GraphicsDevice);
- /********************************************************************/
- /************************************************************
- * Loading the two fonts so that you can tell the difference
- ***********************************************************/
- WeirdoFirstScreen = Content.Load<SpriteFont>("WeirdoFont");
- SillyFontSecondScreen = Content.Load<SpriteFont>("SillyFont");
- /********************************************************************
- * Load an image so you can see it doing something, but not much
- * it must be that the molecule is at absolute zero.
- *******************************************************************/
- t_Molecule = Content.Load<Texture2D>("co2");
- }
- /********************************************************************/
- protected override void Update(GameTime gameTime)
- {
- /********************************************************************
- * For your game to pass the certification process you need to have
- * the back key enabled. Leave this code in, even if it written
- * for the XBox
- *******************************************************************/
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==
- ButtonState.Pressed)
- this.Exit();
- /********************************************************************/
- /********************************************************************
- * Check to see which screen is shown and then update the screen
- * *****************************************************************/
- if (Splash)
- {
- SplashUpdate(gameTime);
- }
- else if (ScreenOne)
- {
- ScreenOneUpdate();
- }
- base.Update(gameTime);
- /********************************************************************/
- }
- /*******************************************************************/
- private void SplashUpdate(GameTime gameTime)
- {
- if (gameTime.TotalGameTime.TotalSeconds> 3)
- {
- /********************************************************************
- * Reset the booleans that are storing the information about which
- * screen is in use.
- *******************************************************************/
- ScreenOne = true;
- Splash = false;
- return;
- }
- }
- /********************************************************************/
- private void ScreenOneUpdate()
- {
- /********************************************************************
- * Call the method that will move the image, as you can see the
- * Method was named in such a way you don't really need this level
- * of comments.
- *******************************************************************/
- MoveImageScreenOne();
- }
- /********************************************************************/
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- /********************************************************************
- * Begin drawing the SpriteBatches
- *******************************************************************/
- spriteBatch.Begin();
- /********************************************************************
- * Test for the screen type
- *******************************************************************/
- if (Splash)
- {
- GraphicsDevice.Clear(Color.Black);
- /********************************************************************
- * Draw the splash screen and wait for 3 seconds, seems longer
- * All this does is present a line stating that you will wait for 3
- * seconds
- *******************************************************************/
- spriteBatch.DrawString(WeirdoFirstScreen,
- "First Screen: Wait 3 Seconds",
- new Vector2(150, 50),
- Color.White);
- /********************************************************************/
- }
- else if (ScreenOne)
- {
- GraphicsDevice.Clear(Color.Orange);
- /********************************************************************
- * Draw the second screen using the second font with an
- * ugly orange hue for the background
- *******************************************************************/
- spriteBatch.DrawString(SillyFontSecondScreen,
- "Second Screen",
- new Vector2(10, 150),
- Color.White);
- GraphicsDevice.Clear(Color.Orange);
- /********************************************************************/
- spriteBatch.DrawString(SillyFontSecondScreen,
- "CO2 attacks",
- new Vector2(10, 250),
- Color.White);
- /********************************************************************
- * Draw the molecule, the position is updated with the
- * MoveImageScreenOne() method called from the Update method
- *******************************************************************/
- spriteBatch.Draw(t_Molecule, //Texture
- r_Molecule, //molecule rectangle and includes the x,y
- Color.White); //Color
- /********************************************************************/
- }
- /********************************************************************
- * Stop drawing the spriteBatch with the end
- *******************************************************************/
- spriteBatch.End();
- base.Draw(gameTime);
- }
- /********************************************************************/
- private void MoveImageScreenOne()
- {
- /********************************************************************
- * Set the speed of the molecule to a constant speed
- * Not a real life example where a molecule would have a lot of
- * randomness
- *******************************************************************/
- r_Molecule.X += h_speed;
- r_Molecule.Y += v_speed;
- /********************************************************************
- * Check for boundaries
- *******************************************************************/
- //BOTTOM
- if (r_Molecule.Y >
- graphics.GraphicsDevice.Viewport.Height -
- r_Molecule.Height)
- {
- v_speed *= -1;
- }
- /********************************************************************/
- //TOP
- if (r_Molecule.Y < 0)
- {
- v_speed *= -1;
- }
- /********************************************************************/
- //LEFT
- if (r_Molecule.X >
- graphics.GraphicsDevice.Viewport.Width - r_Molecule.Width)
- {
- h_speed *= -1;
- }
- /********************************************************************/
- //RIGHT
- if (r_Molecule.X < 0)
- {
- h_speed *= -1;
- }
- /********************************************************************/
- }
- }
- }