2013-04-18 73 views
1

讓我們假設你有一個精靈組,並且添加了一堆東西出來:刪除/從sprites.Group取出前N個或最後N()

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

如何刪除集團例如? 這是我添加的第一個組。我注意到,使用這個shelf_tracking_list

回答

1

如果你跟蹤每個組在精靈的我真的不能修改組,您可以使用sprite.Group.remove(*sprites)功能刪除整個組,如這裏的文檔指定: http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group.remove

# group A 
group_a = list() 
for i in xrange(100): 
    wid = random.randint(120,320) 
    pos = [random.randint(0, WIDTH-wid), random.randint(build_lvl-20, build_lvl), wid] 
    new_shelf = Shelf(pos[0], pos[1], pos[2]) 
    group_a.append(new_shelf) 
    build_lvl = build_lvl - 60 
all_shelfs.add(group_a) 

然後,當你想從all_shelfs刪除整個組:既然你艾斯克

all_shelfs.remove(group_a) 
1

如何刪除邏輯組,而不僅僅是N個元素:根據你的程序,它可能會極大地簡化把精靈放在多個組中的操作。

您可以在多個組中放置一個精靈來引用相同的精靈。那麼如果你kill()它,那將它從所有組中刪除。否則remove(*groups)用於特定組的刪除。

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