Skip to content

Commit 498c40e

Browse files
committed
显示当前得分及最高分。
1 parent 2599958 commit 498c40e

File tree

5 files changed

+115
-17
lines changed

5 files changed

+115
-17
lines changed

alien_invasion.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from settings import Settings
55
from game_stats import GameStats
6+
from scoreboard import Scoreboard
67
from button import Button
78
from ship import Ship
89
import game_functions as gf
@@ -18,8 +19,9 @@ def run_game():
1819

1920
# 创建Play按钮
2021
play_button = Button(ai_settings, screen, "Play")
21-
# 创建一个用于存储游戏统计信息的实例
22+
# 创建一个用于存储游戏统计信息的实例,并创建记分牌
2223
stats = GameStats(ai_settings)
24+
sb = Scoreboard(ai_settings, screen, stats)
2325

2426
# 创建一艘飞船、一个子弹编组和一个外星人编组
2527
ship = Ship(ai_settings, screen)
@@ -36,12 +38,13 @@ def run_game():
3638

3739
if stats.game_active:
3840
ship.update()
39-
gf.update_bullets(ai_settings, screen, ship, aliens, bullets)
41+
gf.update_bullets(ai_settings, screen, stats, sb, ship,
42+
aliens, bullets)
4043
gf.update_aliens(ai_settings, stats, screen, ship, aliens,
4144
bullets)
4245

43-
gf.update_screen(ai_settings, screen, stats, ship, aliens, bullets,
44-
play_button)
46+
gf.update_screen(ai_settings, screen, stats, sb, ship, aliens,
47+
bullets, play_button)
4548

4649

4750
run_game()

game_functions.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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()

game_stats.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ def __init__(self, ai_settings):
99
# 让游戏一开始处于非活动状态
1010
self.game_active = False
1111

12+
# 在任何情况下都不应重置最高得分
13+
self.high_score = 0
14+
1215
def reset_stats(self):
1316
"""初始化在游戏运行期间可能变化的统计信息"""
1417
self.ships_left = self.ai_settings.ship_limit
18+
self.score = 0

scoreboard.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import pygame.font
2+
3+
4+
class Scoreboard():
5+
"""显示得分信息的类"""
6+
7+
def __init__(self, ai_settings, screen, stats):
8+
"""初始化显示得分涉及的属性"""
9+
self.screen = screen
10+
self.screen_rect = screen.get_rect()
11+
self.ai_settings = ai_settings
12+
self.stats = stats
13+
14+
# 显示得分信息时使用的字体设置
15+
self.text_color = (30, 30, 30)
16+
self.font = pygame.font.SysFont(None, 48)
17+
18+
# 准备包含最高得分和当前得分的图像
19+
self.prep_score()
20+
self.prep_high_score()
21+
22+
def prep_score(self):
23+
"""将得分转换为一幅渲染的图像"""
24+
rounded_score = int(round(self.stats.score, -1))
25+
score_str = "{:,}".format(rounded_score)
26+
self.score_image = self.font.render(score_str, True,
27+
self.text_color, self.ai_settings.bg_color)
28+
29+
# 将得分放在屏幕右上角
30+
self.score_rect = self.score_image.get_rect()
31+
self.score_rect.right = self.screen_rect.right - 20
32+
self.score_rect.top = 20
33+
34+
def prep_high_score(self):
35+
"""将最高得分转换为渲染的图像"""
36+
high_score = int(round(self.stats.high_score, -1))
37+
high_score_str = "{:,}".format(high_score)
38+
self.high_score_image = self.font.render(high_score_str, True,
39+
self.text_color, self.ai_settings.bg_color)
40+
41+
# 将最高得分放在屏幕顶部中央
42+
self.high_score_rect = self.high_score_image.get_rect()
43+
self.high_score_rect.centerx = self.screen_rect.centerx
44+
self.high_score_rect.top = self.screen_rect.top
45+
46+
def show_score(self):
47+
"""在屏幕上显示当前得分和最高得分"""
48+
self.screen.blit(self.score_image, self.score_rect)
49+
self.screen.blit(self.high_score_image, self.high_score_rect)

settings.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,48 @@ class Settings():
22
"""一个存储《外星人入侵》的所有设置的类"""
33

44
def __init__(self):
5-
"""初始化游戏的设置"""
5+
"""初始化游戏的静态设置"""
66

77
# 屏幕设置
88
self.screen_width = 800
99
self.screen_height = 600
1010
self.bg_color = (255, 255, 255)
1111

1212
# 飞船的设置
13-
self.ship_speed_factor = 2.5
1413
self.ship_limit = 3
1514

1615
# 子弹设置
17-
self.bullet_speed_factor = 10
1816
self.bullet_width = 300
1917
self.bullet_height = 15
2018
self.bullet_color = 20, 100, 20
21-
self.bullets_allowed = 15
19+
self.bullets_allowed = 5
2220

2321
# 外星人设置
22+
self.fleet_drop_speed = 5
23+
24+
# 以什么样的速度加快游戏节奏
25+
self.speedup_scale = 1.1
26+
27+
# 外星人点数的提高速度
28+
self.score_scale = 1.5
29+
30+
self.initialize_dynamic_settings()
31+
32+
def initialize_dynamic_settings(self):
33+
"""初始化随游戏进行而变化的设置"""
34+
self.ship_speed_factor = 1.5
35+
self.bullet_speed_factor = 3
2436
self.alien_speed_factor = 1
25-
self.fleet_drop_speed = 10
2637

27-
# fleet_direction为1表示向右移,为-1表示向左移
38+
# fleet_direction 为1表示向右; 为-1表示向左
2839
self.fleet_direction = 1
40+
41+
# 记分
42+
self.alien_points = 50
43+
44+
def increase_speed(self):
45+
"""提高速度设置和卫星人点数"""
46+
self.ship_speed_factor *= self.speedup_scale
47+
self.bullet_speed_factor *= self.speedup_scale
48+
self.alien_speed_factor *= self.speedup_scale
49+
self.alien_points = int(self.alien_points * self.score_scale)

0 commit comments

Comments
 (0)