Overoptimizing as per usual (removed a bunch of imaginary variables and cleaned up the code)
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
---
|
||||
BasedOnStyle: Google
|
||||
Standard: Latest
|
||||
# ColumnLimit: 80
|
||||
UseTab: Always
|
||||
TabWidth: 8
|
||||
IndentWidth: 8
|
||||
|
||||
@@ -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
@@ -1,25 +1,23 @@
|
||||
#ifndef GRAPHICS_HPP
|
||||
#define GRAPHICS_HPP
|
||||
|
||||
// Ball, Paddle
|
||||
|
||||
#include <SFML/Graphics/CircleShape.hpp>
|
||||
#include <SFML/Graphics/RectangleShape.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
|
||||
{
|
||||
Ball(sf::Vector2f pos) : velocity({ VELOCITY_X, VELOCITY_Y })
|
||||
{
|
||||
object.setSize({ (float)SIZE_X / 2, (float)SIZE_X / 2 });
|
||||
object.setPosition(pos);
|
||||
};
|
||||
constexpr float SIZE_X = 10.f;
|
||||
constexpr float SIZE_Y = 50.f;
|
||||
constexpr float POS_Y = (HEIGHT / 2) - (SIZE_Y / 2);
|
||||
constexpr float PADDLE_VELOCITY = 4.f;
|
||||
|
||||
sf::Vector2f velocity;
|
||||
sf::RectangleShape object;
|
||||
};
|
||||
constexpr float BALL_VELOCITY_X = 3.f;
|
||||
constexpr float BALL_VELOCITY_Y = 2.f;
|
||||
constexpr float BALL_SIZE = 5.f;
|
||||
|
||||
struct Paddle
|
||||
{
|
||||
@@ -29,17 +27,24 @@ struct Paddle
|
||||
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;
|
||||
};
|
||||
|
||||
void
|
||||
check_and_move();
|
||||
void
|
||||
handle_ball();
|
||||
struct Ball
|
||||
{
|
||||
Ball()
|
||||
{
|
||||
object.setSize({ BALL_SIZE, BALL_SIZE });
|
||||
object.setPosition(
|
||||
{ (WIDTH / 2) - BALL_SIZE, (HEIGHT / 2) - BALL_SIZE });
|
||||
};
|
||||
|
||||
extern Paddle p1;
|
||||
extern Paddle p2;
|
||||
extern Ball ball;
|
||||
sf::Vector2f velocity { BALL_VELOCITY_X, BALL_VELOCITY_Y };
|
||||
sf::RectangleShape object;
|
||||
void handle_ball(Paddle* p1, Paddle* p2);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+26
-65
@@ -1,84 +1,45 @@
|
||||
#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) } };
|
||||
#include <SFML/Graphics/Rect.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
|
||||
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)
|
||||
{
|
||||
p1.rect.setPosition(
|
||||
{ p1.rect.getPosition().x,
|
||||
p1.rect.getPosition().y - VELOCITY });
|
||||
}
|
||||
rect.move({ 0, -PADDLE_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)
|
||||
{
|
||||
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 });
|
||||
}
|
||||
rect.move({ 0, PADDLE_VELOCITY });
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
handle_ball()
|
||||
Ball::handle_ball(Paddle* p1, Paddle* p2)
|
||||
{
|
||||
if (ball.object.getPosition().y <= 0 ||
|
||||
ball.object.getPosition().y >= HEIGHT - ball.object.getSize().y)
|
||||
sf::Vector2f pos = object.getPosition();
|
||||
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;
|
||||
}
|
||||
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++;
|
||||
(pos.x <= 0) ? p2->points++ : p1->points++;
|
||||
object.setPosition(
|
||||
{ (WIDTH / 2) - BALL_SIZE, (HEIGHT / 2) - BALL_SIZE });
|
||||
}
|
||||
|
||||
if (ball.object.getGlobalBounds().findIntersection(
|
||||
p1.rect.getGlobalBounds()) ||
|
||||
ball.object.getGlobalBounds().findIntersection(
|
||||
p2.rect.getGlobalBounds()))
|
||||
bool p1_hit =
|
||||
bounds.findIntersection(p1->rect.getGlobalBounds()).has_value();
|
||||
bool p2_hit =
|
||||
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 });
|
||||
}
|
||||
|
||||
+34
-17
@@ -1,41 +1,58 @@
|
||||
#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"
|
||||
constexpr int FPS = 60;
|
||||
constexpr int MATCHPOINT = 10;
|
||||
|
||||
int
|
||||
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 }),
|
||||
"Pong", sf::Style::Default);
|
||||
window.setFramerateLimit(FPS);
|
||||
sf::Font font("../assets/pixel.ttf");
|
||||
const sf::Font font("../assets/pixel.ttf");
|
||||
sf::Text text(font, "test");
|
||||
window.setFramerateLimit(FPS);
|
||||
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);
|
||||
text.setString(
|
||||
std::format("P1: {} : P2: {}", p1.points, p2.points));
|
||||
while (const std::optional event = window.pollEvent())
|
||||
{
|
||||
if (event->is<sf::Event::Closed>())
|
||||
{
|
||||
window.close();
|
||||
}
|
||||
if (event->is<sf::Event::Closed>()) window.close();
|
||||
}
|
||||
if (running)
|
||||
{
|
||||
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 });
|
||||
}
|
||||
window.clear(sf::Color::Black);
|
||||
if (running)
|
||||
{
|
||||
window.draw(p1.rect);
|
||||
window.draw(p2.rect);
|
||||
window.draw(ball.object);
|
||||
}
|
||||
check_and_move();
|
||||
handle_ball();
|
||||
window.draw(p1.rect);
|
||||
window.draw(p2.rect);
|
||||
window.draw(ball.object);
|
||||
window.draw(text);
|
||||
window.display();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user