Programming in python: Can’t get pygame font to render transparently on image on newest questions tagged python – Stack Overflow
I’ve found lots of tutorials for using pygame, and its font library, but they all show the same exact thing. They all show you how to write some plain text on to a background, which is great, but going beyond that there’s not a lot of useful information that I’ve found.
With the project I’m working on, I have menu buttons that I’ve implemented as sprites. When you hover over them they change colors, and that all works pretty well. What I’d like to do is write text on these buttons, but I’m baffled on how to do that. From the docs font.render works as so:
“This creates a new Surface with the specified text rendered on it. Pygame provides no way to directly draw text on an existing Surface: instead you must use Font.render – draw text on a new Surface to create an image (Surface) of the text, then blit this image onto another Surface.”
So I’ve tried to take the image that’s attached to my sprite and directly blit my text onto that. That seems to mostly do absolutely nothing:
resume = self.button_font.render( 'Resume Game', True, constants.WHITE, (23, 56, 245) # Main color of the button, tried without this as well ) self.resume_button.image.blit( resume, self.resume_button.rect, )
I know that code runs, but you never see any text. If I blit the text directly to my main screen surface, it’ll just write over the top of my resume button (depending on the blit order of course). What am I doing wrong? The docs seem to indicate that this is the right way to handle it, but I’ve yet to find anyone else doing this. Any help would be greatly appreciated.
Programming in python: programming-in-python