wooo
This commit is contained in:
@@ -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
|
||||
@@ -0,0 +1,2 @@
|
||||
build/
|
||||
.cache/
|
||||
@@ -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
|
||||
)
|
||||
Binary file not shown.
@@ -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
|
||||
@@ -0,0 +1,45 @@
|
||||
#ifndef GRAPHICS_HPP
|
||||
#define GRAPHICS_HPP
|
||||
|
||||
// Ball, Paddle
|
||||
|
||||
#include <SFML/Graphics/CircleShape.hpp>
|
||||
#include <SFML/Graphics/RectangleShape.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
|
||||
#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
|
||||
@@ -0,0 +1,84 @@
|
||||
#include "graphics.hpp"
|
||||
|
||||
#include <SFML/Window/Keyboard.hpp>
|
||||
|
||||
#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 });
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#include <SFML/Audio.hpp>
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <SFML/Graphics/Font.hpp>
|
||||
#include <SFML/System.hpp>
|
||||
#include <SFML/Window.hpp>
|
||||
#include <format>
|
||||
|
||||
#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<sf::Event::Closed>())
|
||||
{
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user