Speed Update, New Shapes

This commit is contained in:
foglar 2024-09-21 07:00:53 +02:00
parent 9ccb7eb71d
commit a4e889376e

View File

@ -4,14 +4,16 @@ namespace Tetris
{ {
public class Tetris public class Tetris
{ {
private static int keyDelay = 300; // Delay between keypress handling (in ms) private static int keyDelay = 50; // 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();
Shape shape = new Shape(matrix); Shape shape = new Shape(matrix);
matrix.IncreaseScore(10); int previousScore = 0;
int SleepTime = 400;
while (true) while (true)
{ {
matrix.Print(); matrix.Print();
@ -21,16 +23,25 @@ namespace Tetris
if (!shape.MoveDown()) if (!shape.MoveDown())
{ {
matrix.IncreaseScore(10);
shape = new Shape(matrix); // Create a new shape if current one can't move down shape = new Shape(matrix); // Create a new shape if current one can't move down
matrix.CheckForCompleteLines(); matrix.CheckForCompleteLines();
// Check for game over if (matrix.IsGameOver() == true)
} {
Console.WriteLine("Game Over!");
break;
}
}
System.Threading.Thread.Sleep(400); int score = matrix.GetScore();
if (score >= previousScore + 800)
{
Speed.SpeedUp();
previousScore = matrix.GetScore();
}
System.Threading.Thread.Sleep(Speed.ShowSpeed());
} }
} }
private static void HandleInput(Shape shape) private static void HandleInput(Shape shape)
{ {
if (Console.KeyAvailable && (DateTime.Now - lastKeyPressTime).TotalMilliseconds > keyDelay) if (Console.KeyAvailable && (DateTime.Now - lastKeyPressTime).TotalMilliseconds > keyDelay)
@ -47,6 +58,22 @@ namespace Tetris
} }
} }
public class Speed()
{
private static int speed = 400;
public static int ShowSpeed()
{
return speed;
}
public static void SpeedUp()
{
speed -= 50;
}
}
public class Shape public class Shape
{ {
private int[,] shape; private int[,] shape;
@ -65,7 +92,13 @@ namespace Tetris
}, },
new int[,] { new int[,] {
{1, -2}, {1, -1}, {1, 0}, {1, 1}, {1, 2}, {1, 3}, {0, 2}, {0, 3}, {1, -2}, {1, -1}, {1, 0}, {1, 1}, {1, 2}, {1, 3}, {0, 2}, {0, 3},
} },
new int[,] {
{1, -2}, {1, -1}, {1, 0}, {1, 1}, {1, 2}, {1, 3}, {0, -2}, {0, -1},
},
new int[,] {
{1, -2}, {1,-1}, {1,0}, {1,1}, {0,0},{0,1}, {0,2}, {0,3}
},
}; };
public Shape(Matrix matrix) public Shape(Matrix matrix)
@ -73,6 +106,7 @@ namespace Tetris
Random random = new Random(); Random random = new Random();
this.shape = shapes[random.Next(0, shapes.GetLength(0))]; this.shape = shapes[random.Next(0, shapes.GetLength(0))];
this.matrix = matrix; this.matrix = matrix;
matrix.IncreaseScore(10);
// TODO: Implement random color // TODO: Implement random color
this.x = 0; // Starting position this.x = 0; // Starting position
this.y = 8; // Center of the grid this.y = 8; // Center of the grid
@ -217,12 +251,17 @@ namespace Tetris
Console.WriteLine("Score: " + score); Console.WriteLine("Score: " + score);
} }
public int GetScore()
{
return score;
}
public void IncreaseScore(int score) public void IncreaseScore(int score)
{ {
this.score += score; this.score += score;
} }
public void CheckForCompleteLines() { public void CheckForCompleteLines()
{
for (int i = 0; i < HEIGHT; i++) for (int i = 0; i < HEIGHT; i++)
{ {
bool isComplete = true; bool isComplete = true;
@ -254,6 +293,18 @@ namespace Tetris
} }
} }
public bool IsGameOver()
{
for (int i = 0; i < WIDTH; i++)
{
if (matrix[0, i] != ' ')
{
return true;
}
}
return false;
}
public void Clear() public void Clear()
{ {
for (int i = 0; i < HEIGHT; i++) for (int i = 0; i < HEIGHT; i++)