在下面的133行測試中,我正在測試目標和子彈之間的碰撞。我想知道爲什麼target_hit_list
總是隻有一個目標對象?是否因爲target_hit_list在每個循環中得到更新和新分配,所以它在下一次碰撞發生時總是空的?Pygame sprite.spritecollide()
Here是鏈接到代碼。
在下面的133行測試中,我正在測試目標和子彈之間的碰撞。我想知道爲什麼target_hit_list
總是隻有一個目標對象?是否因爲target_hit_list在每個循環中得到更新和新分配,所以它在下一次碰撞發生時總是空的?Pygame sprite.spritecollide()
Here是鏈接到代碼。
是的。此外,子彈只與一個目標發生碰撞。你也應該在代碼中分離出來,讓您擁有一個主循環是這樣的,對於每一項對自己的一個功能:
check_events()
update_player_position()
collision_check()
draw_all()
而不是擁有一切在同一地區搗成泥在一起。
是這樣的:
running = True
while running:
check_events()
update_player_position()
collision_check()
draw_all()
def check_events():
for e in pygame.event.get():
if e.type==pygame.QUIT or (e.type==pygame.KEYDOWN and e.key==pygame.K_ESCAPE):
runs=0
player.shoot()
def update():
#logics
for ahsm in all_have_same_method:
ahsm.hanging()
all_have_same_method.update()
def collision_check():
for b in bullet_group:
target_hit_list=pygame.sprite.spritecollide(b,target_group,0)
for thl in target_hit_list:
score+=1
print target_hit_list #why is in the target_hit_list allways only one Target object?
def draw():
#drawings
#draw score
draw_score=font.render(str(score),True,RED)
screen.fill(BLACK,(0,0,50,20))
screen.set_colorkey(BLACK)
screen.blit(draw_score,(0,0))
all_have_same_method.draw(screen)
pygame的還有另外一個功能,它在一次檢查所有精靈之間的碰撞。 (collidelistall)試試看...
謝謝。你可能會顯示你的意思是爲了在我的主循環中清晰的主循環?看看它應該是什麼樣子會很有幫助 – amirteymuri