Skip to content

Instantly share code, notes, and snippets.

@macfij
Created December 28, 2013 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macfij/8162008 to your computer and use it in GitHub Desktop.
Save macfij/8162008 to your computer and use it in GitHub Desktop.
ghostfrag
import pygame
from pygame.locals import *
FONT_SIZE = 60
def name():
screen = pygame.display.set_mode((1280, 720))
name = ""
font = pygame.font.SysFont(None, FONT_SIZE)
while True:
# readlines returns a list; having this in
# loop allows pygame to draw recently added
# string; no need to close and open a window
namefile = open('test.txt', 'r')
names = namefile.readlines()
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.unicode.isalpha():
name += event.unicode
elif event.key == K_BACKSPACE:
name = name[:-1]
elif event.key == K_RETURN:
f = open("test.txt", "a")
f.write(str(name) + "\n")
f.close()
name = ""
elif event.type == QUIT:
return
# create a Rectangle container, where yours
# text variable will be drawn
# Rect(left, top, width, height)
textrect = Rect(0, 0, 100, FONT_SIZE)
screen.fill((0, 0, 0))
for i in names:
# iterate through lines from text file (it is stored
# in names variable and it is a list)
# create text variable and draw it to textrect
text = font.render(i[:-1], True, (255,0,0), (0,0,0))
screen.blit(text, textrect)
# change y coordinate of textrect; in next iteration
# next line will appear below the previous line
textrect.centery += FONT_SIZE
block = font.render(name, True, (255, 255, 255))
rect = block.get_rect()
rect.center = screen.get_rect().center
screen.blit(block, rect)
pygame.display.update()
pygame.display.flip()
if __name__ == "__main__":
pygame.init()
name()
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment