Compare commits

...

7 Commits

Author SHA1 Message Date
17ff309a01 port back to 1x64x64 matrix 2023-04-19 15:05:35 +02:00
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
4 changed files with 64 additions and 48 deletions

3
.gitignore vendored
View File

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

107
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 = 3, sh = 3;
private static int score, bounds;
// Matrix variables
@ -56,9 +57,9 @@ namespace Tetris {
new RGBLedMatrixOptions {
HardwareMapping = "adafruit-hat",
LimitRefreshRateHz = 0,
GpioSlowdown = 4,
Brightness = 100,
GpioSlowdown = 5,
ChainLength = 1,
Parallel = 1,
ScanMode = 1,
Rows = 64,
Cols = 64,
@ -81,8 +82,6 @@ namespace Tetris {
canvas.Clear();
canvas = matrix.SwapOnVsync(canvas);
// Inner loop for actual Gameplay
score = 0;
// Initialize board
if (!hasStarted) {
initBoard();
@ -91,6 +90,7 @@ namespace Tetris {
gameOver = false;
while (!gameOver) {
// Start of Frame
stopwatch.Restart();
canvas.Clear();
Console.SetCursorPosition(0, 0);
Console.WriteLine($"Score: {score}");
@ -106,38 +106,60 @@ 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);
canvas.SetPixel(sw*10, i, grey);
// Statically display stored item on the right side
canvas.DrawText(font, 38, 8, col, "Save:");
canvas.DrawText(font, 33, 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);
for (int k = 0; k < sw; k++)
for (int l = 0; l < sh; l++)
canvas.SetPixel(34 + b[0] * sw + k, 13 + b[1] * sh + l, (Color)player.memCol);
// Statically display next item below the previous
canvas.DrawText(font, 38, 40, col, "Next:");
canvas.DrawText(font, 33, 30, 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(34 + b[0] * sw + k, 35 + b[1] * sh + l, next.color);
// Fill bottom
for (int j = 0; j < sw*10; j++)
canvas.SetPixel(j, 63, grey);
// Display score
canvas.DrawText(font, 33, 52, col, $"Score:");
canvas.DrawText(font, 33, 60, col, score.ToString());
// Move Player down every few frames
if (frameCount % 30 == 0 && collisionCheck(player.x, ++player.y, player.positions)) {
player.y--;
@ -206,6 +228,8 @@ namespace Tetris {
frameCount++;
Thread.Sleep(100);
}
// Reset score
score = 0;
}
}
}
@ -286,17 +310,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 +615,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;

View File

@ -9,7 +9,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>linux-arm64</RuntimeIdentifier>
<RuntimeIdentifier>linux-arm</RuntimeIdentifier>
<PublishReadyToRun>true</PublishReadyToRun>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
</PropertyGroup>

Binary file not shown.