我无法创建/绘制tetrominos(俄罗斯方块),使用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;
}
答案 0 :(得分:1)
首先,在使用SFML等库时,始终记住库可能为您提供方便的快捷方式(例如sf::CircleShape
或sf::RectangleShape
)非常重要,但是您不一定要使用它们,特别是如果它给你提供了更灵活的东西,比如sf::VertexArray
。
接下来,您还必须记住,从基本原语构建您的tetrominos的方法不止一种。
这里最明显的一个是使用三角形或四边形。让我们使用四边形构建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}
的翻译绘制这两个形状会在屏幕上产生两个巨大的tetrominos:
对于您的情况,您当然必须正确缩放它们。
如果您仍然遇到问题或想要编译,运行和玩游戏,我已经在GitHub上传了short example。