显示高分列表以显示表面 Pygame

Displaying High Score List to Display Surface Pygame

我正在制作一个 High_Name_Score(玩家姓名和高分)列表以在游戏结束屏幕 (show_go_screen) 中显示。我能够保存和加载列表,按分数排序并将其 blit 到屏幕,但我有两个问题:

1) 它在一行上显示所有屏幕,我想让每个玩家/得分在新的一行

2) 显示括号和引号,例如HIGH SCORE ['3580 AABBCC', '3508 CCBBAA'] 我希望它更干净

我在网上搜索过,但还没有告诉我怎么做

import pygame
import random
import sys
import json
from pygame.locals import *

def (main):
    try:
        with open('High_Name_Score.txt', 'r') as file:
            High_Name_Score = json.load(file)
            High_Name_Score.sort()
    except FileNotFoundError:
        High_Name_Score = []

    High_Score = 0
    PURPLE = (139, 0, 139)

    arial = pygame.font.match_font('arial')

    pygame.init()
    CLOCK = pygame.time.Clock()
    DS = pygame.display.set_mode((W, H))
    pygame.display.set_caption("Racing Fury")

    def events():
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

    def draw_text(surf, text, size, x, y, fonts, color):
        font = pygame.font.Font(fonts, size)
        text_surface = font.render(text, True, color)
        text_rect = text_surface.get_rect()
        text_rect.midtop = (x, y)
        surf.blit(text_surface, text_rect)

    def high_name():
        High_Name = ""
        Enter_Name = True
        while Enter_Name:
            DS.fill(WHITE)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                elif event.type == pygame.KEYUP:
                    if event.key == K_a:
                        High_Name += 'A'
                    if event.key == K_b:
                        High_Name += 'B'
                    if event.key == K_c:
                        High_Name += 'C'
                    if event.key == K_RETURN:
                        High_Name_Score.append(str(High_Score) + " " + High_Name)
                        with open('High_Name_Score.txt', 'w') as file:
                            json.dump(High_Name_Score, file)
                        main()
            draw_text(DS, "HIGH SCORE: " + str(High_Name), 18, W / 2, 700, arial, PURPLE)
            pygame.display.flip()

    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 i in High_Name_Score:
            draw_text(DS, "HIGH SCORE: " + str(High_Name_Score), 18, W / 2, (H / 4 - 160), 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,然后调用 high_name() 来输入玩家姓名。一切正常,玩家姓名和分数被保存,并放入列表中,就在我将其 blit 到屏幕时,我得到了我不想看到的括号和引号,它们都显示在一行上

如有任何建议,我们将不胜感激

好吧,它有点长,但下面的代码将按照从最高到最低的顺序保存您的前 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()

然后我在游戏结束屏幕上显示分数:

    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。

如果有人能用更少的代码做到这一点,请告诉我!!!