[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Improved pygame inputbox



I've been fiddling around with pygame for some simple AI tests, and I've 
discovered a little more about inputting text. Here's an improved version of 
the inputbox script mentioned a little while ago. The benefits are:

- Cleaner and smaller source
- Text aligned a little better (at least in Linux)
- Handles modifiers right: you can type in "A?!@&^" and it renders correctly.
- Quits on the QUIT signal
- Uses far less CPU time

Hope it's of some use.

####################################################
# Adapted by James Reeves from an inputbox by Timothy Downs
# http://www.pygame.org/pcr/inputbox/inputbox.py

import sys
import pygame
from pygame.locals import *

def display_box(screen, message):
	"Print a message in a box in the middle of the screen"
	font = pygame.font.Font(None, 18)
	rect = pygame.Rect([0, 0, 220, 22])
	offset = (3, 3)

	center = screen.get_rect().center
	rect.center = center
  
	pygame.draw.rect(screen, (0, 0, 0), rect, 0)
	pygame.draw.rect(screen, (255,255,255), rect, 1)
  
	rect.left += offset[0]
	rect.top  += offset[1]
  
	if len(message) != 0:
		screen.blit(font.render(message, 1, (255,255,255)), rect.topleft)
	
	pygame.display.flip()


def ask(screen, question):
	"ask(screen, question) -> answer"
	pygame.font.init()  
	text = ""
	display_box(screen, question + ": " + text)
  
	while True:
		pygame.time.wait(50)
		event = pygame.event.poll()
		
		if event.type == QUIT:
			sys.exit()	 
		if event.type != KEYDOWN:
		  continue
		  
		if event.key == K_BACKSPACE:
			text = text[0:-1]
		elif event.key == K_RETURN:
			break
		else:
			text += event.unicode.encode("ascii")
			
		display_box(screen, question + ": " + text)
		
	return text


if __name__ == '__main__':
	pygame.init()
	screen = pygame.display.set_mode((320,240))
	print "'%s' was entered." % ask(screen, "Name")

-- 
James Reeves
http://www.monkeyengines.co.uk/