试图画吃豆人

时间:2020-11-08 06:35:43

标签: c++ sfml

我正在尝试使用SFML中的凸形类绘制“吃豆人”作为作业的一部分,而且我总是从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;
}

它总是这样出现的

Pac-man with (0,0) point

1 个答案:

答案 0 :(得分:3)

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

您的下一个问题是您不会在中心创建一个顶点。因此,您需要一个额外的顶点作为中心。

对这两个问题进行排序后,结果为:pacman

编辑:我现在也意识到Pacman不是凸形的,因此“它可能无法正确绘制”。要正确绘制它,您需要将其拆分为多个凸形,或使用其他原始类型(sf :: TriangleFan?)