如何使用pygame将变量的值显示为文本

时间:2018-08-03 16:58:06

标签: python-3.x pygame pygame-surface

我正在将Windows 10和Python 3.7与pygame 1.9.4一起使用。

我的应用程序要求将变量的值显示在pygame屏幕上。

当前,我有:

tnr = pygame.font.SysFont('Times New Roman', 30)
text2 = tnr.render(timePriorities, False, (0, 0, 0))
screen.blit(text2,(0,0))

这将输出此错误:

TypeError: text must be a unicode or byte

有人知道如何解决这个问题吗?

谢谢!

哦,这是相关代码:

timePriorities = 1.1
timeRightTurn = 2.2
timeDeadEnd = 3.3
timeIntersection = 4.4
array = sorted([timePriorities,timeRightTurn,timeDeadEnd,timeIntersection])
arrayLength = len(array)
tnr = pygame.font.SysFont('Times New Roman', 30)
for i in range(0,arrayLength):
    if timePriorities == array[i]:
        str(timePriorities)
        text1 = tnr.render('Priorities', False, (0, 0, 0))
        text2 = tnr.render(timePriorities, False, (0, 0, 0))
    else:
        if timeRightTurn == array[i]:
            str(timeRightTurn)
            text3 = tnr.render('Right Turn', False, (0, 0, 0))
            text4 = tnr.render(timeRightTurn, False, (0, 0, 0))
        else:
            if timeDeadEnd == array[i]:
                str(timeDeadEnd)
                text5 = tnr.render('Dead End', False, (0, 0, 0))
                text6 = tnr.render(timeDeadEnd, False, (0, 0, 0))
            else:
                if timeIntersection == array[i]:
                    str(timeIntersection)
                    text7 = tnr.render('Intersection', False, (0, 0, 0))
                    text8 = tnr.render(timeIntersection, False, (0, 0, 0))

1 个答案:

答案 0 :(得分:1)

问题在于str(timePriorities)不会将timePriorities更改为字符串,而是将值作为字符串返回。

您需要像这样分配它:

timePriorities = str(timePriorities) 

或者,您可以像这样在渲染中做到这一点:

text2 = tnr.render(str(timePriorities), False, (0, 0, 0))