From 0f722eae702bbc665b595446f3ba0599fed026ab Mon Sep 17 00:00:00 2001 From: "2042271667@qq.com" <2042271667> Date: Tue, 15 Jun 2021 20:05:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=AD=90=E5=BC=B9=E5=8F=8A?= =?UTF-8?q?=E5=A4=96=E6=98=9F=E4=BA=BA=E7=9A=84=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game_functions.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/game_functions.py b/game_functions.py index a590c5a..56bdf7d 100644 --- a/game_functions.py +++ b/game_functions.py @@ -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) + +