XNA Game States

Trouble in implementation

Page 1 of 1

4 Replies - 5332 Views - Last Post: 23 October 2010 - 02:42 PM

#1 UofMCreed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 04-October 08

XNA Game States

Posted 20 October 2010 - 03:02 AM

Hello all,

I have been making games off and on for the past year or two and i am just getting into game states. I have looked on the XNA site and they have but one example which is way to complicated for me at this point in time. I need to make a simple game with a start menu, inGame, and end screen. Ive got the idea down; get an enum list called Gamestates, change the states in the update methode and draw a different background to the screen depending on the game state, and do logic when the state is InGame. Ive got that down and working(except the logic), my problem is implementing the game logic and uploading my sprites.


What i want to do is when my GameState is InGame, i want to call a class(lets call it level1) and then upload the sprites i need, perform logic, and draw to the screen. I get problems with trying to load sprites in the level1 class and having the game perform logic and output.

Ive been researching this for a while and trying to implement for even longer. Am I on the right track? Is there a trick i am missing? What should i do to make my level1 class to load, do logic, and draw? Can anyone show me or point me in the right direction im about to go ape shit.

Game1.cs file is below to show what I have going.

Thanks very much for your time and appreciate anything you can do to help me figure this out
-UofMCreed

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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace GameWithAliensInIt
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
      
        //Rectangles 
        Rectangle mainFrame;
        Rectangle groundRectangle;     

        //Keyboard States
        KeyboardState cK;
        KeyboardState pK = Keyboard.GetState();

        //Backgrounds for the different states
        Texture2D titlescreen;
        Texture2D endscreen;
        Texture2D gamescreen;
        Texture2D ground;
        Texture2D playerTexture;

        enum GameState { Start, InGame, GameOver };
        GameState currentGameState = GameState.Start;

        //Game state level
        private Level level = new Level();

        int counter = 0;

        public int Counter
        {
            get { return counter; }

            set
            {
                counter = value;
                if (counter == 0)
                {
                    currentGameState = GameState.GameOver;
                }
            }
        }

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {

            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //Loading the background textures
            gamescreen = Content.Load<Texture2D>(@"Images/50x50 Pixel Grid");
            titlescreen = Content.Load<Texture2D>(@"Images/mainScreen"); 
            endscreen = Content.Load<Texture2D>(@"Images/endScreen");

            //Rectangle that fits the entire game window, all action happens within this rectangle
            mainFrame = new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);

            //Loading the ground texture
            ground = Content.Load<Texture2D>(@"Images/Ground");

            //Rectangle for the ground, to help collision detection
            groundRectangle = new Rectangle(0, 550, ground.Width, ground.Height);

            playerTexture = Content.Load<Texture2D>(@"Images/player");
        }

        protected override void UnloadContent()
        {
            
        }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit using the keyboard
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            #region GameStates
            cK = Keyboard.GetState();

            switch (currentGameState)
            {
                //If the state of the game 'GameState' is set to start, it will draw titlescreen
                case GameState.Start:
                    if (cK.IsKeyDown(Keys.Space) && pK.IsKeyUp(Keys.Space))
                    {
                        currentGameState = GameState.InGame;
                        counter = 0;
                    }
                    break;

                //If the state of the game is set to ingame, it will draw gamescreen
                case GameState.InGame:
                    if (cK.IsKeyDown(Keys.Space) && pK.IsKeyUp(Keys.Space))
                    {
                        currentGameState = GameState.GameOver;
                    }
                    //LEVEL1 CALLS SHOULD GO HERE I WOULD THINK
                    break;

                //If the state of the game is set to gameover, it will draw the endscreen
                case GameState.GameOver:

                    if (cK.IsKeyDown(Keys.Space) && pK.IsKeyUp(Keys.Space))
                    {   
                        //Exiting the Game
                        this.Exit();
                    }
                    break;
            }

            pK = cK;
            #endregion

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            //GraphicsDevice.Clear(Color.CornflowerBlue);

            //Drawing the different backgrounds to the screen depending on the GameState
            switch (currentGameState)
            {
                //If the state of the game 'GameState' is set to start, it will draw titlescreen
                case GameState.Start:
                    GraphicsDevice.Clear(Color.White);
                    spriteBatch.Begin();
                    spriteBatch.Draw(titlescreen, new Rectangle(0, 0, window.ClientBounds.Width,
                            window.ClientBounds.Height), null, Color.White, 0,Vector2.Zero, SpriteEffects.None, 0);
                    spriteBatch.End();
                    break;

                //If the state of the game is set to ingame, it will draw gamescreen
                case GameState.InGame:
                    GraphicsDevice.Clear(Color.White);
                    spriteBatch.Begin();
                    spriteBatch.Draw(gamescreen, new Rectangle(0, 0, window.ClientBounds.Width,
                            window.ClientBounds.Height), null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0);
                    spriteBatch.Draw(ground, groundRectangle, Color.White);
                    spriteBatch.End();
                    break;

                //If the state of the game is set to gameover, it will draw the endscreen
                case GameState.GameOver:
                    GraphicsDevice.Clear(Color.White);
                    spriteBatch.Begin();
                    spriteBatch.Draw(endscreen, new Rectangle(0, 0, window.ClientBounds.Width,
                            window.ClientBounds.Height), null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0);
                    spriteBatch.End();
                    break;
            }

            base.Draw(gameTime);
        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: XNA Game States

