Skip to content
Snippets Groups Projects
Commit 0c1597d2 authored by Cole2.Hansen@live.uwe.ac.uk's avatar Cole2.Hansen@live.uwe.ac.uk
Browse files

Added Brick Breaker Game Files

parent 54669060
Branches
No related tags found
No related merge requests found
image: ubuntu:latest # Use latest Ubuntu as the base image
stages:
- build
before_script:
- apt-get update && apt-get install -y build-essential libgl1-mesa-dev xorg-dev wget unzip
- wget https://github.com/raysan5/raylib/releases/download/5.0/raylib-5.0_linux_amd64.tar.gz
- tar -xvzf raylib-5.0_linux_amd64.tar.gz
- cp raylib-5.0_linux_amd64/lib/libraylib.a /usr/local/lib/
- cp -r raylib-5.0_linux_amd64/include/* /usr/local/include/
build:
stage: build
script:
- echo "Checking files..."
- ls -R # List all files for debugging
- gcc main.c paddle.c ball.c brick.c -o brickbreaker -I/usr/local/include -L/usr/local/lib -lraylib -lm -ldl -lpthread -lGL -lX11 -lXrandr -lXi -lXinerama -lXcursor
artifacts:
paths:
- brickbreaker
expire_in: 1 day # Keep the binary for 1 day
Makefile 0 → 100644
CC = gcc
CFLAGS = -Wall -std=c99
LDFLAGS = -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
SRC = main.c paddle.c ball.c brick.c
OBJ = $(SRC:.c=.o)
EXEC = brickbreaker
all: $(EXEC)
$(EXEC): $(OBJ)
$(CC) $(OBJ) -o $(EXEC) $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJ) $(EXEC)
ball.c 0 → 100644
#include "ball.h"
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
Ball InitBall() {
Ball ball;
ball.position = (Vector2){SCREEN_WIDTH / 2, 300};
ball.speed = (Vector2){4, -4};
ball.radius = 10;
return ball;
}
void UpdateBall(Ball *ball, Paddle *paddle) {
ball->position.x += ball->speed.x;
ball->position.y += ball->speed.y;
// Bounce off walls
if (ball->position.x <= 0 || ball->position.x >= SCREEN_WIDTH) {
ball->speed.x *= -1;
}
if (ball->position.y <= 0) {
ball->speed.y *= -1;
}
// Paddle collision
if (CheckCollisionCircleRec(ball->position, ball->radius, paddle->rect)) {
ball->speed.y *= -1;
}
// Ball falls off the screen (reset)
if (ball->position.y > SCREEN_HEIGHT) {
*ball = InitBall(); // Reset ball
}
}
void DrawBall(Ball ball) {
DrawCircleV(ball.position, ball.radius, RED);
}
ball.h 0 → 100644
#ifndef BALL_H
#define BALL_H
#include "raylib.h"
#include "paddle.h"
typedef struct {
Vector2 position;
Vector2 speed;
float radius;
} Ball;
Ball InitBall();
void UpdateBall(Ball *ball, Paddle *paddle);
void DrawBall(Ball ball);
#endif
brick.c 0 → 100644
#include "brick.h"
Brick bricks[BRICK_ROWS][BRICK_COLS];
void InitBricks() {
for (int row = 0; row < BRICK_ROWS; row++) {
for (int col = 0; col < BRICK_COLS; col++) {
bricks[row][col].rect = (Rectangle){col * BRICK_WIDTH, row * BRICK_HEIGHT, BRICK_WIDTH, BRICK_HEIGHT};
bricks[row][col].active = true;
}
}
}
void DrawBricks() {
for (int row = 0; row < BRICK_ROWS; row++) {
for (int col = 0; col < BRICK_COLS; col++) {
if (bricks[row][col].active) {
DrawRectangleRec(bricks[row][col].rect, GREEN);
}
}
}
}
void CheckBrickCollision(Ball *ball) {
for (int row = 0; row < BRICK_ROWS; row++) {
for (int col = 0; col < BRICK_COLS; col++) {
if (bricks[row][col].active && CheckCollisionCircleRec(ball->position, ball->radius, bricks[row][col].rect)) {
bricks[row][col].active = false; // Remove brick
ball->speed.y *= -1; // Reverse ball direction
}
}
}
}
brick.h 0 → 100644
#ifndef BRICK_H
#define BRICK_H
#include "raylib.h"
#include "ball.h"
#define BRICK_ROWS 5
#define BRICK_COLS 8
#define BRICK_WIDTH 80
#define BRICK_HEIGHT 30
typedef struct {
Rectangle rect;
bool active;
} Brick;
void InitBricks();
void DrawBricks();
void CheckBrickCollision(Ball *ball);
#endif
main.c 0 → 100644
#include "raylib.h"
#include "paddle.h"
#include "ball.h"
#include "brick.h"
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
int main() {
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Brick Breaker");
SetTargetFPS(60);
Paddle paddle = InitPaddle();
Ball ball = InitBall();
InitBricks();
while (!WindowShouldClose()) {
// Update game elements
UpdatePaddle(&paddle);
UpdateBall(&ball, &paddle);
CheckBrickCollision(&ball);
// Render the game
BeginDrawing();
ClearBackground(BLACK);
DrawPaddle(paddle);
DrawBall(ball);
DrawBricks();
EndDrawing();
}
CloseWindow();
return 0;
}
paddle.c 0 → 100644
#include "paddle.h"
#define PADDLE_WIDTH 100
#define PADDLE_HEIGHT 20
#define SCREEN_WIDTH 800
Paddle InitPaddle() {
Paddle paddle;
paddle.rect = (Rectangle){SCREEN_WIDTH / 2 - PADDLE_WIDTH / 2, 550, PADDLE_WIDTH, PADDLE_HEIGHT};
paddle.speed = 5.0f;
return paddle;
}
void UpdatePaddle(Paddle *paddle) {
if (IsKeyDown(KEY_LEFT) && paddle->rect.x > 0) {
paddle->rect.x -= paddle->speed;
}
if (IsKeyDown(KEY_RIGHT) && paddle->rect.x < SCREEN_WIDTH - paddle->rect.width) {
paddle->rect.x += paddle->speed;
}
}
void DrawPaddle(Paddle paddle) {
DrawRectangleRec(paddle.rect, BLUE);
}
paddle.h 0 → 100644
#ifndef PADDLE_H
#define PADDLE_H
#include "raylib.h"
typedef struct {
Rectangle rect;
float speed;
} Paddle;
Paddle InitPaddle();
void UpdatePaddle(Paddle *paddle);
void DrawPaddle(Paddle paddle);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment