-1

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 Answers1

0

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

Lost_coder
  • 94
  • 8
  • Thanks! Yeah, i was forgetting about the bg thing because i hadn't drawn anything that should be erased (until now, as you made me see, default rendered text surface needs to be erased before drawing the adjusted size text surface). Already added the background refill and, anyway, my adjustfontsize() function doesn't work, and i don't get why. Also, i have been trying to do it with pygame.freetype, but for some reason i can't get the style flag to be recognised. – Nachito Somma Sep 19 '22 at 03:52
  • @NachitoSomma Well it could be that you are defining the font variable in adjustfontsize() Function. Variables defined in a function. Do not change in the global space and only in the function space. Small example:`a = 2` `def changeA():a=1``changeA()``print(a)` results in Output: `2` So you could either update the variable in the global space or return the variable in the function. I could test it if that was the case and post the answer on my original post. – Lost_coder Sep 19 '22 at 14:30
  • Yeah, that was it. Nice explanation! Thank you very much! Now the question is answered and i learned a good lesson :) – Nachito Somma Sep 21 '22 at 05:05