I have this function move()
function in a class which is incorrectly adding 2 instead of 1 in a line containing self.coords[1] += 1
here's the file:
class Actor():
def __init__(self, start, limits, barriers):
self.coords = start
self.limits = limits
self.barriers = barriers
def canmove(self, direction):
moving = self.coords
if(direction == 'up'):
moving[1] -= 1
elif(direction == 'right'):
moving[0] += 1
elif(direction == 'down'):
moving[1] += 1
elif(direction == 'left'):
moving[0] -= 1
if((moving[0] > self.limits[0]) or (moving[1] > self.limits[1]) or (-1 in moving) or (moving in self.barriers)):
return False
else:
return True
def move(self, direction):
if(direction == 'up'):
if self.canmove('up'):
self.coords[1] -= 1
elif(direction == 'right'):
if self.canmove('right'):
self.coords[0] += 1
elif(direction == 'down'):
if self.canmove('down'):
self.coords[1] += 1
elif(direction == 'left'):
if self.canmove('left'):
self.coords[0] -= 1
I know the canmove()
function doesn't quite work yet but it doesn't interfere with the results.
When running Actor.move('up')
it is decreasing Actor.coords[1]
by two instead of one.
Here's what happens (even when ignoring the canmove()
check):
>>> from actor import Actor
>>> actor = Actor([2, 2], [10, 5], [[4, 4]])
>>> actor.move('down')
>>> actor.coords
[2, 4]
and actor.move(down)
is supposed to increase the value of actor.coords[1]
by one, not two.