Skip to content

Commit

Permalink
Added game example: bouncing ball
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuskrause committed Aug 11, 2022
1 parent 6379626 commit 55e517e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
11 changes: 11 additions & 0 deletions include/states/game-state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>
#include <cmath>
#include "engine/include/state/state.hpp"
#include "engine/include/game/game.hpp"
#include "states/pause-state.hpp"
Expand All @@ -17,6 +18,16 @@ class GameState : public pte::State
sf::Sprite background;
sf::Sprite pause_button;

// demo things
float pi = 3.14159f;
float ballRadius = 10.f;
float ballSpeed = 400.f;
float ballAngle = 0.f;
sf::CircleShape ball;

void init_ball();
void update_ball(float delta_time);

public:
GameState(pte::game_data_ref data);

Expand Down
48 changes: 48 additions & 0 deletions src/states/game-state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ void GameState::init()
this->data->assets.load_texture("Pause Button", PAUSE_BUTTON);
pause_button.setTexture(this->data->assets.get_texture("Pause Button"));
pause_button.setPosition(this->data->window.getSize().x - pause_button.getLocalBounds().width - 10, pause_button.getPosition().y + 10);

init_ball();
}

void GameState::handle_input()
Expand All @@ -32,11 +34,57 @@ void GameState::handle_input()

void GameState::update(float delta_time)
{
update_ball(delta_time);
}

void GameState::draw(float delta_time)
{
this->data->window.clear(sf::Color(56, 42, 55));
this->data->window.draw(this->ball);
this->data->window.draw(this->pause_button);
this->data->window.display();
}

void GameState::init_ball()
{
ball.setRadius(ballRadius - 3);
ball.setOutlineThickness(3);
ball.setOutlineColor(sf::Color::White);
ball.setFillColor(sf::Color::White);
ball.setOrigin(ballRadius / 2, ballRadius / 2);
ball.setPosition(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);

do
{
ballAngle = (std::rand() % 360) * 2 * pi / 360;
} while (std::abs(std::cos(ballAngle)) < 0.7f);
}

void GameState::update_ball(float delta_time)
{
// move ball
float factor = ballSpeed * delta_time;
ball.move(std::cos(ballAngle) * factor, std::sin(ballAngle) * factor);

// check colisions
if (ball.getPosition().x - ballRadius < 0.f)
{
ballAngle = -ballAngle + pi;
ball.setPosition(ballRadius + 0.1f, ball.getPosition().y);
}
if (ball.getPosition().x + ballRadius > SCREEN_WIDTH)
{
ballAngle = -ballAngle + pi;
ball.setPosition(SCREEN_WIDTH - ballRadius - 0.1f, ball.getPosition().y);
}
if (ball.getPosition().y - ballRadius < 0.f)
{
ballAngle = -ballAngle;
ball.setPosition(ball.getPosition().x, ballRadius + 0.1f);
}
if (ball.getPosition().y + ballRadius > SCREEN_HEIGHT)
{
ballAngle = -ballAngle;
ball.setPosition(ball.getPosition().x, SCREEN_HEIGHT - ballRadius - 0.1f);
}
}

0 comments on commit 55e517e

Please sign in to comment.