Rotation check
This commit is contained in:
parent
e6ec2633e1
commit
120c3a90e1
44
Program.cs
44
Program.cs
@ -66,8 +66,7 @@ namespace Tetris
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Speed
|
public class Speed()
|
||||||
()
|
|
||||||
{
|
{
|
||||||
private static int speed = 400;
|
private static int speed = 400;
|
||||||
|
|
||||||
@ -87,6 +86,7 @@ namespace Tetris
|
|||||||
|
|
||||||
private int x, y;
|
private int x, y;
|
||||||
private Matrix matrix;
|
private Matrix matrix;
|
||||||
|
|
||||||
public Shape(Matrix matrix)
|
public Shape(Matrix matrix)
|
||||||
{
|
{
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
@ -96,7 +96,6 @@ namespace Tetris
|
|||||||
current_shape = shape[randomRotation];
|
current_shape = shape[randomRotation];
|
||||||
this.matrix = matrix;
|
this.matrix = matrix;
|
||||||
matrix.IncreaseScore(10);
|
matrix.IncreaseScore(10);
|
||||||
// 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
|
||||||
}
|
}
|
||||||
@ -144,20 +143,40 @@ namespace Tetris
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void Rotate()
|
public void Rotate()
|
||||||
|
{
|
||||||
|
int nextRotation = (randomRotation + 1) % shape.Count;
|
||||||
|
int[,] nextShape = shape[nextRotation];
|
||||||
|
|
||||||
|
if (CanRotate(nextShape))
|
||||||
{
|
{
|
||||||
Clear();
|
Clear();
|
||||||
randomRotation = (randomRotation + 1) % shape.Count;
|
current_shape = nextShape;
|
||||||
// !IMPORTANT: Check if rotation is possible
|
randomRotation = nextRotation;
|
||||||
current_shape = shape[randomRotation];
|
|
||||||
Draw();
|
Draw();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool CanRotate(int[,] newShape)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < newShape.GetLength(0); i++)
|
||||||
|
{
|
||||||
|
int newX = x + newShape[i, 0];
|
||||||
|
int newY = y + newShape[i, 1];
|
||||||
|
|
||||||
|
if (newX >= Matrix.HEIGHT || newX < 0 || newY < 0 || newY >= Matrix.WIDTH ||
|
||||||
|
(matrix.GetBlock(newX, newY) != ' ' && !IsPartOfShape(newX, newY)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
private bool IsPartOfShape(int matrixX, int matrixY)
|
private bool IsPartOfShape(int matrixX, int matrixY)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < current_shape.GetLength(0); i++)
|
for (int i = 0; i < current_shape.GetLength(0); i++)
|
||||||
{
|
{
|
||||||
if (x + current_shape[i, 0] == matrixX &&
|
if (x + current_shape[i, 0] == matrixX && y + current_shape[i, 1] == matrixY)
|
||||||
y + current_shape[i, 1] == matrixY)
|
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -173,12 +192,8 @@ namespace Tetris
|
|||||||
int newBlockY = newY + current_shape[i, 1];
|
int newBlockY = newY + current_shape[i, 1];
|
||||||
|
|
||||||
if (newBlockX >= Matrix.HEIGHT || newBlockX < 0 || newBlockY < 0 ||
|
if (newBlockX >= Matrix.HEIGHT || newBlockX < 0 || newBlockY < 0 ||
|
||||||
newBlockY >= Matrix.WIDTH)
|
newBlockY >= Matrix.WIDTH ||
|
||||||
{
|
(matrix.GetBlock(newBlockX, newBlockY) != ' ' && !IsPartOfShape(newBlockX, newBlockY)))
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (matrix.GetBlock(newBlockX, newBlockY) != ' ' &&
|
|
||||||
!IsPartOfShape(newBlockX, newBlockY))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -195,6 +210,7 @@ namespace Tetris
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public class Matrix
|
public class Matrix
|
||||||
{
|
{
|
||||||
public const int HEIGHT = 22;
|
public const int HEIGHT = 22;
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
```bash
|
```bash
|
||||||
git clone https://github.com/foglar/Tetris.git
|
git clone https://github.com/foglar/Tetris.git
|
||||||
cd Tetris
|
cd Tetris
|
||||||
dotnet run ./
|
dotnet run
|
||||||
```
|
```
|
||||||
|
|
||||||
> [!NOTE]
|
> [!NOTE]
|
||||||
@ -17,4 +17,4 @@ dotnet run ./
|
|||||||
## Simple tetris game
|
## Simple tetris game
|
||||||
|
|
||||||
- [SRS](./TetrominoSRS.md)
|
- [SRS](./TetrominoSRS.md)
|
||||||
- [FS](./TetrominoFS.md) - Not ready yet
|
- [FS](./TetrominoFS.md)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user