gpt4 book ai didi

Python - 在 'array' 中寻找最低位置

转载 作者:太空宇宙 更新时间:2023-11-03 11:38:22 25 4
gpt4 key购买 nike

我的目标是将二维网格中的“怪物”(mX, mY) 移向玩家 (pX, pY)。怪物可以向 8 个不同的方向移动。

我有这方面的工作代码,但我对 Python 还很陌生。我有一种强烈的倾向,我的代码很糟糕,而且有更快的方法来做到这一点。

我通过围绕怪物的位置(阵列槽 4)创建一个 3 x 3 阵列,并用该阵列位置到玩家的距离来填充它。然后我检查是否有任何低于怪物当前距离的,如果是,则将怪物移动到它。

enter image description here

这是我当前的代码。如果让您呕吐,我深表歉意,我仍在学习技巧。

# get the distance between the monster and player
dist = math.hypot(pX - mX, pY - mY)

if dist > 1.5 and dist < 10:

# make an 'array' grid to store updated distances in
goto = np.full((3, 3), 10, dtype=float)

# if each position in the array passes a
# collision check, add each new distance

if collisionCheck(mID, (mX-1), (mY-1), mMap) == 0:
goto[0][0] = round(math.hypot(pX - (mX-1), pY - (mY-1)), 1)

if collisionCheck(mID, mX, (mY-1), mMap) == 0:
goto[0][1] = round(math.hypot(pX - mX, pY - (mY-1)), 1)

if collisionCheck(mID, (mX+1), (mY-1), mMap) == 0:
goto[0][2] = round(math.hypot(pX - (mX+1), pY - (mY-1)), 1)

if main.collisionCheck(mID, (mX-1), mY, mMap) == 0:
goto[1][0] = round(math.hypot(pX - (mX-1), pY - mY), 1)

# goto[1][1] is skipped since that is the monsters current position

if collisionCheck(mID, (mX+1), mY, mMap) == 0:
goto[1][2] = round(math.hypot(pX - (mX+1), pY - mY), 1)

if collisionCheck(mID, (mX-1), (mY+1), mMap) == 0:
goto[2][0] = round(math.hypot(pX - (mX-1), pY - (mY+1)), 1)

if collisionCheck(mID, mX, (mY+1), mMap) == 0:
goto[2][1] = round(math.hypot(pX - mX, pY - (mY+1)), 1)

if collisionCheck(mID, (mX+1), (mY+1), mMap) == 0:
goto[2][2] = round(math.hypot(pX - (mX+1), pY - (mY+1)), 1)

# get the lowest distance, and its key
lowest = goto.min()
lowestKey = goto.argmin()

# if the lowest distance is lower than monsters current position, move

if lowest < dist:
if lowestKey == 0:
newX = mX - 1
newY = mY - 1

if lowestKey == 1:
newY = mY - 1

if lowestKey == 2:
newX = mX + 1
newY = mY - 1

if lowestKey == 3:
newX = mX - 1

if lowestKey == 5:
newX = mX + 1

if lowestKey == 6:
newY = mY + 1
newX = mX - 1

if lowestKey == 7:
newY = mY + 1

if lowestKey == 8:
newX = mX + 1
newY = mY + 1

做我正在做的事情最干净、最简单、最快的方法是什么?这将同时遍历许多怪物!


编辑:添加了collisionCheck():

def collisionCheck(mobID, newX, newY, mapName):
blocked = 0
if mobs.mobPos_arr[mapName][newX,newY] > -1:
blocked = 1

if mapCollision_arr[mapName][newX,newY] > 0:
blocked = 1

return int(blocked)

最佳答案

您可以使用数组广播一次计算潜在的新位置:

delta = np.arange(-1, 2)
move = np.stack([np.repeat(delta, 3), np.tile(delta, 3)], axis=1)

# Assuming that m_pos.shape is (N: number of monsters, 2).
options = m_pos[:, None, :] + move # Shape (N, 9, 2).

# Collision check.
zip_pos = tuple(zip(*options.reshape(-1, 2)))
check_1 = mobs.mobPos_arr[mapName][zip_pos] > -1
check_2 = mapCollision_arr[mapName][zip_pos] > 0
valid = ~(check_1 | check_2).reshape(-1, 9)

# Now compute distance.
distance = np.linalg.norm(p_pos - options, axis=-1)

# Incorporate whether moves are valid.
valid_distance = np.where(valid, distance, np.inf)

# Select the best move (the one with smallest valid distance).
best = np.argmin(valid_distance, axis=-1)

# Select new positions from the options, based on best move estimation.
new_pos = options[np.arange(len(options)), best]

关于Python - 在 'array' 中寻找最低位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54756225/

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