commit 6b17d9c501f8281ef222367b00833206ccd10739 Author: Andrew Haynes Date: Tue Feb 24 16:01:56 2026 -0500 wooo diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..e9e3cb2 --- /dev/null +++ b/.clang-format @@ -0,0 +1,20 @@ +--- +BasedOnStyle: Google +Standard: Latest +# ColumnLimit: 80 +UseTab: Always +TabWidth: 8 +IndentWidth: 8 +BreakBeforeBraces: Allman +SpaceBeforeCpp11BracedList: true +SeparateDefinitionBlocks: Always +Cpp11BracedListStyle: false +BreakAfterReturnType: TopLevel +AlignTrailingComments: + Kind: Always +ReflowComments: Always +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: AllIfsAndElse +AllowShortLoopsOnASingleLine: true +IndentAccessModifiers: false # Indent access specifiers by one level +AccessModifierOffset: -4 # Optional: adjust offset relative to the class indentation level diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5b79a54 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +.cache/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..e747b54 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,44 @@ +cmake_minimum_required(VERSION 4.2.1) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +project(pong) + +include_directories(include) +set(SOURCES + src/main.cpp + src/graphics.cpp +) + +include(FetchContent) +FetchContent_Declare(SFML + GIT_REPOSITORY https://github.com/SFML/SFML.git + GIT_TAG 3.0.2 + GIT_SHALLOW ON + EXCLUDE_FROM_ALL + SYSTEM) +FetchContent_MakeAvailable(SFML) + +if(APPLE) + message(STATUS "Detected macOS: installing range-v3") + + include(FetchContent) + FetchContent_Declare( + range-v3 + GIT_REPOSITORY https://github.com/ericniebler/range-v3.git + GIT_TAG 0.12.0 + GIT_SHALLOW ON + ) + FetchContent_MakeAvailable(range-v3) + + include_directories(${range-v3_SOURCE_DIR}/include) +endif() + +add_executable(pong ${SOURCES}) +target_link_libraries(pong + PRIVATE + SFML::Graphics + SFML::Window + SFML::System +) diff --git a/assets/pixel.ttf b/assets/pixel.ttf new file mode 100644 index 0000000..2442aff Binary files /dev/null and b/assets/pixel.ttf differ diff --git a/include/globals.hpp b/include/globals.hpp new file mode 100644 index 0000000..f7bfaf4 --- /dev/null +++ b/include/globals.hpp @@ -0,0 +1,23 @@ +#ifndef GLOBALS_HPP +#define GLOBALS_HPP + +// General + +#define TILESIZE 32.0f +#define WIDTH (TILESIZE * 16) +#define HEIGHT (TILESIZE * 13) +#define FPS 60 + +//Paddle + +#define SIZE_X 10 +#define SIZE_Y 50 +#define POS_Y 200 +#define VELOCITY 4 + +// Ball + +#define VELOCITY_X 3 +#define VELOCITY_Y 2 + +#endif diff --git a/include/graphics.hpp b/include/graphics.hpp new file mode 100644 index 0000000..3035525 --- /dev/null +++ b/include/graphics.hpp @@ -0,0 +1,45 @@ +#ifndef GRAPHICS_HPP +#define GRAPHICS_HPP + +// Ball, Paddle + +#include +#include +#include + +#include "globals.hpp" + +struct Ball +{ + Ball(sf::Vector2f pos) : velocity({ VELOCITY_X, VELOCITY_Y }) + { + object.setSize({ (float)SIZE_X / 2, (float)SIZE_X / 2 }); + object.setPosition(pos); + }; + + sf::Vector2f velocity; + sf::RectangleShape object; +}; + +struct Paddle +{ + Paddle(sf::Vector2f pos) + { + rect.setSize({ SIZE_X, SIZE_Y }); + rect.setPosition(pos); + }; + + int points; + sf::RectangleShape rect; +}; + +void +check_and_move(); +void +handle_ball(); + +extern Paddle p1; +extern Paddle p2; +extern Ball ball; + +#endif diff --git a/src/graphics.cpp b/src/graphics.cpp new file mode 100644 index 0000000..ebb4b14 --- /dev/null +++ b/src/graphics.cpp @@ -0,0 +1,84 @@ +#include "graphics.hpp" + +#include + +#include "globals.hpp" + +Paddle p1 { { (TILESIZE * 2) - SIZE_X, POS_Y } }; +Paddle p2 { { WIDTH - (TILESIZE * 2), POS_Y } }; +Ball ball { { (WIDTH / 2) - (3.0f / 2), (HEIGHT / 2) - (3.0f / 2) } }; + +void +check_and_move() +{ + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) + { + if (p1.rect.getPosition().y - VELOCITY > 0) + { + p1.rect.setPosition( + { p1.rect.getPosition().x, + p1.rect.getPosition().y - VELOCITY }); + } + } + + else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) + { + if (p1.rect.getPosition().y + VELOCITY + SIZE_Y < HEIGHT) + { + p1.rect.setPosition( + { p1.rect.getPosition().x, + p1.rect.getPosition().y + VELOCITY }); + } + } + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up)) + { + if (p2.rect.getPosition().y - VELOCITY > 0) + { + p2.rect.setPosition( + { p2.rect.getPosition().x, + p2.rect.getPosition().y - VELOCITY }); + } + } + + else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down)) + { + if (p2.rect.getPosition().y + VELOCITY + SIZE_Y < HEIGHT) + { + p2.rect.setPosition( + { p2.rect.getPosition().x, + p2.rect.getPosition().y + VELOCITY }); + } + } +} + +void +handle_ball() +{ + if (ball.object.getPosition().y <= 0 || + ball.object.getPosition().y >= HEIGHT - ball.object.getSize().y) + { + ball.velocity.y *= -1; + } + if (ball.object.getPosition().x <= 0) + { + ball.object.setPosition( + { (WIDTH / 2) - (3.0f / 2), (HEIGHT / 2) - (3.0f / 2) }); + p2.points++; + } + if (ball.object.getPosition().x >= WIDTH - ball.object.getSize().x) + { + ball.object.setPosition( + { (WIDTH / 2) - (3.0f / 2), (HEIGHT / 2) - (3.0f / 2) }); + p1.points++; + } + + if (ball.object.getGlobalBounds().findIntersection( + p1.rect.getGlobalBounds()) || + ball.object.getGlobalBounds().findIntersection( + p2.rect.getGlobalBounds())) + { + ball.velocity.x *= -1; + } + ball.object.move({ ball.velocity }); +} diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..8d8a020 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include + +#include "globals.hpp" +#include "graphics.hpp" + +int +main() +{ + sf::RenderWindow window(sf::VideoMode({ (int)WIDTH, (int)HEIGHT }), + "Pong", sf::Style::Default); + window.setFramerateLimit(FPS); + sf::Font font("../assets/pixel.ttf"); + sf::Text text(font, "test"); + text.setCharacterSize(16); + + text.setPosition({ WIDTH / 4 + TILESIZE / 1.5, TILESIZE }); + + while (window.isOpen()) + { + text.setString(std::format("P1: {} : P2: {}", p1.points, p2.points)); + window.clear(sf::Color::Black); + while (const std::optional event = window.pollEvent()) + { + if (event->is()) + { + window.close(); + } + } + check_and_move(); + handle_ball(); + window.draw(p1.rect); + window.draw(p2.rect); + window.draw(ball.object); + window.draw(text); + window.display(); + } + return 0; +}