绘画tetrominos(俄罗斯方块片)

问题描述 投票:0回答:1

我在创建/绘制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;
    }

The L n J shape appear like this.

c++ sfml
1个回答
1
投票

首先,当使用像SFML这样的库时,重要的是要始终记住库可能为您提供方便的快捷方式(如sf::CircleShapesf::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:

Example Screenshot

对于你的情况,你当然必须正确地缩放它们。

如果您仍然遇到问题或想要编译,运行和玩游戏,我已经在GitHub上传了一个short example

© www.soinside.com 2019 - 2024. All rights reserved.