#include <iostream>
#include <conio.h> // For _kbhit() and _getch() on Windows
#include <windows.h> // For Sleep()
#include <cstdlib>
#include <ctime>
using namespace std;
const int WIDTH = 40;
const int HEIGHT = 20;
const char BIRD = 'O';
const char PIPE = '|';
const char SPACE = ' ';
const char GROUND = '_';
int birdY;
int score;
bool gameOver;
int pipeX, gapY;
const int GAP_HEIGHT = 6;
// Draw the game screen
void draw() {
system("cls"); // Clear console
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
if (y == birdY && x == 5) {
cout << BIRD; // Bird position
}
else if (x == pipeX && (y < gapY || y > gapY + GAP_HEIGHT)) {
cout << PIPE; // Pipe
}
else if (y == HEIGHT - 1) {
cout << GROUND; // Ground
}
else {
cout << SPACE;
}
}
cout << "\n";
}
cout << "Score: " << score << "\n";
}
// Update game logic
void update() {
// Move pipe left
pipeX--;
// If pipe goes off screen, reset it
if (pipeX < 0) {
pipeX = WIDTH - 1;
gapY = rand() % (HEIGHT - GAP_HEIGHT - 1);
score++;
}
// Gravity
birdY++;
// Collision detection
if (birdY >= HEIGHT - 1 || birdY < 0) {
gameOver = true;
}
if (pipeX == 5 && (birdY < gapY || birdY > gapY + GAP_HEIGHT)) {
gameOver = true;
}
}
// Handle user input
void input() {
if (_kbhit()) {
char ch = _getch();
if (ch == ' ' || ch == 'w') {
birdY -= 2; // Jump
}
else if (ch == 'q') {
gameOver = true; // Quit
}
}
}
int main() {
srand((unsigned)time(0));
birdY = HEIGHT / 2;
score = 0;
gameOver = false;
pipeX = WIDTH - 1;
gapY = rand() % (HEIGHT - GAP_HEIGHT - 1);
while (!gameOver) {
draw();
input();
update();
Sleep(100); // Control game speed
}
cout << "\nGame Over! Final Score: " << score << "\n";
return 0;
}