- XNA - Intro :
- XNA Setup :
- The Game Loop :
- State Machines :
- 2D Starter - part 1 :
- 2D Starter - part 2 :
- 2D Starter - part 3 :
- 2D Starter - part 4 :
XNA Development - A 2D Game Starter - part 2
Next we'll step back a bit to make a ship class.
This class will handle the loading and drawing of the ship with it's own Draw method.
We'll also keep track of things like position, fuel, etc. in the class.
At this point, the update method is empty.
Later, this is where things like collision detection and checking fuel levels will go.
This brings us to our first design decision.
Realistic motion or not?
In many games, moving around isn't particularly realistic.
Looking at Pac-Man, Space invaders and Galaxian the player is either moving full speed or not at all.
There isn't a feeling of acceleration; objects move at a set pace.
The code for this type of motion is pretty easy; the ship needs to know it's position and be able to change the position.
We'll define an X and Y position as well as some functions to change the position.
If the player presses left, the players X position decreases. Press down and the Y position increases.
For this our ship class would look something like this:
{
Texture2D shipTexture;
int shipX, shipY;
public Ship(Texture2D tex)
{
shipTexture = tex;
x = 0;
y = 0;
}
public void Move(int x, int y)
{
shipX += x;
shipY += y;
}
public void MoveTo(int x, int y)
{
shipX = x;
shipY = y;
}
public void Update(GameTime gameTime)
{
}
public void Draw(SpriteBatch s)
{
s.Begin();
s.Draw(shipTexture, new Vector2((float)shipX, (float)shipY), Color.White);
s.End();
}
}
To make a ship, we'll load the texture, then pass that to the Ship's constructor.
We move the ship by calling the Move function, or 'warp' it to a given location using the MoveTo function.
Note that there is no bounds checking (did we hit the edge of the screen, etc.), but the space key does reset the position.
Also, nothing happens in the update (we'll add that later) and the draw function takes a SpriteBatch as a parameter.
Below is the new game code.
We'll create a ship object and call it's move function if the player presses the arrow keys.
Then in the Draw function of the game, we'll pass the spritebatch from the main gameto the Draw function of the ship.
Then the ship class can happily draw itself.
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Ship playerShip;
KeyboardState kb;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
Texture2D shipTex = Content.Load
playerShip = new Ship(shipTex);
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
getInput();
base.Update(gameTime);
}
private void getInput()
{
int deltaX, deltaY;
deltaX = 0;
deltaY = 0;
kb = Keyboard.GetState();
if (kb.IsKeyDown(Keys.Up))
deltaY -= 5;
if (kb.IsKeyDown(Keys.Down ))
deltaY += 5;
if (kb.IsKeyDown(Keys.Left))
deltaX -= 5;
if (kb.IsKeyDown(Keys.Right))
deltaX += 5;
playerShip.Move(deltaX, deltaY);
if (kb.IsKeyDown(Keys.Space))
playerShip.MoveTo(10, 10);
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
if (playerShip != null)
playerShip.Draw(spriteBatch);
base.Draw(gameTime);
}
}
The source for this one: here.
