Centring text in a rect in PyGame?

Eamonn Rea advanced.world.unlimited at gmail.com
Sat Dec 7 07:41:28 EST 2013


First of all. I’d like to say I have no idea how these ‘mailing lists’ work, so I don’t know if this’ll come through right, but we’ll see I guess :-) I’m coming from the Google Group comp.lang.python, and was suggested to use this instead. Assuming attachments and images work, you should have an image of my code’s result and the attached code files.

Anyway, I have a problem. In my game, I want to draw a button. I’ve gotten the button to draw fine, but I want some text on the button. I’ve gotten the text to draw, but I can’t get it to centre into the button. Here’s the code I’ve used:

self.surface.blit(self.button_text, (self.width / 2 - self.button_text.get_width() / 2, self.height / 2 - self.button_text.get_height() / 2))

self.width is the width of the button, and same goes for self.height. self.button_text is a surface, so I can just call get_width() and get_height() on it because it’s a surface I believe. Here’s the code for drawing the button and it’s border (I have a little border around the button because it’s how I want it to look for my game):

pygame.draw.rect(
            self.surface, self.button_color, Rect((self.x, self.y), (self.width, self.height)))

pygame.draw.rect(
            self.surface, self.border_color, Rect((self.x, self.y), (self.width, self.height)), self.border_width)

I really don’t know why it isn’t centring it. Here’s an image of what I get as a result:



Code files should be attached :-)


Thanks! Any help is appreciated!

My code is formatted the way it is due to my Sublime Text 2 PEP8 auto formatter. I quite like it, actually, and sorry if you find it hard to read :/


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20131207/25a3eaac/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screen Shot 2013-12-07 at 12.34.22.jpg
Type: image/jpg
Size: 35776 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20131207/25a3eaac/attachment.jpg>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20131207/25a3eaac/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: button.py
Type: text/x-python-script
Size: 1726 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20131207/25a3eaac/attachment.bin>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20131207/25a3eaac/attachment-0002.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.py
Type: text/x-python-script
Size: 1486 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20131207/25a3eaac/attachment-0001.bin>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20131207/25a3eaac/attachment-0003.html>


More information about the Python-list mailing list