fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. const int WIDTH = 20;
  8. const int HEIGHT = 10;
  9.  
  10. struct GameObject {
  11. int x, y;
  12. char symbol;
  13. };
  14.  
  15. class Game {
  16. public:
  17. char grid[HEIGHT][WIDTH];
  18. GameObject player;
  19. GameObject people[5];
  20. GameObject meteors[3];
  21. int score;
  22. bool gameOver;
  23.  
  24. Game() {
  25. score = 0;
  26. gameOver = false;
  27. player = {WIDTH / 2, HEIGHT - 2, 'S'};
  28. srand(time(0));
  29.  
  30. // Initialize grid
  31. for (int i = 0; i < HEIGHT; i++) {
  32. for (int j = 0; j < WIDTH; j++) {
  33. grid[i][j] = '.';
  34. }
  35. }
  36.  
  37. // Place people (P) at random positions
  38. for (int i = 0; i < 5; i++) {
  39. people[i] = {rand() % WIDTH, rand() % (HEIGHT - 1), 'P'};
  40. }
  41.  
  42. // Place meteors (O) at random positions
  43. for (int i = 0; i < 3; i++) {
  44. meteors[i] = {rand() % WIDTH, rand() % (HEIGHT - 1), 'O'};
  45. }
  46. }
  47.  
  48. void draw() {
  49. // Clear grid
  50. for (int i = 0; i < HEIGHT; i++) {
  51. for (int j = 0; j < WIDTH; j++) {
  52. grid[i][j] = '.';
  53. }
  54. }
  55.  
  56. // Draw player
  57. grid[player.y][player.x] = player.symbol;
  58.  
  59. // Draw people
  60. for (int i = 0; i < 5; i++) {
  61. if (people[i].x >= 0 && people[i].x < WIDTH && people[i].y >= 0 && people[i].y < HEIGHT) {
  62. grid[people[i].y][people[i].x] = people[i].symbol;
  63. }
  64. }
  65.  
  66. // Draw meteors
  67. for (int i = 0; i < 3; i++) {
  68. if (meteors[i].x >= 0 && meteors[i].x < WIDTH && meteors[i].y >= 0 && meteors[i].y < HEIGHT) {
  69. grid[meteors[i].y][meteors[i].x] = meteors[i].symbol;
  70. }
  71. }
  72.  
  73. // Print grid to console
  74. system("clear"); // For Linux/Unix, use system("cls") on Windows
  75. for (int i = 0; i < HEIGHT; i++) {
  76. for (int j = 0; j < WIDTH; j++) {
  77. cout << grid[i][j];
  78. }
  79. cout << endl;
  80. }
  81.  
  82. // Display score
  83. cout << "Score: " << score << endl;
  84. }
  85.  
  86. void movePlayer(char direction) {
  87. if (direction == 'w' && player.y > 0) player.y--;
  88. if (direction == 's' && player.y < HEIGHT - 1) player.y++;
  89. if (direction == 'a' && player.x > 0) player.x--;
  90. if (direction == 'd' && player.x < WIDTH - 1) player.x++;
  91. }
  92.  
  93. void checkCollisions() {
  94. // Check if player collects a person
  95. for (int i = 0; i < 5; i++) {
  96. if (player.x == people[i].x && player.y == people[i].y) {
  97. people[i].x = -1; // Remove person
  98. score += 10; // Increase score
  99. }
  100. }
  101.  
  102. // Check if player hits a meteor
  103. for (int i = 0; i < 3; i++) {
  104. if (player.x == meteors[i].x && player.y == meteors[i].y) {
  105. gameOver = true; // End game on collision
  106. }
  107. }
  108. }
  109.  
  110. void update() {
  111. // Move meteors down
  112. for (int i = 0; i < 3; i++) {
  113. meteors[i].y++;
  114. if (meteors[i].y >= HEIGHT) {
  115. meteors[i].y = 0; // Reset meteor to top if it goes off screen
  116. meteors[i].x = rand() % WIDTH;
  117. }
  118. }
  119.  
  120. checkCollisions();
  121. }
  122.  
  123. void play() {
  124. while (!gameOver) {
  125. draw();
  126. cout << "Move (WASD): ";
  127. char input;
  128. cin >> input;
  129.  
  130. movePlayer(input);
  131. update();
  132. }
  133.  
  134. // End game message
  135. draw();
  136. cout << "Game Over! Final Score: " << score << endl;
  137. }
  138. };
  139.  
  140. int main() {
  141. Game game;
  142. game.play();
  143. return 0;
  144. }
