0

I am trying to print a json line by line in a pygame window. The thing is, I can't go on a new line. I wrote this code:

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()

It prints where I want and covering old written things, but when I get the result, it is inline and has "]" where it should go on a new line.

Alternatively, can you help me by putting constraits? I mean, is it possible to make the text go on a new line when the window ends?

Can you help me please?

hellomynameisA
  • 546
  • 1
  • 7
  • 28
  • 1
    From [the documentation](https://www.pygame.org/docs/ref/font.html): "The text can only be a single line: newline characters are not rendered." You will have to draw each line one by one. – Jongware Jun 17 '20 at 17:22
  • Thank you very much! I hoped there were tricks ahah... never mind :) – hellomynameisA Jun 17 '20 at 17:30

1 Answers1

2

Pygame cannot print \n (break) to the screen. You have to use a function like this:

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()

This splits the json as text into lines and prints them separately

Deepthought
  • 118
  • 9