ulfben

brick field generation

Sep 29th, 2025
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 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. std::vector<Brick> bricks;
  10.   const int cols = 12;
  11.   const int rows = 6;
  12.   const float marginX = 16.0f;
  13.   const float marginY = 60.0f;
  14.   const float gap = 4.0f;
  15.   const float bw = (WIDTH - marginX * 2 - gap * (cols - 1)) / cols;
  16.   const float bh = 20.0f;
  17.  
  18.   for (int r = 0; r < rows; ++r) {
  19.     for (int c = 0; c < cols; ++c) {
  20.       float x = marginX + c * (bw + gap);
  21.       float y = marginY + r * (bh + gap);
  22.       Color color = Color{(unsigned char)(180 - r * 20),
  23.                           (unsigned char)(120 + r * 20), 255, 255};
  24.       bricks.push_back(Brick{Rectangle{x, y, bw, bh}, color, 1});
  25.     }
  26.   }
  27.  
  28.  
  29. //OR:
  30. int index = 0;
  31. std::generate_n(std::back_inserter(bricks), rows * cols, [&]() {
  32.     int r = index / cols;   // row
  33.     int c = index % cols;   // col
  34.     index++;
  35.  
  36.     float x = marginX + c * (bw + gap);
  37.     float y = marginY + r * (bh + gap);
  38.  
  39.     Color color{ static_cast<unsigned char>(180 - r * 20),
  40.                  static_cast<unsigned char>(120 + r * 20),
  41.                  255, 255 };
  42.  
  43.     return Brick{ Rectangle{x, y, bw, bh}, color, 1 };
  44. });
Advertisement
Add Comment
Please, Sign In to add comment