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

Author Topic: Ball speed  (Read 1300 times)

0 Members and 2 Guests are viewing this topic.

Jeckie

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Ball speed
« on: August 22, 2013, 04:36:44 pm »
Here is my code:
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
        //Creation
        sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Game");
        //60 FPS
        window.setFramerateLimit(60);

        window.setKeyRepeatEnabled(false);
        //States for buttons and events
        sf::Event event;
        bool play= true;
       
       
        //Vars
        int ballXVelocity = 6;
        int ballYVelocity = 6;

        //Render shapes
        sf::CircleShape ball;
        ball.setRadius(25);
        ball.setPosition(375,275);
        ball.setFillColor(sf::Color::Blue);

        //Game loop
        while(play == true)
        {
                //EVENTS
                while (window.pollEvent(event))
                {

                        if(event.type == sf::Event::Closed)
                        {
                                play = false;
                        }
                        if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
                        {
                                play = false;
                        }

               
                }


                //LOGIC
               
                ball.move(ballXVelocity, ballYVelocity);

                if(ball.getPosition().x < 0 || ball.getPosition().x > 750) ballXVelocity = -ballXVelocity;
                if(ball.getPosition().y < 0 || ball.getPosition().y > 550) ballYVelocity = -ballYVelocity;



                //RENDERING

                        window.clear();

                        window.draw(ball);

                        window.display();

        }

        //Clean up
        window.close();

    return 0;
}
I made a ball with x velocity = -6 and y velocity = -6
I want now to increase x and y velocity by 1 every 5 second.
How can I do that?
I hope it's not a dumb question and you understand me.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Ball speed
« Reply #1 on: August 22, 2013, 05:08:58 pm »