Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61288

Centring text in a rect in PyGame?

From Eamonn Rea <advanced.world.unlimited@gmail.com>
Subject Centring text in a rect in PyGame?
Date 2013-12-07 12:41 +0000
Newsgroups comp.lang.python
Message-ID <mailman.3725.1386506182.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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 :/


Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Centring text in a rect in PyGame? Eamonn Rea <advanced.world.unlimited@gmail.com> - 2013-12-07 12:41 +0000

csiph-web