mirror of
https://github.com/thepigeongenerator/breakout_clone.git
synced 2025-12-16 22:05:45 +01:00
make ball launching
This commit is contained in:
27
src/level.c
27
src/level.c
@@ -10,17 +10,17 @@
|
|||||||
void level_init(Level* level) {
|
void level_init(Level* level) {
|
||||||
level->stop = false;
|
level->stop = false;
|
||||||
|
|
||||||
// initialize ball
|
|
||||||
level->ball.pos.x = (SCREEN_WIDTH / 2) - (BALL_SIZE_DEFAULT / 2);
|
|
||||||
level->ball.pos.y = (SCREEN_HEIGHT / 2) - (BALL_SIZE_DEFAULT / 2);
|
|
||||||
level->ball.size = BALL_SIZE_DEFAULT;
|
|
||||||
level->ball.direction = (Vector2){ 0, BALL_SPEED };
|
|
||||||
|
|
||||||
// initialize bouncer
|
// initialize bouncer
|
||||||
level->bouncer.pos.x = (SCREEN_WIDTH / 2) - (BOUNCER_WIDTH_DEFAULT / 2);
|
level->bouncer.pos.x = (SCREEN_WIDTH / 2) - (BOUNCER_WIDTH_DEFAULT / 2);
|
||||||
level->bouncer.pos.y = SCREEN_HEIGHT - (BOUNCER_HEIGHT * 2);
|
level->bouncer.pos.y = SCREEN_HEIGHT - (BOUNCER_HEIGHT * 2);
|
||||||
level->bouncer.width = BOUNCER_WIDTH_DEFAULT;
|
level->bouncer.width = BOUNCER_WIDTH_DEFAULT;
|
||||||
|
|
||||||
|
// initialize ball
|
||||||
|
level->ball.pos.x = (SCREEN_WIDTH / 2) - (BALL_SIZE_DEFAULT / 2);
|
||||||
|
level->ball.pos.y = level->bouncer.pos.y - BOUNCER_HEIGHT - BALL_SIZE_DEFAULT;
|
||||||
|
level->ball.size = BALL_SIZE_DEFAULT;
|
||||||
|
level->ball.direction = (Vector2){ 0, -BALL_SPEED };
|
||||||
|
|
||||||
// initialize bricks
|
// initialize bricks
|
||||||
// define the colours of the brick rows
|
// define the colours of the brick rows
|
||||||
const Colour colours[BRICK_COLUMNS] = {
|
const Colour colours[BRICK_COLUMNS] = {
|
||||||
@@ -51,21 +51,28 @@ void level_init(Level* level) {
|
|||||||
static void update_player(Bouncer* bouncer, Ball* ball, const Uint8* keys) {
|
static void update_player(Bouncer* bouncer, Ball* ball, const Uint8* keys) {
|
||||||
// if move bouncer LEFT
|
// if move bouncer LEFT
|
||||||
if (keys[SDL_SCANCODE_A] || keys[SDL_SCANCODE_LEFT]) {
|
if (keys[SDL_SCANCODE_A] || keys[SDL_SCANCODE_LEFT]) {
|
||||||
ball->moving = true;
|
|
||||||
|
|
||||||
if (bouncer->pos.x < 0 == false) {
|
if (bouncer->pos.x < 0 == false) {
|
||||||
bouncer->pos.x -= BOUNCER_SPEED;
|
bouncer->pos.x -= BOUNCER_SPEED;
|
||||||
|
|
||||||
|
if (ball->moving == false)
|
||||||
|
ball->pos.x -= BOUNCER_SPEED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if move bouncer RIGHT
|
// if move bouncer RIGHT
|
||||||
if (keys[SDL_SCANCODE_D] || keys[SDL_SCANCODE_RIGHT]) {
|
if (keys[SDL_SCANCODE_D] || keys[SDL_SCANCODE_RIGHT]) {
|
||||||
ball->moving = true;
|
|
||||||
|
|
||||||
if ((bouncer->pos.x + bouncer->width) > SCREEN_WIDTH == false) {
|
if ((bouncer->pos.x + bouncer->width) > SCREEN_WIDTH == false) {
|
||||||
bouncer->pos.x += BOUNCER_SPEED; // increase the bouncer pos
|
bouncer->pos.x += BOUNCER_SPEED; // increase the bouncer pos
|
||||||
|
|
||||||
|
if (ball->moving == false)
|
||||||
|
ball->pos.x += BOUNCER_SPEED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ball launching
|
||||||
|
if (ball->moving == false && keys[SDL_SCANCODE_SPACE]) {
|
||||||
|
ball->moving = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// updates the player's ball
|
// updates the player's ball
|
||||||
|
|||||||
@@ -81,5 +81,6 @@ int main(void) {
|
|||||||
|
|
||||||
// frees media and shuts down SDL
|
// frees media and shuts down SDL
|
||||||
close(window, renderer);
|
close(window, renderer);
|
||||||
|
(void)printf("finished!\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user