From 0c1597d23c2f20d3422b11948e70072a1645ae8b Mon Sep 17 00:00:00 2001 From: Hansen <Cole2.hansen@live.uwe.ac.uk> Date: Sat, 8 Mar 2025 08:47:34 +0500 Subject: [PATCH] Added Brick Breaker Game Files --- .gitlab-ci.yml | 22 ++++++++++++++++++++++ Makefile | 18 ++++++++++++++++++ ball.c | 39 +++++++++++++++++++++++++++++++++++++++ ball.h | 17 +++++++++++++++++ brick.c | 33 +++++++++++++++++++++++++++++++++ brick.h | 21 +++++++++++++++++++++ main.c | 36 ++++++++++++++++++++++++++++++++++++ paddle.c | 25 +++++++++++++++++++++++++ paddle.h | 15 +++++++++++++++ 9 files changed, 226 insertions(+) create mode 100644 .gitlab-ci.yml create mode 100644 Makefile create mode 100644 ball.c create mode 100644 ball.h create mode 100644 brick.c create mode 100644 brick.h create mode 100644 main.c create mode 100644 paddle.c create mode 100644 paddle.h diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..348b575 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,22 @@ +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 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4b08998 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +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) diff --git a/ball.c b/ball.c new file mode 100644 index 0000000..3c25f41 --- /dev/null +++ b/ball.c @@ -0,0 +1,39 @@ +#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); +} diff --git a/ball.h b/ball.h new file mode 100644 index 0000000..bc6ff21 --- /dev/null +++ b/ball.h @@ -0,0 +1,17 @@ +#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 diff --git a/brick.c b/brick.c new file mode 100644 index 0000000..35bb713 --- /dev/null +++ b/brick.c @@ -0,0 +1,33 @@ +#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 + } + } + } +} diff --git a/brick.h b/brick.h new file mode 100644 index 0000000..9e08d70 --- /dev/null +++ b/brick.h @@ -0,0 +1,21 @@ +#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 diff --git a/main.c b/main.c new file mode 100644 index 0000000..82eccd0 --- /dev/null +++ b/main.c @@ -0,0 +1,36 @@ +#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; +} diff --git a/paddle.c b/paddle.c new file mode 100644 index 0000000..9474137 --- /dev/null +++ b/paddle.c @@ -0,0 +1,25 @@ +#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); +} diff --git a/paddle.h b/paddle.h new file mode 100644 index 0000000..6440bb6 --- /dev/null +++ b/paddle.h @@ -0,0 +1,15 @@ +#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 -- GitLab