mirror of
https://github.com/thepigeongenerator/breakout_clone.git
synced 2025-12-16 22:05:45 +01:00
add brick breaking
This commit is contained in:
@@ -4,3 +4,5 @@ I wanted a project to learn C and the SDL framework in, and settled upon this ga
|
||||
### TODO:
|
||||
- make a better input system (one that doesn't crash the game)
|
||||
- make beeping sound effects
|
||||
- make the game freeze when you lost instead of closing
|
||||
- fix constants in level colision detection
|
||||
|
||||
32
src/level.c
32
src/level.c
@@ -42,6 +42,7 @@ void level_init(Level* level) {
|
||||
brick->colour = colours[y];
|
||||
brick->pos.x = (x * brick_width) + level_padding_left;
|
||||
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;
|
||||
}
|
||||
|
||||
// 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
|
||||
if ((ball->pos.y + ball->size) > SCREEN_HEIGHT) {
|
||||
level->stop = true;
|
||||
|
||||
@@ -20,6 +20,7 @@ typedef struct {
|
||||
typedef struct {
|
||||
Vector2 pos;
|
||||
Colour colour;
|
||||
bool destroyed;
|
||||
} Brick;
|
||||
|
||||
typedef struct {
|
||||
|
||||
@@ -54,7 +54,7 @@ void renderer_update(RenderData* render_data) {
|
||||
for (int y = 0; y < BRICK_ROWS; y++) {
|
||||
Brick* brick = &level->bricks[x][y];
|
||||
|
||||
if (brick == NULL) {
|
||||
if (brick->destroyed == true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user