Python | Mostrar texto en la ventana de PyGame

Pygame es un conjunto multiplataforma de módulos Python diseñados para escribir videojuegos. Incluye gráficos por computadora y bibliotecas de sonido diseñadas para usarse con el lenguaje de programación Python. Ahora, depende de la imaginación o la necesidad del desarrollador, qué tipo de juego quiere desarrollar usando este conjunto de herramientas.
.

Comando para instalar pygame en un sistema basado en Windows:  

pip install pygame

  
Hay 7 pasos básicos para mostrar texto en la ventana de pygame: 
 

  • Cree un objeto de superficie de visualización utilizando el método display.set_mode() de pygame.
  • Cree un objeto Font usando el método font.Font() de pygame.
  • Cree un objeto de superficie de texto, es decir, un objeto de superficie en el que se dibuje el texto, utilizando el método render() del objeto de fuente pygame.
  • Cree un objeto rectangular para el objeto de superficie de texto usando el método get_rect() del objeto de superficie de texto de pygame.
  • Establezca la posición del objeto Rectangular configurando el valor de la propiedad central del objeto rectangular de pygame.
  • Copiar el objeto de superficie de texto en el objeto de superficie de visualización usando el método blit() del objeto de superficie de visualización de pygame.
  • Muestre el objeto de la superficie de visualización en la ventana de pygame utilizando el método display.update() de pygame.

A continuación se muestra la implementación:
 

Python3

# import pygame module in this program
import pygame
 
# activate the pygame library
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
 
# define the RGB value for white,
#  green, blue colour .
white = (255, 255, 255)
green = (0, 255, 0)
blue = (0, 0, 128)
 
# assigning values to X and Y variable
X = 400
Y = 400
 
# create the display surface object
# of specific dimension..e(X, Y).
display_surface = pygame.display.set_mode((X, Y))
 
# set the pygame window name
pygame.display.set_caption('Show Text')
 
# create a font object.
# 1st parameter is the font file
# which is present in pygame.
# 2nd parameter is size of the font
font = pygame.font.Font('freesansbold.ttf', 32)
 
# create a text surface object,
# on which text is drawn on it.
text = font.render('GeeksForGeeks', True, green, blue)
 
# create a rectangular object for the
# text surface object
textRect = text.get_rect()
 
