@@ -62,6 +62,10 @@ def check_play_button(ai_settings, screen, stats, play_button, ship,
6262 """在玩家单击Play按钮时开始新游戏"""
6363 button_clicked = play_button .rect .collidepoint (mouse_x , mouse_y )
6464 if button_clicked and not stats .game_active :
65+ # 重置游戏设置
66+ ai_settings .initialize_dynamic_settings ()
67+
68+ # 开始游戏
6569 start_game (ai_settings , screen , stats , ship , aliens , bullets )
6670
6771
@@ -83,7 +87,7 @@ def start_game(ai_settings, screen, stats, ship, aliens, bullets):
8387 ship .center_ship ()
8488
8589
86- def update_screen (ai_settings , screen , stats , ship , aliens , bullets ,
90+ def update_screen (ai_settings , screen , stats , sb , ship , aliens , bullets ,
8791 play_button ):
8892 """更新屏幕上的图像,并切换到新屏幕"""
8993 # 每次循环时都重绘屏幕
@@ -96,6 +100,9 @@ def update_screen(ai_settings, screen, stats, ship, aliens, bullets,
96100 ship .blitme ()
97101 aliens .draw (screen )
98102
103+ # 显示得分
104+ sb .show_score ()
105+
99106 # 如果游戏处于非活动状态,就绘制Play按钮
100107 if not stats .game_active :
101108 play_button .draw_button ()
@@ -104,7 +111,7 @@ def update_screen(ai_settings, screen, stats, ship, aliens, bullets,
104111 pygame .display .flip ()
105112
106113
107- def update_bullets (ai_settings , screen , ship , aliens , bullets ):
114+ def update_bullets (ai_settings , screen , stats , sb , ship , aliens , bullets ):
108115 """更新子弹的位置,并删除已消失的子弹"""
109116 # 更新子弹的位置
110117 bullets .update ()
@@ -114,19 +121,26 @@ def update_bullets(ai_settings, screen, ship, aliens, bullets):
114121 if bullet .rect .bottom <= 0 :
115122 bullets .remove (bullet )
116123
117- check_bullet_alien_collisions (ai_settings , screen , ship , aliens ,
118- bullets )
124+ check_bullet_alien_collisions (ai_settings , screen , stats , sb , ship ,
125+ aliens , bullets )
119126
120127
121- def check_bullet_alien_collisions (ai_settings , screen , ship , aliens ,
122- bullets ):
128+ def check_bullet_alien_collisions (ai_settings , screen , stats , sb , ship ,
129+ aliens , bullets ):
123130 """响应子弹和外星人的碰撞"""
124131 # 删除发生碰撞的子弹和外星人
125132 collisions = pygame .sprite .groupcollide (bullets , aliens , True , True )
126133
134+ if collisions :
135+ for aliens in collisions .values ():
136+ stats .score += ai_settings .alien_points * len (aliens )
137+ sb .prep_score ()
138+ check_high_score (stats , sb )
139+
127140 if len (aliens ) == 0 :
128- # 删除现有的子弹并新建一群外星人
141+ # 删除现有的子弹,加快游戏节奏,并创建一群新的外星人
129142 bullets .empty ()
143+ ai_settings .increase_speed ()
130144 create_fleet (ai_settings , screen , ship , aliens )
131145
132146
@@ -231,3 +245,10 @@ def update_aliens(ai_settings, stats, screen, ship, aliens, bullets):
231245
232246 # 检查是否有外星人到达屏幕底端
233247 check_aliens_bottom (ai_settings , stats , screen , ship , aliens , bullets )
248+
249+
250+ def check_high_score (stats , sb ):
251+ """检查是否诞生了新的最高得分"""
252+ if stats .score > stats .high_score :
253+ stats .high_score = stats .score
254+ sb .prep_high_score ()
0 commit comments