如何在pygame窗口中显示腌制的.txt文件中的文本 [英] How to display text from a pickled .txt file in a pygame window

查看:174
本文介绍了如何在pygame窗口中显示腌制的.txt文件中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一款游戏,该游戏可以使用pickle记录用户得分并将其保存到文本文件中.当他们的生活用尽时,他们输入他们的名字,他们的名字和分数将保存到文本文件中.当前,如果在主菜单上选择了高分"部分,则高分会简单地打印在python shell中(如果使用的话则是CMD).我想创建一个单独的窗口以仅显示高分.该窗口将仅显示分数,并且每次打开时都会刷新.

I have built a game that logs and saves the users score, using pickle, to a text file. When their lives are used up, they enter their name and their name and score are saved to a text file. Currently, if the "High Scores" section is selected on the main menu the high scores are simply printed in the python shell (or CMD if they're using that). I would like to create a separate window for just displaying the high scores. The window would simply display the scores and would refresh every time it is opened.

当前,我具有加载腌制文件并创建新窗口的代码.如果我输入静态文本,则可以正常工作,但是当我尝试显示文本文件内容时,出现以下错误:

Currently I have the code to load the pickled file and create a new window. If I enter static text it works fine but when I try to display the text file contents I get the following error:

回溯(最近通话最近): 在第22行中的文件"C:\ LearnArabic \ Program \ Test1.py" textsurface = myfont.render(high_scores,False,(0,0,0)) TypeError:文本必须是unicode或字节

Traceback (most recent call last): File "C:\LearnArabic\Program\Test1.py", line 22, in textsurface = myfont.render(high_scores, False, (0, 0, 0)) TypeError: text must be a 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

是否存在与render不同的功能,可以让我以表格形式显示结果?

Is there a different function from render that would allow me to display the results in a tabular form?

我是编程的新手,正在使用python3.感谢您的帮助!

I'm relatively new to programming and am using python 3. Thanks for the help!

推荐答案

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

You could blit the highscores onto another surface and then blit this surf onto the screen. To blit the highscore list, use a for loop and enumerate the list, so that you can multiply the y-offset by i. To toggle the highscore surface, you can just add a variable highscores_visible = False and then do highscores_visible = not highscores_visible, and in the main loop check if highscores_visible: # blit the surf (press 'h' to update and toggle the highscore table in the example below). Of course you need to make sure that the names and highscores fit onto the surface.

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循环.


Regarding the TypeError, you can't pass a list to myfont.render only a string or byte string, so you have to convert the list, e.g. str(high_scores). However, if you just convert the high_scores list into a string before you pass it, pygame will render the whole list as one line. You need to use a for loop if you want several lines of text.

这篇关于如何在pygame窗口中显示腌制的.txt文件中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