clean up and commentary
This commit is contained in:
parent
b960445e1f
commit
78f1e0e143
|
@ -5,17 +5,13 @@ using rpi_rgb_led_matrix_sharp;
|
|||
|
||||
namespace RPI_Matrix {
|
||||
public static class RPI_Matrix {
|
||||
private static bool hasSelected = false;
|
||||
private static int selectionIndex = 0;
|
||||
// Initialize variables
|
||||
private static bool hasSelected, isInGame, isTetris, isFlappy, isSnake;
|
||||
private static int selectionIndex, keypressFrame, frameCount;
|
||||
private static bool selection = true;
|
||||
private static bool isInGame = false;
|
||||
private static bool isTetris = false;
|
||||
private static bool isFlappy = false;
|
||||
private static bool isSnake = false;
|
||||
private static int keypressFrame = 0;
|
||||
private static int frameCount = 0;
|
||||
|
||||
public static void Main() {
|
||||
// Connect to RGB LED Matrix
|
||||
RGBLedMatrix matrix = new(
|
||||
new RGBLedMatrixOptions {
|
||||
HardwareMapping = "adafruit-hat",
|
||||
|
@ -29,26 +25,34 @@ namespace RPI_Matrix {
|
|||
});
|
||||
RGBLedCanvas canvas = matrix.CreateOffscreenCanvas();
|
||||
|
||||
// Initialize Games
|
||||
SnakeGame.SnakeGame.Init(matrix, canvas);
|
||||
FlappyBird.FlappyBird.Init(matrix, canvas);
|
||||
Tetris.Tetris.Init(matrix, canvas);
|
||||
|
||||
// Start listening and interpreting Gamepad input in parallel
|
||||
(new Thread(InterpretGamepadInput)).Start();
|
||||
|
||||
// Initialize Text-Font and -Color
|
||||
RGBLedFont font = new("fonts/5x8.bdf");
|
||||
Color col = new(255, 255, 255);
|
||||
|
||||
// Game Menu
|
||||
while (true) {
|
||||
// Restart loop
|
||||
Console.Clear();
|
||||
|
||||
selection = true;
|
||||
|
||||
// Game selection Menu
|
||||
while (selection) {
|
||||
// Display Games and Cursor on Matrix
|
||||
canvas.Clear();
|
||||
canvas.DrawText(font, 0, 8, col, "Spielauswahl:");
|
||||
canvas.DrawText(font, 0, 24, col, " ) SnakeGame");
|
||||
canvas.DrawText(font, 0, 32, col, " ) FlappyBird");
|
||||
canvas.DrawText(font, 0, 40, col, " ) Tetris");
|
||||
canvas.DrawText(font, 0, 56, col, " ) Beenden");
|
||||
// Cursor is a '#' at (0, 24 + i * 8) where i may be [0, 1, 2, 4]
|
||||
int nx = selectionIndex + ((selectionIndex == 3) ? 1 : 0);
|
||||
canvas.DrawText(font, 0, 24 + nx * 8, col, "#");
|
||||
canvas = matrix.SwapOnVsync(canvas);
|
||||
|
@ -74,34 +78,42 @@ namespace RPI_Matrix {
|
|||
}
|
||||
}
|
||||
|
||||
// Use selected index to start a Game
|
||||
if (hasSelected) {
|
||||
hasSelected = false;
|
||||
switch (selectionIndex) {
|
||||
case 0:
|
||||
// Start SnakeGame in Singleplayer
|
||||
playSnakeGame("-s");
|
||||
break;
|
||||
case 1:
|
||||
// Start FlappyBird
|
||||
playFlappyBird();
|
||||
break;
|
||||
case 2:
|
||||
// Start Tetris
|
||||
playTetris();
|
||||
break;
|
||||
case 3:
|
||||
// Exit program
|
||||
Environment.Exit(0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Reset Game identification variables after exiting
|
||||
isInGame = false;
|
||||
isTetris = false;
|
||||
isFlappy = false;
|
||||
isSnake = false;
|
||||
|
||||
// If player is absent for 15 seconds, start SnakeGame in Bot Mode
|
||||
if (frameCount >= keypressFrame + 150) {
|
||||
keypressFrame = frameCount;
|
||||
playSnakeGame("-b");
|
||||
}
|
||||
|
||||
// Wait for next frame and keep count
|
||||
Thread.Sleep(100);
|
||||
frameCount = (frameCount+1)%1000000;
|
||||
}
|
||||
|
@ -109,6 +121,7 @@ namespace RPI_Matrix {
|
|||
}
|
||||
|
||||
private static void playSnakeGame(string arg) {
|
||||
// Set Game identification variables and start playing SnakeGame
|
||||
isSnake = true;
|
||||
isInGame = true;
|
||||
selection = false;
|
||||
|
@ -116,6 +129,7 @@ namespace RPI_Matrix {
|
|||
}
|
||||
|
||||
private static void playFlappyBird() {
|
||||
// Set Game identification variables and start playing FlappyBird
|
||||
isFlappy = true;
|
||||
isInGame = true;
|
||||
selection = false;
|
||||
|
@ -123,6 +137,7 @@ namespace RPI_Matrix {
|
|||
}
|
||||
|
||||
private static void playTetris() {
|
||||
// Set Game identification variables and start playing Tetris
|
||||
isTetris = true;
|
||||
isInGame = true;
|
||||
selection = false;
|
||||
|
@ -130,45 +145,54 @@ namespace RPI_Matrix {
|
|||
}
|
||||
|
||||
private static void InterpretGamepadInput() {
|
||||
// Read file "/dev/input/js0" in Raspbian for Raspberry 4 4GB.
|
||||
// All inputs on a Gamepad are accessible from within here.
|
||||
// Interpreting this data may only work in this program using a SNES Controller.
|
||||
FileInfo targetFile = new FileInfo("/dev/input/js0");
|
||||
using FileStream fs = targetFile.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
// Initialize Buffer (byte Array) to put read bytes into
|
||||
byte[] buffer = new byte[1024];
|
||||
while (fs.Read(buffer) > 0) {
|
||||
// Console.SetCursorPosition(0, 1);
|
||||
// If inputs were registered, save frame #
|
||||
keypressFrame = frameCount;
|
||||
// Do the same in Tetris if it's in the 'GameOver' Screen
|
||||
if (isTetris && Tetris.Tetris.selection)
|
||||
Tetris.Tetris.frameCount = 0;
|
||||
// And do it again in SnakeGame while it's playing.
|
||||
else if (isSnake && !SnakeGame.SnakeGame.gameOver)
|
||||
SnakeGame.SnakeGame.keypressFrame = SnakeGame.SnakeGame.frameCount;
|
||||
// Convert Buffer to Hexadecimal, each Byte separated by a '-'
|
||||
switch (BitConverter.ToString(buffer, 4, 4)) {
|
||||
case "01-00-01-00":
|
||||
// X-Button
|
||||
// Console.WriteLine("Pressed Button 'X' ");
|
||||
// Code for X-Button Press
|
||||
if (isTetris)
|
||||
Tetris.Tetris.swapMemory();
|
||||
break;
|
||||
case "01-00-01-01":
|
||||
// A-Button
|
||||
// Console.WriteLine("Pressed Button 'A' ");
|
||||
// Code for A-Button Press
|
||||
// If not already in a Game, now ready up to select one.
|
||||
if (!isInGame)
|
||||
hasSelected = true;
|
||||
// Select given option in Tetris' 'GameOver' Screen
|
||||
else if (isTetris && Tetris.Tetris.selection)
|
||||
Tetris.Tetris.enterSelection();
|
||||
break;
|
||||
case "01-00-01-02":
|
||||
// B-Button
|
||||
// Console.WriteLine("Pressed Button 'B' ");
|
||||
// Code for B-Button Press
|
||||
if (isFlappy) {
|
||||
// Jump
|
||||
if (FlappyBird.FlappyBird.isReady)
|
||||
FlappyBird.FlappyBird.JumpOnce();
|
||||
// Start Game
|
||||
else
|
||||
FlappyBird.FlappyBird.isReady = true;
|
||||
// Place the current Block in Tetris
|
||||
} else if (isTetris)
|
||||
Tetris.Tetris.placeDownBlock();
|
||||
break;
|
||||
case "01-00-01-04":
|
||||
// L-Button
|
||||
// Console.WriteLine("Pressed Button 'L' ");
|
||||
// Code for L-Button Press
|
||||
// If in Tetris, rotate the current Block counter-Clockwise
|
||||
if (isTetris) {
|
||||
Tetris.Tetris.calcBounds();
|
||||
Tetris.Tetris.rotateCounterClockwise();
|
||||
|
@ -176,8 +200,8 @@ namespace RPI_Matrix {
|
|||
}
|
||||
break;
|
||||
case "01-00-01-05":
|
||||
// R-Button
|
||||
// Console.WriteLine("Pressed Button 'R' ");
|
||||
// Code for R-Button Press
|
||||
// If in Tetris, rotate the current Block Clockwise
|
||||
if (isTetris) {
|
||||
Tetris.Tetris.calcBounds();
|
||||
Tetris.Tetris.rotateClockwise();
|
||||
|
@ -185,55 +209,64 @@ namespace RPI_Matrix {
|
|||
}
|
||||
break;
|
||||
case "01-00-01-09":
|
||||
// Start-Button
|
||||
// Console.WriteLine("Pressed Button 'Start' ");
|
||||
// Code for Start-Button Press
|
||||
// If in SnakeGame, end the Game and return to menu
|
||||
if (isSnake)
|
||||
SnakeGame.SnakeGame.EndGame();
|
||||
else if (isFlappy) {
|
||||
// If in FlappyBirds' 'ready' Screen, return to menu
|
||||
if (!FlappyBird.FlappyBird.isReady)
|
||||
FlappyBird.FlappyBird.stopPlaying = true;
|
||||
// Otherwise go back to 'ready' Screen by ending the Game
|
||||
else
|
||||
FlappyBird.FlappyBird.EndGame();
|
||||
// If in Tetris, end the Game and return to menu
|
||||
} else if (isTetris)
|
||||
Tetris.Tetris.EndGame();
|
||||
break;
|
||||
case "01-80-02-01":
|
||||
// Arrow-Up
|
||||
// Console.WriteLine("Pressed 'Arrow-Up' ");
|
||||
// Code for Arrow-Up Press
|
||||
// If in the selection screen, decrement index
|
||||
if (!isInGame && selectionIndex > 0)
|
||||
selectionIndex--;
|
||||
// If in SnakeGame, Steer the Snake upwards
|
||||
else if (isSnake)
|
||||
SnakeGame.SnakeGame.SteerUp(SnakeGame.SnakeGame.self);
|
||||
else if (isTetris)
|
||||
if (Tetris.Tetris.selection && Tetris.Tetris.selectionIndex > 0)
|
||||
// If in Tetris and in Tetris' 'GameOver' Screen, decrement its index.
|
||||
else if (isTetris && Tetris.Tetris.selection && Tetris.Tetris.selectionIndex > 0)
|
||||
Tetris.Tetris.selectionIndex--;
|
||||
break;
|
||||
case "FF-7F-02-01":
|
||||
// Arrow-Down
|
||||
// Console.WriteLine("Pressed 'Arrow-Down' ");
|
||||
// Code for Arrow-Down Press
|
||||
// If in the selection screen, decrement index
|
||||
if (!isInGame && selectionIndex < 3)
|
||||
selectionIndex++;
|
||||
// If in SnakeGame, Steer the Snake downwards
|
||||
else if (isSnake)
|
||||
SnakeGame.SnakeGame.SteerDown(SnakeGame.SnakeGame.self);
|
||||
else if (isTetris)
|
||||
// If in Tetris and in Tetris' 'GameOver' Screen, increment its index.
|
||||
if (Tetris.Tetris.selection && Tetris.Tetris.selectionIndex < 1)
|
||||
Tetris.Tetris.selectionIndex++;
|
||||
// Otherwise, move Tetris piece down
|
||||
else
|
||||
Tetris.Tetris.moveDownOnce();
|
||||
break;
|
||||
case "01-80-02-00":
|
||||
// Arrow-Left
|
||||
// Console.WriteLine("Pressed 'Arrow-Left' ");
|
||||
// Code for Arrow-Left Press
|
||||
// If in SnakeGame, Steer the Snake to the left
|
||||
if (isSnake)
|
||||
SnakeGame.SnakeGame.SteerLeft(SnakeGame.SnakeGame.self);
|
||||
// If in Tetris, move current Block to the left
|
||||
else if (isTetris)
|
||||
Tetris.Tetris.moveLeftSide();
|
||||
break;
|
||||
case "FF-7F-02-00":
|
||||
// Arrow-Right
|
||||
// Console.WriteLine("Pressed 'Arrow-Right' ");
|
||||
// Code for Arrow-Right Press
|
||||
// If in SnakeGame, Steer the Snake to the right
|
||||
if (isSnake)
|
||||
SnakeGame.SnakeGame.SteerRight(SnakeGame.SnakeGame.self);
|
||||
SnakeGame.SnakeGame.SteerRight(SnakeGame.SnakeGame.self);
|
||||
// If in Tetris, move current Block to the right
|
||||
else if (isTetris)
|
||||
Tetris.Tetris.moveRightSide();
|
||||
break;
|
||||
|
|
Loading…
Reference in New Issue
Block a user