C ++ |图形的边界条件

时间:2017-09-12 01:16:57

标签: c++

我的C ++知识很新,但我在过去的两年里一直使用java编写代码。我决定采取信仰的飞跃并学习一门新语言。无论如何,我一直在网上关注这个惊人的SFML教程,我意识到这对我的口味来说有点太容易,所以我想为我们正在编码的这些球对象创建一个边界,这样它们就可以在提供的窗口中反弹。令我惊讶的是,我的算法让我失望了。我将仅展示Red Circle Object的精简版在线教程。

#include "stdafx.h"
#include <SFML/Graphics.hpp>

int main()
{
  sf::RenderWindow window(sf::VideoMode(800, 600), "Variables Demo");

  sf::CircleShape circleRed(10);//Radius to 10

  circleRed.setFillColor(sf::Color(255, 0, 0));//Color to Red

  float x = true;
  float y = true;

  float xRed = 100;
  float yRed = 100;

  circleRed.setPosition(xRed, yRed);

  while (window.isOpen())
  {
    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
        window.close();
    }
    window.clear();

   //x and yRed are modified here.
    if (xRed >= 589 || xRed <= 9)
    x = !x;

    if (x = true)
        xRed = xRed + .3;
    else 
        xRed = xRed - .3;

    if (yRed >= 789 || yRed <= 9)
        y = !y;

    if (y = true)
      yRed = yRed + .3;
    else yRed = yRed - .3;

    circleRed.setPosition(xRed, yRed);

    window.draw(circleRed);

    window.display();

  }//This is the end of the "while" loop

  return 0;
}

0 个答案:

没有答案