Skip to content

Commit

Permalink
Fix #3
Browse files Browse the repository at this point in the history
    Fixes #3 by setting the last ball velocity to its start velocity at
the restart function.

Signed-off-by: Rafael Campos Nunes <[email protected]>
  • Loading branch information
rafaelcn committed Apr 22, 2017
1 parent 267abf6 commit 4f94f97
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
32 changes: 19 additions & 13 deletions src/ball.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ Ball::Ball(SDL_Surface* ball_surface, const float coordinate_x,
const float coordinate_y): m_ball_speed(get_random_pos(5, 7),
get_random_pos(-3, 3)),
m_last_ball_speed(m_ball_speed())
{

if(ball_surface == nullptr) {
{
if (ball_surface == nullptr) {
Debug::log_err("The ball surface is null. Check if res folder is along",
"with the executable.");
}
Expand All @@ -30,25 +29,25 @@ Ball::Ball(SDL_Surface* ball_surface, const float coordinate_x,
m_pBTexture = SDL_CreateTextureFromSurface(Window::get_renderer(),
ball_surface);

if(m_pBTexture == nullptr) {
if (m_pBTexture == nullptr) {
Debug::log_err("Failed to create ball texture!", SDL_GetError());
}

if(m_pBTexture == nullptr) {
if (m_pBTexture == nullptr) {
Debug::log_err("Failed to create ball renderer!", SDL_GetError());
}

SDL_FreeSurface(ball_surface);

if(m_pBTexture != nullptr) {
if (m_pBTexture != nullptr) {
Debug::log("Ball created perfectly!");
}
}
}

Ball::~Ball()
{
if(m_pBTexture != nullptr) {
if (m_pBTexture != nullptr) {
SDL_DestroyTexture(m_pBTexture);
}
}
Expand All @@ -64,17 +63,18 @@ void Ball::move(SDL_Rect* player1, SDL_Rect* player2)
m_BRect.y += velocity_y();

// Detecting the collision in the Y axis.
if(m_BRect.y <= 0) {
if (m_BRect.y <= 0) {
velocity_y(-m_ball_speed.y());
m_last_ball_speed = m_ball_speed;
}
if(m_BRect.y+m_BRect.h > Window::get_height()) {

if (m_BRect.y+m_BRect.h > Window::get_height()) {
velocity_y(-m_ball_speed.y());
m_last_ball_speed = m_ball_speed;
}

// Detecting the collision with the players.
if(collision(player1)) {
if (collision(player1)) {
if (m_BRect.x + abs(velocity_x()) < player1->x + player1->w) {
velocity_y(-m_ball_speed.y());
}
Expand All @@ -91,20 +91,21 @@ void Ball::move(SDL_Rect* player1, SDL_Rect* player2)

m_last_ball_speed = m_ball_speed;

if(Game::audio->is_open()) {
if (Game::audio->is_open()) {
Game::audio->play_effect(Audio::EffectType::hit_paddle);
}

Debug::log("Hit count: ", Paddle::get_hits());
Paddle::add_hit();
}
if(collision(player2)) {

if (collision(player2)) {
if ((m_BRect.x + m_BRect.w)-velocity_x() > player2->x) {
velocity_y(-m_ball_speed.y());
}
else {
velocity_x(-m_ball_speed.x());
if(Paddle::get_hits() == 3) {
if (Paddle::get_hits() == 3) {
add_speed();
Paddle::reset_hit_count();
}
Expand Down Expand Up @@ -168,6 +169,11 @@ SDL_Rect* Ball::get_rect()
return &m_BRect;
}


Vector2D Ball::velocity() {
return m_ball_speed;
}

float Ball::velocity_x()
{
return m_ball_speed.x();
Expand Down
6 changes: 6 additions & 0 deletions src/ball.hpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ class Ball {
*/
void move(SDL_Rect* player1, SDL_Rect* player2);

/**
* @brief Ball::velocity A function that returns a Vector2D representing its
* velocity.
* @return The velocity as a Vector2D.
*/
Vector2D velocity();
/**
* @brief Ball::velocity_x A function to get the velocity of the ball on the
* X axis.
Expand Down
21 changes: 12 additions & 9 deletions src/game.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Game::Game()
std::string pause_background("res/icons/pause-background.png");
std::string pause_message("res/icons/pause.png");

window = std::make_shared<Window>("Pong Game - Alpha 0.9.2", 800, 600,
window = std::make_shared<Window>("Pong Game - Alpha 0.9.3", 800, 600,
SDL_WINDOW_SHOWN);

image = std::make_shared<Image>();
Expand Down Expand Up @@ -131,16 +131,16 @@ void Game::handle_events()
* moving at the same time.
* Thanks to veQue on IRC!
*/
if(player1->key_state[SDL_SCANCODE_W]) {
if (player1->key_state[SDL_SCANCODE_W]) {
player1->move_up();
}
if(player1->key_state[SDL_SCANCODE_S]) {
if (player1->key_state[SDL_SCANCODE_S]) {
player1->move_down();
}
if(player2->key_state[SDL_SCANCODE_UP]) {
if (player2->key_state[SDL_SCANCODE_UP]) {
player2->move_up();
}
if(player2->key_state[SDL_SCANCODE_DOWN]) {
if (player2->key_state[SDL_SCANCODE_DOWN]) {
player2->move_down();
}

Expand Down Expand Up @@ -176,7 +176,7 @@ void Game::handle_events()
int fullscreenFlag = \
SDL_GetWindowFlags(Window::get_window());

if((fullscreenFlag & SDL_WINDOW_FULLSCREEN) ==
if ((fullscreenFlag & SDL_WINDOW_FULLSCREEN) ==
SDL_WINDOW_FULLSCREEN)
SDL_SetWindowFullscreen(Window::get_window(),
0);
Expand Down Expand Up @@ -240,7 +240,7 @@ unsigned int Game::get_fps()

void Game::pause()
{
if(!m_pause)
if (!m_pause)
{
ball->velocity_x(0.0);
ball->velocity_y(0.0);
Expand All @@ -263,13 +263,13 @@ void Game::update_game_state()
{
SDL_Rect* ball_rect = ball->get_rect();

if(ball_rect->x+ball_rect->w > Window::get_width())
if (ball_rect->x+ball_rect->w > Window::get_width())
{
player1->add_score();
reset_game();
Debug::log("Player 1 score: ", player1->score());
}
if(ball_rect->x+ball_rect->w < 0)
if (ball_rect->x+ball_rect->w < 0)
{
player2->add_score();
reset_game();
Expand All @@ -289,6 +289,9 @@ void Game::reset_game()

ball->velocity_x(ball->get_random_pos(-5, 5));
ball->velocity_y(ball->get_random_pos(-3, 3));

ball->m_last_ball_speed = ball->velocity();

ball_rect->x = Window::get_width()/2-(ball_rect->w);
ball_rect->y = Window::get_height()/2-(ball_rect->h);

Expand Down

0 comments on commit 4f94f97

Please sign in to comment.