实现击中外星人的函数
This commit is contained in:
parent
d9509c59b2
commit
c1bc061f54
|
@ -140,3 +140,64 @@ def check_events(ai_settings, screen, game_stats, scoreb, play_button, ship, ali
|
|||
check_keyup_events(event, ship)
|
||||
|
||||
|
||||
def update_screen(ai_settings, screen, game_stats, scoreb, ship, aliens, bullets, play_button):
|
||||
"""
|
||||
更新屏幕上的图像并切换到新屏幕
|
||||
"""
|
||||
# 每次循环时都重绘屏幕
|
||||
screen.fill(ai_settings.bg_color) # 先填充背景后绘制飞船
|
||||
ship.blitem()
|
||||
aliens.draw(screen)
|
||||
|
||||
# 在飞船后重绘所有子弹
|
||||
for bullet in bullets.sprites():
|
||||
bullet.draw_bullet()
|
||||
# 显示得分
|
||||
scoreb.show_score()
|
||||
|
||||
# 如果游戏处于非活动状态就绘制Play按钮
|
||||
if not game_stats.game_active:
|
||||
play_button.draw_button()
|
||||
|
||||
# 让最近绘制的屏幕可见
|
||||
pygame.display.flip()
|
||||
|
||||
|
||||
def start_new_level(ai_settings, screen, game_stats, scoreb, ship, aliens, bullets):
|
||||
"""
|
||||
当消灭干净外星人提高一个等级
|
||||
"""
|
||||
if len(aliens) == 0:
|
||||
# 删除现有子弹
|
||||
bullets.empty()
|
||||
|
||||
# 逐渐提高速度加快游戏节奏
|
||||
ai_settings.increase_speed()
|
||||
|
||||
# 当前屏幕外星人全部被消灭就提高一个等级
|
||||
game_stats.level += 1
|
||||
scoreb.prep_level()
|
||||
|
||||
# 创建一群新的外星人
|
||||
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)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue