class 没有被 del 销毁的实例
Instance of a class not being destroyed with del
我正在尝试删除一个对象(在本例中为 Fruit class 的实例),当它与我的蛇头相交时。问题是当它检测到水果对象时,del
似乎没有做任何事情。不知道是我没有正确实现delete函数,还是我把对象喂给函数的方式有关。
这是处理游戏功能的 main.py 脚本的一部分:
# Main loop
while running:
# event handling, gets all event from the event queue
for event in pygame.event.get():
# only do something if the event is of type QUIT
if event.type == pygame.QUIT:
# change the value to False, to exit the main loop
running = False
# Modify game fruit properties
if fruit_tick < 50:
fruit_tick += 1
else:
game.spawn_fruit()
fruit_tick = 0
# Modify game player properties
if not game.player_python:
game.spawn_player()
else:
game.player_python.move()
game.player_python.detect_object(game.get_objects())
# Modify display properties
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
game.draw_shapes()
pygame.display.flip()
这是当头部与 Fruit 对象位于同一位置时正在执行的函数:
# Detect object
def detect_object(self, objects):
for obj in objects:
if obj.pivot == self.location:
if isinstance(obj, Fruit):
self.grow(obj.get_growth())
del obj
如果我在if isinstance(obj, Fruit):
下添加一个print()
语句,它执行没有问题。问题是 del obj
什么都不做。
不要在循环遍历列表时删除迭代器变量或列表中的项目,因为这是一种不好的做法,无论如何,请尝试另一种方法。也许可以这样删除一些对象:
# Detect object
def detect_object(self, objects):
del_indices = []
for i in len(objects):
obj = objects[i]
if obj.pivot == self.location:
if isinstance(obj, Fruit):
self.grow(obj.get_growth())
# del obj
del_indices.append(i)
# Remove some objects
new_objects = []
for i in len(objects):
if not i in del_indices:
new_objects.append(objects[i])
objects = new_objects
# return objects
我正在尝试删除一个对象(在本例中为 Fruit class 的实例),当它与我的蛇头相交时。问题是当它检测到水果对象时,del
似乎没有做任何事情。不知道是我没有正确实现delete函数,还是我把对象喂给函数的方式有关。
这是处理游戏功能的 main.py 脚本的一部分:
# Main loop
while running:
# event handling, gets all event from the event queue
for event in pygame.event.get():
# only do something if the event is of type QUIT
if event.type == pygame.QUIT:
# change the value to False, to exit the main loop
running = False
# Modify game fruit properties
if fruit_tick < 50:
fruit_tick += 1
else:
game.spawn_fruit()
fruit_tick = 0
# Modify game player properties
if not game.player_python:
game.spawn_player()
else:
game.player_python.move()
game.player_python.detect_object(game.get_objects())
# Modify display properties
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
game.draw_shapes()
pygame.display.flip()
这是当头部与 Fruit 对象位于同一位置时正在执行的函数:
# Detect object
def detect_object(self, objects):
for obj in objects:
if obj.pivot == self.location:
if isinstance(obj, Fruit):
self.grow(obj.get_growth())
del obj
如果我在if isinstance(obj, Fruit):
下添加一个print()
语句,它执行没有问题。问题是 del obj
什么都不做。
不要在循环遍历列表时删除迭代器变量或列表中的项目,因为这是一种不好的做法,无论如何,请尝试另一种方法。也许可以这样删除一些对象:
# Detect object
def detect_object(self, objects):
del_indices = []
for i in len(objects):
obj = objects[i]
if obj.pivot == self.location:
if isinstance(obj, Fruit):
self.grow(obj.get_growth())
# del obj
del_indices.append(i)
# Remove some objects
new_objects = []
for i in len(objects):
if not i in del_indices:
new_objects.append(objects[i])
objects = new_objects
# return objects