Success #stdin #stdout #stderr 0.07s 5280KB
stdin
Standard input is empty
stdout
....................
....................
....................
...............P....
....P...........O...
....................
....O......P.O......
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...............
................O...
...........P........
....O.P...P..O......
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...............
....................
...........P....O...
......P...P.........
....O.....S..O......
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...............
....................
...........P........
......P...P.....O...
..........S.........
....O........O......
Score: 0
Move (WASD): ....O............O..
....................
....................
...............P....
....P...............
....................
...........P........
......P...P.........
..........S.....O...
....................
Score: 0
Move (WASD): ....................
....O............O..
....................
...............P....
....P...............
....................
...........P........
......P...P.........
..........S.........
................O...
Score: 0
Move (WASD): ..O.................
....................
....O............O..
...............P....
....P...............
....................
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
..O.................
....................
....O..........P.O..
....P...............
....................
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
..O.................
...............P....
....O............O..
....................
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
..O............P....
....P...............
....O............O..
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
..O.P...............
....................
....O......P.....O..
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...............
..O.................
...........P........
....O.P...P......O..
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...............
....................
..O........P........
......P...P.........
....O.....S......O..
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...............
....................
...........P........
..O...P...P.........
..........S.........
....O............O..
Score: 0
Move (WASD): .........O.O........
....................
....................
...............P....
....P...............
....................
...........P........
......P...P.........
..O.......S.........
....................
Score: 0
Move (WASD): ....................
.........O.O........
....................
...............P....
....P...............
....................
...........P........
......P...P.........
..........S.........
..O.................
Score: 0
Move (WASD): ...............O....
....................
.........O.O........
...............P....
....P...............
....................
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
...............O....
....................
.........O.O...P....
....P...............
....................
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
...............O....
...............P....
....P....O.O........
....................
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............O....
....P...............
.........O.O........
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P..........O....
....................
.........O.O........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...............
...............O....
...........P........
......P..OPO........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...............
....................
...........P...O....
......P...P.........
.........OSO........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...............
....................
...........P........
......P...P....O....
..........S.........
.........O.O........
Score: 0
Move (WASD): ....O.O.............
....................
....................
...............P....
....P...............
....................
...........P........
......P...P.........
..........S....O....
....................
Score: 0
Move (WASD): ....................
....O.O.............
....................
...............P....
....P...............
....................
...........P........
......P...P.........
..........S.........
...............O....
Score: 0
Move (WASD): ................O...
....................
....O.O.............
...............P....
....P...............
....................
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
................O...
....................
....O.O........P....
....P...............
....................
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
................O...
...............P....
....O.O.............
....................
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............PO...
....P...............
....O.O.............
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...........O...
....................
....O.O....P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...............
................O...
...........P........
....O.O...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...............
....................
...........P....O...
......P...P.........
....O.O...S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...............
....................
...........P........
......P...P.....O...
..........S.........
....O.O.............
Score: 0
Move (WASD): ....O.........O.....
....................
....................
...............P....
....P...............
....................
...........P........
......P...P.........
..........S.....O...
....................
Score: 0
Move (WASD): ....................
....O.........O.....
....................
...............P....
....P...............
....................
...........P........
......P...P.........
..........S.........
................O...
Score: 0
Move (WASD): ........O...........
....................
....O.........O.....
...............P....
....P...............
....................
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
........O...........
....................
....O.........OP....
....P...............
....................
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
........O...........
...............P....
....O.........O.....
....................
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
........O......P....
....P...............
....O.........O.....
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...O...........
....................
....O......P..O.....
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...............
........O...........
...........P........
....O.P...P...O.....
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...............
....................
........O..P........
......P...P.........
....O.....S...O.....
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...............
....................
...........P........
......P.O.P.........
..........S.........
....O.........O.....
Score: 0
Move (WASD): .........OO.........
....................
....................
...............P....
....P...............
....................
...........P........
......P...P.........
........O.S.........
....................
Score: 0
Move (WASD): ....................
.........OO.........
....................
...............P....
....P...............
....................
...........P........
......P...P.........
..........S.........
........O...........
Score: 0
Move (WASD): .....O..............
....................
.........OO.........
...............P....
....P...............
....................
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
.....O..............
....................
.........OO....P....
....P...............
....................
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
.....O..............
...............P....
....P....OO.........
....................
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
.....O.........P....
....P...............
.........OO.........
...........P........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....PO..............
....................
.........OOP........
......P...P.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...............
.....O..............
...........P........
......P..OO.........
..........S.........
....................
Score: 0
Move (WASD): ....................
....................
....................
...............P....
....P...............
....................
.....O.....P........
......P...P.........
.........OO.........
....................
Score: 0
Game Over! Final Score: 0
stderr
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.
TERM environment variable not set.