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


Groups > comp.lang.python > #61288 > unrolled thread

Centring text in a rect in PyGame?

Started byEamonn Rea <advanced.world.unlimited@gmail.com>
First post2013-12-07 12:41 +0000
Last post2013-12-07 12:41 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

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

#61288 — Centring text in a rect in PyGame?

FromEamonn Rea <advanced.world.unlimited@gmail.com>
Date2013-12-07 12:41 +0000
SubjectCentring text in a rect in PyGame?
Message-ID<mailman.3725.1386506182.18130.python-list@python.org>

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


[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web