fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <thread>
  4. #include <chrono>
  5.  
  6. const int WIDTH = 40;
  7. const int HEIGHT = 20;
  8.  
  9. struct Ball {
  10. int x = WIDTH / 2, y = HEIGHT / 2;
  11. int dx = 1, dy = -1;
  12. };
  13.  
  14. struct Paddle {
  15. int x = WIDTH / 2 - 3;
  16. const int y = HEIGHT - 1;
  17. const int width = 7;
  18. };
  19.  
  20. struct Brick {
  21. int x, y;
  22. bool alive = true;
  23. };
  24.  
  25. void draw(const Ball& ball, const Paddle& paddle, const std::vector<Brick>& bricks) {
  26. std::vector<std::string> screen(HEIGHT, std::string(WIDTH, ' '));
  27.  
  28. for (const auto& b : bricks)
  29. if (b.alive)
  30. screen[b.y][b.x] = '#';
  31.  
  32. screen[ball.y][ball.x] = 'O';
  33.  
  34. for (int i = 0; i < paddle.width; ++i)
  35. screen[paddle.y][paddle.x + i] = '=';
  36.  
  37. system("clear"); // use "cls" on Windows
  38. for (const auto& row : screen)
  39. std::cout << row << "\n";
  40. }
  41.  
  42. void updateBall(Ball& ball, Paddle& paddle, std::vector<Brick>& bricks) {
  43. ball.x += ball.dx;
  44. ball.y += ball.dy;
  45.  
  46. if (ball.x <= 0 || ball.x >= WIDTH - 1) ball.dx *= -1;
  47. if (ball.y <= 0) ball.dy *= -1;
  48.  
  49. if (ball.y == paddle.y - 1 && ball.x >= paddle.x && ball.x < paddle.x + paddle.width)
  50. ball.dy *= -1;
  51.  
  52. for (auto& b : bricks) {
  53. if (b.alive && ball.x == b.x && ball.y == b.y) {
  54. b.alive = false;
  55. ball.dy *= -1;
  56. break;
  57. }
  58. }
  59. }
  60.  
  61. int main() {
  62. Ball ball;
  63. Paddle paddle;
  64. std::vector<Brick> bricks;
  65.  
  66. for (int y = 2; y < 6; ++y)
  67. for (int x = 2; x < WIDTH - 2; x += 4)
  68. bricks.push_back({x, y});
  69.  
  70. while (true) {
  71. draw(ball, paddle, bricks);
  72. updateBall(ball, paddle, bricks);
  73.  
  74. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  75.  
  76. if (ball.y >= HEIGHT) {
  77. std::cout << "Game Over!\n";
  78. break;
  79. }
  80. }
  81.  
  82. return 0;
  83. }
Success #stdin #stdout #stderr 0.04s 5324KB
stdin
Standard input is empty
stdout
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                                        
                                        
                                        
                    O                   
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                                        
                                        
                     O                  
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                                        
                      O                 
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                       O                
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
                        O               
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #  O#   #   #     
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   O   #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #       #   #     
  #   #   #   #   #   #   #O  #   #     
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #       #   #     
  #   #   #   #   #   #   #   #   #     
                            O           
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #       #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                             O          
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #       #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                                        
                              O         
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #       #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                                        
                                        
                               O        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #       #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                                        
                                        
                                        
                                O       
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #       #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                                        
                                        
                                        
                                        
                                 O      
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #       #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                                        
                                        
                                        
                                        
                                        
                                  O     
                                        
                                        
                                        
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #       #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                   O    
                                        
                                        
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #       #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                    O   
                                        
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #       #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                     O  
                                        
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #       #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                      O 
                                        
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #       #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                       O
                                        
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #       #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                      O 
                 =======                
                                        
                                        
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #   #   #   #     
  #   #   #   #   #   #       #   #     
  #   #   #   #   #   #   #   #   #     
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                 =======             O  
Game Over!
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.