我想用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;
}
}
Run Code Online (Sandbox Code Playgroud)
这看起来像这样:

但是我如何从形状中获得角落和侧面?如果我使用getPoint(0)作为角落,它不会绘制它所属的点.如果这不是一个好主意,我可以用什么来解决这个问题?
我很久以前就采用了这种机制,这是实现这一目标的一种简单方法.
我的方法是将每个六边形表示为一个圆圈.绘制的六边形嵌入该圆圈中.要检查鼠标是在角落还是侧面,我做了一个简单的检查:
如果该点同时位于3个圆圈内,则为一个角(这3个圆圈的会合角)
如果该点位于2个圆圈内,那么它就是一个侧面.
如果该点在1圈内,则它是整个六边形
概念证明:
蓝色六边形符合正确的板,每个都有一个红色圆圈(略大于圆圈).
绿色六边形不在棋盘中(它们不是游戏板的一部分),它们有助于了解鼠标是否在外六角形的侧面或角落上方.
完整代码在我的Github存储库中,但是很老,可能已经过时了