试图让球在渲染窗口中反弹

时间:2017-05-16 17:49:16

标签: c++ sfml

我正在尝试使用SFML在渲染窗口中弹出一个球。我试图让球每次都出现在一个新的随机位置。但是我无法让球无限畅通。当我播放它并产生一个新位置时,它不会从渲染窗口反弹。我不知道如何做到这一点。

#include <iostream>
#include <SFML/Graphics.hpp>
#include <stdlib.h>
#include <time.h>
using namespace std;
using namespace sf;

int main()
{
    RenderWindow window(sf::VideoMode(1500, 800), "Bouncing Circle");
    window.setFramerateLimit(60);
    srand(time(NULL));
    int randx = rand() % 1500 + 1;
    int randy = rand() % 800 + 1;
    int x;
    int y;
    int distance = 1500 - randx;
    int distance2 = 800 - randy;
    int BREAK = 1;
    sf::CircleShape MyCircle(50);
    MyCircle.setPosition(randx, randy);
    MyCircle.setFillColor(sf::Color(500, 0, 0));

    while (window.isOpen()) {

        sf::Event event;
        while (window.pollEvent(event))
            ;
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        x = randx;
        y = randy;
        for (int ii = 0; ii < distance and ii < distance2; ii++) {

            x++;
            y++;
            MyCircle.setPosition(x, y);
            window.draw(MyCircle);
            window.display();
            window.clear();
            if (x == distance and y == distance2) {

                for (int aa = 0; aa <= 650; aa++) {
                    MyCircle.setPosition(x, y);
                    ////////////////////////////
                    // The code past this mark is from a different thing I tried. It is probably useless[
                    ////////////////////////////
                    x++;
                    y--;
                    window.draw(MyCircle);
                    window.display();
                    window.clear();
                    if (aa == 650) {
                        for (int pp = 0; pp <= 92; pp++) {
                            MyCircle.setPosition(x, y);
                            x++;
                            y++;
                            window.draw(MyCircle);
                            window.display();
                            window.clear();
                            if (pp == 92) {
                                for (int n = 0; n <= 570; n++) {
                                    MyCircle.setPosition(x, y);
                                    x--;
                                    y++;
                                    window.draw(MyCircle);
                                    window.display();
                                    window.clear();
                                }
                                ////////////////////////////
                                // ]
                                ////////////////////////////
                            }
                        }
                    }
                }
            }
        }
    }
    return 0;
}

0 个答案:

没有答案