initial commit
This commit is contained in:
commit
3810bf93e3
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
bin/*
|
||||
obj/*
|
BIN
RGBLedMatrix.dll
Normal file
BIN
RGBLedMatrix.dll
Normal file
Binary file not shown.
236
Tetris.cs
Normal file
236
Tetris.cs
Normal file
|
@ -0,0 +1,236 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
using System.Diagnostics;
|
||||
using rpi_rgb_led_matrix_sharp;
|
||||
|
||||
namespace Tetris {
|
||||
public static class Tetris {
|
||||
// Static variables
|
||||
private static 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
|
||||
|
||||
private static 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[] { 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[] { 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
|
||||
|
||||
// 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 Player player;
|
||||
private static bool canRotate = true;
|
||||
|
||||
// Matrix variables
|
||||
private static RGBLedMatrix? _matrix = null;
|
||||
private static RGBLedCanvas? _canvas = null;
|
||||
|
||||
public static void Init(RGBLedMatrix matrix, RGBLedCanvas canvas) {
|
||||
_matrix = matrix;
|
||||
_canvas = canvas;
|
||||
player = new();
|
||||
|
||||
for (int i = 0; i < board.GetLength(0); i++)
|
||||
for (int j = 0; j < board.GetLength(1); j++)
|
||||
board[i, j] = black;
|
||||
}
|
||||
|
||||
public static void Main() {
|
||||
// Initialize RGBLedMatrix
|
||||
RGBLedMatrix matrix;
|
||||
RGBLedCanvas canvas;
|
||||
if (_matrix != null && _canvas != null) {
|
||||
matrix = _matrix;
|
||||
canvas = _canvas;
|
||||
} else {
|
||||
matrix = new(
|
||||
new RGBLedMatrixOptions {
|
||||
HardwareMapping = "adafruit-hat",
|
||||
LimitRefreshRateHz = 0,
|
||||
GpioSlowdown = 4,
|
||||
Brightness = 100,
|
||||
ChainLength = 1,
|
||||
ScanMode = 1,
|
||||
Rows = 64,
|
||||
Cols = 64,
|
||||
});
|
||||
canvas = matrix.CreateOffscreenCanvas();
|
||||
}
|
||||
// Initialize stopwatch to measure calculation and display time
|
||||
Stopwatch stopwatch = new();
|
||||
|
||||
bool startOver = true;
|
||||
while (startOver) {
|
||||
Console.Clear();
|
||||
canvas.Clear();
|
||||
canvas = matrix.SwapOnVsync(canvas);
|
||||
|
||||
bool gameOver = false;
|
||||
while (!gameOver) {
|
||||
// Start of Frame
|
||||
canvas.Clear();
|
||||
|
||||
// Handle Keypresses
|
||||
if (keyHandler()) {
|
||||
startOver = false;
|
||||
gameOver = true;
|
||||
}
|
||||
|
||||
// Gameplay hahag
|
||||
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))
|
||||
canvas.SetPixel(i * 3 + k, j * 3 + l, current);
|
||||
}
|
||||
for (int j = 0; j < player.positions.Length; j++) {
|
||||
int[] current = player.positions[j];
|
||||
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);
|
||||
}
|
||||
|
||||
// Move Block down every few frames
|
||||
if (frameCount % 30 == 0 && collisionCheck(player.x, ++player.y, player.positions)) {
|
||||
player.y--;
|
||||
placeBlock();
|
||||
}
|
||||
|
||||
// End of Frame
|
||||
canvas = matrix.SwapOnVsync(canvas);
|
||||
frameCount = (frameCount+1)%(FPS*4);
|
||||
|
||||
// Fix framerate
|
||||
int elapsed = (int)stopwatch.ElapsedMilliseconds;
|
||||
if (elapsed < 1000/FPS)
|
||||
Thread.Sleep(1000/FPS - elapsed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static bool collisionCheck(int x, int y, int[][] block) {
|
||||
foreach (int[] b in block) {
|
||||
int nx = x + b[0], ny = y + b[1];
|
||||
if (nx is > 9 or < 0 || ny is > 20 or < 0 || !board[nx, ny].Equals(black))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void placeBlock() {
|
||||
for (int j = 0; j < player.positions.Length; j++) {
|
||||
int[] current = player.positions[j];
|
||||
board[player.x + current[0], player.y + current[1]] = player.color;
|
||||
}
|
||||
int[][]? tempPos = player.memPos;
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
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)) { }
|
||||
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.D:
|
||||
// Rotate block clockwise
|
||||
rotateByAngle(player.positions, (float)Math.PI/2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private class Player {
|
||||
public int[][] positions;
|
||||
public int[][]? memPos;
|
||||
public Color? memCol;
|
||||
public int x, y;
|
||||
public Color color;
|
||||
|
||||
public Player() {
|
||||
int randInt = (new Random()).Next(7);
|
||||
positions = blocks[randInt];
|
||||
color = colors[randInt];
|
||||
x = 3;
|
||||
y = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
17
Tetris.csproj
Normal file
17
Tetris.csproj
Normal file
|
@ -0,0 +1,17 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="RGBLedMatrix.dll" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<MainEntryPoint>Tetris.Tetris</MainEntryPoint>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<SelfContained>true</SelfContained>
|
||||
<RuntimeIdentifier>linux-arm64</RuntimeIdentifier>
|
||||
<PublishReadyToRun>true</PublishReadyToRun>
|
||||
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
BIN
librgbmatrix.so
Normal file
BIN
librgbmatrix.so
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user