Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从sprites.Group()中删除/删除前N个或最后N个_Python_Python 2.7_Pygame - Fatal编程技术网

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()