struct Brick {
Rectangle rect{};
Color color{WHITE};
int hits{1}; // simple durability if you want to extend later
bool alive() const { return hits > 0; }
void render() const { DrawRectangleRec(rect, color); }
};
// Paddle collision
if(CheckCollisionRecs(ball.rect(), pad.rect)){
Axis a = getSeparationAxis(ball.rect(), pad.rect);
ball.bounce(a);
// Add a "twist" based on impact position
float hit = (ball.pos.x - pad.center_x()) / (pad.rect.width * 0.5f);
ball.vel.x += hit * 120.0f;
// Nudge out of the paddle to avoid sticking
if(a == Axis::Y){
ball.pos.y = pad.rect.y - ball.r - 0.01f;
}
}
std::vector<Brick> bricks;
const int cols = 12;
const int rows = 6;
const float marginX = 16.0f;
const float marginY = 60.0f;
const float gap = 4.0f;
const float bw = (WIDTH - marginX * 2 - gap * (cols - 1)) / cols;
const float bh = 20.0f;
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c) {
float x = marginX + c * (bw + gap);
float y = marginY + r * (bh + gap);
Color color = Color{(unsigned char)(180 - r * 20),
(unsigned char)(120 + r * 20), 255, 255};
bricks.push_back(Brick{Rectangle{x, y, bw, bh}, color, 1});
}
}
//OR:
int index = 0;
std::generate_n(std::back_inserter(bricks), rows * cols, [&]() {
int r = index / cols; // row
int c = index % cols; // col
index++;
float x = marginX + c * (bw + gap);
float y = marginY + r * (bh + gap);
Color color{ static_cast<unsigned char>(180 - r * 20),
static_cast<unsigned char>(120 + r * 20),
255, 255 };
return Brick{ Rectangle{x, y, bw, bh}, color, 1 };
});
Comments
0 B
|0 👍
/0 👎
0 B
|👍
/👎
0 B
|👍
/👎