PYGLET – Altura de sprite

En este artículo veremos cómo podemos acceder a la altura del sprite en el módulo PYGLET en python. Pyglet es una biblioteca fácil de usar pero poderosa para desarrollar aplicaciones GUI visualmente ricas como juegos, multimedia, etc. Una ventana es un objeto «pesado» que ocupa los recursos del sistema operativo. Las ventanas pueden aparecer como regiones flotantes o pueden configurarse para llenar una pantalla completa (pantalla completa). Un sprite es una instancia de una imagen que se muestra en la pantalla. Múltiples sprites pueden mostrar la misma imagen en diferentes posiciones en la pantalla. Los sprites también se pueden escalar más grandes o más pequeños, rotar en cualquier ángulo y dibujar en una opacidad fraccionaria. La imagen se carga con la ayuda del módulo de imagen de pyglet. La altura del sprite es la altura escalada del sprite. Es de solo lectura y es invariable bajo rotación.
Podemos crear una ventana y un objeto sprite con la ayuda de los comandos que se indican a continuación. 
 

# creating a window
window = pyglet.window.Window(width, height, title)

# creating a sprite object
sprite = pyglet.sprite.Sprite(img, x, y) 

Para crear una ventana, usamos el atributo de altura con el objeto sprite
. Sintaxis: sprite.height
Argumento: no requiere argumento
Retorno: devuelve un número entero  

A continuación se muestra la implementación. 

Python3

# importing pyglet module
import pyglet
import pyglet.window.key as key
   
# width of window
width = 500
   
# height of window
height = 500
   
# caption i.e title of the window
title = "Geeksforgeeks"
   
# creating a window
window = pyglet.window.Window(width, height, title)
   
# text 
text = "Welcome to GeeksforGeeks"
  
# creating label with following properties
# font = cooper
# position = 250, 150
# anchor position = center
label = pyglet.text.Label(text,
                          font_name ='Cooper',
                          font_size = 16,
                          x = 250, 
                          y = 150,
                          anchor_x ='center', 
                          anchor_y ='center')
 
 
# creating a batch
batch = pyglet.graphics.Batch()
 
# loading geeksforgeeks image
image = pyglet.image.load('gfg.png')
 
 
 
# creating sprite object
# it is instance of an image displayed on-screen
sprite = pyglet.sprite.Sprite(image, x = 200, y = 230)
   
# on draw event
@window.event
def on_draw():
       
    # clear the window
    window.clear()
       
    # draw the label
    label.draw()
     
    # draw the image on screen
    sprite.draw()
       
# key press event    
@window.event
def on_key_press(symbol, modifier):
   
    # key "C" get press
    if symbol == key.C:
         
        # printing the message
        print("Key : C is pressed")
         
# image for icon
img = image = pyglet.resource.image("gfg.png")
 
# setting image as icon
window.set_icon(img)
 
# accessing height of the sprite
value = sprite.height
 
# creating text from sprite
text = "Height : " + str(value)
 
# setting text to the label
label.text = text
    
# start running the application
pyglet.app.run()

Producción : 
 

Publicación traducida automáticamente

Artículo escrito por rakshitarora 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 *