initial commit

This commit is contained in:
Baipyrus 2022-05-15 16:40:33 +02:00
commit d024ff6ca8
3 changed files with 161 additions and 0 deletions

3
.gitignore vendored Normal file
View File

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

13
FlappyBird.csproj Normal file
View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishReadyToRun>true</PublishReadyToRun>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
</PropertyGroup>
</Project>

145
Program.cs Normal file
View File

@ -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);
}
}
}