Overoptimizing as per usual (removed a bunch of imaginary variables and cleaned up the code)

This commit is contained in:
Andrew Haynes
2026-02-25 11:29:27 -05:00
parent 49bd8ac080
commit 31c021b50c
5 changed files with 86 additions and 127 deletions
-1
View File
@@ -1,7 +1,6 @@
--- ---
BasedOnStyle: Google BasedOnStyle: Google
Standard: Latest Standard: Latest
# ColumnLimit: 80
UseTab: Always UseTab: Always
TabWidth: 8 TabWidth: 8
IndentWidth: 8 IndentWidth: 8
-23
View File
@@ -1,23 +0,0 @@
#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
+26 -21
View File
@@ -1,25 +1,23 @@
#ifndef GRAPHICS_HPP #ifndef GRAPHICS_HPP
#define GRAPHICS_HPP #define GRAPHICS_HPP
// Ball, Paddle
#include <SFML/Graphics/CircleShape.hpp> #include <SFML/Graphics/CircleShape.hpp>
#include <SFML/Graphics/RectangleShape.hpp> #include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <SFML/Window/Keyboard.hpp>
#include "globals.hpp" constexpr float TILESIZE = 32.0f;
constexpr float WIDTH = (TILESIZE * 16);
constexpr float HEIGHT = (TILESIZE * 13);
struct Ball constexpr float SIZE_X = 10.f;
{ constexpr float SIZE_Y = 50.f;
Ball(sf::Vector2f pos) : velocity({ VELOCITY_X, VELOCITY_Y }) constexpr float POS_Y = (HEIGHT / 2) - (SIZE_Y / 2);
{ constexpr float PADDLE_VELOCITY = 4.f;
object.setSize({ (float)SIZE_X / 2, (float)SIZE_X / 2 });
object.setPosition(pos);
};
sf::Vector2f velocity; constexpr float BALL_VELOCITY_X = 3.f;
sf::RectangleShape object; constexpr float BALL_VELOCITY_Y = 2.f;
}; constexpr float BALL_SIZE = 5.f;
struct Paddle struct Paddle
{ {
@@ -29,17 +27,24 @@ struct Paddle
rect.setPosition(pos); rect.setPosition(pos);
}; };
int points; int points { 0 };
void check_and_move(enum sf::Keyboard::Key key_up,
enum sf::Keyboard::Key key_down);
sf::RectangleShape rect; sf::RectangleShape rect;
}; };
void struct Ball
check_and_move(); {
void Ball()
handle_ball(); {
object.setSize({ BALL_SIZE, BALL_SIZE });
object.setPosition(
{ (WIDTH / 2) - BALL_SIZE, (HEIGHT / 2) - BALL_SIZE });
};
extern Paddle p1; sf::Vector2f velocity { BALL_VELOCITY_X, BALL_VELOCITY_Y };
extern Paddle p2; sf::RectangleShape object;
extern Ball ball; void handle_ball(Paddle* p1, Paddle* p2);
};
#endif #endif
+26 -65
View File
@@ -1,84 +1,45 @@
#include "graphics.hpp" #include "graphics.hpp"
#include <SFML/Graphics/Rect.hpp>
#include <SFML/Window/Keyboard.hpp> #include <SFML/System/Vector2.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 void
check_and_move() Paddle::check_and_move(enum sf::Keyboard::Key key_up,
enum sf::Keyboard::Key key_down)
{ {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) float y = rect.getPosition().y;
if (sf::Keyboard::isKeyPressed(key_up) && y - PADDLE_VELOCITY > 0)
{ {
if (p1.rect.getPosition().y - VELOCITY > 0) rect.move({ 0, -PADDLE_VELOCITY });
{
p1.rect.setPosition(
{ p1.rect.getPosition().x,
p1.rect.getPosition().y - VELOCITY });
}
} }
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) if (sf::Keyboard::isKeyPressed(key_down) &&
y + PADDLE_VELOCITY + SIZE_Y < HEIGHT)
{ {
if (p1.rect.getPosition().y + VELOCITY + SIZE_Y < HEIGHT) rect.move({ 0, PADDLE_VELOCITY });
{
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 void
handle_ball() Ball::handle_ball(Paddle* p1, Paddle* p2)
{ {
if (ball.object.getPosition().y <= 0 || sf::Vector2f pos = object.getPosition();
ball.object.getPosition().y >= HEIGHT - ball.object.getSize().y) sf::FloatRect bounds = object.getGlobalBounds();
if (pos.y <= 0 || pos.y >= HEIGHT - BALL_SIZE) velocity.y *= -1;
if (pos.x <= 0 || pos.x >= WIDTH - BALL_SIZE)
{ {
ball.velocity.y *= -1; (pos.x <= 0) ? p2->points++ : p1->points++;
} object.setPosition(
if (ball.object.getPosition().x <= 0) { (WIDTH / 2) - BALL_SIZE, (HEIGHT / 2) - BALL_SIZE });
{
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( bool p1_hit =
p1.rect.getGlobalBounds()) || bounds.findIntersection(p1->rect.getGlobalBounds()).has_value();
ball.object.getGlobalBounds().findIntersection( bool p2_hit =
p2.rect.getGlobalBounds())) bounds.findIntersection(p2->rect.getGlobalBounds()).has_value();
if (p1_hit || p2_hit)
{ {
ball.velocity.x *= -1; object.move({ p1_hit ? BALL_VELOCITY_X : -BALL_VELOCITY_X, 0 });
velocity.x *= -1;
} }
ball.object.move({ ball.velocity }); object.move({ velocity });
} }
+29 -12
View File
@@ -1,41 +1,58 @@
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/System.hpp> #include <SFML/System.hpp>
#include <SFML/Window.hpp> #include <SFML/Window.hpp>
#include <format> #include <format>
#include "globals.hpp"
#include "graphics.hpp" #include "graphics.hpp"
constexpr int FPS = 60;
constexpr int MATCHPOINT = 10;
int int
main() main()
{ {
Paddle p1 { { (TILESIZE * 2) - SIZE_X, POS_Y } };
Paddle p2 { { WIDTH - (TILESIZE * 2), POS_Y } };
Ball ball;
bool running = true;
sf::RenderWindow window(sf::VideoMode({ (int)WIDTH, (int)HEIGHT }), sf::RenderWindow window(sf::VideoMode({ (int)WIDTH, (int)HEIGHT }),
"Pong", sf::Style::Default); "Pong", sf::Style::Default);
window.setFramerateLimit(FPS); const sf::Font font("../assets/pixel.ttf");
sf::Font font("../assets/pixel.ttf");
sf::Text text(font, "test"); sf::Text text(font, "test");
window.setFramerateLimit(FPS);
text.setCharacterSize(16); text.setCharacterSize(16);
text.setPosition({ WIDTH / 4 + TILESIZE / 1.5, TILESIZE }); text.setPosition({ WIDTH / 4 + TILESIZE / 1.5, TILESIZE });
while (window.isOpen()) while (window.isOpen())
{ {
text.setString(std::format("P1: {} : P2: {}", p1.points, p2.points)); text.setString(
window.clear(sf::Color::Black); std::format("P1: {} : P2: {}", p1.points, p2.points));
while (const std::optional event = window.pollEvent()) while (const std::optional event = window.pollEvent())
{ {
if (event->is<sf::Event::Closed>()) if (event->is<sf::Event::Closed>()) window.close();
}
if (running)
{ {
window.close(); p1.check_and_move(sf::Keyboard::Key::W,
sf::Keyboard::Key::S);
p2.check_and_move(sf::Keyboard::Key::Up,
sf::Keyboard::Key::Down);
ball.handle_ball(&p1, &p2);
if (p1.points >= MATCHPOINT || p2.points >= MATCHPOINT)
running = false;
} }
else
{
(p1.points > p2.points) ? text.setString("P1 wins")
: text.setString("P2 wins");
text.setPosition({ WIDTH / 3 + TILESIZE, HEIGHT / 2 });
} }
check_and_move(); window.clear(sf::Color::Black);
handle_ball(); if (running)
{
window.draw(p1.rect); window.draw(p1.rect);
window.draw(p2.rect); window.draw(p2.rect);
window.draw(ball.object); window.draw(ball.object);
}
window.draw(text); window.draw(text);
window.display(); window.display();
} }