1

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))
James Z
  • 12,209
  • 10
  • 24
  • 44
Raven
  • 31
  • 7

1 Answers1

0

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()
Kingsley
  • 14,398
  • 5
  • 31
  • 53
  • i got this error size = pygame.font.Font.size(my_font, 50) TypeError: text must be a unicode or bytes – Raven Nov 24 '22 at 02:57
  • @Raven - check what you're passing to `.size()`, it expects a string, you're passing it something that's *not* a string. Ref: https://www.pygame.org/docs/ref/font.html#pygame.font.Font.size – Kingsley Nov 24 '22 at 02:59
  • @Raven - You're using `size()` incorrectly. It should be called with the Font object you've created - an "instance" of a Font object, not the object definition. Please see the usage above `my_font.size( "blah" )`. – Kingsley Nov 24 '22 at 03:03
  • got this error now dis.blit(my_font.render(counter), [550, 450]) TypeError: function takes at least 3 arguments (1 given) – Raven Nov 24 '22 at 03:05
  • here the line of code dis.blit(my_font.render(counter), True, [550, 450]) – Raven Nov 24 '22 at 03:06
  • @Raven - `counter` is a Surface bitmap object of the rendered text. If `dis` is a window, or another surface, you'd want something like `dis.blit( counter, [550, 450] )` – Kingsley Nov 24 '22 at 03:07
  • how do i fix it? – Raven Nov 24 '22 at 03:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/249850/discussion-between-kingsley-and-raven). – Kingsley Nov 24 '22 at 03:09