replayability

This commit is contained in:
Baipyrus 2022-05-17 11:45:58 +02:00
parent d024ff6ca8
commit 79fa7898b7

View File

@ -12,134 +12,145 @@ namespace FlappyBird {
static int FPS = 60;
// Game variables
static char[,] board = new char[82, 37];
static char[,] board;
static int birdX = 5, score = 0;
static float birdY = 20f, velY = 0f;
static bool gameOver = false;
static bool gameOver, startOver = true;
static void Main() {
// Start console window maximized
Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);
while (startOver) {
gameOver = false;
board = new char[82, 37];
// Fill the 'board' Array with blank spaces
for (int i = 0; i < board.GetLength(0); i++)
// 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++) {
bool xC = i%(board.GetLength(0)-1)==0;
bool yC = j%(board.GetLength(1)-1)==0;
board[i, j] = (xC||yC)?'#':' ';
for (int i = 0; i < board.GetLength(0); i++) {
string pipeCon = (i < board.GetLength(0)-1) ? "|" : "";
Console.Write($"{board[i, j]}{pipeCon}");
}
Console.WriteLine();
}
// 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[,];
// 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;
// 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);
// 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:
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;
}
// End of game loop
frameCount = (frameCount+1)%(FPS*4);
Console.SetCursorPosition(board.GetLength(0)*2-1, board.GetLength(1));
Thread.Sleep(1000/FPS);
// 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();
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 = 20f;
velY = 0f;
score = 0;
}
gameOver = true;
Environment.Exit(0);
// Environment.Exit(0);
}
}
}