我正在尝试使用lambdas在pygame应用程序中实现undo和redo,但与引用有关或者我对list.remove()
的实现的理解导致我的程序崩溃。创建可撤消操作的代码如下
elif MOUSEBUTTONUP == event.type:
x, y = pygame.mouse.get_pos()
if leftClick:
if len( objects ) > 0 and objects[ -1 ].is_open():
actions.do( \
[ lambda: objects[ -1 ].add( Point( x, y, 0 ) ) ], \
[ lambda: objects[ -1 ].remove( Point( x, y, 0 ) ) ] \
)
else:
actions.do( \
[ lambda: objects.append( Polygon( ( 255, 255, 255 ) ).add( Point( x, y, 0 ) ) ) ],
[ lambda: objects.pop() ] \
)
其中objects是Polygon
的列表,其定义为
class Polygon:
def __init__( self, colour, width = 0 ):
self._points = []
self._colour = colour
self._isopen = True
self._width = width
def get_colour( self ):
return self._colour
def add( self, point ):
self._points.append( point )
return self
def remove( self, point ):
print "List contains " + str( self._points )
print "Trying to remove " + str( point )
self._points.remove( point )
return self
def num_points( self ):
return len( self._points )
def get_points( self ):
""" Returns a copy of the points in this vector as a list. """
return self._points[:]
def open( self ):
self._isopen = True
return self
def close( self ):
self._isopen = False
return self
def is_open( self ):
return self._isopen
def set_width( self, width ):
self._width = width
return self
def get_width( self ):
return self._width
def is_filled( self ):
return self._filled
,添加的点定义为
class Point:
def __init__( self, x, y, z ):
self.x = x
self.y = y
self.z = z
def rel_to( self, point ):
x = self.move( point.z, point.x, self.z, self.x )
y = self.move( point.z, point.y, self.z, self.y )
return ( x, y )
def move( self, viewer_d, displacement, object_d, object_h ):
over = object_h * viewer_d + object_d * displacement
under = object_d + viewer_d + 1
return over / under
def __str__( self ):
return "(%d, %d, %d)" % ( self.x, self.y, self.z )
def __eq__( self, other ):
return self.x == other.x and self.y == other.y and self.z == other.z
def __ne__( self, other ):
return not ( self == other )
在第一个代码段中, actions
是Action
的一个实例,其定义如下:
class Actions:
#TODO implement immutability where possible
def __init__( self ):
self.undos = []
self.redos = []
def do( self, do_steps, undo_steps ):
for do_step in do_steps:
do_step()
self.undos.append( ( do_steps, undo_steps ) )
self.redos = []
def can_undo( self ):
return len( self.undos ) > 0
def undo( self ):
if self.can_undo():
action = self.undos.pop()
_, undo_steps = action
for undo_step in undo_steps:
undo_step()
self.redos.append( action )
def can_redo( self ):
return len( self.redos ) > 0
def redo( self ):
if self.can_redo():
action = self.redos.pop()
redo_steps, _ = action
for redo_step in redo_steps:
redo_step()
self.undos.append( action )
所以问题是,当我点击两次以上并尝试拨打actions.undo()
时我会收到list.remove(x)
的异常,它说x
在列表中不存在,我认为因为它试图两次删除相同的点。在前两次单击之前不会发生这种情况的原因是第一次撤消尝试删除最近的点,而第二次撤消只是从对象堆栈中弹出多边形。我的问题是为什么Point
actions.undo()
尝试两次删除相同的点,即使第一个点应该从self.undos
堆栈弹出并推到self.redos
堆?非常感谢任何反馈。
答案 0 :(得分:1)
我想我已经弄明白了,无论如何都能正常运作。我推断的理论是lambda动作是在x
参数的y
,z
和Point()
上创建闭包,所以不要复制这些变量的值。创建了匿名函数,而不是维护指向其值的指针。因此,我可以删除最近的点,因为x
,y
和z
仍然引用现有点,但在第二次撤消时失败,因为该点不再存在。我在http://stackp.online.fr/?cat=8使用了stackp的undo / redo教程中的一个叶子,并将参数作为列表传递给匿名函数action.do()
,这意味着在do()
函数时评估参数已执行,因此将适当的参数发送给do()
。对不起,问题很长。