Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Using pygments to highlight code for use in pygame Using pygments to highlight code for use in pygame

View pygments-pygame.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#!/usr/bin/env python
 
import pygame
from pygame.locals import *
from pygame.color import Color
 
from pygments.lexers import PythonLexer
from pygments.styles.emacs import EmacsStyle
 
 
SCREENRECT = Rect(0, 0, 1024, 700)
 
def create_text(screen):
with open("cv.py") as f:
t = f.read()
font = pygame.font.Font("monaco.ttf", 12)
# font.set_bold(True)
height = font.get_height()
y = 0
for i in t.split("\n"):
x = 0
for token, value in PythonLexer().get_tokens(i):
colour = EmacsStyle.style_for_token(token)['color']
if colour:
r, g, b = int(colour[0:2], 16), int(colour[2:4], 16), int(colour[4:6], 16)
else:
r = g = b = 255
print value, colour
tsurface = font.render(value.rstrip(), True, (r,g,b))
screen.blit(tsurface, (x,y))
x += font.size(value)[0]
y += height
 
 
def main():
screen = pygame.display.set_mode(SCREENRECT.size, DOUBLEBUF)
pygame.mouse.set_visible(False)
pygame.font.init()
empty = pygame.Surface(SCREENRECT.size).convert()
create_text(screen)
while 1:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
return
# all.clear(screen,empty)
pygame.display.flip()
 
if __name__ == "__main__":
import sys
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.