Search code examples
c#mathxnagame-physicstiles

I need to fix this Tile bug in XNA


I am currently creating a space shooter game. To this game I want a background with stars that keeps looping for eternity.

In order to create this, I made a tile based system that simply creates a rectangle for each texture. I have been able to make said textures kinda loop, but when they do I get a really weird bug.

Image link for that bug

As you can see, my tiles are sometimes generating a small gap between each other, and I am not sure as of why.

The problem probably lies within my Background.cs file, but I am unsure of where.

Also, I would like a better name for "Background.cs". Do you think that something like "Tile.cs" is better?


Background.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SpaceShooter
{
    class Background
{
    public Texture2D texture;
    public List<Rectangle> tiles = new List<Rectangle>();
    public Color color;
    public int tileSize;
    public int speed;
    GraphicsDevice graphicsDevice;

    public Background(Texture2D texture, GraphicsDevice graphicsDevice, int tileSize)
    {
        this.texture = texture;
        this.graphicsDevice = graphicsDevice;
        this.tileSize = tileSize;
        speed = 3;
        this.color = Color.White;
        CalculateTiles();
    }

    public void Update()
    {
        for (int i = 0; i < tiles.Count(); i++)
        {
            tiles[i] = new Rectangle(tiles[i].X, tiles[i].Y + speed, tiles[i].Width, tiles[i].Height);
            if (tiles[i].Y >= graphicsDevice.Viewport.Height)
            {
                tiles[i] = new Rectangle(tiles[i].X, 0 - tiles[i].Height, tiles[i].Width, tiles[i].Height);
            }
        }
    }

    public void CalculateTiles()
    {
        if (tiles.Count() > 0)
        {
            for (int i = 0; i < tiles.Count(); i++)
            {
                tiles.Remove(tiles[i]);
            }
        }
        int XT = (int)(graphicsDevice.Viewport.Width / (tileSize / 1.5f));
        int YT = (int)(graphicsDevice.Viewport.Height / (tileSize / 1.5f));
        for (int i = 0; i < XT; i++)
        {
            tiles.Add(new Rectangle(tileSize * i, 0, tileSize, tileSize));

            for (int j = 0; j < YT; j++)
            {
                tiles.Add(new Rectangle(tileSize * i, tileSize * j, tileSize, tileSize));
            }
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int i = 0; i < tiles.Count(); i++)
        {
            spriteBatch.Draw(texture, tiles[i], color);
        }
    }
}
}

Game.cs

public class Game1 : Microsoft.Xna.Framework.Game
{
    Background background;

    protected override void LoadContent()
    {
        // Assign Background
        background = new Background(Content.Load<Texture2D>("Stars"),GraphicsDevice, 128);
    }  

    protected override void Update(GameTime gameTime)
    {
        if(Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape))
            this.Exit();

        background.Update();
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(bgc);
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
        // Call all drawing functions here 
        background.Draw(spriteBatch);
        player1.Draw(spriteBatch);
        player2.Draw(spriteBatch);
        // 
        spriteBatch.End();
        base.Draw(gameTime);
    }

Solution

  • The problem has been solved.
    I am now putting the tile up relative to the screen height instead of raw programming it to 0.

                if (tiles[i].Y >= graphicsDevice.Viewport.Height)
                {
                    tiles[i] = new Rectangle(tiles[i].X, tiles[i].Y - graphicsDevice.Viewport.Height - tiles[i].Height, tiles[i].Width, tiles[i].Height);
                }