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 :)