英文:
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
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
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())
答案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) <= 3 :
if event.type == pygame.MOUSEBUTTONDOWN:
bullet_group.add(player.create_bullet())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论