Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.86.MISMATCH!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:text': 0.05; 'true,': 0.05; 'width': 0.09; '12)': 0.16; 'from:addr:timgolden.me.uk': 0.16; 'from:name:tim golden': 0.16; 'message-id:@timgolden.me.uk': 0.16; 'pygame': 0.16; 'pygame.': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'stuff.': 0.16; 'tjg': 0.16; 'used:': 0.16; 'wrote:': 0.18; 'skip:p 40': 0.19; 'code,': 0.22; 'import': 0.22; 'saying': 0.22; 'header:User-Agent:1': 0.23; 'header:In-Reply-To:1': 0.27; 'code': 0.31; 'fine,': 0.31; 'font': 0.31; 'another': 0.32; 'text': 0.33; 'actual': 0.34; 'problem.': 0.35; 'but': 0.35; 'height': 0.36; 'subject:?': 0.36; 'button': 0.38; 'window': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'charset:windows-1252': 0.65; 'gotten': 0.74; 'from:addr:mail': 0.83; 'game,': 0.84; 'i\x92ve': 0.84; 'surface': 0.84; 'world!"': 0.84; 'can\x92t': 0.91 Date: Sun, 08 Dec 2013 15:42:44 +0000 From: Tim Golden User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.1.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Centring text in a rect in PyGame? References: <2BF41548-E665-4678-ACDE-2222C07C3E78@gmail.com> In-Reply-To: <2BF41548-E665-4678-ACDE-2222C07C3E78@gmail.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386517367 news.xs4all.nl 2840 [2001:888:2000:d::a6]:48098 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61297 On 07/12/2013 12:41, Eamonn Rea wrote: > 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))* Side-stepping your actual code, here's a handy thing in pygame. You can center one rect on another to achieve the same effect without bothering with the (x - y) / 2 stuff. import pygame pygame.init() # # For demo purposes, the button is the whole window # button = pygame.display.set_mode((400, 320)) # # Create 12-point text in white saying "Hello, World!" # font = pygame.font.Font(pygame.font.get_default_font(), 12) text = font.render("Hello, World!", True, (0xff, 0xff, 0xff)) # # Use the text's rect to get width / height # Then center that rect on the target surface # text_rect = text.get_rect() text_rect.center = button.get_rect().center button.blit(text, text_rect) pygame.display.flip() TJG