@@ -35,7 +35,8 @@ def check_keyup_events(event, ship):
3535 ship .moving_left = False
3636
3737
38- def check_events (ai_settings , screen , stats , play_button , ship , bullets ):
38+ def check_events (ai_settings , screen , stats , play_button , ship , aliens ,
39+ bullets ):
3940 """响应按键和鼠标事件"""
4041 for event in pygame .event .get ():
4142 if event .type == pygame .QUIT :
@@ -47,16 +48,32 @@ def check_events(ai_settings, screen, stats, play_button, ship, bullets):
4748 check_keyup_events (event , ship )
4849 elif event .type == pygame .MOUSEBUTTONDOWN :
4950 mouse_x , mouse_y = pygame .mouse .get_pos ()
50- check_play_button (stats , play_button , mouse_x , mouse_y )
51+ check_play_button (ai_settings , screen , stats , play_button , ship ,
52+ aliens , bullets , mouse_x , mouse_y )
5153
5254
53- def check_play_button (stats , play_button , mouse_x , mouse_y ):
55+ def check_play_button (ai_settings , screen , stats , play_button , ship ,
56+ aliens , bullets , mouse_x , mouse_y ):
5457 """在玩家单击Play按钮时开始新游戏"""
55- if play_button .rect .collidepoint (mouse_x , mouse_y ):
58+ button_clicked = play_button .rect .collidepoint (mouse_x , mouse_y )
59+ if button_clicked and not stats .game_active :
60+ # 隐藏光标
61+ pygame .mouse .set_visible (False )
62+ # 重置游戏统计信息
63+ stats .reset_stats ()
5664 stats .game_active = True
5765
66+ # 清空外星人列表和子弹列表
67+ aliens .empty ()
68+ bullets .empty ()
69+
70+ # 创建一群新的外星人,并让飞船居中
71+ create_fleet (ai_settings , screen , ship , aliens )
72+ ship .center_ship ()
73+
5874
59- def update_screen (ai_settings , screen , stats , ship , aliens , bullets , play_button ):
75+ def update_screen (ai_settings , screen , stats , ship , aliens , bullets ,
76+ play_button ):
6077 """更新屏幕上的图像,并切换到新屏幕"""
6178 # 每次循环时都重绘屏幕
6279 screen .fill (ai_settings .bg_color )
@@ -86,10 +103,12 @@ def update_bullets(ai_settings, screen, ship, aliens, bullets):
86103 if bullet .rect .bottom <= 0 :
87104 bullets .remove (bullet )
88105
89- check_bullet_alien_collisions (ai_settings , screen , ship , aliens , bullets )
106+ check_bullet_alien_collisions (ai_settings , screen , ship , aliens ,
107+ bullets )
90108
91109
92- def check_bullet_alien_collisions (ai_settings , screen , ship , aliens , bullets ):
110+ def check_bullet_alien_collisions (ai_settings , screen , ship , aliens ,
111+ bullets ):
93112 """响应子弹和外星人的碰撞"""
94113 # 删除发生碰撞的子弹和外星人
95114 collisions = pygame .sprite .groupcollide (bullets , aliens , True , True )
@@ -136,7 +155,8 @@ def create_fleet(ai_settings, screen, ship, aliens):
136155 # 创建外星人群
137156 for row_number in range (number_rows ):
138157 for alien_number in range (number_aliens_x ):
139- create_alien (ai_settings , screen , aliens , alien_number , row_number )
158+ create_alien (ai_settings , screen , aliens , alien_number ,
159+ row_number )
140160
141161
142162def check_fleet_edges (ai_settings , aliens ):
@@ -173,6 +193,7 @@ def ship_hit(ai_settings, stats, screen, ship, aliens, bullets):
173193
174194 else :
175195 stats .game_active = False
196+ pygame .mouse .set_visible (True )
176197
177198
178199def check_aliens_bottom (ai_settings , stats , screen , ship , aliens , bullets ):
0 commit comments