【发布时间】:2017-03-26 23:28:28
【问题描述】:
我正在尝试切换此方法,以允许鱼尝试多达 4 个随机位置以进行下一步。我已经尝试了一些破解,但我还没有想出一个好的方法。
def tryToMove(self):
offsetList = [(-1, 1), (0, 1), (1, 1),
(-1, 0) , (1, 0),
(-1, -1), (0, -1), (1, -1)]
randomOffsetIndex = random.randrange(len(offsetList))
randomOffset = offsetList[randomOffsetIndex]
nextx = self.xpos + randomOffset[0]
nexty = self.ypos + randomOffset[1]
while not(0 <= nextx < self.world.getMaxX() and 0 <= nexty < self.world.getMaxY()):
randomOffsetIndex = random.randrange(len(offsetList))
randomOffset = offsetList[randomOffsetIndex]
nextx = self.xpos + randomOffset[0]
nexty = self.ypos + randomOffset[1]
if self.world.emptyLocation(nextx, nexty):
self.move(nextx, nexty)
【问题讨论】:
-
顺便说一句
randomOffset = random.choice(offsetList) -
有什么问题?你收到错误信息吗?显示有问题?或者创建最小的工作示例,以便我们可以运行它并查看问题。
标签: python random methods turtle-graphics