【问题标题】:trying to draw Pac-man试图画吃豆人
【发布时间】:2020-11-08 06:35:43
【问题描述】:

我正在尝试使用 SFML 中的凸形类将 Pac-man 绘制为作业的一部分,但我总是从点 0,0 绘制一个额外的部分

#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
using namespace std;
#define PI 3.14f
#define Deg2Rad PI/180
ConvexShape Pac(float radius, Vector2f center)
{
    int pointsCount = 315;
    float theta = 0;
    ConvexShape circle;
    circle.setPointCount(pointsCount);
    for (int i = 45, index = 0; i < 315; i++, index++)
    {
        Vector2f point;
        theta = i * Deg2Rad;
        point.x = center.x + radius * cos(theta);
        point.y = center.y + radius * sin(theta);
        cout << point.x << "," << point.y << endl;
        circle.setPoint(index, point);
    }
    return circle;
}
int main()
{
    RenderWindow window(VideoMode(500, 500), "Pac_man");
    ConvexShape pacman;
    pacman = Pac(100.0f, Vector2f(100, 100));
    pacman.setFillColor(sf::Color::Yellow);
    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            switch (event.type)
            {
            case Event::Closed:
                window.close();
                break;
            }
        }
        window.clear();
        window.draw(pacman);
        window.display();
    }
    return 0;
}

总是这样

【问题讨论】:

  • 你能用这个方法正确渲染一个三角形吗?不要计算角位置,而是将它们硬编码到您的源代码中。如果这有效,那么你的错误一定是介于两者之间。如果没有,你有一个minimal reproducible example。作为这里的新用户,也请带上tour并阅读How to Ask

标签: c++ sfml


【解决方案1】:

您的 for 循环仅迭代 270 次 (315 - 45),因此仅设置 315 个顶点中的 270 个。其他 45 个顶点在默认位置 (0, 0)。

您的下一个问题是您没有在中心创建顶点。所以你需要一个额外的顶点作为中心。

将这两个问题排序后,结果为:

编辑:我现在也意识到吃豆子不是凸形,所以“它可能没有正确绘制”。要正确绘制它,您需要将其拆分为多个凸形,或使用不同的原始类型(sf::TriangleFan?)

【讨论】:

    猜你喜欢
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-15
    • 2021-10-13
    相关资源
    最近更新 更多