# set the center of the rectangular object.
textRect.center = (X // 2, Y // 2)
 
# infinite loop
while True:
 
    # completely fill the surface object
    # with white color
    display_surface.fill(white)
 
    # copying the text surface object
    # to the display surface object
    # at the center coordinate.
    display_surface.blit(text, textRect)
 
    # iterate over the list of Event objects
    # that was returned by pygame.event.get() method.
    for event in pygame.event.get():
 
        # if event object type is QUIT
        # then quitting the pygame
        # and program both.
        if event.type == pygame.QUIT:
 
            # deactivates the pygame library
            pygame.quit()
 
            # quit the program.
            quit()
 
        # Draws the surface object to the screen.
        pygame.display.update()

Producción : 
 

Output - 1

Ahora Veremos una de las aplicaciones de Mostrar los textos pero de una manera diferente que es desplazando el texto de 6 maneras diferentes en la ventana de pygame.

1. Desplazamiento del texto en la parte superior de la pantalla.

2. Desplazamiento del texto en la parte inferior de la pantalla.

3. Desplazamiento del texto en el lado izquierdo de la pantalla

4. Desplazamiento del texto en el lado derecho de la pantalla

5. Desplazar el texto en diagonal de izquierda a derecha de la pantalla

6. Desplazar el texto en diagonal desde el lado derecho al lado izquierdo de la pantalla.

Después de ver el siguiente código, puede implementar su propio patrón 

A continuación se muestra la implementación

Python

# import pygame module in this program
import pygame
 
# activate the pygame library
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
 
 
# create the display surface object
# (x, y) is the height and width of pygame window
win=pygame.display.set_mode((500, 500))
 
# set the pygame window name
pygame.display.set_caption("Scrolling Text")
 
# setting the pygame font style(1st parameter)
# and size of font(2nd parameter)
Font=pygame.font.SysFont('timesnewroman',  30)
 
# define the RGB value for white,
# green, yellow, orange colour
white=(255, 255, 255)
yellow=(255, 255, 0)
green=(0, 255, 255)
orange=(255, 100, 0)
done=False
 
# Split the text into letters
# 3rd parameter is font colour and
# 4th parameter is Font background
letter1=Font.render("H", False, orange, yellow)
letter2=Font.render("E", False, orange, green)
letter3=Font.render("M", False, orange, yellow)
letter4=Font.render("A", False, orange, green)
letter5=Font.render("N", False, orange, yellow)
letter6=Font.render("T", False, orange, green)
letter7=Font.render("H", False, orange, yellow)
 
# assigning values to
# i and c variable
i=0
c=1
 
# infinite loop
while not done:
    if(i>=820):
        i=0
        c+=1
        pygame.time.wait(500)
         
    # completely fill the surface object
    # with white color
    win.fill(white)
    if(c%6==0):   
        # Scrolling the text in diagonal
        # on right side of the Screen.
        # copying the text surface object
        # to the display surface object 
        # at the center coordinate.
        win.blit(letter1, (662-i, -162+i))
        win.blit(letter2, (639-i, -139+i))
        win.blit(letter3, (608-i, -108+i))
        win.blit(letter4, (579-i, -79+i))
        win.blit(letter5, (552-i, -52+i))
        win.blit(letter6, (529-i, -29+i))
        win.blit(letter7, (500 -i, 0 + i))
        i+=80
    if(c%6==5):
        # Scrolling the text in diagonal on
        # left side of the Screen.
        win.blit(letter1, (-162+i, -162+i))
        win.blit(letter2, (-135+i, -135+i))
        win.blit(letter3, (-110+i, -110+i))
        win.blit(letter4, (-79+i, -79+i))
        win.blit(letter5, (-52+i, -52+i))
        win.blit(letter6, (-27+i, -27+i))
        win.blit(letter7, (0+i, 0+i))
         
        # Decides the speed of
        # the text on screen
        i+=80   
    if(c%6==4):
       
        # Scrolling the text in
        # right side of the Screen.
        win.blit(letter1, (480, -180+i))
        win.blit(letter2, (480, -150+i))
        win.blit(letter3, (480, -120+i))
        win.blit(letter4, (480, -90+i))
        win.blit(letter5, (480, -60+i))
        win.blit(letter6, (480, -30+i))
        win.blit(letter7, (480, 0+i))
         
        # Decides the speed of
        # the text on screen
        i +=80 
    if(c%6==3): 
        # Scrolling the text in left
        # side of the Screen.
        win.blit(letter1, (0, -180+i))
        win.blit(letter2, (0, -150+i))
        win.blit(letter3, (0, -120+i))
        win.blit(letter4, (0, -90+i))
        win.blit(letter5, (0, -60+i))
        win.blit(letter6, (0, -30+i))
        win.blit(letter7, (0, 0+i))
         
        # Decides the speed of
        # the text on screen
        i+=80  
    if(c%6==1):
 
        win.blit(letter1, (-124+i, 0))
        win.blit(letter2, (-102+i, 0))
        win.blit(letter3, (-82+i, 0))
        win.blit(letter4, (-58+i, 0))
        win.blit(letter5, (-40+i, 0))
        win.blit(letter6, (-19+i, 0))
        win.blit(letter7, (0+i, 0))
         
        # Decides the speed of
        # the text on screen
        i +=80   
    if(c%6==2):
       
       # Scrolling the text in bottom of the Screen.
        win.blit(letter1, (-124+i, 470))
        win.blit(letter2, (-102+i, 470))
        win.blit(letter3, (-82+i, 470))
        win.blit(letter4, (-58+i, 470))
        win.blit(letter5, (-40+i, 470))
        win.blit(letter6, (-19+i, 470))
        win.blit(letter7, (0+i, 470))
         
        # Decides the speed
        # of the text on screen
        i+=80
     
    # Draws the surface object to the screen.
    pygame.display.update()
     
    # iterate over the list of Event objects
    # that was returned by pygame.event.get() method
    for event in pygame.event.get():
        if(event.type==pygame.QUIT):
            done=True
    #Delay with 5ms
    pygame.time.wait(500)
pygame.quit()

Producción:

1. Cuando el texto se desplaza en la parte superior de la pantalla

2. Cuando el texto se desplaza en la parte inferior de la pantalla

3. Cuando el texto se desplaza en el lado izquierdo de la pantalla

4. Cuando el texto se desplaza en el lado derecho de la pantalla

5. Cuando el texto se desplaza en diagonal desde el lado izquierdo de la pantalla

6. Cuando el texto se desplaza en diagonal desde el lado derecho de la pantalla

Publicación traducida automáticamente

Artículo escrito por ankthon y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *