【问题标题】:print json line by line pygame [duplicate]逐行打印json pygame [重复]
【发布时间】:2020-10-07 14:26:16
【问题描述】:

我正在尝试在 pygame 窗口中逐行打印 json。问题是,我不能换一条新路线。我写了这段代码:

def printDict(griglia, nRighe, nColonne, cella, jsonTesto):
    textsurface = myfont.render(jsonTesto, False, (255, 255, 255))
    screen.fill(pygame.Color("yellow"), (0, nRighe*cella+2*cella*cella, nColonne*cella+cella, nRighe*cella+3*cella*cella))
    screen.blit(textsurface, (cella, nRighe*cella+4*cella))
    pygame.display.update()

它在我想要的地方打印并覆盖旧的书面内容,但是当我得到结果时,它是内联的,并且在应该换行的地方有“]”。

或者,您可以通过限制条件来帮助我吗?我的意思是,是否可以在窗口结束时使文本换行?

你能帮帮我吗?

【问题讨论】:

  • 来自the documentation:“文本只能是单行:不呈现换行符。”您必须逐条绘制。
  • 非常感谢!我希望有技巧啊……没关系:)

标签: python json pygame


【解决方案1】:

Pygame 无法将\n(中断)打印到屏幕上。你必须使用这样的函数:

def printDict(jsonText, x, y, line_height):
    jsonParsed = json.dumps(json.loads(jsonText), indent=4, sort_keys=True)
    y_offset = 0
    for line in jsonParsed.split("\n"):
        textsurface = myfont.render(line, False, (0, 0, 0))
        screen.blit(textsurface, (x, y + y_offset))
        y_offset += line_height
    pygame.display.update()

这会将json作为文本分割成行并分别打印

【讨论】:

  • 非常感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-27
  • 1970-01-01
  • 2019-07-03
  • 2022-11-24
  • 1970-01-01
  • 2016-11-19
  • 2016-10-31
相关资源
最近更新 更多