Ну, это немного длинно, но следующий код сохранит ваши первые 3 балла в порядке от высшего к низшему и всегда будет корректироваться, если кто-либо получит новый высокий балл в топ-3 (полностью опустив # 3)
def high_name():
High_Name = ""
Enter_Name = True
try:
with open('HS1.txt', 'r') as f1:
HS1 = json.load(f1)
with open('HS2.txt', 'r') as f2:
HS2 = json.load(f2)
with open('HS3.txt', 'r') as f3:
HS3 = json.load(f3)
with open('HN1.txt', 'r') as f4:
HN1 = json.load(f4)
with open('HN2.txt', 'r') as f5:
HN2 = json.load(f5)
with open('HN3.txt', 'r') as f6:
HN3 = json.load(f6)
except FileNotFoundError:
HS1 = 0
HS2 = 0
HS3 = 0
HN1 = ""
HN2 = ""
HN3 = ""
while Enter_Name:
DS.fill(WHITE)
if Winner == 1:
draw_text(DS, "Congrats Player1, Enter Your Name", 18, W / 2, 650, arial, PURPLE)
elif Winner == 2:
draw_text(DS, "Congrats Player2, Enter Your Name", 18, W / 2, 650, arial, PURPLE)
if len(High_Name) >= 7:
High_Name = High_Name[:-1]
draw_text(DS, "Only 6 Characters", 18, W / 2, 750, arial, PURPLE)
pygame.display.flip()
pygame.time.delay(2000)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key >= K_a and event.key <= K_z:
High_Name += chr(event.key)
elif event.key >= K_0 and event.key <= K_9:
High_Name += chr(event.key)
elif event.key == K_BACKSPACE:
High_Name = High_Name[:-1]
elif event.key == K_RETURN:
High_Name = High_Name.capitalize()
if High_Score >= HS1:
HS3 = HS2
HN3 = HN2
HS2 = HS1
HN2 = HN1
HS1 = High_Score
HN1 = High_Name
elif High_Score >= HS2:
HS3 = HS2
HN3 = HN2
HS2 = High_Score
HN2 = High_Name
elif High_Score >= HS3:
HS3 = High_Score
HN3 = High_Name
with open('HS1.txt', 'w') as f1:
json.dump(HS1, f1)
with open('HS2.txt', 'w') as f2:
json.dump(HS2, f2)
with open('HS3.txt', 'w') as f3:
json.dump(HS3, f3)
with open('HN1.txt', 'w') as f4:
json.dump(HN1, f4)
with open('HN2.txt', 'w') as f5:
json.dump(HN2, f5)
with open('HN3.txt', 'w') as f6:
json.dump(HN3, f6)
main()
draw_text(DS, "HIGH SCORE: " + str(High_Name), 18, W / 2, 700, arial, PURPLE)
pygame.display.flip()
После этого у меня на экране «Game Over» отображаются результаты:
def show_go_screen():
DS.fill(WHITE)
Bckgrnd = [Summer, Winter, Desert, Space]
Bckgrnd_Img = random.choice(Bckgrnd)
if Bckgrnd_Img == Space:
color = WHITE
else:
color = BLACK
DS.blit(Bckgrnd_Img, (0, 0))
for i in range(3):
draw_text(DS, "RACING FURY!", 64, W / 2 + (i + 1), H / 4 - 60 + (i + 1), bodoni, color)
draw_text(DS, "RACING FURY!", 64, W / 2, (H / 4 - 60), bodoni, PURPLE)
draw_text(DS, "Summer Track (S)", 18, W / 2, (HH - 70), arial, color)
draw_text(DS, "Winter Track (W)", 18, W / 2, (HH + 0), arial, color)
draw_text(DS, "Desert Track (D)", 18, W / 2, (HH + 70), arial, color)
for s in range(3):
draw_text(DS, "HIGH SCORES", 24, W / 2 + (s + 1), H / 4 - 180 + (s + 1), arial, color)
draw_text(DS, "HIGH SCORES", 24, W / 2, (H / 4 - 180), arial, PURPLE)
draw_text(DS, str(HN1) + ": " + str(HS1), 18, W / 2, (H / 4 - 140), arial, PURPLE)
draw_text(DS, str(HN2) + ": " + str(HS2), 18, W / 2, (H / 4 - 120), arial, PURPLE)
draw_text(DS, str(HN3) + ": " + str(HS3), 18, W / 2, (H / 4 - 100), arial, PURPLE)
pygame.display.flip()
waiting = True
while waiting:
global TRACK
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
keystate = pygame.key.get_pressed()
if keystate[pygame.K_s]:
TRACK = 1
waiting = False
И, конечно, вам нужно будет получить несколько массивов, таких как High_Score, из других частей вашей игры.
Если любой может сделать это в гораздо меньшем количестве кода, пожалуйста, дайте мне знать !!!