added bricks and the ability to spawn bricks with mouse

This commit is contained in:
Joseph Aquino 2025-08-29 14:18:32 -04:00
parent e5e7028554
commit 6559a9dc95
2 changed files with 20 additions and 6 deletions

View File

@ -38,7 +38,7 @@ struct Color
namespace global
{
inline sf::Vector2f playerSize{500, 25};
inline sf::Vector2f playerSize{500, 10};
inline sf::Vector2f playerHalfSize{playerSize / 2.f};
inline sf::Vector2f brickSize{50, 10};
inline sf::Vector2f brickHalfSize{brickSize / 2.f};

View File

@ -110,6 +110,19 @@ void Game::input()
break;
}
}
if (const auto* MouseButtonPressed = event->getIf<sf::Event::MouseButtonPressed>())
{
switch (MouseButtonPressed->button)
{
case sf::Mouse::Button::Left:
bricks.emplace_back((sf::Vector2f)MouseButtonPressed->position);
break;
default:
break;
}
}
}
}
@ -135,7 +148,7 @@ void Game::collision()
{
brick.alive = false;
ball.velocity.x *= -1;
if (ball.pos.x < player.pos.x)
if (ball.pos.x < brick.pos.x)
{
ball.pos.x -= intersect.x;
}
@ -150,7 +163,7 @@ void Game::collision()
{
brick.alive = false;
ball.velocity.y *= -1;
if (ball.pos.y < player.pos.y)
if (ball.pos.y < brick.pos.y)
{
ball.pos.y -= intersect.y;
}
@ -174,7 +187,7 @@ void Game::collision()
brick.alive = false;
spawnExtraBall = true;
ball.velocity.x *= -1;
if (ball.pos.x < player.pos.x)
if (ball.pos.x < brick.pos.x)
{
ball.pos.x -= intersect.x;
}
@ -190,7 +203,7 @@ void Game::collision()
brick.alive = false;
spawnExtraBall = true;
ball.velocity.y *= -1;
if (ball.pos.y < player.pos.y)
if (ball.pos.y < brick.pos.y)
{
ball.pos.y -= intersect.y;
}
@ -333,6 +346,7 @@ void Game::imgui()
ImGui::Text("A/D or arrow keys: move left and right");
ImGui::Text("R: reset game");
ImGui::Text("F1: expand/collaspe this window");
ImGui::Text("left click: spawn brick");
ImGui::Unindent();
ImGui::End();
@ -343,8 +357,8 @@ void Game::render()
window.clear();
tempRect.setSize(global::brickSize);
tempRect.setOrigin(global::brickHalfSize);
tempRect.setSize(global::brickSize);
for (auto& brick : bricks)
{
tempRect.setPosition(brick.pos);