为什么我将对象添加到它时,我的sprite.Group列表为空?

huangapple go评论29阅读模式
英文:

why is my sprite.Group list empty when I add objects to it?

问题

bullet_group 中存储的对象数量超过 3 个时,我希望它不再添加对象,但是由于某种原因,我的列表保持为空,尽管我已经在其中存储了对象。这是为什么发生的?

bullet_group = pygame.sprite.Group()
bullet_group2 = pygame.sprite.Group.sprites(bullet_group)
enemy_group = pygame.sprite.Group()

prev_time = time.time()
game_active = False

gameName = pygame.font.Font('font/Pixeltype.ttf', 80).render('Asteroids', False, (51, 153, 255))
gameName_rect = gameName.get_rect(center=(400, 70))
text2 = test_font.render('Press "space" to run', False, (51, 153, 255))
text2_rect = text2.get_rect(center=(400, 340))
while True:
    dt = time.time() - prev_time
    prev_time = time.time()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if game_active:
            display_score()
            if len(bullet_group2) <= 3:
                print(bullet_group2)
                if event.type == pygame.MOUSEBUTTONDOWN:
                    bullet_group.add(player.create_bullet())
英文:

I want to make it so that when there are more then 3 objects stored in
bullet_group
it wouldnt add objects to it but for some reason my list stays empty even though I have objects stored in it. Why is that happening?

bullet_group = pygame.sprite.Group()
bullet_group2= pygame.sprite.Group.sprites(bullet_group)
enemy_group = pygame.sprite.Group()

prev_time = time.time()
game_active = False

gameName = pygame.font.Font(&#39;font/Pixeltype.ttf&#39;,80).render(&#39;Asteroids&#39;,False,(51,153,255))
gameName_rect = gameName.get_rect(center = (400,70))
text2 = test_font.render(&#39;Press &quot;space&quot; to run&#39;,False,(51,153,255))
text2_rect = text2.get_rect(center = (400,340))
while True:
    dt = time.time() - prev_time
    prev_time = time.time()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if game_active:
            display_score()
            if len(bullet_group2) &lt;= 3 :
                print(bullet_group2)
                if event.type == pygame.MOUSEBUTTONDOWN:
                    bullet_group.add(player.create_bullet())

答案1

得分: 1

看起来你正在检查bullet_group2的长度,然后将子弹添加到bullet_group。因此,bullet_group2的长度永远不会增加,而你将始终能够不断添加子弹。

尝试像这样做:

if len(bullet_group) <= 3:
    if event.type == pygame.MOUSEBUTTONDOWN:
        bullet_group.add(player.create_bullet())
英文:

It seems that you are checking the length of bullet_group2 but then adding the bullets to bullet_group. As a result, the length of bullet_group2 never increases, and you will always be able to keep adding bullets.

Try something like this:

if len(bullet_group) &lt;= 3 :
    if event.type == pygame.MOUSEBUTTONDOWN:
        bullet_group.add(player.create_bullet())

huangapple
  • 本文由 发表于 2023年2月18日 07:17:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定