Difference between revisions of "Drawing text"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(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...")
 
(Method 2)
 
Line 15: Line 15:
  
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>
 +
#setup font
 
pygame.font.init()
 
pygame.font.init()
 
font_path = "./fonts/newfont.ttf"
 
font_path = "./fonts/newfont.ttf"
 
font_size = 32
 
font_size = 32
 
fontObj = pygame.font.Font(font_path, font_size)
 
fontObj = pygame.font.Font(font_path, font_size)
 +
 +
#Render text
 
label = fontObj.render("Some text!", 1, (255,255,0))
 
label = fontObj.render("Some text!", 1, (255,255,0))
 
screen.blit(label, (100, 100))
 
screen.blit(label, (100, 100))
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 17:02, 30 June 2018

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))