Drawing text

From TRCCompSci - AQA Computer Science
Revision as of 16:01, 30 June 2018 by Admin (talk | contribs) (Created page with "=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: <syntaxhighlight lang=python> # initialize fon...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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:

pygame.font.init()
font_path = "./fonts/newfont.ttf"
font_size = 32
fontObj = pygame.font.Font(font_path, font_size)
label = fontObj.render("Some text!", 1, (255,255,0))
screen.blit(label, (100, 100))