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


Groups > comp.lang.python > #61288

Centring text in a rect in PyGame?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!newsfeed.fsmpi.rwth-aachen.de!news-2.dfn.de!news.dfn.de!usenet-feed.fhg.de!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <advanced.world.unlimited@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.016
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'elif': 0.05; 'subject:text': 0.05; 'filename:fname piece:py': 0.07; 'none:': 0.07; '__name__': 0.09; 'assuming': 0.09; 'width': 0.09; 'python': 0.11; 'def': 0.12; '"hello': 0.16; "'__main__':": 0.16; '0))': 0.16; '0),': 0.16; 'appreciated!': 0.16; 'formatted': 0.16; 'height,': 0.16; 'keydown': 0.16; 'pep8': 0.16; 'pygame': 0.16; 'self.y': 0.16; 'used:': 0.16; 'width,': 0.16; 'files.': 0.16; ':-)': 0.16; 'all.': 0.16; 'drawing': 0.19; 'skip:p 40': 0.19; 'work,': 0.20; 'import': 0.22; 'button,': 0.24; 'instead.': 0.24; 'suggested': 0.26; 'pass': 0.26; 'idea': 0.28; 'received:172.16': 0.29; 'skip:p 30': 0.29; 'code': 0.31; '255,': 0.31; 'fine,': 0.31; 'font': 0.31; 'we\x92ll': 0.31; 'class': 0.32; 'thanks!': 0.32; 'quite': 0.32; 'text': 0.33; 'guess': 0.33; 'skip:# 10': 0.33; 'skip:_ 10': 0.34; 'problem.': 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'google': 0.35; 'really': 0.36; 'height': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'should': 0.36; 'button': 0.38; 'message- id:@gmail.com': 0.38; 'auto': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'first': 0.61; 'header:Message-Id:1': 0.63; '30,': 0.65; 'charset:windows-1252': 0.65; 'due': 0.66; 'filename:fname piece:jpg': 0.67; '600': 0.68; 'image-size:2**15': 0.72; 'gotten': 0.74; 'actually,': 0.84; 'game,': 0.84; 'isn\x92t': 0.84; 'it\x92s': 0.84; 'i\x92d': 0.84; 'i\x92ve': 0.84; 'shadow': 0.84; 'sublime': 0.84; 'surface': 0.84; '800': 0.91; 'can\x92t': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:content-type:subject:message-id:date:to:mime-version; bh=80ACpCTG12jchGRwznfGQZy8WPd3AyDALsktcZh1V+Q=; b=mnAULgoPcAu6FAx11NqpBG5iRCyVRt9rJy5OT0ESqAp4ziW7ddPD2lfyqJoVLW1o5G Xe9TOaJFbmDIDt4DET0Cjy7v9D2ymXNknnNZ5mojuCE37mgXdC4XpKyUrY6RPh6IUMir rltt+J9bfkItMwzG3I7o21PwDbLC4B7JmqqXfcEGaNDZaVm661h6iVoSlXSeP6I/vmJI YG2hYBZQAFgnm5uEiM2F9S17i+HZbrO3nVVdUsHIK7L61WmL+rGyi5+lY3vWBZMlL9PY 5aPEJat2SBxm+7cRkivbDxnCM9Gdixnk7VHM6bUEl0OITTwXm/Vk2FTmB0UPOLcPh4bC vI+g==
X-Received by 10.194.104.42 with SMTP id gb10mr7620142wjb.16.1386420091066; Sat, 07 Dec 2013 04:41:31 -0800 (PST)
From Eamonn Rea <advanced.world.unlimited@gmail.com>
Content-Type multipart/alternative; boundary="Apple-Mail=_1E49A1B3-0D72-4750-95FE-CCF7D0401479"
Subject Centring text in a rect in PyGame?
Date Sat, 7 Dec 2013 12:41:28 +0000
To python-list@python.org
Mime-Version 1.0 (Mac OS X Mail 7.0 \(1822\))
X-Mailer Apple Mail (2.1822)
X-Mailman-Approved-At Sun, 08 Dec 2013 13:36:21 +0100
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3725.1386506182.18130.python-list@python.org> (permalink)
Lines 886
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1386506182 news.xs4all.nl 2971 [2001:888:2000:d::a6]:53993
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:61288

Show key headers only | 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