Text!

I’ve added a little module over in downloads under Misc that demonstrates how to get text going with pygame. With a few changes this will be the basic idea behind getting a text-based UI going in my game. I suppose not making a few rows of tiles at the bottom and sticking a text window there would work, although I still need to find a working/decent system font that’s common across various OS’s. Or I could just add my own, either or.

The meat of the code would be this:

class TextHandler():
def __init__(self):
self.messageQueue = []
self.y = 0
def addMessage(self,message):
self.messageQueue.append(message)
obj = pygame.font.SysFont("Monospace",16)
entry = obj.render(message,0,(255,255,255))
pygame.display.get_surface().blit(entry,(0,self.y))
pygame.display.flip()
self.y+=16
self.y%=768
if not self.y: pygame.display.get_surface().fill((0,0,0))

sidenote: indents dont seem to want to work in wordpress. Le sigh.

It’s still a little light on functionality, but essentially you make a new Text() object, and then instead of ‘print “text” ‘ you call text.addMessage(‘text’).
That adds the message to the queue for later scrolling, then it uses basic pygame to create a font object and render the text onto its own surface. Then it blits that surface onto the display window, the screen updates, and then for position-reasons it recalculates where on the y-axis to actually put the next text. Then if the y value reaches back up to the top of the screen, the screen clears and we start over.

I suppose some sort of deconstructor would be handy for memory-reasons, and instead of flip() a call to update() with a rectangle covering the text region would work, but that’s for later I guess.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: