I'm creating a "simple" Tetris style game for a beginner project. My problem is when I added the background (tiled look consisting of a bunch of squares), the game got very slow. It has to have something to do with the way I am drawing the background but I am not sure of any way to improve it. I thought about using an image as the background, but I really want to be able to have the gameboard size customizable. Here is my code:
GameBoard
public class GameBoard
{
private List<Square> Squares;
public GameBoard()
{
Squares = new List<Square>();
BuildBoard();
}
private void BuildBoard()
{
for (int x = 0; x < 40; x++)
{
for (int y = 0; y < 24; y++)
{
Squares.Add(new Square(new Point(x, y), new Color(50, 50, 0)));
}
}
}
public void Draw(SpriteBatch batch)
{
foreach (Square square in Squares)
square.Draw(batch);
}
}
Square
public class Square
{
private const int HEIGHT = 20;
private const int WIDTH = 20;
public Point Position;
public Rectangle Rectangle;
public Color Color;
public Square(Point Location, Color color)
{
Position = new Point(Location.X * 20, Location.Y * 20);
Color = color;
Rectangle = new Rectangle(Position.X, Position.Y, WIDTH, HEIGHT);
}
public void Move(int xMove, int yMove)
{
Position.X += xMove;
Position.Y += yMove;
Rectangle = new Rectangle(Position.X, Position.Y, WIDTH, HEIGHT);
}
public void MoveRight()
{
Position.X += 20;
Rectangle = new Rectangle(Position.X, Position.Y, WIDTH, HEIGHT);
}
public void MoveLeft()
{
Position.X -= 20;
Rectangle = new Rectangle(Position.X, Position.Y, WIDTH, HEIGHT);
}
public void MoveDown()
{
Position.Y += 20;
Rectangle = new Rectangle(Position.X, Position.Y, WIDTH, HEIGHT);
}
public void Draw(SpriteBatch batch)
{
Texture2D squareTexture = new Texture2D(batch.GraphicsDevice, 1, 1);
Texture2D borderTexture = new Texture2D(batch.GraphicsDevice, 1, 1);
squareTexture.SetData(new[] { Color });
borderTexture.SetData(new[] { Color.Black });
batch.Draw(squareTexture, Rectangle, Color);
batch.Draw(borderTexture, new Rectangle(Position.X, Position.Y, 2, HEIGHT), Color);
batch.Draw(borderTexture, new Rectangle(Position.X, Position.Y, WIDTH, 2), Color);
batch.Draw(borderTexture, new Rectangle(Position.X + 18, Position.Y, 2, HEIGHT), Color);
batch.Draw(borderTexture, new Rectangle(Position.X, Position.Y + 18, WIDTH, 2), Color);
}
}
Game1.cs
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
GameBoard board;
Shape shape;
int downTicker;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
this.TargetElapsedTime = TimeSpan.FromMilliseconds(50);
downTicker = 0;
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
shape = new Shape(Color.Blue);
board = new GameBoard();
}
protected override void UnloadContent()
{
spriteBatch.Dispose();
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
downTicker++;
Debug.WriteLine(TargetElapsedTime.Milliseconds);
if (downTicker == 10)
{
//shape.MoveDown();
downTicker = 0;
}
if (Keyboard.GetState().IsKeyDown(Keys.Right))
shape.MoveRight();
if (Keyboard.GetState().IsKeyDown(Keys.Left))
shape.MoveLeft();
if (Keyboard.GetState().IsKeyDown(Keys.Down))
shape.MoveDown();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
board.Draw(spriteBatch);
shape.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}