【问题标题】:SFML not drawing multiple CirclesSFML 不绘制多个圆
【发布时间】:2021-10-02 12:48:34
【问题描述】:

我正在尝试使用 SFML 在 C++ 中对井字游戏进行编程。单击鼠标左键后,我将其编程为绘制一个圆圈。然后当我再次单击时,它会在另一个位置重新绘制该圆圈,而不是绘制另一个圆圈。我想让它在不同的地方画另一个圆圈。

我在名为CreateO的函数中使用sf::CircleShape SFML 数据类型绘制圆:

sf::CircleShape CreateO(float x_pos, float y_pos)
{
  // Radius: 50px                                                                                                                                                                                                   
  sf::CircleShape o(50);
  o.setFillColor(sf::Color::Black);
  o.setOutlineThickness(1);
  o.setOutlineColor(sf::Color::White);
  o.setPosition(x_pos, y_pos);
  return o;
}

在主函数中:

  sf::RenderWindow window(sf::VideoMode(900, 900), "Tic Tac Toe");
  sf::Event event;
  bool draw_o;
  float x_pos, y_pos;
  while (window.isOpen())
    {
      while (window.pollEvent(event))
    {
      switch (event.type)
        {
        case sf::Event::Closed: window.close(); break;
        case sf::Event::MouseButtonPressed:
              switch (event.mouseButton.button)
                {
                case sf::Mouse::Right:
                  draw_x = true;
                  draw_o = false;
                  x_pos  = event.mouseButton.x;
                  y_pos  = event.mouseButton.y;
                  break;
                default: break;
                }
              break;
            default: break;
            }
        }
      window.clear();
      if (draw_o) window.draw(CreateO(x_pos, y_pos)); // Called here.
      window.display();
    }

【问题讨论】:

    标签: c++ sfml


    【解决方案1】:

    分析你的那部分代码:

    window.clear();
    if (draw_o) window.draw(CreateO(x_pos, y_pos)); // Called here.
    window.display();
    

    它清除整个窗口并绘制函数给出的一个圆圈。

    您只能在游戏循环中画一个圆圈。要拥有多个圆圈,我建议创建一个圆圈矢量,例如:

    std::vector<sf::CircleShape> circles;
    

    与window.draw 部分相比,您需要使用 for 循环绘制整个向量。

    如果draw_o 为真,您的CreateO 函数可以在向量中添加一个圆圈。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      相关资源
      最近更新 更多