首页 > 解决方案 > 使用 SFML 为 Catan 创建板

问题描述

我想用 SFML 为 Catan 游戏创建棋盘,我只需要 19 个形状(六边形),每个形状我都可以利用所有 6 个角和 6 个边来建造城市或道路。对于形状,我这样做:

std::vector<sf::CircleShape> shape(19);
int n = 0;
int shape_y = 100;
for (size_t index = 0; index < shape.size(); index++) {
    if (index < 3) {
        sf::CircleShape sh(80, 6);
        sh.setPosition(200 + n, shape_y);
        sh.setFillColor(sf::Color::Magenta);
        shape[index] = sh;
        n += 140;
    }
    if (index == 3)
        n = 0;
    if (index < 7 && index >= 3) {
        sf::CircleShape sh(80, 6);
        sh.setPosition(130 + n, shape_y + 120);
        sh.setFillColor(sf::Color::Blue);
        shape[index] = sh;
        n += 140;
    }
    if (index == 7)
        n = 0;
    if (index >= 7 && index < 12) {
        sf::CircleShape sh(80, 6);
        sh.setPosition(60 + n, shape_y + 240);
        sh.setFillColor(sf::Color::Red);
        shape[index] = sh;
        n += 140;
    }
    if (index == 12)
        n = 0;
    if (index >= 12 && index < 16) {
        sf::CircleShape sh(80, 6);
        sh.setPosition(130 + n, shape_y + 360);
        sh.setFillColor(sf::Color::Green);
        shape[index] = sh;
        n += 140;
    }
    if (index == 16)
        n = 0;
    if (index >= 16 && index < 19) {
        sf::CircleShape sh(80, 6);
        sh.setPosition(200 + n, shape_y + 480);
        sh.setFillColor(sf::Color::Yellow);
        shape[index] = sh;
        n += 140;
    }
}

这看起来像这样:

在此处输入图像描述

但是我如何从形状中得到角和边?如果我使用 getPoint(0) 作为角落,它不会绘制它所属的点。如果这不是一个好主意,我可以用什么来解决这个问题?

标签: c++sfml

解决方案


我很久以前就做过这种机制,一种简单的方法来实现它。

我的方法是将每个六边形表示为一个圆圈。绘制的六边形嵌入到那个圆圈中。为了检查鼠标是在角落还是一边,我做了一个简单的检查:

  • 如果该点同时在 3 个圆圈内,则它是一个角(这 3 个六边形的交汇角)

  • 如果该点在 2 个圆圈内,则为边。

  • 如果该点在 1 个圆圈内,则它是一个完整的六边形

概念证明:

在此处输入图像描述

蓝色六边形符合正确的板,每个都有一个红色圆圈(略大于六边形)。

绿色六边形在棋盘外(它们不是游戏棋盘的一部分),它们有助于了解鼠标是否在外六边形的边或角上。

完整的代码在我的 Github 存储库中,但是很旧并且可能已经过时了


推荐阅读