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 , sb , ship ,
10+ aliens , bullets ):
1011 """响应按键按下"""
1112 if event .key == pygame .K_RIGHT :
1213 ship .moving_right = True
@@ -18,12 +19,15 @@ 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 , sb , ship , aliens , bullets )
24+
2125
2226def fire_bullet (ai_settings , screen , ship , bullets ):
2327 """如果还没有到达限制,就发射一颗子弹"""
24-
2528 # 创建一颗子弹,并将其加入到编组bullets中
2629 if len (bullets ) < ai_settings .bullets_allowed :
30+
2731 new_bullet = Bullet (ai_settings , screen , ship )
2832 bullets .add (new_bullet )
2933
@@ -36,22 +40,63 @@ def check_keyup_events(event, ship):
3640 ship .moving_left = False
3741
3842
39- def check_events (ai_settings , screen , ship , bullets ):
43+ def check_events (ai_settings , screen , stats , sb , play_button , ship , aliens ,
44+ bullets ):
4045 """响应按键和鼠标事件"""
41-
4246 for event in pygame .event .get ():
4347 if event .type == pygame .QUIT :
4448 sys .exit ()
4549
4650 elif event .type == pygame .KEYDOWN :
47- check_keydown_events (event , ai_settings , screen , ship , bullets )
51+ check_keydown_events (event , ai_settings , screen , stats , sb ,
52+ ship , aliens , bullets )
4853 elif event .type == pygame .KEYUP :
4954 check_keyup_events (event , ship )
55+ elif event .type == pygame .MOUSEBUTTONDOWN :
56+ mouse_x , mouse_y = pygame .mouse .get_pos ()
57+ check_play_button (ai_settings , screen , stats , sb , play_button ,
58+ ship , aliens , bullets , mouse_x , mouse_y )
5059
5160
52- def update_screen (ai_settings , screen , ship , aliens , bullets ):
53- """更新屏幕上的图像,并切换到新屏幕"""
61+ def check_play_button (ai_settings , screen , stats , sb , play_button , ship ,
62+ aliens , bullets , mouse_x , mouse_y ):
63+ """在玩家单击Play按钮时开始新游戏"""
64+ button_clicked = play_button .rect .collidepoint (mouse_x , mouse_y )
65+ if button_clicked and not stats .game_active :
66+ # 重置游戏设置
67+ ai_settings .initialize_dynamic_settings ()
68+
69+ # 开始游戏
70+ start_game (ai_settings , screen , stats , sb , ship , aliens , bullets )
71+
72+
73+ def start_game (ai_settings , screen , stats , sb , ship , aliens , bullets ):
74+ """开始游戏,重置信息,清空列表,并绘制飞船和外星人"""
75+ # 隐藏光标
76+ pygame .mouse .set_visible (False )
5477
78+ # 重置游戏统计信息
79+ stats .reset_stats ()
80+ stats .game_active = True
81+
82+ # 重置记分牌图像
83+ sb .prep_score ()
84+ sb .prep_high_score ()
85+ sb .prep_level ()
86+ sb .prep_ships ()
87+
88+ # 清空外星人列表和子弹列表
89+ aliens .empty ()
90+ bullets .empty ()
91+
92+ # 创建一群新的外星人,并让飞船居中
93+ create_fleet (ai_settings , screen , ship , aliens )
94+ ship .center_ship ()
95+
96+
97+ def update_screen (ai_settings , screen , stats , sb , ship , aliens , bullets ,
98+ play_button ):
99+ """更新屏幕上的图像,并切换到新屏幕"""
55100 # 每次循环时都重绘屏幕
56101 screen .fill (ai_settings .bg_color )
57102
@@ -62,13 +107,19 @@ def update_screen(ai_settings, screen, ship, aliens, bullets):
62107 ship .blitme ()
63108 aliens .draw (screen )
64109
110+ # 显示得分
111+ sb .show_score ()
112+
113+ # 如果游戏处于非活动状态,就绘制Play按钮
114+ if not stats .game_active :
115+ play_button .draw_button ()
116+
65117 # 让最近绘制的屏幕可见
66118 pygame .display .flip ()
67119
68120
69- def update_bullets (ai_settings , screen , ship , aliens , bullets ):
121+ def update_bullets (ai_settings , screen , stats , sb , ship , aliens , bullets ):
70122 """更新子弹的位置,并删除已消失的子弹"""
71-
72123 # 更新子弹的位置
73124 bullets .update ()
74125
@@ -77,32 +128,45 @@ def update_bullets(ai_settings, screen, ship, aliens, bullets):
77128 if bullet .rect .bottom <= 0 :
78129 bullets .remove (bullet )
79130
80- check_bullet_alien_collisions (ai_settings , screen , ship , aliens , bullets )
131+ check_bullet_alien_collisions (ai_settings , screen , stats , sb , ship ,
132+ aliens , bullets )
81133
82134
83- def check_bullet_alien_collisions (ai_settings , screen , ship , aliens , bullets ):
135+ def check_bullet_alien_collisions (ai_settings , screen , stats , sb , ship ,
136+ aliens , bullets ):
84137 """响应子弹和外星人的碰撞"""
85-
86138 # 删除发生碰撞的子弹和外星人
87139 collisions = pygame .sprite .groupcollide (bullets , aliens , True , True )
88140
141+ if collisions :
142+ for aliens in collisions .values ():
143+ stats .score += ai_settings .alien_points * len (aliens )
144+ sb .prep_score ()
145+ check_high_score (stats , sb )
146+
89147 if len (aliens ) == 0 :
90- # 删除现有的子弹并新建一群外星人
148+ # 如果整群外星人都被消灭,就提高一个等级
149+ # 清空子弹列表,加快游戏速度
91150 bullets .empty ()
151+ ai_settings .increase_speed ()
152+
153+ # 提高等级
154+ stats .level += 1
155+ sb .prep_level ()
156+
157+ # 重新创建一批外星人
92158 create_fleet (ai_settings , screen , ship , aliens )
93159
94160
95161def get_number_aliens_x (ai_settings , alien_width ):
96162 """计算每行可容纳多少个外星人"""
97-
98163 available_space_x = ai_settings .screen_width - 2 * alien_width
99164 number_aliens_x = int (available_space_x / (2 * alien_width ))
100165 return number_aliens_x
101166
102167
103168def get_number_rows (ai_settings , ship_height , alien_height ):
104169 """计算屏幕可容纳多少行外星人"""
105-
106170 available_space_y = (ai_settings .screen_height - (3 * alien_height ) -
107171 ship_height )
108172 number_rows = int (available_space_y / (2 * alien_height ))
@@ -121,7 +185,6 @@ def create_alien(ai_settings, screen, aliens, alien_number, row_number):
121185
122186def create_fleet (ai_settings , screen , ship , aliens ):
123187 """创建外星人群"""
124-
125188 # 创建一个外星人,并计算一行可容纳多少个外星人
126189 alien = Alien (ai_settings , screen )
127190 number_aliens_x = get_number_aliens_x (ai_settings , alien .rect .width )
@@ -131,7 +194,8 @@ def create_fleet(ai_settings, screen, ship, aliens):
131194 # 创建外星人群
132195 for row_number in range (number_rows ):
133196 for alien_number in range (number_aliens_x ):
134- create_alien (ai_settings , screen , aliens , alien_number , row_number )
197+ create_alien (ai_settings , screen , aliens , alien_number ,
198+ row_number )
135199
136200
137201def check_fleet_edges (ai_settings , aliens ):
@@ -144,19 +208,20 @@ def check_fleet_edges(ai_settings, aliens):
144208
145209def change_fleet_direction (ai_settings , aliens ):
146210 """将整群外星人下移,并改变它们的方向"""
147-
148211 for alien in aliens .sprites ():
149212 alien .rect .y += ai_settings .fleet_drop_speed
150213 ai_settings .fleet_direction *= - 1
151214
152215
153- def ship_hit (ai_settings , stats , screen , ship , aliens , bullets ):
216+ def ship_hit (ai_settings , stats , screen , sb , ship , aliens , bullets ):
154217 """响应被外星人撞到的飞船"""
155-
156218 if stats .ships_left > 0 :
157219 # 将ships_left减1
158220 stats .ships_left -= 1
159221
222+ # 更新记分牌
223+ sb .prep_ships ()
224+
160225 # 清空外星人列表和子弹列表
161226 aliens .empty ()
162227 bullets .empty ()
@@ -170,20 +235,21 @@ def ship_hit(ai_settings, stats, screen, ship, aliens, bullets):
170235
171236 else :
172237 stats .game_active = False
238+ pygame .mouse .set_visible (True )
173239
174240
175- def check_aliens_bottom (ai_settings , stats , screen , ship , aliens , bullets ):
241+ def check_aliens_bottom (ai_settings , stats , screen , sb , ship , aliens ,
242+ bullets ):
176243 """检查是否有外星人到达屏幕底端"""
177-
178244 screen_rect = screen .get_rect ()
179245 for alien in aliens .sprites ():
180246 if alien .rect .bottom >= screen_rect .bottom :
181247 # 像飞船被撞到一样进行处理
182- ship_hit (ai_settings , stats , screen , ship , aliens , bullets )
248+ ship_hit (ai_settings , stats , screen , sb , ship , aliens , bullets )
183249 break
184250
185251
186- def update_aliens (ai_settings , stats , screen , ship , aliens , bullets ):
252+ def update_aliens (ai_settings , stats , screen , sb , ship , aliens , bullets ):
187253 """
188254 检查是否右外星人位于屏幕边缘,并更新整群外星人的位置
189255 """
@@ -193,7 +259,15 @@ def update_aliens(ai_settings, stats, screen, ship, aliens, bullets):
193259
194260 # 检测外星人和飞船之间的碰撞
195261 if pygame .sprite .spritecollideany (ship , aliens ):
196- ship_hit (ai_settings , stats , screen , ship , aliens , bullets )
262+ ship_hit (ai_settings , stats , screen , sb , ship , aliens , bullets )
263+
264+ # 检查是否有外星人到达屏幕底端
265+ check_aliens_bottom (ai_settings , stats , screen , sb , ship , aliens ,
266+ bullets )
267+
197268
198- #检查是否有外星人到达屏幕底端
199- check_aliens_bottom (ai_settings , stats , screen , ship , aliens , bullets )
269+ def check_high_score (stats , sb ):
270+ """检查是否诞生了新的最高得分"""
271+ if stats .score > stats .high_score :
272+ stats .high_score = stats .score
273+ sb .prep_high_score ()
0 commit comments