Advertisement
onorato

Untitled

Feb 22nd, 2025
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.52 KB | Source Code | 0 0
  1. // Prerequisites:
  2. //    install SDL2. On MacOS: brew install sdl2
  3. // g++ breakout.cpp -o breakout -lSDL2 -std=c++17
  4.  
  5. #include <iostream>
  6. #include <vector>
  7.  
  8. #include <SDL2/SDL.h>
  9.  
  10. // Screen dimensions
  11. const int SCREEN_WIDTH = 640;
  12. const int SCREEN_HEIGHT = 480;
  13.  
  14. // Structure for a brick
  15. struct Brick {
  16.     SDL_Rect rect;
  17.     bool destroyed;
  18. };
  19.  
  20. int main(int argc, char* argv[]) {
  21.     // Initialize SDL video subsystem
  22.     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  23.         std::cerr << "SDL could not initialize! SDL_Error: "
  24.                   << SDL_GetError() << std::endl;
  25.         return 1;
  26.     }
  27.    
  28.     // Create the window
  29.     SDL_Window* window = SDL_CreateWindow("Breakout",
  30.                                           SDL_WINDOWPOS_CENTERED,
  31.                                           SDL_WINDOWPOS_CENTERED,
  32.                                           SCREEN_WIDTH,
  33.                                           SCREEN_HEIGHT,
  34.                                           SDL_WINDOW_SHOWN);
  35.     if (!window) {
  36.         std::cerr << "Window could not be created! SDL_Error: "
  37.                   << SDL_GetError() << std::endl;
  38.         SDL_Quit();
  39.         return 1;
  40.     }
  41.    
  42.     // Create renderer
  43.     SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
  44.     if (!renderer) {
  45.         std::cerr << "Renderer could not be created! SDL_Error: "
  46.                   << SDL_GetError() << std::endl;
  47.         SDL_DestroyWindow(window);
  48.         SDL_Quit();
  49.         return 1;
  50.     }
  51.    
  52.     // Define the paddle
  53.     SDL_Rect paddle;
  54.     paddle.w = 80;
  55.     paddle.h = 20;
  56.     paddle.x = (SCREEN_WIDTH - paddle.w) / 2;
  57.     paddle.y = SCREEN_HEIGHT - paddle.h - 10;
  58.    
  59.     // Define the ball
  60.     SDL_Rect ball;
  61.     ball.w = 10;
  62.     ball.h = 10;
  63.     ball.x = SCREEN_WIDTH / 2 - ball.w / 2;
  64.     ball.y = SCREEN_HEIGHT / 2 - ball.h / 2;
  65.    
  66.     float ballVelX = 4.0f;
  67.     float ballVelY = -4.0f;
  68.    
  69.     // Create bricks: 5 rows x 10 columns
  70.     std::vector<Brick> bricks;
  71.     const int brickRows = 5;
  72.     const int brickCols = 10;
  73.     int brickWidth = SCREEN_WIDTH / brickCols;
  74.     int brickHeight = 20;
  75.     for (int row = 0; row < brickRows; row++) {
  76.         for (int col = 0; col < brickCols; col++) {
  77.             Brick brick;
  78.             brick.rect.x = col * brickWidth + 1; // +1 for a small gap
  79.             brick.rect.y = row * brickHeight + 50; // offset from top
  80.             brick.rect.w = brickWidth - 2;
  81.             brick.rect.h = brickHeight - 2;
  82.             brick.destroyed = false;
  83.             bricks.push_back(brick);
  84.         }
  85.     }
  86.    
  87.     bool quit = false;
  88.     SDL_Event e;
  89.    
  90.     // Main game loop
  91.     while (!quit) {
  92.         // Handle events
  93.         while (SDL_PollEvent(&e) != 0) {
  94.             if (e.type == SDL_QUIT)
  95.                 quit = true;
  96.         }
  97.        
  98.         // Handle paddle movement using keyboard state
  99.         const Uint8* keystate = SDL_GetKeyboardState(NULL);
  100.         if (keystate[SDL_SCANCODE_LEFT]) {
  101.             paddle.x -= 6;
  102.             if (paddle.x < 0)
  103.                 paddle.x = 0;
  104.         }
  105.         if (keystate[SDL_SCANCODE_RIGHT]) {
  106.             paddle.x += 6;
  107.             if (paddle.x + paddle.w > SCREEN_WIDTH)
  108.                 paddle.x = SCREEN_WIDTH - paddle.w;
  109.         }
  110.        
  111.         // Update ball position
  112.         ball.x += static_cast<int>(ballVelX);
  113.         ball.y += static_cast<int>(ballVelY);
  114.        
  115.         // Bounce off left and right walls
  116.         if (ball.x <= 0 || ball.x + ball.w >= SCREEN_WIDTH) {
  117.             ballVelX = -ballVelX;
  118.         }
  119.         // Bounce off the top wall
  120.         if (ball.y <= 0) {
  121.             ballVelY = -ballVelY;
  122.         }
  123.         // If the ball goes below the paddle, reset it (loss condition)
  124.         if (ball.y + ball.h >= SCREEN_HEIGHT) {
  125.             ball.x = SCREEN_WIDTH / 2 - ball.w / 2;
  126.             ball.y = SCREEN_HEIGHT / 2 - ball.h / 2;
  127.             ballVelY = -4.0f;
  128.         }
  129.        
  130.         // Check collision with paddle
  131.         if (SDL_HasIntersection(&ball, &paddle)) {
  132.             ballVelY = -ballVelY;
  133.             // Change horizontal velocity based on where the ball hit the paddle
  134.             int paddleCenter = paddle.x + paddle.w / 2;
  135.             int ballCenter = ball.x + ball.w / 2;
  136.             ballVelX = (ballCenter - paddleCenter) / 10.0f;
  137.         }
  138.        
  139.         // Check collision with bricks
  140.         for (auto &brick : bricks) {
  141.             if (!brick.destroyed && SDL_HasIntersection(&ball, &brick.rect)) {
  142.                 brick.destroyed = true;
  143.                 ballVelY = -ballVelY;
  144.                 break;
  145.             }
  146.         }
  147.        
  148.         // Rendering section
  149.         SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // Black background
  150.         SDL_RenderClear(renderer);
  151.        
  152.         // Draw paddle (white)
  153.         SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  154.         SDL_RenderFillRect(renderer, &paddle);
  155.        
  156.         // Draw ball (white)
  157.         SDL_RenderFillRect(renderer, &ball);
  158.        
  159.         // Draw bricks (red)
  160.         for (const auto &brick : bricks) {
  161.             if (!brick.destroyed) {
  162.                 SDL_SetRenderDrawColor(renderer, 200, 0, 0, 255);
  163.                 SDL_RenderFillRect(renderer, &brick.rect);
  164.             }
  165.         }
  166.        
  167.         SDL_RenderPresent(renderer);
  168.         SDL_Delay(16); // Roughly 60 frames per second
  169.     }
  170.    
  171.     // Clean up resources
  172.     SDL_DestroyRenderer(renderer);
  173.     SDL_DestroyWindow(window);
  174.     SDL_Quit();
  175.    
  176.     return 0;
  177. }
  178.  
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement