fork download
  1. #include <iostream>
  2. #include <conio.h> // For _kbhit() and _getch() on Windows
  3. #include <windows.h> // For Sleep()
  4. #include <cstdlib>
  5. #include <ctime>
  6.  
  7. using namespace std;
  8.  
  9. const int WIDTH = 40;
  10. const int HEIGHT = 20;
  11. const char BIRD = 'O';
  12. const char PIPE = '|';
  13. const char SPACE = ' ';
  14. const char GROUND = '_';
  15.  
  16. int birdY;
  17. int score;
  18. bool gameOver;
  19. int pipeX, gapY;
  20. const int GAP_HEIGHT = 6;
  21.  
  22. // Draw the game screen
  23. void draw() {
  24. system("cls"); // Clear console
  25.  
  26. for (int y = 0; y < HEIGHT; y++) {
  27. for (int x = 0; x < WIDTH; x++) {
  28. if (y == birdY && x == 5) {
  29. cout << BIRD; // Bird position
  30. }
  31. else if (x == pipeX && (y < gapY || y > gapY + GAP_HEIGHT)) {
  32. cout << PIPE; // Pipe
  33. }
  34. else if (y == HEIGHT - 1) {
  35. cout << GROUND; // Ground
  36. }
  37. else {
  38. cout << SPACE;
  39. }
  40. }
  41. cout << "\n";
  42. }
  43. cout << "Score: " << score << "\n";
  44. }
  45.  
  46. // Update game logic
  47. void update() {
  48. // Move pipe left
  49. pipeX--;
  50.  
  51. // If pipe goes off screen, reset it
  52. if (pipeX < 0) {
  53. pipeX = WIDTH - 1;
  54. gapY = rand() % (HEIGHT - GAP_HEIGHT - 1);
  55. score++;
  56. }
  57.  
  58. // Gravity
  59. birdY++;
  60.  
  61. // Collision detection
  62. if (birdY >= HEIGHT - 1 || birdY < 0) {
  63. gameOver = true;
  64. }
  65. if (pipeX == 5 && (birdY < gapY || birdY > gapY + GAP_HEIGHT)) {
  66. gameOver = true;
  67. }
  68. }
  69.  
  70. // Handle user input
  71. void input() {
  72. if (_kbhit()) {
  73. char ch = _getch();
  74. if (ch == ' ' || ch == 'w') {
  75. birdY -= 2; // Jump
  76. }
  77. else if (ch == 'q') {
  78. gameOver = true; // Quit
  79. }
  80. }
  81. }
  82.  
  83. int main() {
  84. srand((unsigned)time(0));
  85.  
  86. birdY = HEIGHT / 2;
  87. score = 0;
  88. gameOver = false;
  89. pipeX = WIDTH - 1;
  90. gapY = rand() % (HEIGHT - GAP_HEIGHT - 1);
  91.  
  92. while (!gameOver) {
  93. draw();
  94. input();
  95. update();
  96. Sleep(100); // Control game speed
  97. }
  98.  
  99. cout << "\nGame Over! Final Score: " << score << "\n";
  100. return 0;
  101. }
Compilation error #stdin compilation error #stdout 0s 5320KB
stdin
Standard input is empty
compilation info
prog.cpp:2:10: fatal error: conio.h: No such file or directory
 #include <conio.h>   // For _kbhit() and _getch() on Windows
          ^~~~~~~~~
compilation terminated.
stdout
Standard output is empty