gpt4 book ai didi

c++ - 绘制俄罗斯方 block (俄罗斯方 block )

转载 作者:行者123 更新时间:2023-11-28 04:52:05 28 4
gpt4 key购买 nike

我在创建/绘制俄罗斯方 block (俄罗斯方 block )时遇到问题,我在使用 SFML 的 C++ 中有哪些选择?

我尝试使用 switch 语句将它们绘制为凸形并给它们编号,但这没有用,因为 L 和 J 形状是凹形。

 ConvexShape blocks;
blocks.setTexture(&tile);

// block generation and shape
default_random_engine randomGenerator(time(0));
uniform_int_distribution<int> blocktype(0,6);
int n = blocktype(randomGenerator);
switch (n)
{
case (0): blocks.setPointCount(4);
blocks.setPoint(0,Vector2f(0.0f,0.0f));
blocks.setPoint(1,Vector2f(18.0f,0.0f));
blocks.setPoint(2,Vector2f(18.0f,72.0f));
blocks.setPoint(3,Vector2f(0.0f,72.0f));
break;
case (1): blocks.setPointCount(4);
blocks.setPoint(0,Vector2f(0.0f,0.0f));
blocks.setPoint(1,Vector2f(36.0f,0.0f));
blocks.setPoint(2,Vector2f(36.0f,36.0f));
blocks.setPoint(3,Vector2f(0.0f,36.0f));
break;
case (2): blocks.setPointCount(8);
blocks.setPoint(0,Vector2f(0.0f,0.0f));
blocks.setPoint(1,Vector2f(18.0f,0.0f));
blocks.setPoint(2,Vector2f(18.0f,18.0f));
blocks.setPoint(3,Vector2f(36.0f,18.0f));
blocks.setPoint(4,Vector2f(36.0f,36.0f));
blocks.setPoint(5,Vector2f(18.0f,36.0f));
blocks.setPoint(6,Vector2f(18.0f,54.0f));
blocks.setPoint(7,Vector2f(0.0f,54.0f));
break;
case (3): blocks.setPointCount(8);
blocks.setPoint(0,Vector2f(0.0f,0.0f));
blocks.setPoint(1,Vector2f(18.0f,0.0f));
blocks.setPoint(2,Vector2f(18.0f,18.0f));
blocks.setPoint(3,Vector2f(36.0f,18.0f));
blocks.setPoint(4,Vector2f(36.0f,54.0f));
blocks.setPoint(5,Vector2f(18.0f,54.0f));
blocks.setPoint(6,Vector2f(18.0f,36.0f));
blocks.setPoint(7,Vector2f(0.0f,36.0f));
break;
case (4): blocks.setPointCount(8);
blocks.setPoint(0,Vector2f(0.0f,18.0f));
blocks.setPoint(1,Vector2f(18.0f,18.0f));
blocks.setPoint(2,Vector2f(18.0f,0.0f));
blocks.setPoint(3,Vector2f(36.0f,0.0f));
blocks.setPoint(4,Vector2f(36.0f,36.0f));
blocks.setPoint(5,Vector2f(18.0f,36.0f));
blocks.setPoint(6,Vector2f(18.0f,54.0f));
blocks.setPoint(7,Vector2f(0.0f,54.0f));
break;
case (5): blocks.setPointCount(6);
blocks.setPoint(0,Vector2f(0.0f,54.0f));
blocks.setPoint(1,Vector2f(0.0f,0.0f));
blocks.setPoint(2,Vector2f(18.0f,0.0f));
blocks.setPoint(3,Vector2f(18.0f,40.0f));
blocks.setPoint(4,Vector2f(36.0f,36.0f));
blocks.setPoint(5,Vector2f(36.0f,54.0f));
break;
case (6): blocks.setPointCount(6);
blocks.setPoint(0,Vector2f(0.0f,0.0f));
blocks.setPoint(1,Vector2f(54.0f,0.0f));
blocks.setPoint(2,Vector2f(54.0f,18.0f));
blocks.setPoint(3,Vector2f(36.0f,18.0f));
blocks.setPoint(4,Vector2f(36.0f,36.0f));
blocks.setPoint(5,Vector2f(18.0f,36.0f));
break;
}

The L n J shape appear like this.

最佳答案

首先,在使用诸如 SFML 之类的库时,务必记住该库可能会为您提供方便的快捷方式(如 sf::CircleShapesf::RectangleShape ),但您不一定非要使用它们,尤其是当它向您展示一些更灵活的东西时,例如 sf::VertexArray

接下来,您还必须记住,有不止一种方法可以从基本原语构建您的四联骨牌。

这里最明显的是使用三角形或四边形。让我们使用四边形构建 T 形:

sf::VertexArray quads(sf::Quads);

// Left piece
quads.append({{0, 0}, sf::Color::White});
quads.append({{95, 0}, sf::Color::Red});
quads.append({{95, 95}, sf::Color::Green});
quads.append({{0, 95}, sf::Color::Blue});

// Center piece
quads.append({{100, 0}, sf::Color::White});
quads.append({{195, 0}, sf::Color::Red});
quads.append({{195, 95}, sf::Color::Green});
quads.append({{100, 95}, sf::Color::Blue});

// Right piece
quads.append({{200, 0}, sf::Color::White});
quads.append({{295, 0}, sf::Color::Red});
quads.append({{295, 95}, sf::Color::Green});
quads.append({{200, 95}, sf::Color::Blue});

// Bottom piece
quads.append({{100, 100}, sf::Color::White});
quads.append({{195, 100}, sf::Color::Red});
quads.append({{195, 195}, sf::Color::Green});
quads.append({{100, 195}, sf::Color::Blue});

请注意,sf::Quads 在移动平台上不受支持,您必须定义三角形而不是四边形。我还特意将四边形的长度/宽度缩短了 5 倍,以留下可见的间隙。

可能更优雅(和有效)的解决方案是使用三角风扇。由于 T tetromino 是一个凹形,您必须为您的风扇选择正确的起点,即使形状凹入的点之一。在其他情况下,您可能必须为此专门创建一个新的(不可见的)点,例如在形状内。

sf::VertexArray triangleFan(sf::TriangleFan);

// Starting point is the bottom left corner of the center piece
triangleFan.append({{100, 100}, sf::Color::White});
triangleFan.append({{0, 100}, sf::Color::Red});
triangleFan.append({{0, 0}, sf::Color::Green});
triangleFan.append({{300, 0}, sf::Color::Blue});
triangleFan.append({{300, 100}, sf::Color::Magenta});
// The next point creates an invisible triangle just to move to the center piece
triangleFan.append({{200, 100}, sf::Color::Black});
triangleFan.append({{200, 200}, sf::Color::Cyan});
triangleFan.append({{100, 200}, sf::Color::Yellow});

{50, 50}{50, 350} 的翻译分别绘制这两个形状会在屏幕上产生两个巨大的四联骨牌:

Example Screenshot

对于您的情况,您当然必须适本地缩放它们。

如果您仍然遇到问题或想要编译、运行和试用某些东西,我已经上传了 short example在 GitHub 上。

关于c++ - 绘制俄罗斯方 block (俄罗斯方 block ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47958910/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com