add ball movement

This commit is contained in:
Quinn
2024-06-10 20:27:56 +02:00
parent 50d9cbbc41
commit b6c7e6f2ba
5 changed files with 80 additions and 18 deletions

58
level.c
View File

@@ -2,14 +2,18 @@
#include <stdbool.h>
#include <SDL2/SDL.h>
#include "vector2.h"
#include "constants.h"
//prepares the level to be in a playable state
void level_init(Level* level) {
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.2F, 0.2F };
// initialize bouncer
level->bouncer.pos.x = (SCREEN_WIDTH / 2) - (BOUNCER_WIDTH_DEFAULT / 2);
@@ -17,24 +21,64 @@ void level_init(Level* level) {
level->bouncer.width = BOUNCER_WIDTH_DEFAULT;
}
void ball_collisions(struct Ball* ball) {
}
//updates the level
void level_update(Level* level, bool* keys) {
Position* bouncer_pos = &level->bouncer.pos;
struct Bouncer* bouncer = &level->bouncer;
struct Ball* ball = &level->ball;
// if move bouncer LEFT
if (keys[SDLK_a]) {
bouncer_pos->x--;
bouncer->pos.x -= 0.5F;
ball->moving = true;
if (bouncer_pos->x == 0) {
bouncer_pos->x = SCREEN_WIDTH - level->bouncer.width;
if (bouncer->pos.x == 0) {
bouncer->pos.x = SCREEN_WIDTH - bouncer->width;
}
}
// if move bouncer RIGHT
if (keys[SDLK_d]) {
bouncer_pos->x++; // increase the bouncer pos
bouncer->pos.x += 0.5F; // increase the bouncer pos
ball->moving = true;
if ((bouncer_pos->x + level->bouncer.width) > SCREEN_WIDTH) {
bouncer_pos->x = 1; //set the bouncer pos to 1
if ((bouncer->pos.x + bouncer->width) > SCREEN_WIDTH) {
bouncer->pos.x = 1; //set the bouncer pos to 1
}
}
if (ball->moving == false)
return;
// update ball position
ball->pos.x += ball->direction.x;
ball->pos.y += ball->direction.y;
// check X axis out of bounds collisions
if ((ball->pos.x + ball->size) > SCREEN_WIDTH || ball->pos.x < 0) {
ball->direction.x *= -1;
}
// check Y axis out of bounds collisions
if (ball->pos.y < 0) {
ball->direction.y *= -1;
}
// check bouncer collisions
if ((ball->pos.x + ball->size) > bouncer->pos.x && ball->pos.x < (bouncer->pos.x + bouncer->width) &&
(ball->pos.y + ball->size) > bouncer->pos.y && ball->pos.y < (bouncer->pos.y + BOUNCER_HEIGHT)) {
ball->direction.y *= -1;
}
// check lose condition
if ((ball->pos.y + ball->size) > SCREEN_HEIGHT) {
level->stop = true;
ball->moving = false;
}
}

16
level.h
View File

@@ -1,21 +1,21 @@
#pragma once
#include <stdbool.h>
#include <SDL2/SDL.h>
#include "vector2.h"
typedef struct {
unsigned x;
unsigned y;
} Position;
bool stop;
typedef struct {
struct {
struct Ball {
bool moving;
unsigned size;
Position pos;
Vector2 pos;
Vector2 direction;
} ball;
struct {
struct Bouncer {
unsigned width;
Position pos;
Vector2 pos;
} bouncer;
} Level;

2
main.c
View File

@@ -85,7 +85,7 @@ int main(void) {
// game-loop
RenderData render_data = { window, renderer, &level }; //contains the data which is used to render the game
while (update(&level, keys, &render_data));
while (update(&level, keys, &render_data) && (level.stop == false));
// frees media and shuts down SDL
close(window, renderer);

View File

@@ -45,8 +45,8 @@ void renderer_update(RenderData* render_data) {
//draw player components
success |= SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
success |= SDL_RenderFillRect(renderer, &(SDL_Rect) {level->bouncer.pos.x, level->bouncer.pos.y, level->bouncer.width, 5});
success |= SDL_RenderFillRect(renderer, &(SDL_Rect) {level->ball.pos.x, level->ball.pos.y, level->ball.size, level->ball.size});
success |= SDL_RenderFillRectF(renderer, &(SDL_FRect) {level->bouncer.pos.x, level->bouncer.pos.y, level->bouncer.width, 5});
success |= SDL_RenderFillRectF(renderer, &(SDL_FRect) {level->ball.pos.x, level->ball.pos.y, level->ball.size, level->ball.size});
if (success < 0) {
printf("something went wrong whilst rendering: %s\n", SDL_GetError());

18
vector2.h Normal file
View File

@@ -0,0 +1,18 @@
#pragma once
#define VECTOR2_UP ((Vector2){ 0, 1 })
#define VECTOR2_DOWN ((Vector2){ 0, -1 })
#define VECTOR2_LEFT ((Vector2){ -1, 0 })
#define VECTOR2_RIGHT ((Vector2){ 1, 0 })
#define VECTOR2_ZERO ((Vector2){ 0, 0 })
#define VECTOR2_ONE ((Vector2){ 1, 1 })
#define vec2_add(v1, v2) ((Vector2){v1.x + v2.x, v1.y + v2.y})
#define vec2_subt(v1, v2) ((Vector2){v1.x - v2.x, v1.y - v2.y})
#define vec2_mult(v, a) ((Vector2){v.x * a, v.y * a})
#define vec2_div(v, a) ((Vector2){v.x / a, v.y / a})
typedef struct {
float x;
float y;
} Vector2;