Compare commits

...

6 Commits

Author SHA1 Message Date
7baabd7cc3 tetris ghost piece 2023-04-14 13:43:33 +02:00
1b8c0a32a5 clean up 2023-04-14 11:13:39 +02:00
d6e3b48700 clean up and dynamically detecting multiple controllers 2023-03-28 11:06:25 +02:00
c603e9932e ignore jetbrains .idea 2022-12-11 15:10:31 +01:00
df1ae2ea4b debug fixes 2022-12-07 08:19:50 +01:00
34ff7be91a clean up and expansion 2022-11-30 08:36:59 +01:00
2 changed files with 72 additions and 58 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
bin/*
obj/*
obj/*
.idea/*

127
Tetris.cs
View File

@ -7,13 +7,13 @@ namespace Tetris {
public static class Tetris {
// Static variables
private static readonly Color[] colors = {
new(255, 0, 0), // Red
new(255, 127, 0), // Orange
new(255, 255, 0), // Yellow
new(0, 255, 0), // Green
new(0, 255, 255), // Cyan
new(0, 127, 255), // Blue
new(255, 0, 255) }; // Purple
new(255, 0, 0), // Red
new(255, 127, 0), // Orange
new(255, 255, 0), // Yellow
new( 0, 255, 0), // Green
new( 0, 255, 255), // Cyan
new( 0, 127, 255), // Blue
new(255, 0, 255) }; // Purple
private static readonly int[][][] blocks = {
new[] { new[] { 0, 0 }, new[] { 1, 0 }, new[] { 1, 1 }, new[] { 2, 1 } }, // Mirrored-Z
@ -32,6 +32,7 @@ namespace Tetris {
private static Player player = new(), next = new(true);
public static bool selection, ptbLogo = true;
public static int selectionIndex, frameCount;
public static int sw = 9, sh = 9;
private static int score, bounds;
// Matrix variables
@ -54,12 +55,9 @@ namespace Tetris {
} else {
matrix = new(
new RGBLedMatrixOptions {
HardwareMapping = "adafruit-hat",
LimitRefreshRateHz = 0,
GpioSlowdown = 4,
Brightness = 100,
ChainLength = 1,
ScanMode = 1,
GpioSlowdown = 5,
ChainLength = 3,
Parallel = 3,
Rows = 64,
Cols = 64,
});
@ -69,7 +67,7 @@ namespace Tetris {
Stopwatch stopwatch = new();
// Initialize Console Color and Text-Font and -Color
RGBLedFont font = new("fonts/5x8.bdf");
RGBLedFont font = new("fonts/9x18B.bdf");
Console.ForegroundColor = ConsoleColor.White;
Color col = new(255, 255, 255), grey = new(127, 127, 127);
@ -81,8 +79,6 @@ namespace Tetris {
canvas.Clear();
canvas = matrix.SwapOnVsync(canvas);
// Inner loop for actual Gameplay
score = 0;
// Initialize board
if (!hasStarted) {
initBoard();
@ -91,6 +87,7 @@ namespace Tetris {
gameOver = false;
while (!gameOver) {
// Start of Frame
stopwatch.Restart();
canvas.Clear();
Console.SetCursorPosition(0, 0);
Console.WriteLine($"Score: {score}");
@ -106,38 +103,61 @@ namespace Tetris {
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 k = 0; k < sw; k++)
for (int l = 0; l < sh; l++)
canvas.SetPixel(i * sw + k, j * sh + l, current);
}
// Display player onto Matrix
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);
for (int k = 0; k < sw; k++)
for (int l = 0; l < sh; l++)
canvas.SetPixel((player.x + current[0]) * sw + k, (player.y + current[1]) * sh + l, player.color);
}
// Calculate ghost piece y-coordinate
int ty = player.y;
while (!collisionCheck(player.x, ++ty, player.positions)) { }
ty--;
// Display ghost piece onto Matrix
foreach (var current in player.positions) {
for (int k = 0; k < sw; k+=sw-1)
for (int l = 0; l < sh; l++)
canvas.SetPixel((player.x + current[0]) * sw + k, (ty + current[1]) * sh + l, player.color);
for (int k = 0; k < sw; k++)
for (int l = 0; l < sh; l+=sh-1)
canvas.SetPixel((player.x + current[0]) * sw + k, (ty + current[1]) * sh + l, player.color);
}
// Draw line "in the middle"
for (int i = 0; i < 64; i++)
canvas.SetPixel(30, i, grey);
for (int i = 0; i < 192; i++)
canvas.SetPixel(sw*10, i, grey);
// Statically display stored item on the right side
canvas.DrawText(font, 38, 8, col, "Save:");
canvas.DrawText(font, 92, 18, 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);
for (int k = 0; k < sw; k++)
for (int l = 0; l < sh; l++)
canvas.SetPixel(96 + b[0] * sw + k, 36 + b[1] * sh + l, (Color)player.memCol);
// Statically display next item below the previous
canvas.DrawText(font, 38, 40, col, "Next:");
canvas.DrawText(font, 92, 90, col, "Next:");
if (next.memDis != null)
foreach (int[] b in next.memDis)
for (int k = 0; k < 3; k++)
for (int l = 0; l < 3; l++)
canvas.SetPixel((14 + b[0]) * 3 + k, (15+ b[1]) * 3 + l, (Color)next.color);
for (int k = 0; k < sw; k++)
for (int l = 0; l < sh; l++)
canvas.SetPixel(96 + b[0] * sw + k, 108 + b[1] * sh + l, next.color);
// Fill bottom
for (int i = 1; i <= sh/3; i++)
for (int j = 0; j < sw*10; j++)
canvas.SetPixel(j, 192-i, grey);
// Display score
canvas.DrawText(font, 92, 162, col, $"Score:");
canvas.DrawText(font, 92, 180, col, score.ToString());
// Move Player down every few frames
if (frameCount % 30 == 0 && collisionCheck(player.x, ++player.y, player.positions)) {
player.y--;
@ -162,12 +182,12 @@ namespace Tetris {
while (selection) {
// Display Options and Cursor on Matrix
canvas.Clear();
canvas.DrawText(font, 0, 8, col, "Game Over!");
canvas.DrawText(font, 0, 24, col, " ) Weiter");
canvas.DrawText(font, 0, 32, col, " ) Beenden");
canvas.DrawText(font, 0, 48, col, $"Score: {score}");
canvas.DrawText(font, 0, 18, col, "Game Over!");
canvas.DrawText(font, 0, 54, col, " ) Weiter");
canvas.DrawText(font, 0, 72, col, " ) Beenden");
canvas.DrawText(font, 0, 108, col, $"Score: {score}");
// Cursor is a '#' at (0, 24 + i * 8) where i may be [0, 1]
canvas.DrawText(font, 0, 24+selectionIndex*8, col, "#");
canvas.DrawText(font, 0, 54+selectionIndex*18, col, "#");
canvas = matrix.SwapOnVsync(canvas);
// Handle key presses for continue game or exit
@ -206,6 +226,8 @@ namespace Tetris {
frameCount++;
Thread.Sleep(100);
}
// Reset score
score = 0;
}
}
}
@ -286,17 +308,14 @@ namespace Tetris {
}
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;
switch (rows) {
case 1: score += 40; break;
case 2: score += 100; break;
case 3: score += 300; break;
case 4: score += 1200; break;
}
// Generating new Block
@ -594,15 +613,9 @@ namespace Tetris {
public void initSelf(int blockIndex) {
int xB;
switch (blockIndex) {
default:
xB = 3;
break;
case 2:
xB = 2;
break;
case 4:
xB = 4;
break;
default: xB = 3; break;
case 2: xB = 2; break;
case 4: xB = 4; break;
}
x = 4 - xB / 2;