I am trying to make a game and I am trying to render a lot of text. When the text renders, the rest of the text goes off the screen. Is there any easy way to make the text go to the next line of the pygame window?
helpT = sys_font.render \
("This game is a combination of all of the trends\n of 2016. When you press 'Start Game,' a menu will pop up. In order to beat the game, you must get a perfect score on every single one of these games.",0,(hecolor))
screen.blit(helpT,(0, 0))
One thing you could do is use a monospaced font. They have the same size for all characters and so are beloved by programmers. That's going to be my solution for handling the height/width problem.
As I said in the comments; you have to render each word separately and calculate if the width of the text extends the width of the surface (or screen). Here's an example:
Result
I recommend the
ptext
library which is able to recognize newline (\n
) characters. You only need to callptext.draw(text, position)
.You could use a .json file to load each line.
.json file (called first.json):
And then load it into the file:
Don't forget to
import json
Result:![enter image description here](https://i.stack.imgur.com/NpJFu.png)
Hope this helps!
This is how I did it
but of course, I can only do that because my text starts a line distance from the top of the screen. If you start further down the screen you could do
There is no easy way to render text on multiple lines in pygame, but this helper function could provide some use to you. Just pass in your text (with newlines), x, y, and font size.