使用向量来创建形状

时间:2016-04-08 04:16:43

标签: c++ vector sfml

我最近一直在使用C ++ sfml库,我尝试使用向量来创建相同形状的多个,但我根本不知道如何使用sfml形状实现向量。

这是我正在使用的代码,我试图在每次移动时在蓝色方块后面创建更多BlueTiles,但我不知道如何。我也想知道如何缩短网格段。

#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>

int main()
{
// Window Declarations Start
sf::RenderWindow Window(sf::VideoMode(673,673), "TileEat");
Window.setFramerateLimit(5);
sf::Event event;
// Window Declarataions End

// The Blue Start
sf::RectangleShape Blue(sf::Vector2f(32,32));
Blue.setPosition(0,0);
Blue.setFillColor(sf::Color(0,0,255));
sf::RectangleShape BlueTile(sf::Vector2f(32,32));
BlueTile.setFillColor(sf::Color(0,0,255));
std::vector<sf::RectangleShape>BlueTileVector;
// The Blue End

// The Grid Start
sf::RectangleShape VerticalLine(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine2(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine3(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine4(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine5(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine6(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine7(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine8(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine9(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine10(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine11(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine12(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine13(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine14(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine15(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine16(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine17(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine18(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine19(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine20(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine21(sf::Vector2f(1,700));
sf::RectangleShape VerticalLine22(sf::Vector2f(1,700));
VerticalLine.setPosition(32,0);
VerticalLine2.setPosition(64,0);
VerticalLine3.setPosition(96,0);
VerticalLine4.setPosition(128,0);
VerticalLine5.setPosition(160,0);
VerticalLine6.setPosition(192,0);
VerticalLine8.setPosition(224,0);
VerticalLine9.setPosition(256,0);
VerticalLine10.setPosition(288,0);
VerticalLine11.setPosition(320,0);
VerticalLine12.setPosition(352,0);
VerticalLine13.setPosition(384,0);
VerticalLine14.setPosition(416,0);
VerticalLine15.setPosition(448,0);
VerticalLine16.setPosition(480,0);
VerticalLine17.setPosition(512,0);
VerticalLine18.setPosition(544,0);
VerticalLine19.setPosition(576,0);
VerticalLine20.setPosition(608,0);
VerticalLine21.setPosition(640,0);
VerticalLine22.setPosition(672,0);
sf::RectangleShape HorizontalLine(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine2(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine3(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine4(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine5(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine6(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine7(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine8(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine9(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine10(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine11(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine12(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine13(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine14(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine15(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine16(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine17(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine18(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine19(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine20(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine21(sf::Vector2f(700,1));
sf::RectangleShape HorizontalLine22(sf::Vector2f(700,1));
HorizontalLine.setPosition(0,32);
HorizontalLine2.setPosition(0,64);
HorizontalLine3.setPosition(0,96);
HorizontalLine4.setPosition(0,128);
HorizontalLine5.setPosition(0,160);
HorizontalLine6.setPosition(0,192);
HorizontalLine7.setPosition(0,224);
HorizontalLine8.setPosition(0,256);
HorizontalLine9.setPosition(0,288);
HorizontalLine10.setPosition(0,320);
HorizontalLine11.setPosition(0,352);
HorizontalLine12.setPosition(0,384);
HorizontalLine13.setPosition(0,416);
HorizontalLine14.setPosition(0,448);
HorizontalLine15.setPosition(0,480);
HorizontalLine16.setPosition(0,512);
HorizontalLine17.setPosition(0,544);
HorizontalLine18.setPosition(0,576);
HorizontalLine19.setPosition(0,608);
HorizontalLine20.setPosition(0,640);
HorizontalLine21.setPosition(0,672);
// The Grid End

// Game Loop Start
while (Window.isOpen())
{
    while (Window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            Window.close();
    }

    // Blue Movement Start
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){Blue.move(0,32);BlueTileVector.push_back(BlueTile);}
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){Blue.move(0,-32);}
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){Blue.move(32,0);}
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){Blue.move(-32,0);}
    if(Blue.getPosition().x >= 672){Blue.setPosition(Blue.getPosition().x-32,Blue.getPosition().y);}
    else if(Blue.getPosition().x <= -32){Blue.setPosition(Blue.getPosition().x+32,Blue.getPosition().y);}
    if(Blue.getPosition().y >= 672){Blue.setPosition(Blue.getPosition().x,Blue.getPosition().y-32);}
    else if(Blue.getPosition().y <= -32){Blue.setPosition(Blue.getPosition().x,Blue.getPosition().y+32);}
    // Blue Movement End

    // Drawing Table Start
    Window.clear();
    Window.draw(Blue);
    Window.draw(VerticalLine);
    Window.draw(VerticalLine2);
    Window.draw(VerticalLine3);
    Window.draw(VerticalLine4);
    Window.draw(VerticalLine5);
    Window.draw(VerticalLine6);
    Window.draw(VerticalLine7);
    Window.draw(VerticalLine8);
    Window.draw(VerticalLine9);
    Window.draw(VerticalLine10);
    Window.draw(VerticalLine11);
    Window.draw(VerticalLine12);
    Window.draw(VerticalLine13);
    Window.draw(VerticalLine14);
    Window.draw(VerticalLine15);
    Window.draw(VerticalLine16);
    Window.draw(VerticalLine17);
    Window.draw(VerticalLine18);
    Window.draw(VerticalLine19);
    Window.draw(VerticalLine20);
    Window.draw(VerticalLine21);
    Window.draw(VerticalLine22);
    Window.draw(HorizontalLine);
    Window.draw(HorizontalLine2);
    Window.draw(HorizontalLine3);
    Window.draw(HorizontalLine4);
    Window.draw(HorizontalLine5);
    Window.draw(HorizontalLine6);
    Window.draw(HorizontalLine7);
    Window.draw(HorizontalLine8);
    Window.draw(HorizontalLine9);
    Window.draw(HorizontalLine10);
    Window.draw(HorizontalLine11);
    Window.draw(HorizontalLine12);
    Window.draw(HorizontalLine13);
    Window.draw(HorizontalLine14);
    Window.draw(HorizontalLine15);
    Window.draw(HorizontalLine16);
    Window.draw(HorizontalLine17);
    Window.draw(HorizontalLine18);
    Window.draw(HorizontalLine19);
    Window.draw(HorizontalLine20);
    Window.draw(HorizontalLine21);
    Window.draw(HorizontalLine22);
    Window.display();
    // Drawing Table End
}
std::cout << "BlueTileVector Size: " << BlueTileVector.size();
return 0;
// Game Loop End
}

2 个答案:

答案 0 :(得分:3)

创建一个矢量来保存你的对象:

std::vector<sf::RectangleShape> rectangles;

在循环中向向量中添加元素:

// Create 20 rectangle shapes
for (int i = 0; i != 20; ++i)
    rectangles.emplace_back(sf::Vector2f(1, 700));

你也可以在上面的循环或它自己的循环中设置它们的位置:

for (auto& i : rectangles)
    i.setPosition(200.f, 200.f);

您还可以使用循环绘制形状:

for (const auto& rect : rectangles)
    window.draw(rect);

FYI。上面的例子需要-std=c++11标志来编译。否则,可以使用常规for循环和push_back(sf::RectangleShape(sf::Vector2f(1, 700)))

答案 1 :(得分:1)

您可能想了解循环和容器。他们真的很有帮助:

enter image description here

#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>

int main()
{
  const size_t numberOfRowsAndColunms = 20;

  sf::VideoMode videoMode = sf::VideoMode(600, 600);

  sf::RenderWindow Window(videoMode, "TileEat");

  Window.setFramerateLimit(30);

  sf::Event event;

  const sf::Vector2f gridSize = sf::Vector2f(videoMode.width / numberOfRowsAndColunms, videoMode.height / numberOfRowsAndColunms);

  // The Blue Start
  sf::RectangleShape Blue(gridSize);
  Blue.setPosition(0, 0);
  Blue.setFillColor(sf::Color(0, 0, 255));

  std::vector<sf::RectangleShape> shadows;
  // The Blue End

  std::vector<sf::RectangleShape> grid;

  for (int x = 0; x < numberOfRowsAndColunms; x++)
  {
    for (int y = 0; y < numberOfRowsAndColunms; y++)
    {
      sf::RectangleShape cell(gridSize);

      cell.setFillColor(sf::Color::Transparent);
      cell.setOutlineColor(sf::Color::White);
      cell.setOutlineThickness(2.0f);
      cell.setPosition(gridSize.x * x, gridSize.y * y);

      grid.push_back(cell);
    }
  }

  // Game Loop Start
  while (Window.isOpen())
  {
    while (Window.pollEvent(event))
    {
      if (event.type == sf::Event::Closed)
        Window.close();
    }

    // Blue Movement Start
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
    {
      Blue.move(0, gridSize.y); 
      shadows.push_back(Blue);
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
    {
      Blue.move(0, -gridSize.y);
      shadows.push_back(Blue);
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
    {
      Blue.move(gridSize.x, 0);
      shadows.push_back(Blue);
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
    {
      Blue.move(-gridSize.x, 0);
      shadows.push_back(Blue);
    }

    // Blue bounds check
    auto position = Blue.getPosition();

    position.x = std::min(videoMode.width - gridSize.x, std::max(0.0f, position.x));
    position.y = std::min(videoMode.height - gridSize.y, std::max(0.0f, position.y));

    Blue.setPosition(position);

    // Drawing
    Window.clear();

    int shadowCount = 0;

    for (auto shadow : shadows)
    {
      shadowCount++;

      shadow.setScale(sf::Vector2f(shadowCount / (float)shadows.size(), shadowCount / (float)shadows.size()));

      Window.draw(shadow);
    }

    Window.draw(Blue);

    for (auto line : grid)
    {
      Window.draw(line);
    }

    Window.display();
  }

  return 0;
}