Python 从sprites.Group()中删除/删除前N个或最后N个
假设你有一个精灵小组,你在其中添加了很多东西:Python 从sprites.Group()中删除/删除前N个或最后N个,python,python-2.7,pygame,Python,Python 2.7,Pygame,假设你有一个精灵小组,你在其中添加了很多东西: all_shelfs = pygame.sprite.Group() shelf_tracking_list = [] #making shelfs build_lvl = HEIGHT - 150 #group A for i in xrange(100): wid = random.randint(120,320) pos = [random.randint(0, WIDTH-wid), random.randint(buil
all_shelfs = pygame.sprite.Group()
shelf_tracking_list = []
#making shelfs
build_lvl = HEIGHT - 150
#group A
for i in xrange(100):
wid = random.randint(120,320)
pos = [random.randint(0, WIDTH-wid), random.randint(build_lvl-20, build_lvl), wid]
all_shelfs.add(Shelf(pos[0],pos[1], pos[2]))
build_lvl = build_lvl - 60
#group B
for i in xrange(100):
wid = random.randint(120,320)
pos = [random.randint(0, WIDTH-wid), random.randint(build_lvl-20, build_lvl), wid]
all_shelfs.add(Shelf(pos[0],pos[1], pos[2]))
build_lvl = build_lvl - 60
#group C
for i in xrange(100):
wid = random.randint(120,320)
pos = [random.randint(0, WIDTH-wid), random.randint(build_lvl-20, build_lvl), wid]
all_shelfs.add(Shelf(pos[0],pos[1], pos[2]))
build_lvl = build_lvl - 60
shelf_tracking_list = all_shelfs.sprites()
例如,如何删除组A?
这是我添加的第一组。我注意到我无法使用此工具架跟踪列表修改组如果要跟踪每个组中的精灵,可以使用
sprite.group.remove(*sprites)
函数删除整个组,如此处文档中所述:
然后,当您要从所有\u shelfs
中删除整个组时:
all_shelfs.remove(group_a)
因为您正在询问如何删除逻辑组,而不仅仅是N个元素:根据您的程序,将精灵放入多个组可能会大大简化操作
可以将精灵放置在多个组中以引用同一精灵。然后,如果您kill()
it,则会将其从所有组中删除。否则删除(*组)
以删除特定组
for i in xrange(100):
wid = random.randint(120,320)
pos = [random.randint(0, WIDTH-wid), random.randint(build_lvl-20, build_lvl), wid]
shelf = Shelf(pos[0],pos[1], pos[2])
all_shelfs.add(shelf)
shelfs_a.add(shelf)
build_lvl = build_lvl - 60
#group B
for i in xrange(100):
wid = random.randint(120,320)
pos = [random.randint(0, WIDTH-wid), random.randint(build_lvl-20, build_lvl), wid]
shelf = Shelf(pos[0],pos[1], pos[2])
all_shelfs.add(shelf)
shelfs_b.add(shelf)
build_lvl = build_lvl - 60
# ...
# then to erase from both groups
for shelf in shelfs_a:
shelf.kill()
for i in xrange(100):
wid = random.randint(120,320)
pos = [random.randint(0, WIDTH-wid), random.randint(build_lvl-20, build_lvl), wid]
shelf = Shelf(pos[0],pos[1], pos[2])
all_shelfs.add(shelf)
shelfs_a.add(shelf)
build_lvl = build_lvl - 60
#group B
for i in xrange(100):
wid = random.randint(120,320)
pos = [random.randint(0, WIDTH-wid), random.randint(build_lvl-20, build_lvl), wid]
shelf = Shelf(pos[0],pos[1], pos[2])
all_shelfs.add(shelf)
shelfs_b.add(shelf)
build_lvl = build_lvl - 60
# ...
# then to erase from both groups
for shelf in shelfs_a:
shelf.kill()