How To: Make a Scrolling Background

This article demonstrates how to draw a scrolling background sprite using the SpriteBatch class.

The Complete Sample

The code in this tutorial illustrates the technique described in the text. A complete code sample for this tutorial is available for you to download, including full source code and any additional supporting files required by the sample.

To draw a scrolling background sprite

  1. Create the game class. Load resources as described in steps 1–3 of How To: Draw a Sprite.
  2. Load the background texture.

    private ScrollingBackground myBackground;
    protected override void LoadContent()
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
        myBackground = new ScrollingBackground();
        Texture2D background = Content.Load<Texture2D>("starfield");
        myBackground.Load(GraphicsDevice, background);
  3. Determine the size of the background texture and the size of the screen. The texture size is determined using the Height and Width properties, and the screen size is determined using the Viewport property on the graphics device. Using the texture and screen information, set the origin of the texture to the center of the top edge of the texture, and the initial screen position to the center of the screen.

    // class ScrollingBackground
    private Vector2 screenpos, origin, texturesize;
    private Texture2D mytexture;
    private int screenheight;
    public void Load( GraphicsDevice device, Texture2D backgroundTexture )
        mytexture = backgroundTexture;
        screenheight = device.Viewport.Height;
        int screenwidth = device.Viewport.Width;
        // Set the origin so that we're drawing from the 
        // center of the top edge.
        origin = new Vector2( mytexture.Width / 2, 0 );
        // Set the screen position to the center of the screen.
        screenpos = new Vector2( screenwidth / 2, screenheight / 2 );
        // Offset to draw the second texture, when necessary.
        texturesize = new Vector2( 0, mytexture.Height );
  4. To scroll the background, change the screen position of the background texture in your Update method. This example moves the background down 100 pixels per second by increasing the screen position's Y value.

    protected override void Update(GameTime gameTime)
        // The time since Update was called last.
        float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
        // TODO: Add your game logic here.
        myBackground.Update(elapsed * 100);

    The Y value is kept no larger than the texture height, making the background scroll from the bottom of the screen back to the top.

    public void Update( float deltaY )
        screenpos.Y += deltaY;
        screenpos.Y = screenpos.Y % mytexture.Height;
    // ScrollingBackground.Draw
  5. Draw the background using the origin and screen position calculated in LoadContent and Update.

    protected override void Draw(GameTime gameTime)

    In case the texture doesn't cover the screen, another texture is drawn, subtracting the texture height from the screen position using the texturesize vector created at load time. This creates the illusion of a loop.

    public void Draw( SpriteBatch batch )
        // Draw the texture, if it is still onscreen.
        if (screenpos.Y < screenheight)
            batch.Draw( mytexture, screenpos, null,
                 Color.White, 0, origin, 1, SpriteEffects.None, 0f );
        // Draw the texture a second time, behind the first,
        // to create the scrolling illusion.
        batch.Draw( mytexture, screenpos - texturesize, null,
             Color.White, 0, origin, 1, SpriteEffects.None, 0f );

Community Additions