准备在游戏页面添加一个历史最高分的显示,在关卡得分和关卡等级后面,recordscore 在这之前还没有定义,直接做了一个 global recordscore 全局变量,但是还是报错;
报错信息如下:
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "G:/plane/main.py", line 539, in <module>
main()
File "G:/plane/main.py", line 463, in main
record_score_text = score_font.render("历史最高分: %d" % recordscore, True, WHITE)
NameError: name 'recordscore' is not defined
代码:
if life_num:
for i in range(life_num):
screen.blit(life_image, \
(width - 10 - (i + 1) * life_rect.width, \
height - 10 - life_rect.height))
score_text = score_font.render(str("本关卡得分: %s" % score), True, WHITE)
screen.blit(score_text, (10, 5))
level1_text = level1_font.render("关卡等级:%s "% level, True, WHITE)
screen.blit(level1_text, (10, 40))
record_score_text = score_font.render("历史最高分: %d" % recordscore, True, WHITE)
screen.blit(record_score_text, (10, 60))
elif life_num == 0:
pygame.mixer.music.stop()
pygame.mixer.stop()
# 停止发放补给
pygame.time.set_timer(SUPPLY_TIME, 0)
if not recorded:
recorded = True
# 读取历史最高分
with open("record.txt", "r") as f:
recordscore = int(f.read())
if score > recordscore:
with open("record.txt", "w") as f:
f.write(str(score))
# 绘制结束画面
record_score_text = score_font.render("Best: %d" % recordscore, True, WHITE)
screen.blit(record_score_text, (50, 50))