[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Text() class, a pygame.font.Font() helper classes
- To: pygame-users@xxxxxxxx
- Subject: [pygame] Text() class, a pygame.font.Font() helper classes
- From: "Jake b" <ninmonkeys@xxxxxxxxx>
- Date: Tue, 6 Jan 2009 00:19:42 -0600
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Tue, 06 Jan 2009 01:19:46 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:mime-version:content-type; bh=WWxfZ+LpMcrOUvEVOXBxcO8+2xFpCPvIXJZ2CMMaGiE=; b=M7MvFgEcf4vanb1TCfCpQnWSthcjaYCcegQbAzkfiEllcwGBfNvK12vt/PYuwQEYTS IIXavVHUpkSdt2yJbFXPLDHlFWLyQ8k+y1TJ2BWQDGQvnZcVOBxxEd4eT5tl3hjZ+ERW 7EPJuTZ1RV2V2JV0mT3/+fwbZQ+EQ/vUfagf8=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type; b=I5giECR1GUU4E556X28glwv68dh7orPtcKBL+NnzM5D42nZQXM2DDzeBoV6LBKW8sH JNAIfgS/sfBS1rwpdYroMPWSPk+dMb/8ko/flptu8qpSU7c5nD1F3bfT3zYbDgQ8cSx5 v96UrHi4Hn8HKDeqrPAkwCjpzNYepobSUwHME=
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
I'm sure you have your own text wrappers. I think pygame should get some support. I'm thinking of having a Text() or TextGroup() that is similar to SpriteGroups().
I have a Text() class that is a wrapper to pygame.font.Font(), and does some basic functions. It cache's the font.render() result, and uses cache on .draw(). It also only re-renders the text, if it has changed ( in color, size, text, font, etc... ) otherwise uses cache.
The second class, FPSText() is a quick way to get a Text() object with the current FPS.
I was going to make a TextGroup() that acts like a spritegroup, containing my Text() objects. I also planned a multi-line object, and the ability to have different colors in a single Text() line. ( think of standard RPG has a paragraph of text with certain words escaped with color codes ). Right now I need to improve the color methods to use multiple types of color definitions ( str, or tuple )
[ In the future could maybe even make clickable links ( aka: url), that onclick() call a callback function defined on Text() creation. example: civilization in-game-help. )
1) Is this something that pygame would add? ( I know theres a new dev version of pygame, but i'm not sure if it has anything like this. )
2) Should I be using pygame.color.Color() OR pygame.Color() ? ( Or use either, they point to same class? )
3) Any feedback would be appreciated, thanks.
Here is example usage of Text() and FPSText()
Example usage:
in Game.init():
# create text, later change font
t = Text('foo')
t.match_font('arial')
# 2) use filepath to tff
t2 = Text('foo', font='ttf/freesansbold.ttf')
# 3) list of font names (without .ttf) to search OS for.
t3 = Text('foo', font='arial, verdana, foobar')
# set loc using alignment [and optional offset ]
t = Text("align=topright,offset(x,y)", size=16,color="darkred")
t.align( "bottomleft", self.width, self.height)
t.align( "topright", self.width, self.height, (-60,80))
# usage in Game()
self.points = Text("Points: None!")
self.fps = FPSText()
# ... append a bunch of Text() objects to align_list
self.align_list = []
self.align_list.append( t1 )
self.align_list.append( t2 )
self.align_list.append( t3 )
in Game.draw(self):
self.points = "Points: %s" % ( player_points ) # caches if same value
self.text.draw()
self.fps.draw()
self.fps.tick()
for t in self.align_list: t.draw()
[ note: the attached code has a module 'game', which is just a wrapper to boilerplate of a pygame game. makes test_text.py code simpler. ]
Attachment:
jakelib.text - v0.0.6.zip
Description: Zip archive
Attachment:
screenshot.v0.0.6.png
Description: PNG image