
Alright, so, what we're going to do in this tutorial is we're going to use the arrow keys to move a spaceship around the screen, this will be the first official part of an asteroidish game tutorial.
We will create a spaceship class with a constructor, an update class, and a draw class which will be called on the main Update and Draw functions.
So first as always gather your art assets. All you need is a top down spaceship.
So if you've been following my other tutorials we of course need to start by creating some variables. Not much though.
SpriteBatch batch; //Create the SpriteBatch required to draw in 2D
SpaceShip mainShip;
//Create an instance of our SpaceShip class that will be written soon.
In the Intialize function we want to intialize the spritebatch, the instance, and change the window title.
batch = new SpriteBatch(graphics.GraphicsDevice); //Like before, initalize
mainShip = new SpaceShip(new Vector2(100, 100), //Where do we want to start?content.Load
//SpaceShip is the art asset name of the ship we want to draw.
Window.Title = "Asteroids! Written by (Your Name Here)!"; //Our window title.
Now on the Update Function put this line:
mainShip.Update(graphics.PreferredBackBufferHeight,graphics.PreferredBackBufferWidth);
//We call the mainShip's Update function and input the dimensions of the screen for
//the world wrap function that's in the update function.
In the Draw function put this:
batch.Begin(); //Between Begin() and End() we can draw.
mainShip.Draw(batch); //Call the mainShip's function with the spriteBatch parameter.
batch.End();
Now for the main part of the program. The SpaceShip class.
Depdendencies:
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using namespace PutNameSpaceHere
{
class SpaceShip
{
private Vector2 pos; //Our position in 2D.
private Texture2D tex; //Our texture
private float speed = 8; //The speed to move.
private float angleOfRotation = 0;
//We will just face whatever direction the art is already facing.
The constructor:
public SpaceShip(Vector2 pos, Texture2D tex) //Parameters needed.
{
this.pos = pos; //Our starting position
this.tex = tex; //Our texture
}
The WorldWrap function:
private void WorldWrap(int worldWidth, int worldHeight) //Take window dimensions
{
if (this.pos.X < 0)
//If we've past the left side of the window
{
//Set on other side of the world.
this.pos.X = worldWidth + this.tex.Width;
}
if (this.pos.X > worldWidth)
//If we've passed the right side of the window
{
//Set on other side of the world.
this.pos.X = this.tex.Width;
}
if (this.pos.Y < 0)
//If we've passed the top side of the window
{
//Set on other side of the world.
this.pos.Y = worldHeight - this.tex.Height;
}
if (this.pos.Y > worldHeight)
//If we've passed the bottom side of the window
{
//Set on other side of the world.
this.pos.Y = this.tex.Height;
}
}
The Update Function:
public void Update(int WorldWidth, int WorldHeight) //Get world dimensions
{
KeyboardState keys = Keyboard.GetState(); //Current state of keyboard
if (keys.IsKeyDown(Keys.A))
{
//We want to turn the ship LEFT
this.angleOfRotation -= 2f; //Rotate it counter-clockwise
}
if (keys.IsKeyDown(Keys.D))
{
//We want to turn the ship RIGHT
this.angleOfRotation += 2f; //Rotate it clockwise
}
if (keys.IsKeyDown(Keys.W))
{
//We're pressing the up key (W) so go Forwards!
this.pos.X += (float)(Math.Cos(this.angleOfRotation * Math.PI / 180) * this.speed);
//Math.Cos(directionInRadians)*SpeedToMoveAt, to move at a certain angle
this.pos.Y += (float)(Math.Sin(this.angleOfRotation * Math.PI / 180) * this.speed);
}
if (keys.IsKeyDown(Keys.S))
{
//We're pressing the down key (S) so go Backwards!
this.pos.X -= (float)(Math.Cos(this.angleOfRotation * Math.PI / 180) * this.speed);
//-= instead of += (float)(...) because we want to go backwards not forwards
this.pos.Y -= (float)(Math.Sin(this.angleOfRotation * Math.PI / 180) * this.speed);
}
//Call the WorldWrap function to check if we need to move the ship.
WorldWrap(WorldWidth, WorldHeight);
}
Draw Function:
public void Draw(SpriteBatch batch)
{
batch.Draw(this.tex, //Texture to use.
this.pos, //Position to draw at
new Rectangle(0, 0, this.tex.Width, this.tex.Height),
//Rectangle to draw. We want the whole thing so (0,0) is top left and (width,height)
//the bottom left corner
Color.White, //No color tint.
(float)((this.angleOfRotation +90)* Math.PI / 180), //Our angle +90 (we face up but the angle says we face right so increase our angle that we draw at by 90).
new Vector2(this.tex.Width / 2, this.tex.Height / 2),
1.0f, //Scale, doesn't really matter right now. 1 = 100% to texture's original scale.
SpriteEffects.None, //No special effects.
1.0f); //Depth, doesn't really matter right now.
//batch.Draw(
} //End Draw function
} //End class
} //End namespace
Done!!! Alright, great. Not that hard huh? We should now be able to move the spaceship around the screen with WASD, W and S to move forwards and backwards and A and D to turn left and right. We can also like in asteroids *teleport* from side to side.