Pyglet-Based Rectangle Drawing Function

  • Share this:

Code introduction


This function uses the Pyglet library to draw a rectangle in a window. It accepts the rectangle's width, height, and color as parameters.


Technology Stack : Pyglet, Graphics drawing

Code Type : Graphics drawing

Code Difficulty : Intermediate


                
                    
def create_random_rectangle(width, height, color):
    """
    This function creates a rectangle with the given width, height, and color.
    """
    from pyglet.graphics import draw
    from pyglet.window import Window
    from pyglet.gl import *

    window = Window(width=800, height=600)

    @window.event
    def on_draw():
        glClear(GL_COLOR_BUFFER_BIT)
        draw(2, GL_LINE_LOOP, ('v2f', (0, 0, width, 0, width, height, 0, height)), color=color)

    window.run()

# Example usage
create_random_rectangle(100, 200, (1.0, 0.0, 0.0))