Search code examples
pythonfontspygameattributeerror

When changing font size and what font im using, pygame gives me an error


I'm trying to run this snippet of code in my Python Pygame project

my_font = font.SysFont('freesansbold', 50)
my_font.set_bold(True)
size = pygame.font.Font.size(my_font, 50)
counter = font.render(str(round((time+1000)/1000)), True, (50,50,50))

However when I try to run this code it returns this error

File "c:\Users\s12073\Desktop\digi tech stuff\testing.py", line 93, in <module>
size = pygame.font.Font.size(font.SysFont('Courier', 50).bold(True), str(round((time+1000)/1000)))
AttributeError: 'pygame.font.Font' object has no attribute 'SysFont'

Updated code:

    coun = str(round((time+1000)/1000))
    my_font = pygame.font.SysFont('freesansbold', 50)
    my_font.set_bold(True)
    size = pygame.font.Font.size(my_font, 50)
    width, height = my_font.size(coun)
    counter = font.render(coun, True, (50,50,50))

Solution

  • Your code is a bit wrong in places.

    First the font object (pygame.font.Font) needs to be created:

    pygame.init()
    pygame.font.init()
    
    my_font = pygame.font.SysFont('freesansbold', 50)
    my_font.set_bold(True)
    

    The function pygame.font.SysFont() returns a configured Font object. Once it's made, generally you always use that for further operations.

    Thus, the the size() function needs your Font object, and a string:

    width, height = my_font.size( "Some example Text" )
    

    And to render to a Surface, use it again:

    counter = my_font.render(str(round((time+1000)/1000)), True, (50,50,50))
    

    The code in the question is changing between using font and my_font. You normally want the Font object, that is my_font.

    Putting all that together:

    import pygame
    
    pygame.init()
    pygame.font.init()
    
    time = 1  # to make the example work
    
    my_font = pygame.font.SysFont('freesansbold', 50)
    my_font.set_bold(True)
    
    font_text = str(round((time+1000)/1000))
    size      = my_font.size( font_text )
    counter   = my_font.render( font_text, True, (50,50,50))
    

    Full Text of worked example:

    import pygame
    
    WINDOW_WIDTH = 600
    WINDOW_HEIGHT= 600
    
    pygame.init()
    pygame.font.init()
    window = pygame.display.set_mode(( WINDOW_WIDTH, WINDOW_HEIGHT))
    
    # Create the Font object
    my_font = pygame.font.SysFont('freesansbold', 50)
    my_font.set_bold(True)
    
    time = 1
    
    running = True
    clock = pygame.time.Clock() # for framerate timing
    while running:
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False # stops animation
    
        time += 3  # just so it changes
    
        # Convert the numer to a string
        counter_text = str( time ) # str(round((time+1000)/1000)
    
        # Convert the string to a bitmap (Surface)
        counter_surface = my_font.render( counter_text, True, (50,50,50))
    
        # paint the screen
        window.fill( ( 0,0,0 ) )                      # black background
        window.blit( counter_surface, ( 100, 100 ) )  # draw the counter
    
        pygame.display.update()
        clock.tick( 60 )
    
    pygame.quit()