ConsoleFlappyBird/FlappyBird.cs
2022-10-13 15:28:25 +02:00

161 lines
6.8 KiB
C#

using System;
using System.Threading;
namespace FlappyBird {
class Program {
// 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;
// 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);
static void Main() {
while (startOver) {
gameOver = false;
board = new char[width + 2, height + 2];
// Fill the 'board' Array with blank spaces
for (int i = 0; i < board.GetLength(0); i++)
for (int j = 0; j < board.GetLength(1); j++) {
bool xC = i%(board.GetLength(0)-1)==0;
bool yC = j%(board.GetLength(1)-1)==0;
board[i, j] = (xC||yC)?'#':' ';
}
// Place the bird on the board
int rBY = board.GetLength(1)-1-(int)Math.Round(birdY);
board[birdX, rBY] = 'O';
// 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();
}
// Start the game loop
Console.ReadKey(true);
int frameCount = 0;
while (!gameOver) {
char[,] copy = board.Clone() as char[,];
// Move all pipes to the left every other frame
if (frameCount % 2 == 0) {
bool hasInvremented = 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();
board[i-1, j] = '$';
if (i-1 == birdX && !hasInvremented) {
score++;
hasInvremented = true;
}
}
}
}
// Generate pipe after 'pipeInterval' amount of frames
if (frameCount % pipeInterval == 0) {
Random rng = new Random();
int pipeX = board.GetLength(0)-2;
int spaceY = rng.Next(2,board.GetLength(1)-6-spaceHeight);
for (int j = 1; j < board.GetLength(1)-1; j++)
if (j < spaceY || j > spaceY+spaceHeight)
board[pipeX, j] = '$';
}
// Handle key presses for jumping and exiting
if (Console.KeyAvailable)
switch (Console.ReadKey(true).Key) {
case ConsoleKey.Escape:
Environment.Exit(0);
break;
case ConsoleKey.Spacebar:
velY = jumpVel;
break;
}
// Handle velocity
velY -= gravity;
float avy = Math.Abs(velY);
velY = (avy > jumpVel) ? ((avy/velY)*jumpVel) : velY;
// Handle position
birdY += velY;
if (birdY < 1)
gameEnd();
if (birdY > board.GetLength(1)-2)
velY = 0;
birdY = Math.Max(birdY, 1);
birdY = Math.Min(birdY, board.GetLength(1)-2);
// Update bird position on board
int tempY = board.GetLength(1)-1-(int)Math.Round(birdY);
if (tempY != rBY) {
board[birdX, rBY] = ' ';
if (board[birdX, tempY] == '$')
gameEnd();
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++) {
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);
}
}
// End of game loop
frameCount = (frameCount+1)%(FPS*4);
Console.SetCursorPosition(board.GetLength(0)*2-1, board.GetLength(1));
Thread.Sleep(1000/FPS);
}
}
}
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;
}
gameOver = true;
// Environment.Exit(0);
}
}
}