From d024ff6ca823b8ca236496184e6620ec65dfd240 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sun, 15 May 2022 16:40:33 +0200 Subject: [PATCH] initial commit --- .gitignore | 3 + FlappyBird.csproj | 13 +++++ Program.cs | 145 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 .gitignore create mode 100644 FlappyBird.csproj create mode 100644 Program.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2a16d1a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +bin/* +!bin/Debug/net5.0/win-x64/publish/* +obj/* \ No newline at end of file diff --git a/FlappyBird.csproj b/FlappyBird.csproj new file mode 100644 index 0000000..ebf61da --- /dev/null +++ b/FlappyBird.csproj @@ -0,0 +1,13 @@ + + + + Exe + net5.0 + true + true + win-x64 + true + true + + + diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..c31228a --- /dev/null +++ b/Program.cs @@ -0,0 +1,145 @@ +using System; +using System.Threading; + +namespace FlappyBird { + class Program { + // Constants + static string scoreText = "Your score is: "; + static float gravity = 0.15f; + static float jumpVel = 1.75f; + static int pipeInterval = 60; + static int spaceHeight = 8; + static int FPS = 60; + + // Game variables + static char[,] board = new char[82, 37]; + static int birdX = 5, score = 0; + static float birdY = 20f, velY = 0f; + static bool gameOver = false; + + static void Main() { + // Start console window maximized + Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight); + + // 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) ? "|" : ""; + 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(1,board.GetLength(1)-2-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: + gameOver = true; + 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 i = 1; i < board.GetLength(0)-1; i++) + for (int j = 1; j < board.GetLength(1)-1; j++) { + 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.WriteLine("You lost!"); + Console.ReadLine(); + gameOver = true; + Environment.Exit(0); + } + } +} \ No newline at end of file