蛇游戏与尾巴相撞

时间:2018-02-08 09:04:26

标签: c++ sfml stdvector

我在蛇游戏中遇到了 TailCollision 功能的问题。现在它根本不起作用。除此之外,蛇被绘制并完美地移动。提前谢谢谁指出我在哪里搞笑^ - ^

我是sfml的新手并使用矢量...几乎是我的第一次所以请不要太苛刻!

这是我的Snake.cpp

date_started

2 个答案:

答案 0 :(得分:1)

我认为最好使用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;
}

答案 1 :(得分:0)

我已经意识到问题出在我的移动功能上,甚至没有碰撞!谢谢大家,我很抱歉浪费你的时间! ^ - ^我真的很蠢...

Snake::Snake(){

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

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

    Snakey.setPosition(180,60);

}


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

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

    window.draw(Snakey);

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

    window.draw(Body);
    }

}

void Snake::Grow(){

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

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

    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"){

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

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

        Snakey.move(-10, 0);

        Dir = ("Left");
    }

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

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

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

        Snakey.move(10, 0);

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

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

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

        Snakey.move(0, -10);

        Dir = ("Up");
    }
    else if (sf::Keyboard::isKeyPressed && sf::Keyboard::Key::Down == event.key.code && Dir != "Up"){

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

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

        Snakey.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)){

                window.close();

        }

    }
}