Skip to content
Advertisement

How to wrap text in pygame using pygame.font.Font()?

I am making a would you rather game, and I would like to not have character restrictions for the W.Y.R. questions. I have seen many examples here on Stack Overflow and other websites, but they use other modules and methods I don’t understand how to use or want to use. So I would rather use

JavaScript

I would like to know how to use this method and, for example, somehow input how far the text can go until it wraps to the next line.

Thanks

P.S. If you could, please also include centering text, etc.

Advertisement

Answer

This is adapted from some very old code I wrote:

JavaScript

The basic algorithm is to split the text into words and iteratively build up lines word by word checking the resulting width each time and splitting to a new line when you would exceed the width.

As you can query how wide the rendered text will be, you can figure out where to render it to centre it.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement