XNA 2D Infinite Scrolling Space Background

I am working on a small 2D space game for windows phone using XNA and just finished making my background space atmosphere. Its all made using one texture and looks pretty decent..

There is no edge or limit, it will keep going forever and always look the same in the same place and also seem different when flying to new areas.

I used parallax scrolling with 7 different layers to get the feel I was looking for. The code can easily be adapted for more or less depth levels.

Here is an example video of the code in action (the video is stuttering a bit but that is the screen capture software and not because of XNA/coding).

Here is the code used in the video, adapt it and change it to your needs.. enjoy!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpaceRunner.GameStates.GameObjects;
using SpaceRunner.Common;
using SpaceRunner.GameStates;
using Microsoft.Xna.Framework;
using SpaceRunner.GameStates.Play;
using Microsoft.Xna.Framework.Graphics;

namespace SpaceRunner.GameObjects.Common
{
    public class StarBackground
    {
        private Texture2D _StarTexture;
        private Texture2D _CloudTexture;

        private List<Star> _Stars;
        private int _Intensity;
        private Random _Random;

        private int MaxX;
        private int MaxY;

        public StarBackground(CommonObjects Common, AbstractGameState State, int Intensity)
        {
            _Intensity = Intensity;
            _Stars = new List<Star>();
            _Random = new Random(DateTime.Now.Millisecond);

            MaxX = _Objects.Game.GraphicsDevice.Viewport.Width ;
            MaxY = _Objects.Game.GraphicsDevice.Viewport.Height ;

            Vector2 PlayPos = ((PlayState)_State)._Player._Position;
            _StarTexture = _Objects.Tools.GetSinglePixleWhiteTexture();
            _CloudTexture = _Objects.Content.Load<Texture2D>("Assets/Textures/FluffyBall");

            for (int i = 0; i <= Intensity; i++)
            {
                int StarColorR = _Random.Next(25, 100);
                int StarColorG = _Random.Next(10, 100);
                int StarColorB = _Random.Next(90, 150);
                int StarColorA = _Random.Next(10, 50);

                float Scale = _Random.Next(100, 900) / 100f;
                int Depth = _Random.Next(4, 7);

                _Stars.Add(new Star(new Vector2(_Random.Next(MaxX / -2 - 500, MaxX / 2 + 500), _Random.Next(MaxY / -2 - 500, MaxY / 2 + 500)), new Color(StarColorR / 3, StarColorG / 3, StarColorB / 3, StarColorA / 3), Scale, Depth, true));
            }

            for (int i = 0; i <= Intensity/2; i++)
            {
                int StarColor = _Random.Next(100, 200);
                int Depth = _Random.Next(2, 6);
                float Scale = _Random.Next(2, 9) / 100f;

                _Stars.Add(new Star(new Vector2(_Random.Next(MaxX / -2 - 200, MaxX / 2 + 200), _Random.Next(MaxY / -2 - 200, MaxY / 2 + 200)), new Color(StarColor, StarColor, StarColor, StarColor), Scale, Depth, false));
            }

        }

        public void Update(GameTime time)
        {
            foreach (Star s in _Stars)
            {
                s.Position += ((PlayState)_State)._Player._DeltaSpeed * -1f / s.Depth;

                if (s.Position.X > MaxX + 501)
                    s.Position.X -= s.Position.X +500;

                if (s.Position.Y > MaxY + 500)
                    s.Position.Y -= s.Position.Y + 500;

                if (s.Position.X < -560)
                    s.Position.X += MaxX +550;

                if (s.Position.Y < -570)
                    s.Position.Y += MaxY +510;
            }
        }

        public void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            spriteBatch.End();
            spriteBatch.Begin();
            foreach(Star s in _Stars)
            {
                    spriteBatch.Draw(_CloudTexture, s.Position, null, s.Color, 0, Vector2.Zero, s.Scale, SpriteEffects.None, 0);
            }
            spriteBatch.End();
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null, _Objects.Camera.View);
        }

    }
    class Star{
        public Vector2 Position;
        public Color Color;
        public float Scale;
        public float Depth;
        public bool isCloud;
        public Star(Vector2 Position, Color Color, float Scale, int Depth, bool isCloud)
        {
            this.Position = Position;
            this.Color = Color;
            this.Scale = Scale;
            this.Depth = Depth;
            this.isCloud = isCloud;
        }
    }
}

 

//The method
GetSinglePixleWhiteTexture();

//Simply returns a single white pixel.
Texture2D SinglePixleWhiteTexture = new Texture2D(_Objects.Graphics, 1, 1);
SinglePixleWhiteTexture.SetData(new Color[] { Color.White });

Here is the texture FluffyBall.png used for the background.

//The video is run with the following parameters
new StarBackground(_Objects, this, 100);

Good luck ;)

  • Laz

    I’m having a hard time making a simple app that only implements this code. I’m kinda lost in the code, so I don’t think I could explain what’s going on :) Here’s the project, if you’d mind to take a look at it:
    http://dl.dropbox.com/u/1951098/WindowsGame3.rar