Drawing text

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

Method 1

When drawing text you need to use the blit method of the screen you have declared. You can change the font and size:

# initialize font; must be called after 'pygame.init()' to avoid 'Font not Initialized' error
myfont = pygame.font.SysFont("monospace", 15)

# render text
label = myfont.render("Some text!", 1, (255,255,0))
screen.blit(label, (100, 100))

Method 2

You can use variables to pass into the previous example:

#setup font
pygame.font.init()
font_path = "./fonts/newfont.ttf"
font_size = 32
fontObj = pygame.font.Font(font_path, font_size)

#Render text
label = fontObj.render("Some text!", 1, (255,255,0))
screen.blit(label, (100, 100))