在我进入我的问题之前,该计划的一些背景知识:
我正在尝试创建一个模拟抢劫的程序。强盗,警卫和门都有硬编码值。
随着while循环的每次迭代,强盗移近门以逃脱。此外,每次循环迭代,守卫都会越来越靠近强盗。这两个操作都由 computeChanges 函数处理。
如果强盗在守卫到达他之前(0,0)到达门口,那么强盗就会逃跑,并打印卫兵的最后位置。
如果守卫在进入门前抓住强盗,则会打印强盗和守卫的最后位置。
我在执行时遇到的问题是强盗和守卫的值在传递到 computeChanges 函数后没有改变。这会导致无限循环。
我错过了什么?重新分配X和Y位置应该在 computeChanges 函数的最后两行发生。
感谢任何和所有帮助。
代码:
from numpy import arctan, sin, cos
from math import pi, copysign, sqrt
# Used to prevent objects from overshooting one another
def normalizeSpeed(speed1, speed2, maxSpeed):
if speed1 > speed2:
larger = speed1
elif speed2 > speed1:
larger = speed2
speed1 = speed1 / larger * maxSpeed
speed2 = speed2 / larger * maxSpeed
# Used to compute the change in position for given object
def computeChanges(movingX, movingY, speed, targetX, targetY):
bigTriangleWidth = movingX - targetX
bigTriangleHeight = movingY - targetY
bigTriangleRatio = bigTriangleWidth / bigTriangleHeight
bigTriangleAngle = arctan(bigTriangleRatio)
ratioX = sin(bigTriangleAngle)
changeX = ratioX * speed
ratioY = cos(bigTriangleAngle)
changeY = ratioY * speed
movingX += copysign(changeX, bigTriangleWidth * -1)
movingY += copysign(changeY, bigTriangleHeight * -1)
# Used to calculate the absolute distance between two objects to be compared to the closeEnough variable
def absDistance(object1X, object1Y, object2X, object2Y):
diffX = object2X - object1X
diffY = object2Y - object1Y
d = sqrt(diffX ** 2 + diffY ** 2)
return abs(d)
#------------------------------------------------------------------------#
#------------------------------------------------------------------------#
robberX = -10.0
robberY = 100.0
robberSpeed = 11
guardX = -5.0
guardY = 140.0
guardSpeed = 13
normalizeSpeed(robberSpeed, guardSpeed, 0.1)
doorX = 0.0
doorY = 0.0
closeEnough = 0.2
while ((absDistance(robberX, robberY, guardX, guardY) > closeEnough) and (absDistance(robberX, robberY, doorX, doorY) > closeEnough)):
computeChanges(robberX, robberY, robberSpeed, doorX, doorY)
print("Robber position: ({0}, {1})".format(robberX, robberY))
computeChanges(guardX, guardY, guardSpeed, robberX, robberY)
print("Guard position: ({0}, {1})".format(guardX, guardY))
if (absDistance(robberX, robberY, doorX, doorY) <= closeEnough):
print("The robber escapes!\nguardX = {0}\nguardY = {1}".format(guardX, guardY))
elif (absDistance(robberX, robberY, guardX, guardY) <= closeEnough):
print("The guard catches the robber!\nrobberX = {0}\nrobberY = {1}\nguardX = {2}\nguardY = {3}".format(robberX, robberY, guardX, guardY))
答案 0 :(得分:1)
你的问题在于python处理变量范围的方式。是的,在computeChanges
的最后一行,您正在更改movingX
和movingY
。但是,您只是在该函数体的范围内更改那一个调用的变量。一旦函数完成,那些局部变量就会被丢弃。
你应该退出这两个变量并更新while循环中的强盗和守卫位置。
def computeChanges(movingX, movingY, speed, targetX, targetY):
...
return movingX, movingY
...
while ((absDistance(robberX, robberY, guardX, guardY) > closeEnough) and (absDistance(robberX, robberY, doorX, doorY) > closeEnough)):
robberX, robberY = computeChanges(robberX, robberY, robberSpeed, doorX, doorY)
print("Robber position: ({0}, {1})".format(robberX, robberY))
gaurdX, gaurdY = computeChanges(guardX, guardY, guardSpeed, robberX, robberY)
print("Guard position: ({0}, {1})".format(guardX, guardY))