commit 9180e55c883553c2d966598771d883569a771989 Author: Aadhavan Srinivasan Date: Wed Jan 17 23:25:43 2024 -0500 First commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..86a4ec7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +main.o +pong diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..386f38f --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +LDLIBS = -lraylib +CXXFLAGS += -Wall -Wextra -pedantic -Werror + +pong: main.o paddle.hpp ball.hpp + g++ $(CXXFLAGS) main.o -o pong $(LDLIBS) + +main.o : main.cpp + g++ $(CXXFLAGS) -c main.cpp -o main.o diff --git a/ball.hpp b/ball.hpp new file mode 100644 index 0000000..b22bf34 --- /dev/null +++ b/ball.hpp @@ -0,0 +1,39 @@ +#ifndef BALL_H +#define BALL_H + +class Ball { +public: + /* Variables */ + raylib::Vector2 pos; + raylib::Vector2 vel; + int radius; + raylib::Color color; + + /* Functions */ + Ball(int pos_x, int pos_y, int radius, int vel_x = 0, int vel_y = 0) { + this->pos = raylib::Vector2(pos_x, pos_y); + this->radius = radius; + this->color = raylib::Color::White(); + this->vel = raylib::Vector2(vel_x, vel_y); + return; + } + + void setPosition(Vector2 pos) { + this->pos = pos; + } + + void updatePosition() { + this->pos.x += this->vel.x; + this->pos.y += this->vel.y; + return; + } + + void draw() { + updatePosition(); + DrawCircle(this->pos.x, this->pos.y, this->radius, this->color); // This is a raylib function, not a raylib-cpp function + return; + } + +}; + +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..fcdeba5 --- /dev/null +++ b/main.cpp @@ -0,0 +1,84 @@ +#include +#include +#include "paddle.hpp" +#include "ball.hpp" + +int main() { + /* Initialize variables used to instantiate structs*/ + const int WIDTH = 600; + const int HEIGHT = 480; + const int RECT_H = HEIGHT / 3; + const int RECT_W = 30; + const int CIRC_RAD = 10; + + /* Initialize window */ + raylib::Window window = raylib::Window(WIDTH, HEIGHT, "Pong"); + SetTraceLogLevel(LOG_INFO); + window.ClearBackground(BLACK); + SetTargetFPS(60); + SetExitKey(KEY_Q); + + /* Instantiate Paddle and Ball objects */ + Paddle pad1 = Paddle(10, 10, RECT_W, RECT_H); + Paddle pad2 = Paddle(window.GetWidth() - RECT_W - 10, 10, RECT_W, RECT_H); + Ball ball = Ball(window.GetWidth()/2, window.GetHeight()/2, CIRC_RAD, 3, 3); + + window.BeginDrawing(); + pad1.draw(); + pad2.draw(); + ball.draw(); + + /* Main loop */ + while (!window.ShouldClose()) { + /* Update paddle velocity */ + if (IsKeyPressed(KEY_S)) { + pad1.velocity.y = 5; /* Set positive (downward) velocity, since (0,0) is top-left */ + } + if (IsKeyPressed(KEY_W)) { + pad1.velocity.y = -5; /* Set negative (upward) velocity */ + } + + if (IsKeyReleased(KEY_S) || IsKeyReleased(KEY_W)) { + pad1.velocity.y = 0; + } + + if (IsKeyPressed(KEY_UP)) { + pad2.velocity.y = -5; + } + if (IsKeyPressed(KEY_DOWN)) { + pad2.velocity.y = 5; + } + if (IsKeyReleased(KEY_UP) || IsKeyReleased(KEY_DOWN)) { + pad2.velocity.y = 0; + } + + /* Update ball velocity based on collision detection*/ + if (pad1.getRect().CheckCollision(ball.pos, ball.radius) || pad2.getRect().CheckCollision(ball.pos, ball.radius)) { + ball.vel.x = ball.vel.x * (-1); + } + if (ball.pos.x + ball.radius >= window.GetWidth()) { + pad1.incrementPoints(); + ball.pos = raylib::Vector2(window.GetWidth() / 2, window.GetHeight() / 2); + } + if (ball.pos.x - ball.radius <= 0) { + pad2.incrementPoints(); + ball.pos = raylib::Vector2(window.GetWidth() / 2, window.GetHeight() / 2); + } + if (ball.pos.y - ball.radius <= 0 || ball.pos.y + ball.radius >= window.GetHeight()) { + ball.vel.y = ball.vel.y * -1; + } + + /* Draw objects */ + + window.BeginDrawing(); + window.ClearBackground(BLACK); + pad1.draw(); + pad2.draw(); + ball.draw(); + window.EndDrawing(); + } + + window.Close(); + + return 0; +} diff --git a/paddle.hpp b/paddle.hpp new file mode 100644 index 0000000..27b4944 --- /dev/null +++ b/paddle.hpp @@ -0,0 +1,47 @@ +#ifndef PADDLE_H +#define PADDLE_H + +class Paddle { +private: + /* Variables */ + raylib::Rectangle rectangle; + raylib::Color color; + int points; + +public: + /* Variables */ + raylib::Vector2 velocity; /* This variable is made public to allow easy modification (no need for getters/setters) */ + + /* Functions */ + Paddle(int pos_x, int pos_y, int width, int height) { + this->rectangle = raylib::Rectangle(pos_x, pos_y, width, height); + this->color = raylib::Color::White(); + this->velocity = raylib::Vector2(0,0); + points = 0; + return; + } + + raylib::Rectangle getRect() { + return this->rectangle; + } + + int getPoints() { + return points; + } + void incrementPoints() { + points++; + } + + void updatePosition() { + this->rectangle.x += this->velocity.x; + this->rectangle.y += this->velocity.y; + return; + } + void draw() { + updatePosition(); + this->rectangle.Draw(this->color); + return; + } +}; + +#endif