Collision detection and new shapes

This commit is contained in:
foglar 2024-09-20 13:30:50 +02:00
parent 1f91a12901
commit daae59c580

View File

@ -1,187 +1,214 @@
using System; using System;
// TODO: Create methods to clear and set blocks in the matrix, to replace method SetBlock and Clear in Matrix class
namespace Tetris namespace Tetris
{ {
public class Tetris public class Tetris
{ {
private static int keyDelay = 300; // Delay between keypress handling (in ms) private static int keyDelay = 300; // Delay between keypress handling (in ms)
private static DateTime lastKeyPressTime = DateTime.Now; private static DateTime lastKeyPressTime = DateTime.Now;
public static void Main(string[] args) public static void Main(string[] args)
{ {
Matrix matrix = new Matrix(); Matrix matrix = new Matrix();
Block blocks = new Block(matrix); Shape shape = new Shape(matrix);
blocks.DrawBlock();
while (true) while (true)
{ {
//matrix.Clear();
matrix.Print(); matrix.Print();
shape.Draw();
for (int i = 0; i < 5; i++) HandleInput(shape);
if (!shape.MoveDown())
{ {
HandleInput(blocks); shape = new Shape(matrix); // Create a new shape if current one can't move down
} }
if (!blocks.MoveDown())
{
blocks = new Block(matrix);
blocks.DrawBlock();
}
System.Threading.Thread.Sleep(400); System.Threading.Thread.Sleep(400);
} }
} }
private static void HandleInput(Block blocks)
private static void HandleInput(Shape shape)
{ {
if (Console.KeyAvailable) if (Console.KeyAvailable && (DateTime.Now - lastKeyPressTime).TotalMilliseconds > keyDelay)
{ {
// Read key input without blocking
ConsoleKeyInfo key = Console.ReadKey(true); ConsoleKeyInfo key = Console.ReadKey(true);
// Debounce keypress handling to prevent spamming if (key.Key == ConsoleKey.LeftArrow)
if ((DateTime.Now - lastKeyPressTime).TotalMilliseconds > keyDelay) shape.MoveLeft();
{ else if (key.Key == ConsoleKey.RightArrow)
if (key.Key == ConsoleKey.LeftArrow) shape.MoveRight();
{
blocks.MoveLeft();
}
else if (key.Key == ConsoleKey.RightArrow)
{
blocks.MoveRight();
}
// Update the time of the last key press lastKeyPressTime = DateTime.Now;
lastKeyPressTime = DateTime.Now;
}
} }
} }
} }
//int[,] box_shape = new int[,] { public class Shape
// {0, 0},
// {0, 1},
// {1, 0},
// {1, 1},
//};
//
//public class Shape {
// // Object containing the shape of the block
// // The shape is a 2D array of characters
//
// public Shape(char[,] shape)
// {
// this.shape = shape;
// }
//}
public class Block
{ {
private int x; private int[,] shape;
private int y; private int x, y;
private Matrix matrix; private Matrix matrix;
private static readonly int[][,] shapes = new int[][,] {
public Block(Matrix matrix) new int[,]{
{ {0, 0}, {0, 1}, {0, 2}, {0, 3}, {1, 0}, {1, 1}, {1, 2}, {1, 3},
this.x = 0 % Matrix.HEIGHT; },
this.y = 6 % Matrix.WIDTH; new int[,] {
this.matrix = matrix; {0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, -1}, {0, -2}, {0, 4}, {0, 5},
} },
new int[,] {
public void MoveLeft() {1,-2}, {1,-1}, {1,0}, {1,1}, {1, 2}, {1, 3}, {0, 0}, {0, 1},
{ },
if (y > 0) new int[,] {
{ {1, -2}, {1, -1}, {1, 0}, {1, 1}, {1, 2}, {1, 3}, {0, 2}, {0, 3},
y--;
y--;
matrix.SetBlock(x, y, '█');
matrix.SetBlock(x, y+1, '█');
matrix.SetBlock(x, y+2, ' ');
matrix.SetBlock(x, y+3, ' ');
} }
};
public Shape(Matrix matrix)
{
Random random = new Random();
this.shape = shapes[random.Next(0, shapes.GetLength(0))];
this.matrix = matrix;
// TODO: Implement random color
this.x = 0; // Starting position
this.y = 8; // Center of the grid
} }
public void MoveRight() public void Draw()
{ {
if (y < Matrix.WIDTH - 2) for (int i = 0; i < shape.GetLength(0); i++)
{ {
y++; matrix.SetBlock(x + shape[i, 0], y + shape[i, 1], '█');
y++;
matrix.SetBlock(x, y,'█');
matrix.SetBlock(x, y+1, '█');
matrix.SetBlock(x, y-1, ' ');
matrix.SetBlock(x, y-2, ' ');
} }
} }
public bool MoveDown() public bool MoveDown()
{ {
if (matrix.GetBlock(x+1, y) == '█') if (CanMove(x + 1, y))
{
return false;
}
else if (x < Matrix.HEIGHT - 1)
{ {
Clear();
x++; x++;
matrix.SetBlock(x, y, '█'); Draw();
matrix.SetBlock(x, y + 1, '█');
matrix.SetBlock(x - 1, y, ' ');
matrix.SetBlock(x - 1, y + 1, ' ');
return true; return true;
} }
else return false;
}
public void MoveLeft()
{
if (CanMove(x, y - 1))
{ {
x = 0; Clear();
return false; y--;
y--;
Draw();
} }
} }
public void DrawBlock() public void MoveRight()
{ {
if (x < Matrix.HEIGHT && y < Matrix.WIDTH) if (CanMove(x, y + 1))
{ {
matrix.SetBlock(x, y, '█'); Clear();
matrix.SetBlock(x, y + 1, '█'); y++;
y++;
Draw();
}
}
// TODO: Implement Rotate method
// public void Rotate()
private bool IsPartOfShape(int matrixX, int matrixY)
{
for (int i = 0; i < shape.GetLength(0); i++)
{
if (x + shape[i, 0] == matrixX && y + shape[i, 1] == matrixY)
{
return true;
}
}
return false;
}
private bool CanMove(int newX, int newY)
{
for (int i = 0; i < shape.GetLength(0); i++)
{
int newBlockX = newX + shape[i, 0];
int newBlockY = newY + shape[i, 1];
if (newBlockX >= Matrix.HEIGHT || newBlockX < 0 || newBlockY < 0 || newBlockY >= Matrix.WIDTH)
{
return false;
}
if (matrix.GetBlock(newBlockX, newBlockY) != ' ' && !IsPartOfShape(newBlockX, newBlockY))
{
return false;
}
}
return true;
}
private void Clear()
{
for (int i = 0; i < shape.GetLength(0); i++)
{
matrix.ClearBlock(x + shape[i, 0], y + shape[i, 1]);
} }
} }
} }
public class Matrix public class Matrix
{ {
public const int HEIGHT = 22; public const int HEIGHT = 22;
public const int WIDTH = 20; public const int WIDTH = 20;
private char[,] matrix; private char[,] matrix;
public Matrix() public Matrix()
{ {
matrix = new char[HEIGHT, WIDTH]; matrix = new char[HEIGHT, WIDTH];
Clear();
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
matrix[i, j] = ' ';
}
}
} }
public void SetBlock(int x, int y, char c) public void SetBlock(int x, int y, char c)
{ {
if (x >= 0 && x < HEIGHT && y >= 0 && y < WIDTH) if (IsInBounds(x, y))
{ {
matrix[x, y] = c; matrix[x, y] = c;
} }
} }
public void ClearBlock(int x, int y)
{
if (IsInBounds(x, y))
{
matrix[x, y] = ' ';
}
}
public char GetBlock(int x, int y) public char GetBlock(int x, int y)
{ {
if (x >= 0 && x < HEIGHT && y >= 0 && y < WIDTH) return IsInBounds(x, y) ? matrix[x, y] : 'E';
}
public void Print()
{
Console.Clear();
for (int i = 0; i < HEIGHT; i++)
{ {
return matrix[x, y]; Console.Write("│ ");
for (int j = 0; j < WIDTH; j++)
{
Console.ForegroundColor = matrix[i, j] == ' ' ? ConsoleColor.DarkBlue : ConsoleColor.DarkBlue;
Console.Write(matrix[i, j]);
}
Console.ResetColor();
Console.WriteLine(" │");
} }
return 'E'; Console.WriteLine(" ──────────────────────");
} }
public void Clear() public void Clear()
@ -195,31 +222,9 @@ namespace Tetris
} }
} }
public void Print() private bool IsInBounds(int x, int y)
{ {
Console.Clear(); return x >= 0 && x < HEIGHT && y >= 0 && y < WIDTH;
for (int i = 0; i < HEIGHT; i++)
{
Console.Write("│ ");
for (int j = 0; j < WIDTH; j++)
{
if (matrix[i, j] == ' ')
{
//Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.ForegroundColor = ConsoleColor.DarkBlue;
}
else
{
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.ForegroundColor = ConsoleColor.DarkRed;
}
Console.Write(matrix[i, j]);
Console.ResetColor();
}
Console.WriteLine(" │");
}
Console.WriteLine(" ──────────────────────");
} }
} }
} }