[Python] Font not displaying?

Discussão em 'Python' iniciado por Stack, Julho 1, 2025.

  1. Stack

    Stack Membro Participativo

    I've tried changing font size, font color, and font location. What is wrong with code?

    import pygame
    pygame.font.init()#to start pygame
    screen_width, screen_height = 1920, 1080
    screen=pygame.display.set_mode((screen_width,screen_height))
    pygame.display.set_caption("my pygame window")


    def objects():
    player=pygame.Rect(400,300,100,100)
    boundary = pygame.Rect(100, 80, 600, 400)
    boundary_water= pygame.Rect(-50, -70, 900, 700)
    obstacle=pygame.Rect(300,400,50,50)
    speedofplayer=5
    score=0
    collide_frame=False
    clock = pygame.time.Clock()
    running=True
    return player,boundary,obstacle,speedofplayer,clock,running,boundary_water,collide_frame,score

    def obstacles(keys,player,obstacle):
    if keys[pygame.K_a] or keys[pygame.K_LEFT] :
    player.left=obstacle.right
    if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
    player.right=obstacle.left
    if keys[pygame.K_w] or keys[pygame.K_UP]:
    player.top=obstacle.bottom
    if keys[pygame.K_s] or keys[pygame.K_DOWN]:
    player.bottom=obstacle.top

    def player_movement(keys,player,speedofplayer):
    if keys[pygame.K_a] or keys[pygame.K_LEFT] :
    player.x-=speedofplayer
    if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
    player.x+=speedofplayer
    if keys[pygame.K_w] or keys[pygame.K_UP]:
    player.y-=speedofplayer
    if keys[pygame.K_s] or keys[pygame.K_DOWN]:
    player.y+=speedofplayer


    def boundaries(player,boundary):
    if player.left<boundary.left:
    player.left=boundary.left
    if player.right>boundary.right:
    player.right=boundary.right
    if player.top<boundary.top:
    player.top=boundary.top
    if player.bottom>boundary.bottom:
    player.bottom=boundary.bottom

    def score_ofplayer(screen,score,collide_frame,player,obstacle,font):

    pass

    def run_program():

    player,boundary,obstacle,speedofplayer,clock,running,boundary_water,collide_frame,score=objects()
    while running:
    for event in pygame.event.get():
    if event.type==pygame.QUIT:
    running=False
    keys=pygame.key.get_pressed()
    player_movement(keys,player,speedofplayer)
    boundaries(player,boundary)
    if player.colliderect(obstacle):
    obstacles(keys,player,obstacle)
    camera_x = player.x - screen_width // 2
    camera_y = player.y - screen_height // 2
    player_draw = pygame.Rect(player.x - camera_x, player.y - camera_y, player.width, player.height)
    obstacle_draw = pygame.Rect(obstacle.x - camera_x, obstacle.y - camera_y, obstacle.width, obstacle.height)
    boundary_draw = pygame.Rect(boundary.x - camera_x, boundary.y - camera_y, boundary.width, boundary.height)
    boundary_water_draw=pygame.Rect(boundary_water.x - camera_x, boundary_water.y-camera_y,boundary_water.width,boundary_water.height)

    font=pygame.font.SysFont(None,36)

    screen.fill((0,255,0))


    pygame.draw.rect(screen,'blue',obstacle_draw)
    pygame.draw.rect(screen,'blue',boundary_water_draw, width=100)
    pygame.draw.rect(screen, "white", boundary_draw, width=3)
    pygame.draw.rect(screen,'orange',player_draw)

    if player.colliderect(obstacle):
    if not collide_frame:
    score+=1
    collide_frame = True
    else:
    collide_frame=False
    screentext=font.render(f"score= {score}",True,(255, 255, 255))
    screen.blit(screentext,(20,20))

    pygame.display.flip()
    clock.tick(60)
    pygame.quit()
    run_program()



    My font doesn't display on pygame window, I've removed the font from a function for now and directly inserted it in the game loop, but it still doesn't work, I've tried changing color of font also.

    Continue reading...

Compartilhe esta Página