66from alien import Alien
77
88
9- def check_keydown_events (event , ai_settings , screen , ship , bullets ):
9+ def check_keydown_events (event , ai_settings , screen , stats , ship , aliens ,
10+ bullets ):
1011 """响应按键按下"""
1112 if event .key == pygame .K_RIGHT :
1213 ship .moving_right = True
@@ -18,6 +19,9 @@ def check_keydown_events(event, ai_settings, screen, ship, bullets):
1819 elif event .key == pygame .K_q :
1920 sys .exit ()
2021
22+ elif event .key == pygame .K_p :
23+ start_game (ai_settings , screen , stats , ship , aliens , bullets )
24+
2125
2226def fire_bullet (ai_settings , screen , ship , bullets ):
2327 """如果还没有到达限制,就发射一颗子弹"""
@@ -43,7 +47,8 @@ def check_events(ai_settings, screen, stats, play_button, ship, aliens,
4347 sys .exit ()
4448
4549 elif event .type == pygame .KEYDOWN :
46- check_keydown_events (event , ai_settings , screen , ship , bullets )
50+ check_keydown_events (event , ai_settings , screen , stats , ship ,
51+ aliens , bullets )
4752 elif event .type == pygame .KEYUP :
4853 check_keyup_events (event , ship )
4954 elif event .type == pygame .MOUSEBUTTONDOWN :
@@ -55,21 +60,27 @@ def check_events(ai_settings, screen, stats, play_button, ship, aliens,
5560def check_play_button (ai_settings , screen , stats , play_button , ship ,
5661 aliens , bullets , mouse_x , mouse_y ):
5762 """在玩家单击Play按钮时开始新游戏"""
58- button_clicked = play_button .rect .collidepoint (mouse_x , mouse_y )
63+ button_clicked = play_button .rect .collidepoint (mouse_x , mouse_y )
5964 if button_clicked and not stats .game_active :
60- # 隐藏光标
61- pygame .mouse .set_visible (False )
62- # 重置游戏统计信息
63- stats .reset_stats ()
64- stats .game_active = True
65+ start_game (ai_settings , screen , stats , ship , aliens , bullets )
6566
66- # 清空外星人列表和子弹列表
67- aliens .empty ()
68- bullets .empty ()
6967
70- # 创建一群新的外星人,并让飞船居中
71- create_fleet (ai_settings , screen , ship , aliens )
72- ship .center_ship ()
68+ def start_game (ai_settings , screen , stats , ship , aliens , bullets ):
69+ """开始游戏,重置信息,清空列表,并绘制飞船和外星人"""
70+ # 隐藏光标
71+ pygame .mouse .set_visible (False )
72+
73+ # 重置游戏统计信息
74+ stats .reset_stats ()
75+ stats .game_active = True
76+
77+ # 清空外星人列表和子弹列表
78+ aliens .empty ()
79+ bullets .empty ()
80+
81+ # 创建一群新的外星人,并让飞船居中
82+ create_fleet (ai_settings , screen , ship , aliens )
83+ ship .center_ship ()
7384
7485
7586def update_screen (ai_settings , screen , stats , ship , aliens , bullets ,
@@ -108,7 +119,7 @@ def update_bullets(ai_settings, screen, ship, aliens, bullets):
108119
109120
110121def check_bullet_alien_collisions (ai_settings , screen , ship , aliens ,
111- bullets ):
122+ bullets ):
112123 """响应子弹和外星人的碰撞"""
113124 # 删除发生碰撞的子弹和外星人
114125 collisions = pygame .sprite .groupcollide (bullets , aliens , True , True )
@@ -218,5 +229,5 @@ def update_aliens(ai_settings, stats, screen, ship, aliens, bullets):
218229 if pygame .sprite .spritecollideany (ship , aliens ):
219230 ship_hit (ai_settings , stats , screen , ship , aliens , bullets )
220231
221- #检查是否有外星人到达屏幕底端
232+ # 检查是否有外星人到达屏幕底端
222233 check_aliens_bottom (ai_settings , stats , screen , ship , aliens , bullets )
0 commit comments