0 Replies - 2474 Views - Last Post: 17 February 2013 - 10:56 AM

#1 bboyzeez  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 143
  • Joined: 16-December 12

trying to understand gamestate,enum and main menu

Posted 17 February 2013 - 10:56 AM

hi all, so i have followed a tutorial and made a basic space shooter but now i trying to go from scratch so i can fully understand each aspect .

i understand in the main class you create a enum for each of the scenes in the game, i have it so when i click a on my controller it switched from one to another, the next step is to get my simple characterand background to be drawn onto screen and then i will understand most of the rest until later stages,for now i have just made the background for the action scene drawn within the main class but i know its not correct way to draw every aspect of the action scene and to keep the updates in main class either? .

if someone can look at my main game class and then i will include the action scene code where i want to have the background,character,enemies tiles maps etc and just to to guide me to where to command the action scene draw, update methods etc?

thanks

Main Game Class
namespace HeliAttack
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class HeliAttack : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics; 
        SpriteBatch spriteBatch;
        GamePadState oldgamepadstate;
        private AudioLibrary audio;

        Texture2D backgroundTexture, optionsTexture;
        

        //scenes
        ActionScene actionScene;
        Texture2D actionBackTexture;

        //crosshair stuff
        Texture2D crosshairTexture;
        Rectangle crosshairRect;
        Vector2 crosshairPosition;
        float rotation = 0f;
        Vector2 origin;
        int index = 0;
        enum GameState
        {
            MainMenu,
            Options,
            Help,
            Playing,
        }
        GameState CurrentGameState = GameState.MainMenu;

        //Screen Adjustments
        int screenWidth = 1600, screenHeight = 900;

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

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
           
            

            

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            graphics.PreferredBackBufferWidth = screenWidth;
            graphics.PreferredBackBufferHeight = screenHeight;
            graphics.ApplyChanges();

            //scenes
           
            actionBackTexture = Content.Load<Texture2D>("images/landscape wallpaper");
            actionScene = new ActionScene(actionBackTexture);
            
            
            //audio elements 
            audio = new AudioLibrary();
            audio.LoadContent(Content);
            Services.AddService(typeof(AudioLibrary), audio);

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            backgroundTexture = Content.Load<Texture2D>("images/Mainmenu");
            optionsTexture = Content.Load<Texture2D>("images/OptionsMenu");
            crosshairTexture = Content.Load<Texture2D>("images/target");
            
            crosshairPosition = new Vector2(600, 530);
           
            origin = new Vector2(crosshairTexture.Width/2,crosshairTexture.Height/2);
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            crosshairRect = new Rectangle((int)crosshairPosition.X, (int)crosshairPosition.Y, crosshairTexture.Width, crosshairTexture.Height);
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            switch (CurrentGameState)
            {
                case GameState.Playing:
                    actionScene.LoadContent(Content);

                    break;


                case GameState.MainMenu:
                    rotation += 0.02f;
                    HandleMainMenuInput();

                    break;
                case GameState.Options:
                    if (GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Pressed)
                    {
                       CurrentGameState = GameState.MainMenu;
                    }
                    break;
                
            }

            base.Update(gameTime);
        }

        public void HandleMainMenuInput()
        {

            if (index == 0)
                crosshairPosition.Y = 530;
            if (index == 1)
                crosshairPosition.Y = 647;
            if (index == 2)
                crosshairPosition.Y = 760;
           GamePadState gamepadstate = GamePad.GetState(PlayerIndex.One);
            if (gamepadstate.DPad.Down == ButtonState.Pressed &&
                oldgamepadstate.DPad.Down == ButtonState.Released )
            {
                index += 1;
                audio.MenuChange.Play();

                if (index > 2)
                    index = 0;
                if (index < 0)
                    index = 2;
            }
            if (gamepadstate.DPad.Up == ButtonState.Pressed &&
                oldgamepadstate.DPad.Up == ButtonState.Released)
            {
                index -= 1;
                audio.MenuChange.Play();

                if (index > 2)
                    index = 0;
                if (index < 0)
                    index = 2;
            }

            CheckForA();
            oldgamepadstate = gamepadstate;
        }
        public void CheckForA()
        {
            if (CurrentGameState == GameState.MainMenu)
            {
                if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
                {
                    if (index == 0)
                    {
                        CurrentGameState = GameState.Playing;
                        audio.Select.Play();
                    }
                    if (index == 1)
                    {
                        CurrentGameState = GameState.Options;
                        audio.Select.Play();
                    }
                    if (index == 2)
                    {
                        audio.Select.Play();
                        this.Exit();
                    }
                }
            }
        }
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            switch (CurrentGameState)
            {
                case GameState.Playing:
                    spriteBatch.Draw(actionBackTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
                    break;

                case GameState.MainMenu:
                    spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
                    spriteBatch.Draw(crosshairTexture,crosshairRect,null,Color.White,rotation,origin,SpriteEffects.None,0);
                    break;
                case GameState.Options:
                    spriteBatch.Draw(optionsTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
                    break;

            }

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}



Action Scene Code
namespace HeliAttack
{
    class ActionScene
    {
        GraphicsDeviceManager graphics; 
        private Texture2D backgroundTexture;
        private Rectangle backgroundRect;

        Player charater;

        public ActionScene(Texture2D newTexture)
        {
            backgroundTexture = newTexture; 
        }
   
        public void LoadContent(ContentManager Content)
        {

            charater = new Player(Content.Load<Texture2D>("images/quick character"), new Rectangle(0, 800, 64, 64));
           
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(backgroundTexture,backgroundRect,Color.White);
        }
    }
}



ps. this is just my remake of the game heli attack so please dont tell me i am using an original name its purely for personal use and teaching

thanks

if any other code is needed please let me know thanks

Is This A Good Question/Topic? 0
  • +

Page 1 of 1