add brick breaking

This commit is contained in:
Quinn
2024-08-14 14:37:56 +02:00
parent c8baf5dcc3
commit 315eaf1636
4 changed files with 36 additions and 1 deletions

View File

@@ -4,3 +4,5 @@ I wanted a project to learn C and the SDL framework in, and settled upon this ga
### TODO: ### TODO:
- make a better input system (one that doesn't crash the game) - make a better input system (one that doesn't crash the game)
- make beeping sound effects - make beeping sound effects
- make the game freeze when you lost instead of closing
- fix constants in level colision detection

View File

@@ -42,6 +42,7 @@ void level_init(Level* level) {
brick->colour = colours[y]; brick->colour = colours[y];
brick->pos.x = (x * brick_width) + level_padding_left; brick->pos.x = (x * brick_width) + level_padding_left;
brick->pos.y = (y * brick_height) + BRICK_PADDING_TOP; brick->pos.y = (y * brick_height) + BRICK_PADDING_TOP;
brick->destroyed = false;
} }
} }
} }
@@ -100,6 +101,37 @@ void level_update(Level* level, bool* keys) {
ball->direction.y = -SDL_cosf(angle) * BALL_SPEED; ball->direction.y = -SDL_cosf(angle) * BALL_SPEED;
} }
// check brick collisions
for (int x = 0; x < BRICK_COLUMNS; x++) {
for (int y = 0; y < BRICK_ROWS; y++) {
const Brick* brick = &level->bricks[x][y];
if (brick->destroyed == true) {
continue;
}
const float max_brick_x = brick->pos.x + BRICK_WIDTH;
const float max_brick_y = brick->pos.y + BRICK_HEIGHT;
if (ball->pos.x < max_brick_x && (ball->pos.x + ball->size) > brick->pos.x &&
ball->pos.y < max_brick_y && (ball->pos.y + ball->size) > brick->pos.y) {
// manage ball bounce direction
if (ball->direction.x > ball->direction.y) {
ball->direction.x *= -1;
}
if (ball->direction.x < ball->direction.y) {
ball->direction.y *= -1;
}
else {
ball->direction.x *= -1;
ball->direction.y *= -1;
}
level->bricks[x][y].destroyed = true;
}
}
}
// check lose condition // check lose condition
if ((ball->pos.y + ball->size) > SCREEN_HEIGHT) { if ((ball->pos.y + ball->size) > SCREEN_HEIGHT) {
level->stop = true; level->stop = true;

View File

@@ -20,6 +20,7 @@ typedef struct {
typedef struct { typedef struct {
Vector2 pos; Vector2 pos;
Colour colour; Colour colour;
bool destroyed;
} Brick; } Brick;
typedef struct { typedef struct {

View File

@@ -54,7 +54,7 @@ void renderer_update(RenderData* render_data) {
for (int y = 0; y < BRICK_ROWS; y++) { for (int y = 0; y < BRICK_ROWS; y++) {
Brick* brick = &level->bricks[x][y]; Brick* brick = &level->bricks[x][y];
if (brick == NULL) { if (brick->destroyed == true) {
continue; continue;
} }