(*1) En el ejemplo se utiliza la fuente dejavu.ttf que se puede obtener del artículo sobre el menú de esta misma web. Aunque el tipo de fuente es lo de menos.
El código está bajo la licencia GNU GPL v3 siendo yo el autor.
- Código: Seleccionar todo
import sys
import pygame
from pygame import *
class Escritura():
def __init__(self):
self.line = 0
self.strings = ['',]
#self.font = pygame.font.Font('dejavu.ttf', 12) #(*1)
self.font = pygame.font.Font(None, 28)
self.dist = 20
self.ipos_x = 50
self.ipos_y = 50
def update(self, events):
for event in events:
if event.type == KEYDOWN:
if event.key == K_RETURN:
self.strings.append('')
self.line += 1
elif event.key == K_ESCAPE:
sys.exit()
elif event.key == K_BACKSPACE:
if self.strings[self.line] == '' and self.line > 0:
self.strings = self.strings[0:-1]
self.line -= 1
else:
self.strings[self.line] = self.strings[self.line][0:-1]
else:
self.strings[self.line] = str(self.strings[self.line] + event.unicode)
def draw(self, screen):
print self.strings
screen.fill((0, 0, 0))
for line in range(len(self.strings)):
img_line = self.font.render(self.strings[line], 1, (255, 255, 255))
screen.blit(img_line, (self.ipos_x, self.ipos_y + self.dist * line))
def main():
pygame.init()
screen = pygame.display.set_mode((600, 600))
salir = False
escritura = Escritura()
while salir == False:
events = pygame.event.get()
for event in events:
if event.type == QUIT:
salir = True
escritura.update(events)
escritura.draw(screen)
display.update()
if __name__ == '__main__': main()
Como siempre: acepto críticas y/o comentarios.
Saludos.
Edit:
(*1) Ver siguiente mensaje de hugoruscitti