DEFCOM1  
 

XNA Level 1 Lesson 5

Computer Controlled Sprites

  1. Add a new sprite
  2. Create new Texture and Position
  3. Load new texture
  4. Draw Sprite
  5. Update cube
  6. Random Number
 

1. Add a new sprite

Save the asteroid sprte above into your project and the add the asteroid to your project.

If you have forgotten how see lesson 3

2. Create new Texture and Position

In the Global Variabls where you created your textures and Vectors for the ufo add the code for asteroidTex and asteroidPos

// This is a texture we can render.
Texture2D ufoTex;
Texture2D asteroidTex;

// Set the coordinates to draw the sprite at.
Vector2 ufoPos = Vector2.Zero;
Vector2 asteroidPos = Vector2.Zero;

 

3. Load Texture

In the void LoadContent function add the code to load the asteroid texture after the penguinTex

asteroidTex = Content.Load<Texture2D>("asteroid");
 

 

4. Draw Sprite

We can edit the void Draw function to draw our asteroid

add the spriteBatch.Draw(asteroidTex... after the ufoTex

spriteBatch.Begin();
spriteBatch.Draw(ufoTex, ufoPos, Color.White);
spriteBatch.Draw(asteroidTex, asteroidPos, Color.White);      //NEW LINE    
spriteBatch.End();
 

 

Pressing F5 should display your cube

 

5. Update asteroid

We now need to add code to the Update function.

To make the asteroid move we just add 5 to it's current Y position each frame

Add this line just before the if statements for moving your ufo

//update asteroid
asteroidPos.Y += 5

 

Pressing F5 should show your asteroid falling off the bottom of the screen never to be seen again :(

We are now going to instruct our asteroid to reset to above the screen and randomly pick a new X position

Add the code directly after your last line.

if (asteroidPos.Y > 800)
{
    Random rand = new Random();
    asteroidPos.X = rand.Next(800 - asteroidTex.Width);
    asteroidPos.Y = -asteroidTex.Height;
}

 

rand.Next(800 - cubeTex.Width) generates a number between 0 and 800 take away the width of the cube

 

The Complete Listing should now look something like this.

 

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    // This is a texture we can render.
    Texture2D ufoTex;
    Texture2D asteroidTex;

    // Set the coordinates to draw the sprite at.
    Vector2 ufoPos = Vector2.Zero;
    Vector2 asteroidPos = Vector2.Zero;


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


    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        graphics.PreferredBackBufferWidth = 800;
        graphics.PreferredBackBufferHeight = 600;

        graphics.IsFullScreen = false;
        graphics.ApplyChanges();

        base.Initialize();
    }

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);


        //load our graphic
        ufoTex = Content.Load<Texture2D>("ufo");
        asteroidTex = Content.Load<Texture2D>("asteroid");

    }


    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

        //update asteroid
        asteroidPos.Y += 5;

        if (asteroidPos.Y > 800)
        {
            Random rand = new Random();
            asteroidPos.X = rand.Next(800 - asteroidTex.Width);
            asteroidPos.Y = -asteroidTex.Height;
        }


        //update ufo
        KeyboardState key = Keyboard.GetState();

        if (key.IsKeyDown(Keys.Left) && ufoPos.X > 0)
        {
            ufoPos.X -= 5;
        }
        if (key.IsKeyDown(Keys.Right) && ufoPos.X < 800- ufoTex.Width)
        {
            ufoPos.X += 5;
        }
        if (key.IsKeyDown(Keys.Up) && ufoPos.Y > 0)
        {
            ufoPos.Y -= 5;
        }
        if (key.IsKeyDown(Keys.Down) && ufoPos.Y < 600 - ufoTex.Height)
        {
            ufoPos.Y += 5;
        }



        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here

        spriteBatch.Begin();
        spriteBatch.Draw(ufoTex, ufoPos, Color.White);
        spriteBatch.Draw(asteroidTex, asteroidPos, Color.White);          
        spriteBatch.End();

        base.Draw(gameTime);
    }
}