DEFCOM1  
 

XNA Level 1 Lesson 4

Moving Sprites

Ok, Now we need to move our sprite.

So What if we press the left arrow key? What if we press up?

The key word is 'if'

if(something is true){do something}

In the Update function add the code to make the sprite respond to keys

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

    KeyboardState key = Keyboard.GetState();

    if (key.IsKeyDown(Keys.Left) )
    {       
        ufoPos.X -= 5;
    }
    if (key.IsKeyDown(Keys.Right))
    {
        ufoPos.X += 5;
    }
    if (key.IsKeyDown(Keys.Up))
    {
        ufoPos.Y -= 5;
    }
    if (key.IsKeyDown(Keys.Down))
    {
        ufoPos.Y += 5;
    }



    base.Update(gameTime);
}

 

 

At the moment the sprite can move out of the bounds of the screen, the following code will keep it in bounds

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

    KeyboardState key = Keyboard.GetState();

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



    base.Update(gameTime);
}

 

At the moment the screen is the default size (whatever size that is?) I want us to fix the screen reso;

In the Initialise Function add the following code

 

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

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

    graphics.IsFullScreen = false;
    graphics.ApplyChanges();
            
    base.Initialize();
}

 

 

Complete Listings

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

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

    // Set the coordinates to draw the sprite at.
    Vector2 ufoPos = 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");

    }


    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

        KeyboardState key = Keyboard.GetState();

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



        base.Update(gameTime);
    }

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

        // TODO: Add your drawing code here

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

        base.Draw(gameTime);
    }
}