mirror of
https://github.com/thepigeongenerator/breakout_clone.git
synced 2025-12-16 22:05:45 +01:00
Added brick reneration + Bouncer speed
This commit is contained in:
9
src/colour.h
Normal file
9
src/colour.h
Normal file
@@ -0,0 +1,9 @@
|
||||
typedef union {
|
||||
struct {
|
||||
unsigned char r;
|
||||
unsigned char g;
|
||||
unsigned char b;
|
||||
unsigned char a;
|
||||
};
|
||||
unsigned packed;
|
||||
} Colour;
|
||||
@@ -5,8 +5,16 @@
|
||||
|
||||
#define BOUNCER_HEIGHT 5
|
||||
#define BOUNCER_WIDTH_DEFAULT 60
|
||||
#define BOUNCER_SPEED 10.0F
|
||||
|
||||
#define BALL_SIZE_DEFAULT 10
|
||||
#define BALL_SPEED 0.3F
|
||||
#define BALL_SPEED 5.0F
|
||||
|
||||
#define BRICK_WIDTH 50
|
||||
#define BRICK_HEIGHT 15
|
||||
#define BRICK_PADDING 2
|
||||
#define BRICK_PADDING_TOP 30
|
||||
#define BRICK_ROWS 5
|
||||
#define BRICK_COLUMNS (int)(SCREEN_WIDTH / (BRICK_WIDTH + BRICK_PADDING))
|
||||
|
||||
#define PI 3.14159265358979323846F // pi was being fucky! :D
|
||||
|
||||
35
src/level.c
35
src/level.c
@@ -4,6 +4,7 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include "vector2.h"
|
||||
#include "constants.h"
|
||||
#include "renderer.h"
|
||||
|
||||
//prepares the level to be in a playable state
|
||||
void level_init(Level* level) {
|
||||
@@ -13,25 +14,49 @@ void level_init(Level* level) {
|
||||
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};
|
||||
level->ball.direction = (Vector2){ 0, BALL_SPEED };
|
||||
|
||||
// initialize bouncer
|
||||
level->bouncer.pos.x = (SCREEN_WIDTH / 2) - (BOUNCER_WIDTH_DEFAULT / 2);
|
||||
level->bouncer.pos.y = SCREEN_HEIGHT - (BOUNCER_HEIGHT * 2);
|
||||
level->bouncer.width = BOUNCER_WIDTH_DEFAULT;
|
||||
|
||||
// initialize bricks
|
||||
// define the colours of the brick rows
|
||||
const Colour colours[BRICK_COLUMNS] = {
|
||||
{0x5B, 0xCE, 0xFA, 0xFF},
|
||||
{0xF5, 0xA9, 0xB8, 0xFF},
|
||||
{0xFF, 0xFF, 0xFF, 0xFF},
|
||||
{0xF5, 0xA9, 0xB8, 0xFF},
|
||||
{0x5B, 0xCE, 0xFA, 0xFF}
|
||||
};
|
||||
|
||||
int brick_width = BRICK_WIDTH + BRICK_PADDING;
|
||||
int brick_height = BRICK_HEIGHT + (BRICK_PADDING / 2);
|
||||
float level_padding_left = ((float)SCREEN_WIDTH - ((SCREEN_WIDTH / brick_width) * brick_width)) / 2.0F; //for centering
|
||||
|
||||
// store bricks in the level
|
||||
for (int x = 0; x < BRICK_COLUMNS; x++) {
|
||||
for (int y = 0; y < BRICK_ROWS; y++) {
|
||||
Brick* brick = &level->bricks[x][y];
|
||||
brick->colour = colours[y];
|
||||
brick->pos.x = (x * brick_width) + level_padding_left;
|
||||
brick->pos.y = (y * brick_height) + BRICK_PADDING_TOP;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//updates the level
|
||||
void level_update(Level* level, bool* keys) {
|
||||
struct Bouncer* bouncer = &level->bouncer;
|
||||
struct Ball* ball = &level->ball;
|
||||
Bouncer* bouncer = &level->bouncer;
|
||||
Ball* ball = &level->ball;
|
||||
|
||||
// if move bouncer LEFT
|
||||
if (keys[SDLK_a]) {
|
||||
ball->moving = true;
|
||||
|
||||
if (bouncer->pos.x < 0 == false) {
|
||||
bouncer->pos.x -= 0.5F;
|
||||
bouncer->pos.x -= BOUNCER_SPEED;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +65,7 @@ void level_update(Level* level, bool* keys) {
|
||||
ball->moving = true;
|
||||
|
||||
if ((bouncer->pos.x + bouncer->width) > SCREEN_WIDTH == false) {
|
||||
bouncer->pos.x += 0.5F; // increase the bouncer pos
|
||||
bouncer->pos.x += BOUNCER_SPEED; // increase the bouncer pos
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
34
src/level.h
34
src/level.h
@@ -2,21 +2,31 @@
|
||||
#include <stdbool.h>
|
||||
#include <SDL2/SDL.h>
|
||||
#include "vector2.h"
|
||||
#include "constants.h"
|
||||
#include "colour.h"
|
||||
|
||||
typedef struct {
|
||||
Vector2 pos;
|
||||
Vector2 direction;
|
||||
unsigned size;
|
||||
bool moving;
|
||||
} Ball;
|
||||
|
||||
typedef struct {
|
||||
Vector2 pos;
|
||||
unsigned width;
|
||||
} Bouncer;
|
||||
|
||||
typedef struct {
|
||||
Vector2 pos;
|
||||
Colour colour;
|
||||
} Brick;
|
||||
|
||||
typedef struct {
|
||||
bool stop;
|
||||
|
||||
struct Ball {
|
||||
bool moving;
|
||||
unsigned size;
|
||||
Vector2 pos;
|
||||
Vector2 direction;
|
||||
} ball;
|
||||
|
||||
struct Bouncer {
|
||||
unsigned width;
|
||||
Vector2 pos;
|
||||
} bouncer;
|
||||
Ball ball;
|
||||
Bouncer bouncer;
|
||||
Brick bricks[BRICK_COLUMNS][BRICK_ROWS];
|
||||
} Level;
|
||||
|
||||
|
||||
|
||||
@@ -43,10 +43,25 @@ void renderer_update(RenderData* render_data) {
|
||||
success |= SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
|
||||
success |= SDL_RenderClear(renderer);
|
||||
|
||||
//draw player components
|
||||
// draw player components
|
||||
success |= SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||
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});
|
||||
success |= SDL_RenderFillRectF(renderer, &(SDL_FRect) {level->bouncer.pos.x, level->bouncer.pos.y, level->bouncer.width, 5}); // draw bouncer
|
||||
success |= SDL_RenderFillRectF(renderer, &(SDL_FRect) {level->ball.pos.x, level->ball.pos.y, level->ball.size, level->ball.size}); // draw ball
|
||||
|
||||
// draw bricks
|
||||
int bricks_left = 0;
|
||||
for (int x = 0; x < BRICK_COLUMNS; x++) {
|
||||
for (int y = 0; y < BRICK_ROWS; y++) {
|
||||
Brick* brick = &level->bricks[x][y];
|
||||
|
||||
if (brick == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
success |= SDL_SetRenderDrawColor(renderer, brick->colour.r, brick->colour.g, brick->colour.b, brick->colour.a);
|
||||
success |= SDL_RenderFillRectF(renderer, &(SDL_FRect) {brick->pos.x, brick->pos.y, BRICK_WIDTH, BRICK_HEIGHT}); // draw brick
|
||||
}
|
||||
}
|
||||
|
||||
if (success < 0) {
|
||||
printf("something went wrong whilst rendering: %s\n", SDL_GetError());
|
||||
|
||||
Reference in New Issue
Block a user