hey hey folks. I am making a tetris clone for my class project in college. langauge is XNA and I have run into a minor issue. When my bricks get to the bottom, rather than stack, they simply reset back up to the top. I have tried everything I can think of...to no avail. I have been working on this bad boy since about 2pm, its now 1:46 at the time of posting. I THINK the issue is somwhere inside my collision detection code way down at the bottom, but im not sure. any halp would surely be appreciated.
variables and declerations:
`GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D backgroundTexture;
Texture2D storage;
Texture2D block;
Color[] blockColor;
Random blockColorGen;
SpriteFont MessageFont;
int[,] bottomRow;
int _total_frames = 0;
float _elapsed_time = 0.0f;
int _fps = 0;
int fallSpeed = 5;
int maxSpeed = 5;
int color;
List<Vector2> fallingBrick;
int[,] rows;
initialization method:
protected override void Initialize()
{
// TODO: Add your initialization logic here
graphics.PreferredBackBufferHeight = 620;
graphics.PreferredBackBufferWidth = 600;
graphics.ApplyChanges();
rows = new int[700, 700];
blockColorGen = new Random();
bottomRow = new int[700,700];
blockColor = new Color[] { Color.Red, Color.Blue, Color.Orange, Color.Purple, Color.Green, Color.Yellow };
for (int i = 0; i < bottomRow.GetLength(0); i++)
{
for (int j = 0; j < bottomRow.GetLength(1); j++)
{
bottomRow[i, j] = -1;
blockColorGen.Next(0, 6);
}
}
Window.Title = "Adavidson_tetris";
blockReset();
base.Initialize();
`protected override void Update(GameTime gameTime)
{
collisionDetection();
_elapsed_time += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
// 1 Second has passed
if (_elapsed_time >= 1000.0f)
{
_fps = _total_frames;
_total_frames = 0;
_elapsed_time = 0;
}
base.Update(gameTime);
}
draw method:
` protected override void Draw(GameTime gameTime)
{
int currentColor = color;
_total_frames++;
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(storage, new Vector2(0f, 0f), Color.White);
for (int i = 0; i < bottomRow.GetLength(0); i++)
{
for (int j = 0; j < bottomRow.GetLength(1); j++)
{
currentColor = bottomRow[i, j];
if (currentColor != -1)
{
spriteBatch.Draw(block, new Rectangle(i * 32, j * 32, 32, 32), blockColor[color]);
}
}
}
for (int i = 0; i < fallingBrick.Count; i++)
{
int x = (int)fallingBrick[i].X;
int y = (int)fallingBrick[i].Y;
spriteBatch.Draw(block, new Rectangle(x, y, 32, 32), blockColor[color]);
}
spriteBatch.DrawString(MessageFont, string.Format("FPS={0}", _fps),
new Vector2(10.0f, 20.0f), Color.Black);
spriteBatch.End();
base.Draw(gameTime);
}`
collision detection:
private void collisionDetection()
{
bool spaceEmpty = true;
KeyboardState currentKey = Keyboard.GetState();
if (currentKey.IsKeyDown(Keys.Left))
{
for (int i = 0; i < fallingBrick.Count; i++)
{
var brick = fallingBrick[i];
if (brick.X < 135)
{
var brickLocation = brick.X;
}
else
{
brick.X--;
fallingBrick[i] = brick;
}
}
}
if (currentKey.IsKeyDown(Keys.Right))
{
for (int i = 0; i < fallingBrick.Count; i++)
{
var brick = fallingBrick[i];
if (brick.X > 433)
{
var brickLocation = brick.X;
}
else
{
brick.X++;
fallingBrick[i] = brick;
}
}
}
if (currentKey.IsKeyDown(Keys.Down))
{
for (int i = 0; i < fallingBrick.Count; i++)
{
var brick = fallingBrick[i];
brick.Y++;
fallingBrick[i] = brick;
}
}
if (fallSpeed <= 0)
{
fallSpeed = maxSpeed;
for (int i = 0; i < fallingBrick.Count; i++)
{
var brick = fallingBrick[i];
brick.Y++;
int x = (int)brick.X;
int y = (int)brick.Y;
spaceEmpty &= brick.Y <= 585 && bottomRow[x, y] == -1;
fallingBrick[i] = brick;
}
if (spaceEmpty)
{
for (int i = 0; i < fallingBrick.Count; i++)
{
var brick = fallingBrick[i];
brick.Y++;
int x = (int)brick.X;
int y = (int)brick.Y;
spaceEmpty &= brick.Y <= 585 && bottomRow[x, y] == -1;
fallingBrick[i] = brick;
}
}
else
{
while (fallingBrick.Count > 0)
{
var brick = fallingBrick;
int x = (int)fallingBrick[0].X;
int y = (int)fallingBrick[0].Y;
bottomRow[x,y] = color;
fallingBrick.RemoveAt(0);
Console.WriteLine(bottomRow[x,y]);
}
blockReset();
}
}
else
{
fallSpeed--;
}
}
private void blockReset()
{
fallingBrick = new List<Vector2>();
fallingBrick.Add(new Vector2(275, 0));
color = blockColorGen.Next(0,6);
}
any ideas?