clean up, bugfixes and snes controller
This commit is contained in:
parent
13b59a4d55
commit
b60d8efc85
336
Tetris.cs
336
Tetris.cs
|
@ -25,12 +25,15 @@ namespace Tetris {
|
|||
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 int FPS = 60;
|
||||
public static int frameCount = 0;
|
||||
private static Color[,] board = new Color[10, 21];
|
||||
private static readonly Color black = new(0, 0, 0);
|
||||
private static bool canSwap = true, gameOver = false;
|
||||
private static Player player;
|
||||
private static int score = 0;
|
||||
private static bool canSwap = true, gameOver = false, startOver = true;
|
||||
private static int score = 0, bounds = 0;
|
||||
public static int selectionIndex = 0;
|
||||
public static bool selection = false;
|
||||
private static Player player = new(), next = new(true);
|
||||
|
||||
// Matrix variables
|
||||
private static RGBLedMatrix? _matrix = null;
|
||||
|
@ -39,7 +42,6 @@ namespace Tetris {
|
|||
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++)
|
||||
|
@ -73,8 +75,8 @@ namespace Tetris {
|
|||
RGBLedFont font = new("fonts/5x8.bdf");
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Color col = new(255, 255, 255), grey = new(127, 127, 127);
|
||||
|
||||
bool startOver = true;
|
||||
|
||||
startOver = true;
|
||||
while (startOver) {
|
||||
Console.Clear();
|
||||
canvas.Clear();
|
||||
|
@ -88,11 +90,11 @@ namespace Tetris {
|
|||
Console.WriteLine($"Score: {score}");
|
||||
|
||||
// Handle Keypresses
|
||||
if (keyHandler()) {
|
||||
startOver = false;
|
||||
gameOver = true;
|
||||
if (Console.KeyAvailable && keyHandler()) {
|
||||
EndGame();
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// Gameplay hahaha
|
||||
for (int i = 0; i < board.GetLength(0); i++)
|
||||
for (int j = 0; j < board.GetLength(1); j++) {
|
||||
|
@ -126,6 +128,14 @@ namespace Tetris {
|
|||
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);
|
||||
// Statically display next item below the previous
|
||||
canvas.DrawText(font, 38, 40, 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);
|
||||
|
||||
|
||||
// End of Frame
|
||||
canvas = matrix.SwapOnVsync(canvas);
|
||||
|
@ -138,36 +148,46 @@ namespace Tetris {
|
|||
}
|
||||
|
||||
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);
|
||||
|
||||
int fC = 0;
|
||||
bool selection = true;
|
||||
frameCount = 0;
|
||||
selection = true;
|
||||
while (selection) {
|
||||
canvas.Clear();
|
||||
canvas.DrawText(font, 0, 8, col, "Game Over!");
|
||||
canvas.DrawText(font, 0, 24, col, " ) Beenden");
|
||||
canvas.DrawText(font, 0, 32, col, " ) Weiter");
|
||||
canvas.DrawText(font, 0, 24+selectionIndex*8, col, "#");
|
||||
canvas = matrix.SwapOnVsync(canvas);
|
||||
|
||||
// Handle key presses for continue game or exit
|
||||
if (Console.KeyAvailable) {
|
||||
frameCount = 0;
|
||||
switch (Console.ReadKey(true).Key) {
|
||||
case ConsoleKey.D0:
|
||||
case ConsoleKey.Escape:
|
||||
selection = false;
|
||||
startOver = false;
|
||||
break;
|
||||
case ConsoleKey.D1:
|
||||
selection = false;
|
||||
case ConsoleKey.DownArrow:
|
||||
if (selectionIndex < 1)
|
||||
selectionIndex++;
|
||||
break;
|
||||
case ConsoleKey.UpArrow:
|
||||
if (selectionIndex > 0)
|
||||
selectionIndex--;
|
||||
break;
|
||||
case ConsoleKey.Enter:
|
||||
enterSelection();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Exit if no response for 30 secs
|
||||
if (fC >= 300) {
|
||||
if (frameCount >= 300) {
|
||||
selection = false;
|
||||
startOver = false;
|
||||
}
|
||||
|
||||
// Wait for input
|
||||
fC++;
|
||||
frameCount++;
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
|
@ -185,7 +205,7 @@ namespace Tetris {
|
|||
|
||||
private static void placeBlock() {
|
||||
// Block placing
|
||||
foreach (var current in player.positions)
|
||||
foreach (int[] current in player.positions)
|
||||
board[player.x + current[0], player.y + current[1]] = player.color;
|
||||
|
||||
// Count rows lines
|
||||
|
@ -220,17 +240,24 @@ namespace Tetris {
|
|||
|
||||
|
||||
// Generating new Block
|
||||
int[][]? tempPos = player.memPos;
|
||||
int[][]? tempDis = player.memDis;
|
||||
Color? tempCol = player.memCol;
|
||||
player = new();
|
||||
if (!collisionCheck(player.x, player.y, player.positions)) {
|
||||
// Carry memory
|
||||
player.memPos = tempPos;
|
||||
player.memDis = tempDis;
|
||||
player.memCol = tempCol;
|
||||
int blockIndex = 0;
|
||||
for (int i = 1; i < blocks.Length; i++)
|
||||
if (blocks[i].Equals(next.positions)) {
|
||||
blockIndex = i;
|
||||
break;
|
||||
}
|
||||
|
||||
player = new(
|
||||
blockIndex,
|
||||
player.memPos,
|
||||
player.memDis,
|
||||
player.memCol
|
||||
);
|
||||
next = new(true);
|
||||
|
||||
if (!collisionCheck(player.x, player.y, player.positions))
|
||||
canSwap = true;
|
||||
} else {
|
||||
else {
|
||||
// Reset Game
|
||||
gameOver = true;
|
||||
for (int i = 0; i < board.GetLength(0); i++)
|
||||
|
@ -243,15 +270,66 @@ namespace Tetris {
|
|||
|
||||
private static bool keyHandler() {
|
||||
// If key is pressed, handle logic
|
||||
if (!Console.KeyAvailable) return false;
|
||||
|
||||
switch (Console.ReadKey(true).Key) {
|
||||
case ConsoleKey.Escape:
|
||||
// Exit program.
|
||||
return true;
|
||||
case ConsoleKey.Spacebar:
|
||||
// Place block on lowest point at current x
|
||||
placeDownBlock();
|
||||
break;
|
||||
case ConsoleKey.DownArrow:
|
||||
// Lower block by 1 on y-axis
|
||||
moveDownOnce();
|
||||
break;
|
||||
case ConsoleKey.LeftArrow:
|
||||
// Move block to the left on the x axis
|
||||
moveLeftSide();
|
||||
break;
|
||||
case ConsoleKey.RightArrow:
|
||||
// Move block to the right on the x axis
|
||||
moveRightSide();
|
||||
break;
|
||||
case ConsoleKey.A:
|
||||
// Rotate block counter-clockwise
|
||||
calcBounds();
|
||||
rotateCounterClockwise();
|
||||
adjustXPos();
|
||||
break;
|
||||
case ConsoleKey.S:
|
||||
// Swap current block with memory
|
||||
swapMemory();
|
||||
break;
|
||||
case ConsoleKey.D:
|
||||
// Rotate block clockwise
|
||||
calcBounds();
|
||||
rotateClockwise();
|
||||
adjustXPos();
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void enterSelection() {
|
||||
switch (selectionIndex) {
|
||||
case 0:
|
||||
selection = false;
|
||||
startOver = false;
|
||||
break;
|
||||
case 1:
|
||||
selection = false;
|
||||
frameCount = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void calcBounds() {
|
||||
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;
|
||||
|
@ -263,72 +341,10 @@ namespace Tetris {
|
|||
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--;
|
||||
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
|
||||
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;
|
||||
}
|
||||
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
|
||||
foreach (int[] a in player.positions) {
|
||||
int temp = a[0];
|
||||
a[0] = bounds - 1 - a[1];
|
||||
a[1] = temp;
|
||||
}
|
||||
break;
|
||||
}
|
||||
public static void adjustXPos() {
|
||||
// Adjust x-postions if necessary
|
||||
foreach (int[] b in player.positions) {
|
||||
int nx = player.x + b[0];
|
||||
while (nx > 9) {
|
||||
|
@ -340,7 +356,71 @@ namespace Tetris {
|
|||
nx = player.x + b[0];
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void swapMemory() {
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public static void rotateClockwise() {
|
||||
foreach (int[] a in player.positions) {
|
||||
int temp = a[0];
|
||||
a[0] = bounds - 1 - a[1];
|
||||
a[1] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
public static void rotateCounterClockwise() {
|
||||
foreach (int[] a in player.positions) {
|
||||
int temp = a[1];
|
||||
a[1] = bounds - 1 - a[0];
|
||||
a[0] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
public static void moveRightSide() {
|
||||
if (collisionCheck(++player.x, player.y, player.positions))
|
||||
player.x--;
|
||||
}
|
||||
|
||||
public static void moveLeftSide() {
|
||||
if (collisionCheck(--player.x, player.y, player.positions))
|
||||
player.x++;
|
||||
}
|
||||
|
||||
public static void moveDownOnce() {
|
||||
if (collisionCheck(player.x, ++player.y, player.positions))
|
||||
player.y--;
|
||||
}
|
||||
|
||||
public static void placeDownBlock() {
|
||||
while (!collisionCheck(player.x, ++player.y, player.positions)) { }
|
||||
player.y--;
|
||||
placeBlock();
|
||||
}
|
||||
|
||||
public static void EndGame() {
|
||||
gameOver = true;
|
||||
startOver = false;
|
||||
}
|
||||
|
||||
private class Player {
|
||||
|
@ -351,25 +431,61 @@ namespace Tetris {
|
|||
public Color color;
|
||||
public int x, y;
|
||||
|
||||
public Player() {
|
||||
public Player(bool displaySelf=false) {
|
||||
int randInt = (new Random()).Next(7);
|
||||
|
||||
if (displaySelf) {
|
||||
int[][] tempPos = blocks[randInt];
|
||||
memDis = new int[tempPos.Length][];
|
||||
for (int i = 0; i < tempPos.Length; i++) {
|
||||
memDis[i] = new int[tempPos[i].Length];
|
||||
for (int j = 0; j < tempPos[i].Length; j++)
|
||||
memDis[i][j] = tempPos[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
positions = blocks[randInt];
|
||||
color = colors[randInt];
|
||||
|
||||
int bounds;
|
||||
int xB;
|
||||
switch (randInt) {
|
||||
default:
|
||||
bounds = 3;
|
||||
xB = 3;
|
||||
break;
|
||||
case 2:
|
||||
bounds = 2;
|
||||
xB = 2;
|
||||
break;
|
||||
case 4:
|
||||
bounds = 4;
|
||||
xB = 4;
|
||||
break;
|
||||
}
|
||||
|
||||
x = 4 - bounds / 2;
|
||||
x = 4 - xB / 2;
|
||||
y = 0;
|
||||
}
|
||||
|
||||
public Player(int blockIndex, int[][]? _memPos, int[][]? _memDis, Color? _memCol) {
|
||||
memPos = _memPos;
|
||||
memDis = _memDis;
|
||||
memCol = _memCol;
|
||||
|
||||
positions = blocks[blockIndex];
|
||||
color = colors[blockIndex];
|
||||
|
||||
int xB;
|
||||
switch (blockIndex) {
|
||||
default:
|
||||
xB = 3;
|
||||
break;
|
||||
case 2:
|
||||
xB = 2;
|
||||
break;
|
||||
case 4:
|
||||
xB = 4;
|
||||
break;
|
||||
}
|
||||
|
||||
x = 4 - xB / 2;
|
||||
y = 0;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user