我认为这是一个相当容易实现的事情,但我只是希望幽灵开始一次向玩家移动一个空间,如果玩家在幽灵的5个空间内。在move_to_x_y中的if语句中,应该添加更改。如何根据玩家的位置移动if语句?
这是检查鬼是否在五个空格内的函数:
bool Game::nearby (Ghost & ghost) const
{
if (abs(m_player->get_x() - ghost.get_x()) < 5)
return true;
if (abs(m_player->get_y() - ghost.get_y()) < 5)
return true;
return false;
}
然后这是幽灵的移动功能,他通常只是随机移动,这是玩家不在五个空间内的其他情况:
void Ghost::move (Game & game) {
Floor * floor;
int r;
bool moved = false;
floor = game.get_curr_floor();
do
{
if (game.nearby(*this))
r = //WHAT TO ADD HERE???
else {
r = rand() % 4;
switch (r)
{
case 0:
moved = floor->move_to_x_y (game, *this, 1, 0);
break;
case 1:
moved = floor->move_to_x_y (game, *this, 0, -1);
break;
case 2:
moved = floor->move_to_x_y(game, *this, -1, 0);
break;
case 3:
moved = floor->move_to_x_y(game, *this, 0, 1);
break;
}
}
}
while (! moved);
}
因此,根据玩家的位置,向上,向下,向左或向右移动。
感谢您的帮助!
答案 0 :(得分:5)
我可能会这样做:
if (game.nearby(*this))
{
int x = player_position_x - ghost_position_x;
int y = player_position_y - ghost_position_y;
if (abs(x) > abs(y))
{
assert(x != 0);
x = x / abs(x);
y = 0;
}
else
{
assert(y != 0);
x = 0;
y = y / abs(y);
}
floor->move_to_x_y (game, *this, x, y);
moved = true;
}
答案 1 :(得分:3)
你的函数返回玩家是否在附近,但没有提供玩家所处方向的信息。将bool的返回值更改为int,或指示方向。例如:
int Game::nearby (Ghost & ghost) const
{
if (ghost.get_x() - m_player->get_x() < 5 && ghost.get_x() - m_player->get_x() > 0)
return 2;
if (m_player->get_x() - ghost.get_x() < 5 && m_player->get_x() - ghost.get_x() > 0)
return 0;
if (ghost.get_y() - m_player->get_y() < 5 && ghost.get_y() - m_player->get_y() > 0)
return 3;
if (m_player->get_y() - ghost.get_y() < 5 && m_player->get_y() - ghost.get_y() > 0)
return 1;
return -1;
}
这将返回已经与您希望他在switch语句中移动的方向对应的数字。所以你在移动函数中所要做的就是将r设置为int&#34; near&#34;返回,如果返回-1,则将其设置为随机方向。
r = game.nearby(*this);
if (r == -1)
r = rand() % 4;
switch (r) .... etc
答案 2 :(得分:2)
如果玩家在附近,我会更改bool Game::nearby
以返回指南针点。
编辑:对误解设计的评论
Game::nearby()
并非旨在强制移动幽灵。它旨在为幽灵提供有关球员位置的信息。如何使用这些信息取决于鬼魂战略。因此附近应返回度数,罗盘点或距离矢量方向。然后幽灵可能决定接近玩家或坐下来躲避。在pacman游戏中,所有4个怪物都可以使用此功能,但应用不同的策略。
答案 3 :(得分:0)
两部分。首先,尝试靠近玩家
void Ghost::move (Game & game) {
Floor * floor game.get_curr_floor();
bool moved = false;
if (game.nearby(*this)) {
//try to move closer in x direction
if (game.get_player()->get_x() > ghost.get_x())
moved = floor->move_to_x_y (game, *this, 1, 0);
else if (ghost.get_x() > game.get_player()->get_x())
moved = floor->move_to_x_y (game, *this, -1, 0);
//if we haven't moved, try to move closer in y direction
if (!moved && game.get_player()->get_y() > ghost.get_y())
moved = floor->move_to_x_y (game, *this, 0, 1);
else if (!moved && ghost.get_y() > game.get_player()->get_y())
moved = floor->move_to_x_y (game, *this, 0, -1);
}
如果失败或玩家距离太远,请尝试随机移动。如果随机方向失败,请按顺序尝试下一个方向。这意味着我们将尝试最多四个方向,没有无限循环的机会。这在三向交叉口处略有偏差,但这可能并不明显。它还有副作用,即如果鬼魂无法移动,游戏就不会冻结。
//if not moved closer, move randomly
if (!moved) {
int r = rand() % 4; //pick random direction
switch(r) {
case 0:
if(floor->move_to_x_y (game, *this, 1, 0))
break; //if it failed, try the next one
case 1:
if(floor->move_to_x_y (game, *this, -1, 0))
break; //if it failed, try the next one
case 2:
if(floor->move_to_x_y (game, *this, 0, 1))
break; //if it failed, try the next one
case 3:
if(floor->move_to_x_y (game, *this, 0, -1))
break; //if it failed, try the next one
default:
//if it was case 3, need to try all other directions still
if(floor->move_to_x_y (game, *this, 1, 0))
break; //if it failed, try the next one
if(floor->move_to_x_y (game, *this, -1, 0))
break; //if it failed, try the next one
floor->move_to_x_y (game, *this, 0, 1);
//at this point, all directions have been tried. We're done.
}//switch
} //if
}
评论指出nearby
函数中存在一个错误,它认为同一x轴上的任何内容都是&#34;关闭&#34;。
答案 4 :(得分:0)
解决方案:
对于每次更新的每个幽灵:
return
/ continue
)return
/ continue
)另外,不要使用&#34;魔术数字&#34; (case 0:
)在您的代码中。使用枚举或常量。
所以,摆脱&#34;附近&#34;并用返回玩家位置的函数替换它。