我的蛇游戏中的TailCollision函数遇到问题。现在,它根本不起作用。除此之外,蛇被抽出并完美地移动。在此先感谢那些指出我犯了错误的人^-^

EM我是sfml的新手,正在使用向量...几乎是我第一次来,所以请不要太苛刻!

这是我的Snake.cpp

#include "Snake.h"


Snake::Snake(){

    Snakey.setSize(sf::Vector2f(10, 10));

    Snakey.setFillColor(sf::Color(0,255,0));


    enter code here
    Snakey.setPosition(180,60);

    BodyList.push_back(Body);

}


Snake::~Snake(){
    BodyList.clear();
}

void Snake::Draw(sf::RenderWindow &window){

    window.draw(Snakey);

    for (sf::RectangleShape& Body : BodyList){

    window.draw(Body);
    }

    BodyList.erase(BodyList.begin());
    BodyList.push_back(Body);

}

void Snake::Grow(){

    Body.setSize(sf::Vector2f(10, 10));

    Body.setFillColor(sf::Color(0,100,0));

    Body.setPosition(Snakey.getPosition().x,Snakey.getPosition().y);

    BodyList.push_back(Body);


}



void Snake::Move(sf::Event event, sf::RenderWindow &window){

    if (sf::Keyboard::isKeyPressed && sf::Keyboard::Left == event.key.code && Dir != "Right"){
        Snakey.move(-10, 0);
        Body.move(-10,0);
        Dir = ("Left");
    }

    else if (sf::Keyboard::isKeyPressed && sf::Keyboard::Key::Right == event.key.code && Dir != "Left"){
        Snakey.move(10, 0);
        Body.move(10,0);

        Dir = ("Right");
    }
    else if (sf::Keyboard::isKeyPressed && sf::Keyboard::Key::Up == event.key.code && Dir != "Down"){
        Snakey.move(0, -10);
        Body.move(0,-10);

        Dir = ("Up");
    }
    else if (sf::Keyboard::isKeyPressed && sf::Keyboard::Key::Down == event.key.code && Dir != "Up"){
        Snakey.move(0, 10);
        Body.move(0,10);

        Dir = ("Down");
    }

    else if (sf::Keyboard::isKeyPressed && sf::Keyboard::Key::Down == event.key.code && Dir == "Up" ||
        sf::Keyboard::isKeyPressed && sf::Keyboard::Key::Up == event.key.code && Dir == "Down" ||
        sf::Keyboard::isKeyPressed && sf::Keyboard::Key::Right == event.key.code && Dir == "Left"||
        sf::Keyboard::isKeyPressed && sf::Keyboard::Left == event.key.code && Dir == "Right"){

        window.close();
    }
}

void Snake::TailCollision(sf::RenderWindow &window){

    for (int i = 0 ; i < BodyList.size(); i++){
        if ((Snakey.getPosition().x == BodyList[i].getPosition().x && Snakey.getPosition().y == BodyList[i].getPosition().y)&&
            (BodyList[i].getPosition().x != BodyList.back().getPosition().x &&
            BodyList[i].getPosition().y != BodyList.back().getPosition().y)){

                window.close();

        }

    }
}


sf::RectangleShape Snake::GetShape(){
    return Snakey;
}

最佳答案

我认为最好使用SFML内置函数,例如getGlobalBounds()
如果Snakey是sf::Shape子类(我希望是),它应该可以工作。

const auto & headBounds = Snakey.getGlobalBounds();
for (int i = 0 ; i < BodyList.size(); i++)
{
    const auto & bodyBounds = BodyList[i].getGlobalBounds();
    if( bodyBounds.intersects( headBounds )
    {
        // process collision here
        break;
    }
}


甚至更短,如注释中所建议:

const auto & headBounds = Snakey.getGlobalBounds();
if(
    std::any_of(
            BodyList.begin()
        ,   BodyList.end()
        ,   [ headBounds ] ( const auto & body )
            {
                return body.getGlobalBounds().intersects( headBounds );
            }
   )
)
{
    // process collision;
}

关于c++ - 蛇游戏与尾部碰撞,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48681492/

10-10 11:57