complete version
This commit is contained in:
parent
3810bf93e3
commit
5200943396
315
Tetris.cs
315
Tetris.cs
|
@ -6,7 +6,7 @@ using rpi_rgb_led_matrix_sharp;
|
|||
namespace Tetris {
|
||||
public static class Tetris {
|
||||
// Static variables
|
||||
private static Color[] colors = {
|
||||
private static readonly Color[] colors = {
|
||||
new(255, 0, 0), // Red
|
||||
new(255, 127, 0), // Orange
|
||||
new(255, 255, 0), // Yellow
|
||||
|
@ -15,21 +15,22 @@ namespace Tetris {
|
|||
new(0, 127, 255), // Blue
|
||||
new(255, 0, 255) }; // Purple
|
||||
|
||||
private static int[][][] blocks = {
|
||||
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[] { 0, 1 }, new[] { 0, 2 }, new[] { 1, 2 } }, // Normal-L
|
||||
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, 1 }, new[] { 1, 1 }, new[] { 1, 0 }, new[] { 2, 0 } }, // Normal-Z
|
||||
new[] { new[] { 0, 0 }, new[] { 0, 1 }, new[] { 0, 2 }, new[] { 0, 3 } }, // Strich
|
||||
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[] { 0, 0 }, new[] { 1, 0 }, new[] { 2, 0 }, new[] { 1, 1 } } }; // T-Block
|
||||
new[] { new[] { 0, 1 }, new[] { 1, 1 }, new[] { 2, 1 }, new[] { 1, 2 } } }; // T-Block
|
||||
|
||||
// Global Variables
|
||||
private static int frameCount = 0, FPS = 60;
|
||||
private static Color[,] board = new Color[10, 21];
|
||||
private static Color black = new(0, 0, 0);
|
||||
private static readonly Color black = new(0, 0, 0);
|
||||
private static bool canSwap = true, gameOver = false;
|
||||
private static Player player;
|
||||
private static bool canRotate = true;
|
||||
private static int score = 0;
|
||||
|
||||
// Matrix variables
|
||||
private static RGBLedMatrix? _matrix = null;
|
||||
|
@ -69,16 +70,22 @@ namespace Tetris {
|
|||
// Initialize stopwatch to measure calculation and display time
|
||||
Stopwatch stopwatch = new();
|
||||
|
||||
RGBLedFont font = new("fonts/5x8.bdf");
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Color col = new(255, 255, 255), grey = new(127, 127, 127);
|
||||
|
||||
bool startOver = true;
|
||||
while (startOver) {
|
||||
Console.Clear();
|
||||
canvas.Clear();
|
||||
canvas = matrix.SwapOnVsync(canvas);
|
||||
|
||||
bool gameOver = false;
|
||||
|
||||
gameOver = false;
|
||||
while (!gameOver) {
|
||||
// Start of Frame
|
||||
canvas.Clear();
|
||||
Console.SetCursorPosition(0, 0);
|
||||
Console.WriteLine($"Score: {score}");
|
||||
|
||||
// Handle Keypresses
|
||||
if (keyHandler()) {
|
||||
|
@ -86,17 +93,17 @@ namespace Tetris {
|
|||
gameOver = true;
|
||||
}
|
||||
|
||||
// Gameplay hahag
|
||||
// Gameplay hahaha
|
||||
for (int i = 0; i < board.GetLength(0); i++)
|
||||
for (int j = 0; j < board.GetLength(1); j++)
|
||||
for (int k = 0; k < 3; k++)
|
||||
for (int l = 0; l < 3; l++) {
|
||||
Color current = board[i, j];
|
||||
if (!current.Equals(black))
|
||||
for (int j = 0; j < board.GetLength(1); j++) {
|
||||
Color current = board[i, j];
|
||||
if (!current.Equals(black))
|
||||
for (int k = 0; k < 3; k++)
|
||||
for (int l = 0; l < 3; l++)
|
||||
canvas.SetPixel(i * 3 + k, j * 3 + l, current);
|
||||
}
|
||||
for (int j = 0; j < player.positions.Length; j++) {
|
||||
int[] current = player.positions[j];
|
||||
}
|
||||
|
||||
foreach (var current in player.positions) {
|
||||
for (int k = 0; k < 3; k++)
|
||||
for (int l = 0; l < 3; l++)
|
||||
canvas.SetPixel((player.x + current[0]) * 3 + k, (player.y + current[1]) * 3 + l, player.color);
|
||||
|
@ -107,8 +114,20 @@ namespace Tetris {
|
|||
player.y--;
|
||||
placeBlock();
|
||||
}
|
||||
|
||||
// Draw line
|
||||
for (int i = 0; i < 64; i++)
|
||||
canvas.SetPixel(30, i, grey);
|
||||
|
||||
// Statically display stored item on the right side
|
||||
canvas.DrawText(font, 38, 8, col, "Save:");
|
||||
if (player.memDis != null && player.memCol != null)
|
||||
foreach (int[] b in player.memDis)
|
||||
for (int k = 0; k < 3; k++)
|
||||
for (int l = 0; l < 3; l++)
|
||||
canvas.SetPixel((14 + b[0]) * 3 + k, (4+ b[1]) * 3 + l, (Color)player.memCol);
|
||||
|
||||
// End of Frame
|
||||
// End of Frame
|
||||
canvas = matrix.SwapOnVsync(canvas);
|
||||
frameCount = (frameCount+1)%(FPS*4);
|
||||
|
||||
|
@ -117,6 +136,32 @@ namespace Tetris {
|
|||
if (elapsed < 1000/FPS)
|
||||
Thread.Sleep(1000/FPS - elapsed);
|
||||
}
|
||||
|
||||
if (startOver) {
|
||||
canvas.Clear();
|
||||
canvas.DrawText(font, 0, 8, col, "Game Over!");
|
||||
canvas.DrawText(font, 0, 24, col, "0) Beenden");
|
||||
canvas.DrawText(font, 0, 32, col, "1) Weiter");
|
||||
canvas = matrix.SwapOnVsync(canvas);
|
||||
|
||||
bool selection = true;
|
||||
while (selection) {
|
||||
// Handle key presses for continue game or exit
|
||||
if (Console.KeyAvailable) {
|
||||
switch (Console.ReadKey(true).Key) {
|
||||
case ConsoleKey.D0:
|
||||
selection = false;
|
||||
startOver = false;
|
||||
break;
|
||||
case ConsoleKey.D1:
|
||||
selection = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,88 +175,161 @@ namespace Tetris {
|
|||
}
|
||||
|
||||
private static void placeBlock() {
|
||||
for (int j = 0; j < player.positions.Length; j++) {
|
||||
int[] current = player.positions[j];
|
||||
// Block placing
|
||||
foreach (var current in player.positions)
|
||||
board[player.x + current[0], player.y + current[1]] = player.color;
|
||||
|
||||
// Count rows lines
|
||||
int rows = 0;
|
||||
for (int j = board.GetLength(1)-1; j >= 0;) {
|
||||
bool isRow = true;
|
||||
for (int i = 0; i < board.GetLength(0); i++)
|
||||
if (board[i, j].Equals(black)) {
|
||||
isRow = false;
|
||||
break;
|
||||
}
|
||||
if (isRow) {
|
||||
rows++;
|
||||
for (int i = j-1; i >= 0; i--)
|
||||
for (int k = 0; k < board.GetLength(0); k++)
|
||||
board[k, i + 1] = board[k, i];
|
||||
continue;
|
||||
}
|
||||
j--;
|
||||
}
|
||||
// Add to score
|
||||
if (rows >= 4) {
|
||||
int tetris = rows / 4;
|
||||
if (tetris > 1)
|
||||
score += 1200 * tetris;
|
||||
else
|
||||
score += 800;
|
||||
} else if (rows == 3)
|
||||
score += 400;
|
||||
else
|
||||
score += 100 * rows;
|
||||
|
||||
|
||||
// Generating new Block
|
||||
int[][]? tempPos = player.memPos;
|
||||
int[][]? tempDis = player.memDis;
|
||||
Color? tempCol = player.memCol;
|
||||
player = new();
|
||||
player.memPos = tempPos;
|
||||
player.memCol = tempCol;
|
||||
canRotate = true;
|
||||
}
|
||||
|
||||
private static void rotateByAngle(int[][] block, float angle) {
|
||||
float mx = 0, my = 0;
|
||||
foreach (int[] b in block) {
|
||||
mx += b[0];
|
||||
my += b[1];
|
||||
}
|
||||
mx /= block.Length;
|
||||
my /= block.Length;
|
||||
foreach (int[] b in block) {
|
||||
float dx = b[0] + (float)Math.Round(mx), dy = b[1] + (float)Math.Round(my);
|
||||
float r = (float)Math.Sqrt(Math.Pow(mx,2)+Math.Pow(dy,2));
|
||||
float a = (float)Math.Atan(dy / dx) + angle;
|
||||
b[0] = (int)Math.Round(r * Math.Cos(a));
|
||||
b[1] = (int)Math.Round(r * Math.Sin(a));
|
||||
if (!collisionCheck(player.x, player.y, player.positions)) {
|
||||
// Carry memory
|
||||
player.memPos = tempPos;
|
||||
player.memDis = tempDis;
|
||||
player.memCol = tempCol;
|
||||
canSwap = true;
|
||||
} else {
|
||||
// Reset Game
|
||||
gameOver = true;
|
||||
for (int i = 0; i < board.GetLength(0); i++)
|
||||
for (int j = 0; j < board.GetLength(1); j++)
|
||||
board[i, j] = black;
|
||||
score = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool keyHandler() {
|
||||
// If key is pressed, handle logic
|
||||
if (Console.KeyAvailable) {
|
||||
switch (Console.ReadKey(true).Key) {
|
||||
case ConsoleKey.Escape:
|
||||
// Exit program.
|
||||
return true;
|
||||
case ConsoleKey.Spacebar:
|
||||
// Place block on lowest point at current x
|
||||
while (!collisionCheck(player.x, ++player.y, player.positions)) { }
|
||||
if (!Console.KeyAvailable) return false;
|
||||
|
||||
int blockIndex = 0;
|
||||
for (int i = 1; i < blocks.Length; i++)
|
||||
if (blocks[i].Equals(player.positions)) {
|
||||
blockIndex = i;
|
||||
break;
|
||||
}
|
||||
int bounds;
|
||||
switch (blockIndex) {
|
||||
default:
|
||||
bounds = 3;
|
||||
break;
|
||||
case 2:
|
||||
bounds = 2;
|
||||
break;
|
||||
case 4:
|
||||
bounds = 4;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (Console.ReadKey(true).Key) {
|
||||
case ConsoleKey.Escape:
|
||||
// Exit program.
|
||||
return true;
|
||||
case ConsoleKey.Spacebar:
|
||||
// Place block on lowest point at current x
|
||||
while (!collisionCheck(player.x, ++player.y, player.positions)) { }
|
||||
player.y--;
|
||||
placeBlock();
|
||||
break;
|
||||
case ConsoleKey.DownArrow:
|
||||
// Lower block by 1 on y-axis
|
||||
if (collisionCheck(player.x, ++player.y, player.positions))
|
||||
player.y--;
|
||||
placeBlock();
|
||||
break;
|
||||
case ConsoleKey.DownArrow:
|
||||
// Lower block by 1 on y-axis
|
||||
if (collisionCheck(player.x, ++player.y, player.positions))
|
||||
player.y--;
|
||||
break;
|
||||
case ConsoleKey.LeftArrow:
|
||||
// Move block to the left on the x axis
|
||||
if (collisionCheck(--player.x, player.y, player.positions))
|
||||
player.x++;
|
||||
break;
|
||||
case ConsoleKey.RightArrow:
|
||||
// Move block to the right on the x axis
|
||||
if (collisionCheck(++player.x, player.y, player.positions))
|
||||
player.x--;
|
||||
break;
|
||||
case ConsoleKey.A:
|
||||
// Rotate block counter-clockwise
|
||||
rotateByAngle(player.positions, (float)-Math.PI / 2);
|
||||
break;
|
||||
case ConsoleKey.S:
|
||||
// Swap current block with memory
|
||||
if (canRotate) {
|
||||
int[][] tempPos = player.positions;
|
||||
int[][]? tempMem = player.memPos;
|
||||
Color tempCol1 = player.color;
|
||||
Color? tempCol2 = player.memCol;
|
||||
player = new();
|
||||
player.memPos = tempPos;
|
||||
player.memCol = tempCol1;
|
||||
if (tempMem != null && tempCol2 != null) {
|
||||
player.positions = tempMem;
|
||||
player.color = (Color)tempCol2;
|
||||
}
|
||||
canRotate = false;
|
||||
break;
|
||||
case ConsoleKey.LeftArrow:
|
||||
// Move block to the left on the x axis
|
||||
if (collisionCheck(--player.x, player.y, player.positions))
|
||||
player.x++;
|
||||
break;
|
||||
case ConsoleKey.RightArrow:
|
||||
// Move block to the right on the x axis
|
||||
if (collisionCheck(++player.x, player.y, player.positions))
|
||||
player.x--;
|
||||
break;
|
||||
case ConsoleKey.A:
|
||||
// Rotate block counter-clockwise
|
||||
// player.positions = rotateByAngle(player.positions, Math.PI / 2);
|
||||
foreach (int[] a in player.positions) {
|
||||
int temp = a[1];
|
||||
a[1] = bounds - 1 - a[0];
|
||||
a[0] = temp;
|
||||
}
|
||||
break;
|
||||
case ConsoleKey.S:
|
||||
// Swap current block with memory
|
||||
if (canSwap) {
|
||||
int[][] tempPos = player.positions;
|
||||
int[][]? tempMem = player.memPos;
|
||||
Color tempCol1 = player.color;
|
||||
Color? tempCol2 = player.memCol;
|
||||
player = new();
|
||||
player.memPos = tempPos;
|
||||
player.memCol = tempCol1;
|
||||
if (tempMem != null && tempCol2 != null) {
|
||||
player.positions = tempMem;
|
||||
player.color = (Color)tempCol2;
|
||||
}
|
||||
break;
|
||||
case ConsoleKey.D:
|
||||
// Rotate block clockwise
|
||||
rotateByAngle(player.positions, (float)Math.PI/2);
|
||||
break;
|
||||
player.memDis = new int[tempPos.Length][];
|
||||
for (int i = 0; i < tempPos.Length; i++) {
|
||||
player.memDis[i] = new int[tempPos[i].Length];
|
||||
for (int j = 0; j < tempPos[i].Length; j++)
|
||||
player.memDis[i][j] = tempPos[i][j];
|
||||
}
|
||||
canSwap = false;
|
||||
}
|
||||
break;
|
||||
case ConsoleKey.D:
|
||||
// Rotate block clockwise
|
||||
// player.positions = rotateByAngle(player.positions, Math.PI/2);
|
||||
foreach (int[] a in player.positions) {
|
||||
int temp = a[0];
|
||||
a[0] = bounds - 1 - a[1];
|
||||
a[1] = temp;
|
||||
}
|
||||
break;
|
||||
}
|
||||
foreach (int[] b in player.positions) {
|
||||
int nx = player.x + b[0];
|
||||
while (nx > 9) {
|
||||
player.x--;
|
||||
nx = player.x + b[0];
|
||||
}
|
||||
while (nx < 0) {
|
||||
player.x++;
|
||||
nx = player.x + b[0];
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
@ -220,15 +338,30 @@ namespace Tetris {
|
|||
private class Player {
|
||||
public int[][] positions;
|
||||
public int[][]? memPos;
|
||||
public int[][]? memDis;
|
||||
public Color? memCol;
|
||||
public int x, y;
|
||||
public Color color;
|
||||
public int x, y;
|
||||
|
||||
public Player() {
|
||||
int randInt = (new Random()).Next(7);
|
||||
positions = blocks[randInt];
|
||||
color = colors[randInt];
|
||||
x = 3;
|
||||
|
||||
int bounds;
|
||||
switch (randInt) {
|
||||
default:
|
||||
bounds = 3;
|
||||
break;
|
||||
case 2:
|
||||
bounds = 2;
|
||||
break;
|
||||
case 4:
|
||||
bounds = 4;
|
||||
break;
|
||||
}
|
||||
|
||||
x = 4 - bounds / 2;
|
||||
y = 0;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user