This is my code:
#include <iostream>
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Paupav ball game");
sf::Clock clock;
sf::Clock clock2;
sf::Clock clock3;
sf::Time borderBounce = clock3.getElapsedTime();
bool returning = false;
bool userControls = true;
float positionx = 10;
float positiony = 16;
sf::CircleShape shape(30, 999);
sf::Texture texture, texture2;
if(!texture.loadFromFile("texture.jpg"))
{
std::cout << "Could not load ball texture" << std::endl;
}
shape.setTexture(&texture);
//floor
sf::RectangleShape floor(sf::Vector2f(800, 60));
if(!texture2.loadFromFile("brick.png"))
{
std::cout << "Could not load floor texture" << std::endl;
}
floor.setTexture(&texture2);
floor.setPosition(0, 540);
while(window.isOpen())
{
sf::Time time = clock.getElapsedTime();
sf::Time physic = clock2.getElapsedTime();
shape.setPosition(positionx, positiony);
if(!returning)
positiony = time.asMicroseconds() / 2319 + physic.asMilliseconds()/ 30;
if(positiony > 480)
returning = true;
if(returning )
{
positiony -=8;
clock.restart();
}
if(positiony < 15 + physic.asMilliseconds()/ 30)
returning = false;
if(physic.asMilliseconds() > 17000.00)
{
clock.restart();
clock2.restart();
}
sf::Event event;
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
}
}
if(userControls)
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
positionx += 10;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
positionx -= 10;
}
if(positionx < 9)
{
while(positionx < 40)
positionx += 0.001;
}
std::cout << "time: " << borderBounce.asMilliseconds() << std::endl;
window.clear(sf::Color::White);
window.draw(shape);
window.draw(floor);
window.display();
}
}
I know it's a bit messy, those booleans aren't needed etc. I made it using only SFML documentation. So I want to create that when ball hits borders it bounces back. I wanted to do it like this:
while(positionx < 40)
{increase positionx
delay}
Any suggestions, except to burn this code. The best way of doing this that I can remember is to set positionx every few milli seconds