Pygame: Can't get adjust font size to window size custom function to work

Viewed 50

I am a complete beginner and i'm trying to do this:

pygame.init()
fps=60
FramePerSec=pygame.time.Clock()

WHITE=(255,255,255)

font = pygame.font.SysFont('comicsansms',20)


DISPLAYSURF=pygame.display.set_mode((640,480),pygame.RESIZABLE)
pygame.display.set_caption("3")

QUIT=pygame.QUIT
TIME=0

def adjustfontsize():
    font=pygame.font.SysFont('comicsansms',int (min(DISPLAYSURF.get_width()/32,DISPLAYSURF.get_height()/24)))



nl=pygame.font.Font.get_linesize(font)*3/4
def writetext(x,y,n,z):DISPLAYSURF.blit(font.render(z,True,WHITE),(x,y+nl*(n-1)))

while True:
    DISPLAYSURF.fill((0,0,0))
    adjustfontsize()


    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            sys.exit()
    TIME=pygame.time.get_ticks()
    if TIME>=2000:
        writetext(DISPLAYSURF.get_width()/3,DISPLAYSURF.get_height()/4,1,"¡Hola! Soy la primer línea")
    if TIME>=4000:
        writetext(DISPLAYSURF.get_width()/3,DISPLAYSURF.get_height()/4,2,"¡Hola! Yo soy la segunda línea")
    pygame.display.update()
    FramePerSec.tick(fps)


Now, what I would like to know is why the font size still doesn't change. Already added the missing bg draw pointed by Lost Coder, but that was not the issue here. I know there are other ways to do this detailed out there, just want to learn by getting to know what i did wrong. There are not similar functions involved in suggested questions. Thank you very much!

1 Answers

Hey you need to fill your screen black (In your example as you are using a black background) because otherwise the updated text will overlap with the other text and will cause render problems.

Imagine the Screen as a painting canvas where when you "draw" a new picture you first have to clear the canvas. That's what you essentially do with display.fill((0,0,0))

If you still have problems with the relative resizing there is already a thread on stack overflow regarding that topic. (How to scale the font size in pygame based on display resolution?)

Update: I've tested it yeah it seems that the problem was that you have edited the a global variable in the function and haven't returned it. Keep in mind that the function acts like its own "safe space" you can use global variables in the function but you can't change them on a global space unless you return the variable

def adjustfontsize():
    font=pygame.font.SysFont('comicsansms',int (min(DISPLAYSURF.get_width()/32,DISPLAYSURF.get_height()/24)))
    return font

#...
#...


while True:
    font = adjustfontsize() #This will actually change the font in the global space.

(references) https://www.geeksforgeeks.org/global-local-variables-python/

You could also define the variable in the function as a global variable. But you shouldn't as it is bad practice. Just return the desired variable.

def adjustFont():
    global font
    font = pygame.font.SysFont('comicsansms',int (min(DISPLAYSURF.get_width()/32,DISPLAYSURF.get_height()/24)))

Related