'How can I use gpio from raspberry pi 4 to control a pyglet script

I have a program that i would like to control with buttons and sensors from the gpio pins of raspberry pi 4. The idea is to make the script run when a sensor or a button is pressed.

here is the code...

import pyglet
from gpiozero import LED, Button
import random


led = LED(26)
button = Button(21)

window = pyglet.window.Window(1920,1080)

img = pyglet.image.load('bv.png')
sprite = pyglet.sprite.Sprite(img, x=window.width/3, y=window.height/3)


music = pyglet.resource.media('teste.mp3')
music.play()

@window.event

def on_key_press(button, modifiers):
    if button_is_pressed:
        print("button pressed")


@window.event
def on_draw():
    window.clear()
    sprite.draw()
    
def update(dt):
    sprite.x += random.randint(-10,10)
    sprite.y += random.randint(-10,10)




pyglet.clock.schedule_interval(update,1/3000)

pyglet.app.run()
print("teste")

   
led.blink()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source