@@ -21,7 +21,6 @@ def check_keydown_events(event, ai_settings, screen, ship, bullets):
2121
2222def fire_bullet (ai_settings , screen , ship , bullets ):
2323 """如果还没有到达限制,就发射一颗子弹"""
24-
2524 # 创建一颗子弹,并将其加入到编组bullets中
2625 if len (bullets ) < ai_settings .bullets_allowed :
2726 new_bullet = Bullet (ai_settings , screen , ship )
@@ -36,9 +35,8 @@ def check_keyup_events(event, ship):
3635 ship .moving_left = False
3736
3837
39- def check_events (ai_settings , screen , ship , bullets ):
38+ def check_events (ai_settings , screen , stats , play_button , ship , bullets ):
4039 """响应按键和鼠标事件"""
41-
4240 for event in pygame .event .get ():
4341 if event .type == pygame .QUIT :
4442 sys .exit ()
@@ -47,11 +45,19 @@ def check_events(ai_settings, screen, ship, bullets):
4745 check_keydown_events (event , ai_settings , screen , ship , bullets )
4846 elif event .type == pygame .KEYUP :
4947 check_keyup_events (event , ship )
48+ elif event .type == pygame .MOUSEBUTTONDOWN :
49+ mouse_x , mouse_y = pygame .mouse .get_pos ()
50+ check_play_button (stats , play_button , mouse_x , mouse_y )
51+
52+
53+ def check_play_button (stats , play_button , mouse_x , mouse_y ):
54+ """在玩家单击Play按钮时开始新游戏"""
55+ if play_button .rect .collidepoint (mouse_x , mouse_y ):
56+ stats .game_active = True
5057
5158
5259def update_screen (ai_settings , screen , stats , ship , aliens , bullets , play_button ):
5360 """更新屏幕上的图像,并切换到新屏幕"""
54-
5561 # 每次循环时都重绘屏幕
5662 screen .fill (ai_settings .bg_color )
5763
@@ -65,13 +71,13 @@ def update_screen(ai_settings, screen, stats, ship, aliens, bullets, play_button
6571 # 如果游戏处于非活动状态,就绘制Play按钮
6672 if not stats .game_active :
6773 play_button .draw_button ()
74+
6875 # 让最近绘制的屏幕可见
6976 pygame .display .flip ()
7077
7178
7279def update_bullets (ai_settings , screen , ship , aliens , bullets ):
7380 """更新子弹的位置,并删除已消失的子弹"""
74-
7581 # 更新子弹的位置
7682 bullets .update ()
7783
@@ -85,7 +91,6 @@ def update_bullets(ai_settings, screen, ship, aliens, bullets):
8591
8692def check_bullet_alien_collisions (ai_settings , screen , ship , aliens , bullets ):
8793 """响应子弹和外星人的碰撞"""
88-
8994 # 删除发生碰撞的子弹和外星人
9095 collisions = pygame .sprite .groupcollide (bullets , aliens , True , True )
9196
@@ -97,15 +102,13 @@ def check_bullet_alien_collisions(ai_settings, screen, ship, aliens, bullets):
97102
98103def get_number_aliens_x (ai_settings , alien_width ):
99104 """计算每行可容纳多少个外星人"""
100-
101105 available_space_x = ai_settings .screen_width - 2 * alien_width
102106 number_aliens_x = int (available_space_x / (2 * alien_width ))
103107 return number_aliens_x
104108
105109
106110def get_number_rows (ai_settings , ship_height , alien_height ):
107111 """计算屏幕可容纳多少行外星人"""
108-
109112 available_space_y = (ai_settings .screen_height - (3 * alien_height ) -
110113 ship_height )
111114 number_rows = int (available_space_y / (2 * alien_height ))
@@ -124,7 +127,6 @@ def create_alien(ai_settings, screen, aliens, alien_number, row_number):
124127
125128def create_fleet (ai_settings , screen , ship , aliens ):
126129 """创建外星人群"""
127-
128130 # 创建一个外星人,并计算一行可容纳多少个外星人
129131 alien = Alien (ai_settings , screen )
130132 number_aliens_x = get_number_aliens_x (ai_settings , alien .rect .width )
@@ -147,15 +149,13 @@ def check_fleet_edges(ai_settings, aliens):
147149
148150def change_fleet_direction (ai_settings , aliens ):
149151 """将整群外星人下移,并改变它们的方向"""
150-
151152 for alien in aliens .sprites ():
152153 alien .rect .y += ai_settings .fleet_drop_speed
153154 ai_settings .fleet_direction *= - 1
154155
155156
156157def ship_hit (ai_settings , stats , screen , ship , aliens , bullets ):
157158 """响应被外星人撞到的飞船"""
158-
159159 if stats .ships_left > 0 :
160160 # 将ships_left减1
161161 stats .ships_left -= 1
@@ -177,7 +177,6 @@ def ship_hit(ai_settings, stats, screen, ship, aliens, bullets):
177177
178178def check_aliens_bottom (ai_settings , stats , screen , ship , aliens , bullets ):
179179 """检查是否有外星人到达屏幕底端"""
180-
181180 screen_rect = screen .get_rect ()
182181 for alien in aliens .sprites ():
183182 if alien .rect .bottom >= screen_rect .bottom :
0 commit comments