fixed rotating into blocks
This commit is contained in:
parent
abe0457681
commit
7652d2cb2a
54
Tetris.cs
54
Tetris.cs
|
@ -16,12 +16,12 @@ namespace Tetris {
|
|||
new(255, 0, 255) }; // Purple
|
||||
|
||||
private static readonly int[][][] blocks = {
|
||||
new[] { new[] { 0, 0 }, new[] { 1, 0 }, new[] { 1, 1 }, new[] { 2, 1 } }, // Spiegel-Z
|
||||
new[] { new[] { 0, 0 }, new[] { 1, 0 }, new[] { 1, 1 }, new[] { 2, 1 } }, // Mirrored-Z
|
||||
new[] { new[] { 1, 0 }, new[] { 1, 1 }, new[] { 1, 2 }, new[] { 2, 2 } }, // Normal-L
|
||||
new[] { new[] { 0, 0 }, new[] { 1, 0 }, new[] { 0, 1 }, new[] { 1, 1 } }, // Viereck
|
||||
new[] { new[] { 0, 0 }, new[] { 1, 0 }, new[] { 0, 1 }, new[] { 1, 1 } }, // Square
|
||||
new[] { new[] { 0, 1 }, new[] { 1, 1 }, new[] { 1, 0 }, new[] { 2, 0 } }, // Normal-Z
|
||||
new[] { new[] { 2, 0 }, new[] { 2, 1 }, new[] { 2, 2 }, new[] { 2, 3 } }, // Strich
|
||||
new[] { new[] { 1, 0 }, new[] { 1, 1 }, new[] { 1, 2 }, new[] { 0, 2 } }, // Spiegel-L
|
||||
new[] { new[] { 2, 0 }, new[] { 2, 1 }, new[] { 2, 2 }, new[] { 2, 3 } }, // Line
|
||||
new[] { new[] { 1, 0 }, new[] { 1, 1 }, new[] { 1, 2 }, new[] { 0, 2 } }, // Mirrored-L
|
||||
new[] { new[] { 0, 1 }, new[] { 1, 1 }, new[] { 2, 1 }, new[] { 1, 2 } } }; // T-Block
|
||||
|
||||
// Global Variables
|
||||
|
@ -351,9 +351,7 @@ namespace Tetris {
|
|||
break;
|
||||
case ConsoleKey.A:
|
||||
// Rotate block counter-clockwise
|
||||
calcBounds();
|
||||
rotateCounterClockwise();
|
||||
adjustXPos();
|
||||
break;
|
||||
case ConsoleKey.S:
|
||||
// Swap current block with memory
|
||||
|
@ -361,9 +359,7 @@ namespace Tetris {
|
|||
break;
|
||||
case ConsoleKey.D:
|
||||
// Rotate block clockwise
|
||||
calcBounds();
|
||||
rotateClockwise();
|
||||
adjustXPos();
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
|
@ -384,7 +380,7 @@ namespace Tetris {
|
|||
}
|
||||
|
||||
// Depending on currently used block, save its width and height (they're always the same)
|
||||
public static void calcBounds() {
|
||||
private static void calcBounds() {
|
||||
int blockIndex = 0;
|
||||
for (int i = 1; i < blocks.Length; i++)
|
||||
if (blocks[i].Equals(player.positions)) {
|
||||
|
@ -405,7 +401,7 @@ namespace Tetris {
|
|||
}
|
||||
|
||||
// Adjust x-positions if necessary due to any previous movement
|
||||
public static void adjustXPos() {
|
||||
private static void adjustXPos() {
|
||||
foreach (int[] b in player.positions) {
|
||||
int nx = player.x + b[0];
|
||||
while (nx > 9) {
|
||||
|
@ -448,20 +444,49 @@ namespace Tetris {
|
|||
|
||||
// Rotate current block Clockwise
|
||||
public static void rotateClockwise() {
|
||||
Player memory = prepareRotation();
|
||||
foreach (int[] a in player.positions) {
|
||||
int temp = a[0];
|
||||
a[0] = bounds - 1 - a[1];
|
||||
a[1] = temp;
|
||||
}
|
||||
finishRotation(memory);
|
||||
}
|
||||
|
||||
|
||||
// Rotate current block counter-Clockwise
|
||||
public static void rotateCounterClockwise() {
|
||||
Player memory = prepareRotation();
|
||||
foreach (int[] a in player.positions) {
|
||||
int temp = a[1];
|
||||
a[1] = bounds - 1 - a[0];
|
||||
a[0] = temp;
|
||||
}
|
||||
finishRotation(memory);
|
||||
}
|
||||
|
||||
// Method to remember current positions of chosen block without memory reference & calculate bounds of it
|
||||
private static Player prepareRotation() {
|
||||
int[][] memory = new int[player.positions.Length][];
|
||||
for (int i = 0; i < player.positions.Length; i++) {
|
||||
memory[i] = new int[player.positions[i].Length];
|
||||
for (int j = 0; j < player.positions[i].Length; j++)
|
||||
memory[i][j] = player.positions[i][j];
|
||||
}
|
||||
calcBounds();
|
||||
return new Player(player.x, player.y, memory);
|
||||
}
|
||||
|
||||
// Method to adjust x-Position based on bounds of chosen block and width of Matrix, then reset rotation if is colliding with others
|
||||
private static void finishRotation(Player memory) {
|
||||
adjustXPos();
|
||||
if (collisionCheck(player.x, player.y, player.positions)) {
|
||||
player.x = memory.x;
|
||||
player.y = memory.y;
|
||||
for (int i = 0; i < player.positions.Length; i++)
|
||||
for (int j = 0; j < player.positions[i].Length; j++) {
|
||||
player.positions[i][j] = memory.positions[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Move current block to the right
|
||||
|
@ -565,6 +590,13 @@ namespace Tetris {
|
|||
x = 4 - xB / 2;
|
||||
y = 0;
|
||||
}
|
||||
|
||||
// Constructor to use Player Object as a simple memory
|
||||
public Player(int _x, int _y, int[][] _positions) {
|
||||
x = _x;
|
||||
y = _y;
|
||||
positions = _positions;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user