【问题标题】:How to display text from a pickled .txt file in a pygame window如何在 pygame 窗口中显示腌制 .txt 文件中的文本
【发布时间】:2017-10-09 18:15:08
【问题描述】:

我已经构建了一个游戏,它使用 pickle 将用户得分记录并保存到文本文件中。当他们的生命用尽时,他们输入他们的名字,他们的名字和分数被保存到一个文本文件中。目前,如果在主菜单上选择了“高分”部分,则高分会简单地打印在 python shell(或 CMD,如果他们正在使用它)中。我想创建一个单独的窗口来显示高分。该窗口将简单地显示分数并在每次打开时刷新。

目前我有代码来加载腌制文件并创建一个新窗口。如果我输入静态文本,它可以正常工作,但是当我尝试显示文本文件内容时,我收到以下错误:

Traceback(最近一次调用最后一次): 文件“C:\LearnArabic\Program\Test1.py”,第 22 行,在 textsurface = myfont.render(high_scores, False, (0, 0, 0)) TypeError: text must be an unicode or bytes

这是我的代码:

import pygame
from operator import itemgetter
import pickle

pygame.font.init()

high_scores = []

with open("C:\\LearnArabic\\HighScores\\HighScores.txt", 'rb') as f:
    high_scores = pickle.load(f)

#Background color 
background_color = (255,255,255)
(width, height) = (400, 500)

HighScoreScreen = pygame.display.set_mode((width, height))
pygame.display.set_caption('High Scores')
HighScoreScreen.fill(background_color)

#Displaying text on window
myfont = pygame.font.SysFont('Comic Sans MS', 30)
textsurface = myfont.render(high_scores, False, (0, 0, 0))
HighScoreScreen.blit(textsurface,(0,0))

pygame.display.flip()

running = True
while running:
    for event in pygame.event.get():
        if event.type ==pygame.QUIT:
            running = False

是否有与渲染不同的功能可以让我以表格形式显示结果?

我对编程比较陌生,并且正在使用 python 3。感谢您的帮助!

【问题讨论】:

    标签: python-3.x pygame pickle


    【解决方案1】:

    您可以将高分粘贴到另一个表面上,然后将这个冲浪粘贴到屏幕上。要对高分列表进行 blit,请使用 for 循环并枚举列表,以便您可以将 y 偏移量乘以 i。要切换高分表,您只需添加一个变量highscores_visible = False,然后执行highscores_visible = not highscores_visible,然后在主循环中检查if highscores_visible: # blit the surf(在下面的示例中按“h”更新和切换高分表)。当然,您需要确保名称和高分符合表面。

    import pygame
    
    
    pygame.font.init()
    
    screen = pygame.display.set_mode((400, 500))
    clock = pygame.time.Clock()
    
    high_scores = [
        ('Carrie', 350),
        ('Arthur', 200),
        ('Doug', 100),
        ]
    
    background_color = (255, 255, 255)
    
    highscore_surface = pygame.Surface((300, 400))
    highscore_surface.fill((90, 100, 120))
    
    myfont = pygame.font.SysFont('Comic Sans MS', 30)
    highscores_visible = False
    
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_h:
                    highscores_visible = not highscores_visible
                    if highscores_visible:
                        highscore_surface.fill((90, 100, 120))
                        for i, (name, score) in enumerate(high_scores):
                            text = myfont.render('{} {}'.format(name, score), True, (0, 0, 0))
                            highscore_surface.blit(text, (50, 30*i+5))
    
        screen.fill(background_color)
        if highscores_visible:
            screen.blit(highscore_surface, (50, 50))
    
        pygame.display.flip()
        clock.tick(60)
    
    pygame.quit()
    

    关于TypeError,您不能只将一个字符串或字节字符串传递给myfont.render,因此您必须转换列表,例如str(high_scores)。但是,如果您只是在传递 high_scores 列表之前将其转换为字符串,那么 pygame 会将整个列表呈现为一行。如果需要多行文本,则需要使用 for 循环。

    【讨论】:

    • 感谢您的帮助!我不确定这如何解决我的问题。现在我无法在 .txt 文件中显示信息,因为 TypeError: text must be an unicode or bytes。有没有办法可以显示或呈现我的 txt 文件的内容?。
    • 当我运行你的代码时,我只是得到一个空白窗口。甚至不会显示您设置的 high_scores。我错过了什么吗?
    • 那是我的问题!感谢您指出了这一点。我对编程有点陌生,所以我不太清楚我的字符串和列表。我只是把 str(high_scores) 放进去,现在它可以工作了。
    • 顺便说一句,泡菜要小心。您永远不应该从不受信任的来源中提取文件,因为它们可能包含恶意代码。 json 模块是更安全的选择。
    • 您能否将您对 TypeError 的评论作为您问题的答案?那是我真正的目标(即使我表达得很糟糕)。
    猜你喜欢
    • 2013-08-12
    • 2014-01-17
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    • 2020-11-24
    相关资源
    最近更新 更多