ulfben

brick field generation

Sep 29th, 2025 (edited)
2,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. struct Brick {
  2.   Rectangle rect{};
  3.   Color color{WHITE};
  4.   int hits{1}; // simple durability if you want to extend later
  5.   bool alive() const { return hits > 0; }
  6.   void render() const { DrawRectangleRec(rect, color); }
  7. };
  8.  
  9. // Paddle collision
  10. if(CheckCollisionRecs(ball.rect(), pad.rect)){
  11.     Axis a = getSeparationAxis(ball.rect(), pad.rect);
  12.     ball.bounce(a);
  13.     // Add a "twist" based on impact position
  14.     float hit = (ball.pos.x - pad.center_x()) / (pad.rect.width * 0.5f);
  15.     ball.vel.x += hit * 120.0f;
  16.     // Nudge out of the paddle to avoid sticking
  17.     if(a == Axis::Y){
  18.         ball.pos.y = pad.rect.y - ball.r - 0.01f;
  19.     }
  20. }
  21.  
  22.  
  23. std::vector<Brick> bricks;
  24.   const int cols = 12;
  25.   const int rows = 6;
  26.   const float marginX = 16.0f;
  27.   const float marginY = 60.0f;
  28.   const float gap = 4.0f;
  29.   const float bw = (WIDTH - marginX * 2 - gap * (cols - 1)) / cols;
  30.   const float bh = 20.0f;
  31.  
  32.   for (int r = 0; r < rows; ++r) {
  33.     for (int c = 0; c < cols; ++c) {
  34.       float x = marginX + c * (bw + gap);
  35.       float y = marginY + r * (bh + gap);
  36.       Color color = Color{(unsigned char)(180 - r * 20),
  37.                           (unsigned char)(120 + r * 20), 255, 255};
  38.       bricks.push_back(Brick{Rectangle{x, y, bw, bh}, color, 1});
  39.     }
  40.   }
  41.  
  42. //OR:
  43. int index = 0;
  44. std::generate_n(std::back_inserter(bricks), rows * cols, [&]() {
  45.     int r = index / cols;   // row
  46.     int c = index % cols;   // col
  47.     index++;
  48.  
  49.     float x = marginX + c * (bw + gap);
  50.     float y = marginY + r * (bh + gap);
  51.  
  52.     Color color{ static_cast<unsigned char>(180 - r * 20),
  53.                  static_cast<unsigned char>(120 + r * 20),
  54.                  255, 255 };
  55.  
  56.     return Brick{ Rectangle{x, y, bw, bh}, color, 1 };
  57. });
Advertisement