'文本必须是unicode或字节'

时间:2018-02-19 22:40:35

标签: python pygame

我正在使用python创建一个2D平台游戏,除了显示玩家健康等重要属性之外,还设法让所有工作都有效。

我已经成功提取了玩家的健康状况,但是当我尝试显示它时,我收到标题中的错误。

这是我用来显示它的代码:

localplayerhealth=player.gethealth()

healthmessage=('HEALTH: ',localplayerhealth,'%')

messagedisplay(healthmessage,BLACK,500,600, 30, 30, 'mediumfont')

localplayerhealth是整数数据类型。我已经尝试了很多方法让它工作,但由于某种原因它们不起作用。

完整追溯:

loop(gameclass,s,ishost,received)
  File "C:\Users\Luke\Desktop\Year13CA\Base.py", line 579, in loop
    player.updateposition(True,False)
  File "C:\Users\Luke\Desktop\Year13CA\PlayerClasses.py", line 249, in updateposition
    playerhealth=messagedisplay(self.health,BLACK,500,500, 30, 30, 'smallfont')
  File "C:\Users\Luke\Desktop\Year13CA\Shared.py", line 22, in messagedisplay
    surface, rectangle = textobject(message,colour,font)  #assigns both the variables of surface and rectangle to the output of the subroutine called with the imported parameters.
  File "C:\Users\Luke\Desktop\Year13CA\Shared.py", line 10, in textobject
    textSurface = smallfont.render(text, True, colour)
TypeError: text must be a unicode or bytes

1 个答案:

答案 0 :(得分:4)

如果要构造要显示的字符串,则必须提供正确的格式。现在你正在创建一个元组:

>>> localplayerhealth=1
>>> 'HEALTH: ',localplayerhealth,'%'
('HEALTH: ', 1, '%')

我猜你需要一个字符串:

>>> to_display = "HEALTH: {}%".format(localplayerhealth)
>>> to_display
'HEALTH: 1%'