更新子弹及外星人的位置

This commit is contained in:
2042271667@qq.com 2021-06-15 20:05:46 +08:00
parent c1bc061f54
commit 0f722eae70
1 changed files with 46 additions and 0 deletions

View File

@ -201,3 +201,49 @@ def check_bullet_alien_collisions(ai_settings, screen, game_stats, scoreb, ship,
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)
def get_number_aliens_X(ai_settings, alien_width):
"""
计算每行可容纳多少个外星人
"""
availabble_space_x = ai_settings.screen_width - 2 * alien_width # 一行可可容纳的水平长度
number_aliens_x = int(availabble_space_x / (2 * alien_width)) # 一行可容纳多少个外星人
return number_aliens_x
def get_number_rows(ai_settings, alien_height, ship_height):
"""
计算屏幕可容纳多少行外星人
"""
availables_space_y = (ai_settings.screen_height-ship_height-
(3*alien_height))
number_rows = int(availables_space_y / (2 * alien_height))
return number_rows
def create_alien(ai_settings, screen, aliens, alien_number, row_number):
"""
创建一个外星人并将其放在当前行
"""
alien = Alien(ai_settings, screen)
alien_width = alien.rect.width
alien_height = alien.rect.height + 3 # 让外星人之间间隔大些
alien.x = alien_width + 2 * alien_width * alien_number # 获取新建外星人所移动的位置
alien.rect.x = alien.x # 新建外星人的位置
alien.rect.y = alien_height + 2 * alien_height * row_number # 新行的位置
aliens.add(alien)