I'm trying to switch up this method to allow the fish to try up to 4 random locations for its next move. I've tried a few cracks at it, but I haven't figured out a good way yet.
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)
Aucun commentaire:
Enregistrer un commentaire