功能函数更改
This commit is contained in:
parent
be8d7bfa2e
commit
f5ccc6a2c5
|
@ -182,3 +182,36 @@ def start_new_level(ai_settings, screen, game_stats, scoreb, ship, aliens, bulle
|
|||
create_fleet(ai_settings, screen, ship, aliens) # 调用创建外星人群的函数
|
||||
|
||||
|
||||
def check_bullet_alien_collisions(ai_settings, screen, game_stats, scoreb, ship, aliens, bullets):
|
||||
"""子弹与外星人相撞后的操作"""
|
||||
# 检查是否有子弹击中外星人
|
||||
# 如果击中了就删除相应的子弹与外星人
|
||||
collisions = pygame.sprite.groupcollide(bullets, aliens, True, True)
|
||||
"""方法sprite.groupcollide()检查两个编组的成员之间的碰撞(碰撞是指游戏元素重叠在一起):
|
||||
将每颗子弹同每个外星人的rect作比较 并返回一个字典 字典中的键为子弹 值为被击中的外星人"""
|
||||
|
||||
# 每消灭一个外星人都将加一个points且显示最新得分的图像
|
||||
if collisions:
|
||||
music.voice_small() # 子弹与外星人相撞的声音
|
||||
for aliens in collisions.values(): # aliens指被同一颗子弹击中的外星人---是一个列表
|
||||
game_stats.score += ai_settings.alien_points * len(aliens)
|
||||
scoreb.prep_score()
|
||||
check_high_score(game_stats, scoreb)
|
||||
|
||||
start_new_level(ai_settings, screen, game_stats, scoreb, ship, aliens, bullets)
|
||||
|
||||
|
||||
def update_bullets(ai_settings, screen, game_stats, scoreb, ship, aliens, bullets):
|
||||
"""
|
||||
更新子弹位置并删除已消失的子弹
|
||||
"""
|
||||
# 更新子弹位置
|
||||
bullets.update()
|
||||
|
||||
# 删除已消失的子弹
|
||||
for bullet in bullets.copy():
|
||||
if bullet.rect.bottom <= 0: # 当子弹底部越过屏幕顶部(0)时删除子弹
|
||||
bullets.remove(bullet)
|
||||
check_bullet_alien_collisions(ai_settings, screen, game_stats, scoreb, ship, aliens, bullets)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue