c++ - 使用 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) 作为角落,它不会绘制它所属的点。如果这不是一个好主意,我可以用什么来解决这个问题?
解决方案
我很久以前就做过这种机制,一种简单的方法来实现它。
我的方法是将每个六边形表示为一个圆圈。绘制的六边形嵌入到那个圆圈中。为了检查鼠标是在角落还是一边,我做了一个简单的检查:
如果该点同时在 3 个圆圈内,则它是一个角(这 3 个六边形的交汇角)
如果该点在 2 个圆圈内,则为边。
如果该点在 1 个圆圈内,则它是一个完整的六边形
概念证明:
蓝色六边形符合正确的板,每个都有一个红色圆圈(略大于六边形)。
绿色六边形在棋盘外(它们不是游戏棋盘的一部分),它们有助于了解鼠标是否在外六边形的边或角上。
完整的代码在我的 Github 存储库中,但是很旧并且可能已经过时了
推荐阅读
- r - 我想根据样本 ID 中具有特定字母的观察结果在我的数据集中创建一个新变量
- php - 配置和添加客户端以使用 wordpress 作为 OpenId 提供程序
- c# - 在类库中定义 ref 类类型时出错
- reactjs - 如何解决 s3 key not found 错误
- ms-access - SubTotal Query based on condition of other column
- raku - Raku 中的 int32 失败
- c++ - 在对象中存储和检索不同的数据类型
- bash - BASH 脚本回显文本和命令
- c++ - 将 r 值引用唯一指针传递给 lambda 的正确方法
- ios - 为什么 Xcode 自动修复会创建两个同名的方法 `func tableView`?