Random Shape Generator Using Arcade Library

  • Share this:

Code introduction


This function uses the Arcade library to create a window and randomly generates circles, rectangles, or polygons within it.


Technology Stack : Arcade library, random number generation

Code Type : Graphics drawing

Code Difficulty : Intermediate


                
                    
def arcade_random_shape_generator(width, height):
    import arcade
    from random import randint
    
    # Create a window
    arcade.open_window(width, height, "Random Shape Generator")
    
    # Set the background color
    arcade.set_background_color(arcade.color.WHITE)
    
    # Start the render loop
    arcade.start_render()
    
    # Generate a random shape
    shape_type = randint(1, 3)
    if shape_type == 1:
        arcade.draw_circle_filled(randint(50, width-50), randint(50, height-50), randint(20, 50))
    elif shape_type == 2:
        arcade.draw_rectangle_filled(randint(50, width-50), randint(50, height-50), randint(20, 50), randint(20, 50))
    else:
        points = [(randint(50, width-50), randint(50, height-50)) for _ in range(randint(3, 6))]
        arcade.draw_polygon_filled(points, arcade.color.RED)
    
    # Finish the render loop
    arcade.finish_render()
    
    # Keep the window open until the user closes it
    arcade.run()