gpt4 book ai didi

c++ - 如果玩家在附近,将幽灵移向迷宫中的玩家?

转载 作者:太空狗 更新时间:2023-10-29 23:39:48 24 4
gpt4 key购买 nike

我认为这是一件相当容易实现的事情,但我只想让鬼魂开始一次向玩家移动一个空间,如果玩家在鬼魂的 5 个空间内。在我的 if 语句中,move_to_x_y 是应该添加更改的地方。我怎样才能让 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;
}

然后是幽灵的移动函数,他通常只是朝一个随机的方向移动,当玩家不在五个空间内时是else情况:

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);
}

因此,根据玩家的位置,向上、向下、向左或向右移动,朝向他们。

感谢您的帮助!

最佳答案

我可能会做这样的事情:

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;
}

关于c++ - 如果玩家在附近,将幽灵移向迷宫中的玩家?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29109332/

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