#2 Skaggles  Icon User is offline

  • I AM THE LAW!
  • member icon





Reputation: 248
  • View blog
  • Posts: 637
  • Joined: 01-March 09

Re: XNA Game States

Posted 20 October 2010 - 03:57 AM

I haven't done much work with game states in XNA, but if it helps there are a few base projects you can download through the Visual Studio Extension Manager. They'll let you start a new XNA project with a pre-coded game state system that you can choose to use or dissect and possibly figure them out.

Also, SixOfEleven has a tutorial series here on DIC about screen management for XNA 3.x.

Hope this helped.
Was This Post Helpful? 0
  • +
  • -

#3 frenchwolf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-September 10

Re: XNA Game States

Posted 20 October 2010 - 06:10 PM

You can put a switch in your Update and Draw method with your specific game state.
Was This Post Helpful? 0
  • +
  • -

#4 Drakken255  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-October 10

Re: XNA Game States

Posted 23 October 2010 - 09:16 AM

Simple, when in menu state, draw rows of text and check collisions between the mouse and rectangles that correspond to the location of the text. If so, change to InGame. Then make your Level1 class require a ContentManager in it's constructor, using that to load your graphics. Use a class level ContentManager variable to keep the constructor parameter until it's needed. Make a struct that holds one of each of the needed graphics resources, and add a method to your Level1 class like so (for example)

public Level1Resources GetGraphicsResources()
{
Level1Resources resources = new Level1Resources(Content.Load<Texture2D>(@"Images/example-img"), ..., and so on);
return resources;
}

public struct Level1Resources
{
Texture2D exampleGraphicsResource;

public Level1Resources(Texture2D exampleResource, ..., and so on)
{
exampleGraphicsResource = exampleResource;
}
}

Your main engine will then be able to access that resource for use once GetGraphicsResources is called:

Level1Resources resourceContainer = Level1.GetGraphicsResources();

This post has been edited by Drakken255: 23 October 2010 - 09:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 Hellbroth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 189
  • Joined: 15-August 09

Re: XNA Game States

Posted 23 October 2010 - 02:42 PM

An example from one of my games.


enum State
        {
            Walking,
            Jumping,
            Punching,
            Kicking
        }

State mCurrentState = State.Walking;






Then you can use if statements to check :)

if (mCurrentState == State.Walking)
{ 
   //Your code...
 

}


This post has been edited by Hellbroth: 23 October 2010 - 02:43 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1