Compare commits

...

10 Commits

5 changed files with 208 additions and 71 deletions

4
.gitignore vendored
View File

@ -1,3 +1,3 @@
bin/*
!bin/Debug/net5.0/win-x64/publish/*
obj/*
obj/*
.idea/*

View File

@ -1,29 +1,76 @@
using System;
using System.Threading;
using System.Diagnostics;
using rpi_rgb_led_matrix_sharp;
namespace FlappyBird {
class Program {
public static class FlappyBird {
// Constants
static string scoreText = "Your score is: ";
static float factor = 32/1.4f;
static int pipeInterval = 60;
static int height = 15;
static int width = 30;
static int FPS = 60;
private static string scoreText = "Your score is: ";
private static float factor = 32/1.4f;
private static int pipeInterval = 60;
private static int sw = 6, sh = 6;
private static int height = 96;
private static int width = 96;
private static int FPS = 60;
// Game variables
static char[,] board;
static int spaceHeight = height/5;
static float jumpVel = height/factor;
static bool gameOver, startOver = true;
static int birdX = width/16+1, score = 0;
static float birdY = height/2f, velY = 0f;
static float gravity = height/(factor*10);
private static char[,] board;
public static float jumpVel = 1.85f;
public static int spaceHeight = height/5;
private static bool gameOver, startOver = true;
public static bool isReady, stopPlaying, selection;
private static float birdY = height/2f, velY;
public static int selectionIndex, frameCount;
public static int birdX = width/16+1, score;
public static float gravity = 0.135f;
// Matrix variables
private static RGBLedMatrix? _matrix;
private static RGBLedCanvas? _canvas;
static void Main() {
public static void Init(RGBLedMatrix matrix, RGBLedCanvas canvas) {
_matrix = matrix;
_canvas = canvas;
}
public static void Main() {
// Initialize RGBLedMatrix
RGBLedMatrix matrix;
RGBLedCanvas canvas;
if (_matrix != null && _canvas != null) {
matrix = _matrix;
canvas = _canvas;
} else {
matrix = new(
new RGBLedMatrixOptions {
GpioSlowdown = 5,
ChainLength = 3,
Parallel = 3,
Rows = 64,
Cols = 64,
});
canvas = matrix.CreateOffscreenCanvas();
}
// Initialize stopwatch to measure calculation and display time
Stopwatch stopwatch = new();
// Initialize colors
Color yellow = new(255, 255, 0);
Color green = new(0, 255, 0);
// Initialize Console Color and Text-Font and -Color
RGBLedFont font = new("fonts/9x18B.bdf");
Console.ForegroundColor = ConsoleColor.White;
Color textCol = new(255, 255, 255), grey = new(127, 127, 127);
Console.Clear();
startOver = true;
stopPlaying = false;
while (startOver) {
score = 0;
gameOver = false;
board = new char[width + 2, height + 2];
canvas.Clear();
// Fill the 'board' Array with blank spaces
for (int i = 0; i < board.GetLength(0); i++)
@ -34,43 +81,55 @@ namespace FlappyBird {
}
// Place the bird on the board
int rBY = board.GetLength(1)-1-(int)Math.Round(birdY);
board[birdX, rBY] = 'O';
int rBY = height-1-(int)Math.Round(birdY);
board[birdX, rBY ] = 'O';
for (int k = 0; k < sw; k++)
for (int l = 0; l < sh; l++)
canvas.SetPixel((birdX-1)*2+k, (rBY-1)*2+l, yellow);
canvas = matrix.SwapOnVsync(canvas);
// Draw the board
Console.Clear();
Console.WriteLine($"{scoreText}{score}");
for (int j = 0; j < board.GetLength(1); j++) {
for (int i = 0; i < board.GetLength(0); i++) {
string pipeCon = (i < board.GetLength(0)-1) ? "|" : "";
bool yC = j%(board.GetLength(1)-1)==0;
pipeCon = (yC && pipeCon != "") ? "#" : pipeCon;
Console.Write($"{board[i, j]}{pipeCon}");
}
Console.WriteLine();
}
Console.WriteLine($"{scoreText}{score/2}");
// Start the game loop
Console.ReadKey(true);
int frameCount = 0;
isReady = false;
while (!isReady) {
if (Console.KeyAvailable)
switch (Console.ReadKey(true).Key) {
case ConsoleKey.Spacebar:
isReady = true;
break;
case ConsoleKey.Escape:
return;
}
else if (isReady)
break;
else if (stopPlaying)
return;
Thread.Sleep(100);
}
frameCount = 0;
while (!gameOver) {
char[,] copy = board.Clone() as char[,];
canvas.Clear();
// char[,] copy = board.Clone() as char[,];
// Move all pipes to the left every other frame
if (frameCount % 2 == 0) {
bool hasInvremented = false;
bool hasIncremented = false;
for (int i = 1; i < board.GetLength(0)-1; i++)
for (int j = 1; j < board.GetLength(1)-1; j++)
if (board[i, j] == '$') {
board[i, j] = ' ';
if (i-1 > 0) {
if (board[i-1, j] == 'O')
gameEnd();
EndGame();
board[i-1, j] = '$';
if (i-1 == birdX && !hasInvremented) {
if (i-1 == birdX && !hasIncremented) {
score++;
hasInvremented = true;
hasIncremented = true;
}
}
}
@ -90,10 +149,10 @@ namespace FlappyBird {
if (Console.KeyAvailable)
switch (Console.ReadKey(true).Key) {
case ConsoleKey.Escape:
Environment.Exit(0);
EndGame();
break;
case ConsoleKey.Spacebar:
velY = jumpVel;
JumpOnce();
break;
}
// Handle velocity
@ -103,7 +162,7 @@ namespace FlappyBird {
// Handle position
birdY += velY;
if (birdY < 1)
gameEnd();
EndGame();
if (birdY > board.GetLength(1)-2)
velY = 0;
birdY = Math.Max(birdY, 1);
@ -111,51 +170,125 @@ namespace FlappyBird {
// Update bird position on board
int tempY = board.GetLength(1)-1-(int)Math.Round(birdY);
if (tempY != rBY) {
board[birdX, rBY] = ' ';
board[birdX, rBY ] = ' ';
if (board[birdX, tempY] == '$')
gameEnd();
board[birdX, tempY] = 'O';
EndGame();
board[birdX, tempY ] = 'O';
rBY = tempY;
}
// Update the display based on changes between the 'board' and 'copy'
Console.SetCursorPosition(scoreText.Length, 0);
Console.Write(score);
for (int j = 1; j < board.GetLength(1)-1; j++)
for (int i = 1; i < board.GetLength(0)-1; i++) {
Console.Write(score/2);
for (int j = 1; j < board.GetLength(1) - 1; j++)
for (int i = 1; i < board.GetLength(0) - 1; i++) {
Color col = new(255, 255, 255);
bool doDisplay = true;
char bC = board[i, j];
if (bC != copy[i, j]) {
Console.SetCursorPosition(i*2, j+1);
ConsoleColor col = (bC=='O')?ConsoleColor.Yellow:((bC=='$')?ConsoleColor.Green:ConsoleColor.White);
Console.ForegroundColor = col;
Console.Write(bC);
switch (bC) {
case '$':
col = green;
break;
case 'O':
col = yellow;
break;
case ' ':
doDisplay = false;
break;
}
}
if (doDisplay)
for (int k = 0; k < sw; k++)
for (int l = 0; l < sh; l++)
canvas.SetPixel((i-1)*2+k, (j-1)*2+l, col);
}
// Swap Frame on Vsync
canvas = matrix.SwapOnVsync(canvas);
// End of game loop
// Fix framerate
frameCount = (frameCount+1)%(FPS*4);
Console.SetCursorPosition(board.GetLength(0)*2-1, board.GetLength(1));
Thread.Sleep(1000/FPS);
int elapsed = (int)stopwatch.ElapsedMilliseconds;
if (elapsed < 1000/FPS)
Thread.Sleep(1000/FPS - elapsed);
}
// Start loop to select next step in
frameCount = 0;
while (selection) {
// Display Options and Cursor on Matrix
canvas.Clear();
canvas.DrawText(font, 0, 18, textCol, "Game Over!");
canvas.DrawText(font, 0, 54, textCol, " ) Weiter");
canvas.DrawText(font, 0, 72, textCol, " ) Beenden");
canvas.DrawText(font, 0, 108, textCol, $"Score: {score/2}");
// Cursor is a '#' at (0, 24 + i * 8) where i may be [0, 1]
canvas.DrawText(font, 0, 54+selectionIndex*18, textCol, "#");
canvas = matrix.SwapOnVsync(canvas);
// Handle key presses for continue game or exit
if (Console.KeyAvailable) {
frameCount = 0;
switch (Console.ReadKey(true).Key) {
case ConsoleKey.Escape:
// Exit Game
selection = false;
startOver = false;
break;
case ConsoleKey.DownArrow:
// Increment index
if (selectionIndex < 1)
selectionIndex++;
break;
case ConsoleKey.UpArrow:
// Decrement index
if (selectionIndex > 0)
selectionIndex--;
break;
case ConsoleKey.Enter:
// Enter selected option
enterSelection();
break;
}
}
// Exit if no response for 30 secs
if (frameCount >= 300) {
selection = false;
startOver = false;
}
// Wait for input
frameCount++;
Thread.Sleep(100);
}
}
}
static void gameEnd() {
Console.Clear();
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("You lost! ");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Start over? (Y/n)");
string uin = Console.ReadLine();
if (uin.ToLower() == "n")
startOver = false;
else {
birdY = height/2f;
velY = 0f;
score = 0;
// Method to choose what a given index has to do
public static void enterSelection() {
switch (selectionIndex) {
case 1:
selection = false;
startOver = false;
break;
case 0:
selection = false;
frameCount = 0;
break;
}
}
// Method for Jumping
public static void JumpOnce() {
velY = jumpVel;
}
// Method for ending the current Game
public static void EndGame() {
birdY = height/2f;
velY = 0f;
gameOver = true;
// Environment.Exit(0);
selection = true;
}
}
}

View File

@ -1,11 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<Reference Include="RGBLedMatrix.dll" />
</ItemGroup>
<PropertyGroup>
<MainEntryPoint>FlappyBird.FlappyBird</MainEntryPoint>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<PublishSingleFile>true</PublishSingleFile>
<TargetFramework>net6.0</TargetFramework>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<RuntimeIdentifier>linux-arm64</RuntimeIdentifier>
<PublishReadyToRun>true</PublishReadyToRun>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
</PropertyGroup>

BIN
RGBLedMatrix.dll Normal file

Binary file not shown.

BIN
librgbmatrix.so Normal file

Binary file not shown.