我想用SFML创建卡坦卡游戏的棋盘,所有我需要的是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)作为角点,它不会画出它所属的点。如果这不是一个好主意,我可以用什么来解决这个问题?
发布于 2018-11-30 20:24:47
我很久以前就做过这种机制,一种简单的实现方法。
我的方法是将每个六边形表示为一个圆形。画出的六边形被嵌入到那个圆中。为了检查鼠标是在角落上还是在一边,我做了一个简单的检查:
概念验证:

蓝色的六边形符合正确的棋盘,每一个都有一个红色的圆圈(比六边形稍大一点)。
绿色的六边形不在棋盘上(它们不是游戏棋盘的一部分),它们有助于知道鼠标是否在外六角的边上或角落上。
完整的代码在my Github repository中,但是太旧了,可能已经过时了
https://stackoverflow.com/questions/53553034
复制相似问题