Pygame试图显示从.txt fi随机选择的单词

2024-10-05 14:23:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个打字游戏,我试图从一个.txt文件中随机显示一行文本,唯一的问题是,当它出现在pygame窗口时,它的末尾是这样的:[]。有什么建议吗?关于其他代码的任何未显示的问题都可以回答。我不想把整件事都贴出来,真是一团糟。文字在自己的文件行中。正在使用的代码:

gameDisplay = pygame.display.set_mode((display_width, display_height))

vSmallFont = pygame.font.SysFont("Comicsansms", 10)
smallFont = pygame.font.SysFont("Comicsansms", 20)
medFont = pygame.font.SysFont("Comicsansms", 45)
largeFont = pygame.font.SysFont("Comicsansms", 55)

def text_objects(text, color, size):
    if size == "small":
        textSurf = smallFont.render(text, True, color)
    elif size == "medium":
        textSurf = medFont.render(text, True, color)
    elif size == "large":
        textSurf = largeFont.render(text, True, color)
    elif size == "verySmall":
        textSurf = vSmallFont.render(text, True, color)

    return textSurf, textSurf.get_rect()


def messageToScreen(msg, color, y_displace = 0, size = "small"):
    textSurface, textRect = text_objects(msg, color, size)
    textRect.center = (display_width/2), (display_height/2) + y_displace
    gameDisplay.blit(textSurface, textRect)

#This function is later called
def randWord():
    rand = random.randint(0,1000)
    fp = open("1.txt")
    for i, line in enumerate(fp):
        if i == rand:
            messageToScreen(line,black,-200,size = "large")
    fp.close()

Tags: texttruesizedefdisplayrenderpygamecolor