gpt4 book ai didi

c++ - 与尾部的蛇比赛碰撞

转载 作者:行者123 更新时间:2023-11-28 04:48:37 27 4
gpt4 key购买 nike

我的贪吃蛇游戏中的 TailCollision 函数有问题。现在它根本不起作用。除此之外,蛇被绘制并完美移动。提前感谢指出我错误之处的人 ^-^

Em 我是 sfml 的新手,使用 vector ...这几乎是我的第一次,所以请不要太苛刻!

这是我的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com