Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Pong Game Help  (Read 1881 times)

0 Members and 1 Guest are viewing this topic.

peanut13th

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Pong Game Help
« on: March 10, 2013, 10:24:09 pm »
Hello, I am new here and still new to SFML. I have a pong game im working on in SFML 2.0 and i need help getting the ball to angle when it bounces off one of the paddles. I have tried to use the examples in this thread http://en.sfml-dev.org/forums/index.php?topic=5515.0 and apply them to 2.0 but it either brakes my collision or the ball wont move. If anyone could give me some help or point me in the right direction that would be great.

#include <SFML/Graphics.hpp>
#include <iostream>
#include <cmath>
#include <time.h>
#include <cstdlib>

sf::Clock Clock;
float Speed = 10000.0f;
float BallSpeed = 99.3f;
bool BallDirct;


int main()
{

    float PI = 3.14159265f;
    float BallAngleRad;

    //Starts the ball in a random direction.
    srand(time(0));
    BallDirct = rand() % 2;

    //Making window
    sf::RenderWindow App;
    App.create(sf::VideoMode(800, 600), "Pung");

    //Making Shapes
    sf::RectangleShape Bar1(sf::Vector2f(15, 125));
    sf::RectangleShape Bar2(sf::Vector2f(15, 125));
    sf::RectangleShape Ball(sf::Vector2f(20, 20));
    Ball.setPosition(400, 300);
    Bar1.setPosition(1, 250);
    Bar2.setPosition(784, 250);

    Clock.restart();

    //Game Loop
    while(App.isOpen())
    {

        sf::Event Event;
        while (App.pollEvent(Event))
        {
            switch(Event.type)
            {
                case sf::Event::Closed:
                App.close();
                break;
                case sf::Event::KeyPressed:
                if(Event.key.code == sf::Keyboard::Escape)
                {
                    App.close();
                }
                break;
            }

            //Movment
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
            {
                Bar1.move(0, -Speed * Clock.getElapsedTime().asSeconds());
                if(Bar1.getPosition().y < 0)
                Bar1.setPosition(1, 0);
            }

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
            {
                Bar1.move(0, Speed * Clock.getElapsedTime().asSeconds());
                if(Bar1.getPosition().y > 491)
                Bar1.setPosition(1, 490);
            }

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
            {
                Bar2.move(0, -Speed * Clock.getElapsedTime().asSeconds());
                if(Bar2.getPosition().y < 0)
                Bar2.setPosition(784, 0);
            }

            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
            {
                Bar2.move(0, Speed * Clock.getElapsedTime().asSeconds());
                if(Bar2.getPosition().y > 491)
                Bar2.setPosition(784, 490);
            }

        }

        //Collision and changes the balls direction and speed.
        if(BallDirct == true)
        {

            Ball.move(-BallSpeed * Clock.getElapsedTime().asSeconds() * std::cos(BallAngleRad * PI / 180), -BallSpeed * Clock.getElapsedTime().asSeconds() * std::sin(BallAngleRad * PI / 180));
            if(Bar1.getGlobalBounds().intersects(Ball.getGlobalBounds()))
            {
                BallDirct = false;
                Ball.move(BallSpeed * Clock.getElapsedTime().asSeconds() * std::cos(BallAngleRad * PI / 180), BallSpeed * Clock.getElapsedTime().asSeconds() * std::sin(BallAngleRad * PI / 180));
                BallSpeed = BallSpeed + 10.5f;

            }
        }

        if(BallDirct == false)
        {
            Ball.move(BallSpeed * Clock.getElapsedTime().asSeconds() * std::cos(BallAngleRad * PI / 180), BallSpeed * Clock.getElapsedTime().asSeconds() * std::sin(BallAngleRad * PI / 180));
            if(Ball.getGlobalBounds().intersects(Bar2.getGlobalBounds()))
            {;
                Ball.move(-BallSpeed * Clock.getElapsedTime().asSeconds() * std::cos(BallAngleRad * PI / 180), -BallSpeed * Clock.getElapsedTime().asSeconds() * std::sin(BallAngleRad * PI / 180));;
                BallDirct = true;
                BallSpeed = BallSpeed + 10.5f;
            }
        }


        Clock.restart();
        App.draw(Bar1);
        App.draw(Bar2);
        App.draw(Ball);
        App.display();
        App.clear();
    }

    return EXIT_SUCCESS;
}

 
« Last Edit: March 10, 2013, 10:34:43 pm by peanut13th »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Pong Game Help
« Reply #1 on: March 10, 2013, 10:47:53 pm »
I don't really feel like going through your code, especially since you don't give a precise problem description. ;)

But you might want to take a look at the pong example that ships with SFML.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

peanut13th

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Pong Game Help
« Reply #2 on: March 10, 2013, 11:18:29 pm »
Thank you, I didn't know where to find the code for that example. That should be all the help i need. and sorry if my OP was to vague. 

Bryston

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Pong Game Help
« Reply #3 on: March 15, 2013, 12:44:51 pm »
I just multiplied the balls x-axis movement by -1 in my pong game.
Code: [Select]
//Collision
if(ball.getGlobalBounds().intersects(paddle1.getGlobalBounds()))
{
xVelBall *= -1;
hitSound.play();
}
if(ball.getGlobalBounds().intersects(paddle2.getGlobalBounds()))
{
xVelBall *= -1;
hitSound.play();
}

 

anything