以下是一个示例代码,演示了如何保存高分并在屏幕上显示:
# 假设分数保存在一个名为score的变量中
score = 90
# 保存分数到文件中
with open("high_score.txt", "w") as file:
file.write(str(score))
# 从文件中读取分数
with open("high_score.txt", "r") as file:
high_score = int(file.read())
# 在屏幕上显示分数
print("最高分是:" + str(high_score))
这段代码首先将分数保存到一个名为high_score.txt的文件中。然后,通过打开文件并读取其中的内容,将保存的高分读取出来,并保存在一个变量high_score中。最后,使用print函数在屏幕上显示最高分。
请注意,这只是一个示例,实际应用中可能需要根据具体需求进行适当修改。