python pygame what does font mean? -
i studying online pygame tutorial. however, not sure how works when trying place text on screen. according official docs pygame.font.sysfont()
:
return new font object loaded system fonts. font match requested bold , italic flags. if suitable system font not found fall on loading default pygame font. font name can comma separated list of font names for.
what font?
font = pygame.font.sysfont(none, 25) # message user def message_to_screen(msg,color): screen_text = font.render(msg, true, color) screen.blit(screen_text, [screen_width/2,screen_height/2])
ok, here simplest explanation can give you:
modules such pygame simple (or not simple...) codes add new features , functions normal built in python functions. means when import module inherent module of functions , classes. example, normal python not contain function "draw"
pygame.draw.rect(arguments)
however when import pygame, inherent function pygame code. allowing draw , develop gui better programs.
same objects. python 'object orientated programming language". objects type of data store defines , structures code. example, sprites in pygame can want. sprite can want monkey, or freaking mummy eating zombie, simple rectangle. create exact sprite want right shape, color, rect, , image, need structure class. class create object sprite. @ here:
#here class named 'button' of type ' pygame.sprite.sprite' class button(pygame.sprite.sprite): def __init__(self): super().__init__() #here define shape of sprite. in case simple #150 75 rectangle surface. shape can image or #or geometric shape want self.image = pygame.surface((150, 75)) #here define color of sprite self.image.fill(green) #and here make sure sprite has rect self.rect = self.image.get_rect()
so see class defines need create simple sprite. of course can have many more variables depending on sprite is, lets stick simple now
now class stores information in object, used later. this:
myspriteobject = button()
simple enough say. have sprite object , can use pygames' many function draw on screen, add interaction it, group it, , lot of other things.
so understand idea of object in python. you're actual question.
what font? font object when import pygame. don't have class stuff @ top pygame module you. create object , use function 'render'. object can change 2 things in like. font, , size
myfontobject = pygame.font.font(#font here, #size here)
if make font argument none, give default pygame font. thats do. however, if want change font, can either download font (usually .ttf) , type in folder path in font argument, or can use font have on computer. instead of
myfontobject = pygame.font.font(#font here,#size here)
you use
myfontobject = pygame.font.sysfont(#name of font here, #size here)
where #name of font here is, can replace font installed on computer. list of names of fonts on computer pygame can identify:
pygame.font.get_fonts()
ok, how create font object. rendering it. rendering font uses font object change shape , color of text want display. here how done
text = myfontobject.render(#your text here,#true or false,#color) screen.blit(tex,t(#x axis,#y axis))
pretty self explanatory. except #true or false guess. pretty asks if want use technique helps text less pixeled , square-like. if provide true will. if dont text awefull, keep true.
so that's pretty have say. here short summary:
1.) object type of data store stores different variables structure , define code. therefore font object object defines different things font, such size , font type
2.) create object use class shown above
3.) font class there pygame module have call font object straight away:
myfontobject = pygame.font.font(#filepath or none default pygame font,#size here)
or font installed on system such ariel (which can viewed pygame.font.get_font())
myfontobject = pygame.font.sysfont(#name of system font here,#size here)
4.) put font object use render it:
text = myfontobject.render(#text here,#true or false,#color of text)
then blit on screen , call pygame.display.update
screen.blit(text,(#x axis,#y axis) pygame.display.update()
i hope helps. know i'm not best explainer , write much, should read summary @ least.
p.s: sorry using sprites explain classes , objects. know went of topic example.
Comments
Post a Comment