trying to understand gamestate,enum and main menu

Started by
6 comments, last by crohnsandme 11 years, 1 month ago

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
002 {
003 /// <summary>
004 /// This is the main type for your game
005 /// </summary>
006 public class HeliAttack : Microsoft.Xna.Framework.Game
007 {
008 GraphicsDeviceManager graphics;
009 SpriteBatch spriteBatch;
010 GamePadState oldgamepadstate;
011 private AudioLibrary audio;
012
013 Texture2D backgroundTexture, optionsTexture;
014
015
016 //scenes
017 ActionScene actionScene;
018 Texture2D actionBackTexture;
019
020 //crosshair stuff
021 Texture2D crosshairTexture;
022 Rectangle crosshairRect;
023 Vector2 crosshairPosition;
024 float rotation = 0f;
025 Vector2 origin;
026 int index = 0;
027 enum GameState
028 {
029 MainMenu,
030 Options,
031 Help,
032 Playing,
033 }
034 GameState CurrentGameState = GameState.MainMenu;
035
036 //Screen Adjustments
037 int screenWidth = 1600, screenHeight = 900;
038
039 public HeliAttack()
040 {
041 graphics = new GraphicsDeviceManager(this);
042 Content.RootDirectory = "Content";
043 }
044
045 /// <summary>
046 /// Allows the game to perform any initialization it needs to before starting to run.
047 /// This is where it can query for any required services and load any non-graphic
048 /// related content. Calling base.Initialize will enumerate through any components
049 /// and initialize them as well.
050 /// </summary>
051 protected override void Initialize()
052 {
053
054
055
056
057
058 base.Initialize();
059 }
060
061 /// <summary>
062 /// LoadContent will be called once per game and is the place to load
063 /// all of your content.
064 /// </summary>
065 protected override void LoadContent()
066 {
067 graphics.PreferredBackBufferWidth = screenWidth;
068 graphics.PreferredBackBufferHeight = screenHeight;
069 graphics.ApplyChanges();
070
071 //scenes
072
073 actionBackTexture = Content.Load<Texture2D>("images/landscape wallpaper");
074 actionScene = new ActionScene(actionBackTexture);
075
076
077 //audio elements
078 audio = new AudioLibrary();
079 audio.LoadContent(Content);
080 Services.AddService(typeof(AudioLibrary), audio);
081
082 // Create a new SpriteBatch, which can be used to draw textures.
083 spriteBatch = new SpriteBatch(GraphicsDevice);
084 backgroundTexture = Content.Load<Texture2D>("images/Mainmenu");
085 optionsTexture = Content.Load<Texture2D>("images/OptionsMenu");
086 crosshairTexture = Content.Load<Texture2D>("images/target");
087
088 crosshairPosition = new Vector2(600, 530);
089
090 origin = new Vector2(crosshairTexture.Width/2,crosshairTexture.Height/2);
091 // TODO: use this.Content to load your game content here
092 }
093
094 /// <summary>
095 /// UnloadContent will be called once per game and is the place to unload
096 /// all content.
097 /// </summary>
098 protected override void UnloadContent()
099 {
100 // TODO: Unload any non ContentManager content here
101 }
102
103 /// <summary>
104 /// Allows the game to run logic such as updating the world,
105 /// checking for collisions, gathering input, and playing audio.
106 /// </summary>
107 /// <param name="gameTime">Provides a snapshot of timing values.</param>
108 protected override void Update(GameTime gameTime)
109 {
110 crosshairRect = new Rectangle((int)crosshairPosition.X, (int)crosshairPosition.Y, crosshairTexture.Width, crosshairTexture.Height);
111 // Allows the game to exit
112 if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
113 this.Exit();
114
115 switch (CurrentGameState)
116 {
117 case GameState.Playing:
118 actionScene.LoadContent(Content);
119
120 break;
121
122
123 case GameState.MainMenu:
124 rotation += 0.02f;
125 HandleMainMenuInput();
126
127 break;
128 case GameState.Options:
129 if (GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Pressed)
130 {
131 CurrentGameState = GameState.MainMenu;
132 }
133 break;
134
135 }
136
137 base.Update(gameTime);
138 }
139
140 public void HandleMainMenuInput()
141 {
142
143 if (index == 0)
144 crosshairPosition.Y = 530;
145 if (index == 1)
146 crosshairPosition.Y = 647;
147 if (index == 2)
148 crosshairPosition.Y = 760;
149 GamePadState gamepadstate = GamePad.GetState(PlayerIndex.One);
150 if (gamepadstate.DPad.Down == ButtonState.Pressed &&
151 oldgamepadstate.DPad.Down == ButtonState.Released )
152 {
153 index += 1;
154 audio.MenuChange.Play();
155
156 if (index > 2)
157 index = 0;
158 if (index < 0)
159 index = 2;
160 }
161 if (gamepadstate.DPad.Up == ButtonState.Pressed &&
162 oldgamepadstate.DPad.Up == ButtonState.Released)
163 {
164 index -= 1;
165 audio.MenuChange.Play();
166
167 if (index > 2)
168 index = 0;
169 if (index < 0)
170 index = 2;
171 }
172
173 CheckForA();
174 oldgamepadstate = gamepadstate;
175 }
176 public void CheckForA()
177 {
178 if (CurrentGameState == GameState.MainMenu)
179 {
180 if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
181 {
182 if (index == 0)
183 {
184 CurrentGameState = GameState.Playing;
185 audio.Select.Play();
186 }
187 if (index == 1)
188 {
189 CurrentGameState = GameState.Options;
190 audio.Select.Play();
191 }
192 if (index == 2)
193 {
194 audio.Select.Play();
195 this.Exit();
196 }
197 }
198 }
199 }
200 /// <summary>
201 /// This is called when the game should draw itself.
202 /// </summary>
203 /// <param name="gameTime">Provides a snapshot of timing values.</param>
204 protected override void Draw(GameTime gameTime)
205 {
206 GraphicsDevice.Clear(Color.CornflowerBlue);
207
208 spriteBatch.Begin();
209 switch (CurrentGameState)
210 {
211 case GameState.Playing:
212 spriteBatch.Draw(actionBackTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
213 break;
214
215 case GameState.MainMenu:
216 spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
217 spriteBatch.Draw(crosshairTexture,crosshairRect,null,Color.White,rotation,origin,SpriteEffects.None,0);
218 break;
219 case GameState.Options:
220 spriteBatch.Draw(optionsTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
221 break;
222
223 }
224
225 spriteBatch.End();
226
227 base.Draw(gameTime);
228 }
229 }
230

}

Action Scene Code

namespace HeliAttack
02 {
03 class ActionScene
04 {
05 GraphicsDeviceManager graphics;
06 private Texture2D backgroundTexture;
07 private Rectangle backgroundRect;
08
09 Player charater;
10
11 public ActionScene(Texture2D newTexture)
12 {
13 backgroundTexture = newTexture;
14 }
15
16 public void LoadContent(ContentManager Content)
17 {
18
19 charater = new Player(Content.Load<Texture2D>("images/quick character"), new Rectangle(0, 800, 64, 64));
20
21 }
22
23 public void Draw(SpriteBatch spriteBatch)
24 {
25 spriteBatch.Draw(backgroundTexture,backgroundRect,Color.White);
26 }
27 }
28

}

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

ps. this is my first post on here but im hoping to make more posts on here i have been a guestie for a while now :)

Advertisement

Not sure if I'm understanding your question exactly, but I'll try and answer what I think you're asking.

To start, you enum looks fine, and the switches in Update and Draw are how you'd utilise them. In order to get the most out of the structure though what you'll want to do is flesh out your ActionScene object.

It's Draw method should handle drawing all the things to be drawn while in the playing state such that


//this
case GameState.Playing: 
     spriteBatch.Draw(actionBackTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White); 
     break;

//becomes something like this
case GameState.Playing: 
     actionScene.Draw(spriteBatch);
     break;

You'll also want to do more or less the same thing with Update. Right now you have actionScene.LoadContent in the Playing state. This is bad because it's going to call LoadContent every frame, when all it should be doing is calling actionScene.Update(gameTime); With that said, you'll probably want to add an update method to that object to handle any updates that need to happen.

Your load content call could be moved to line 185 to ensure it only happens when needed, although you'll probably want to clean that up once it starts becoming more of a game.

My Games -- My Music 

Come join us for some friendly game dev discussions over in XNA Chat!

ahh you understood correctly :) yes i spent last night playing and got it working like you said , i realised the loadcontent was a bad idea when my character would keep resetting it self when it moved :)

