gpt4 book ai didi

python - 从 sprites.Group() 中删除/移除前 N 或最后 N

转载 作者:行者123 更新时间:2023-12-01 05:46:03 25 4
gpt4 key购买 nike

假设您有一个 Sprite 组,并且向其中添加了很多东西:

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 组?这是我添加的第一组。我注意到我无法使用这个 shelf_tracking_list 真正修改组

最佳答案

如果您要跟踪每个组中的 Sprite ,则可以使用 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)

关于python - 从 sprites.Group() 中删除/移除前 N 或最后 N,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16083829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com