Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Pygame text editing example
- # Author: Weeble
- # License: http://www.opensource.org/licenses/MIT
- import pygame
- pygame.init()
- screen = pygame.display.set_mode((1024,200))
- text = u"Is this effective?"
- cursorpos = 10
- times = pygame.font.SysFont("Times New Roman", 100, False, True)
- pygame.key.set_repeat(300,50)
- while True:
- screen.fill((255,255,255))
- # Draw the text
- textsurf = times.render(text, True, (0,0,0))
- screen.blit(textsurf, (0,40))
- # Draw the cursor
- lefttext = text[:cursorpos]
- lefttextwidth = times.size(lefttext)[0]
- if cursorpos > 0 and len(text) > 0:
- leftchar = text[cursorpos - 1]
- _, xmax, _, _, advance = times.metrics(leftchar)[0]
- if xmax > advance:
- # Character immediately left of the cursor has
- # an overhang.
- lefttextwidth -= xmax - advance
- screen.fill((0,0,0),(lefttextwidth, 40, 2, times.get_height()))
- pygame.display.flip()
- ev = pygame.event.wait()
- if ev.type == pygame.QUIT:
- break
- elif ev.type == pygame.KEYDOWN:
- if ev.key == pygame.K_BACKSPACE and cursorpos > 0:
- cursorpos -= 1
- text = text[:cursorpos] + text[cursorpos+1:]
- if ev.key == pygame.K_DELETE and cursorpos < len(text):
- text = text[:cursorpos] + text[cursorpos+1:]
- if ev.key == pygame.K_LEFT and cursorpos > 0:
- cursorpos -= 1
- if ev.key == pygame.K_RIGHT and cursorpos < len(text):
- cursorpos += 1
- if ev.key == pygame.K_ESCAPE:
- break
- if ev.unicode != '' and not (0<=ord(ev.unicode)<=31) and not (127<=ord(ev.unicode)<=159):
- text = text[:cursorpos] + ev.unicode + text[cursorpos:]
- cursorpos+=1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement