How to put a text on the pygame screen?

Asked

Viewed 1,125 times

0

I was trying to put a text inside a pygame screen in a specific coordinate, what can I do to print a text and restrict its area?

    import sys, pygame
    pygame.init()

    size = width, height = 800, 600
    speed = [0, 0]
    black = 0, 0, 0

    screen = pygame.display.set_mode(size)

    fundo = pygame.image.load("baner.jpg")
    dialogo = pygame.image.load("dialogo.png")
    estatico = fundo.get_rect()

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()

        estatico = estatico.move(speed)
        if estatico.left < 0 or estatico.right > width:
            speed[0] = -speed[0]
        if estatico.top < 0 or estatico.bottom > height:
            speed[1] = -speed[1]

        screen.fill(black)
        screen.blit(fundo, estatico)
        screen.blit(dialogo, estatico)
        pygame.display.flip()

This is what I have so far, two images and wanted to put a text on top of the dialog boxinserir a descrição da imagem aqui

1 answer

-1

from pygame import font

txt='hello world'                                 ##### armazena o texto
pygame.font.init()                                ##### inicia font
fonte=pygame.font.get_default_font()              ##### carrega com a fonte padrão
fontesys=pygame.font.SysFont(fonte, 60)           ##### usa a fonte padrão
txttela = fontesys.render(txt, 1, (255,255,255))  ##### renderiza o texto na cor desejada
screen.blit(txttela,(50,900)                      ##### coloca na posição 50,900 (tela FHD)
pygame.display.update()                           ##### CARREGA A TELA E EXIBE
  • 1

    It’s cool that you want to help, but you need to take some care with formatting, and maybe an explanation not only within the code would be interesting. I formatted to try to make it more readable, but feel free to [Edit] as you think necessary. Note that to format the code I used the button { } from the format bar.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.