我正在制作一个简单的钓鱼游戏,但碰撞检测有问题。我有4个班级(渔夫,鱼,钩,鲨鱼),需要检测渔夫和鲨鱼之间以及鱼和钩之间的碰撞。
两次碰撞都没有起作用,但我现在只发布一个鲨鱼/渔夫。当我说不工作时,它们不会在处理中产生任何错误,并且所有其他操作都可以正常工作(当对象到达屏幕顶部时重置高度)但是它没有检测到碰撞并且没有按预期停止游戏
// shark类中的布尔值在与Fisherman(player1)碰撞时返回true
boolean sharkHitBoat(Fisherman player1)
{
int player1Left = player1.x;
int player1Right = player1.x + 80;
int player1Top = player1.y;
int player1Bottom = player1.y + 20;
int shark1Left = shark1.x;
int shark1Right = shark1.x+shark1.w;
int shark1Top = shark1.y;
if (shark1Top + shark1.dy >= player1Bottom && shark1Right >= player1Left && shark1Left <= player1Right && shark1Top <= player1Top)
{
return true;
}
else {
return false;
}
}
//然后我在我的主类中使用了void语句
下的if语句//if shark reaches top then reset, if shark hits boat then game over
if (shark1.sharkHitBoat(player1) == false && shark1.sharkReachTop() == false)
{
shark1.y=1000;
}
else if (shark1.sharkHitBoat(player1) == true)
{
gameMode = GAMEOVER;
textSize(60);
fill(255,30,0);
text("YOU LOSE", 270, 200);
textSize(30);
fill(255,255,255);
text("PRESS SPACE TO PLAY AGAIN", 200, 230);
}
// sharkReachTop boolean
boolean sharkReachTop()
{
y=y-dy;
return (y>=110);
}