all works now just got the character to shoot at the crosshair which is being controlled by the right axis on the controller so im happy with the progress plus i am going through each part once it works commenting on each section of code so i understand for future reference , i start a games programming course at university in september so looking for that headstart which i hope to get by making a second game in xna before moving onto some C++ and directX as i know that is alot harder to grasp and its needed for my course :)

Switch cases are not the way to handle game states. You should glean some inspiration from this.
General Game/Engine Structure

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

thanks for that link spiro but i will be honest i am a beginner, learning c# for the last 2-3 months from books on my own so looking at that C++ is very intimidating as i cant follow it 100% i understand to use polymorphism instead of the switch cases but as for now i feel i would be jumping ahead and not understanding things if i was to try and implement what you are suggesting i expect once i have more confidence and experience i will learn better ways etc i suppose if i had knowledge of C++ i would understand that tutorial better and would be able to follow what they are saying

Baby steps. We all get there eventually, except those of us who give up.

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

While I agree that a switch statement is not an ideal way to handle game states, if your project is simple enough (read: You only have a few states) and you don't already have a more robust system handy, then a switch/case will work fine. Spending time building (or getting acquainted with a pre-built) GameState Management system is certainly a good investment in the long run, but for a simple learning project where you're trying to get used to the language and high level game dev concepts it can get in the way of the more interesting things like making things move around on the screen. smile.png

My Games -- My Music 

Come join us for some friendly game dev discussions over in XNA Chat!

thanks yes atm the moment i have gone back to create a sprite manager (which i just learnt) so its helping me make sure i understand it proerly , then i will go back to focusing on getting the camera to follow camera plus side scrolling, then finally create a map then i will be happy , i want to then look at perpixelcollision plus getting enemies into the game maybe attacking with swords instead of bullets everywhere plus it helps show the perpixelcollision :) so alot of work to do i hope to try spend a few hours a day on this before university starts in september so i have a decent background in making the 2d games so i can then focu on the 3D games

This topic is closed to new replies.

Advertisement