Random Shape Generator Function

  • Share this:

Code introduction


This function draws a random shape, such as a circle, square, triangle, or rectangle, at a specified position on the screen.


Technology Stack : Arcade

Code Type : Graphics drawing

Code Difficulty : Intermediate


                
                    
import arcade
import random

def random_shape_draw(x, y):
    """
    Draw a random shape on the screen at the given position.
    """
    # Randomly select a shape type
    shape_type = random.choice(["circle", "square", "triangle", "rectangle"])

    # Set the color for the shape
    color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

    # Draw the shape
    if shape_type == "circle":
        arcade.draw_circle_filled(x, y, 50, color)
    elif shape_type == "square":
        arcade.draw_rectangle_filled(x, y, 50, 50, color)
    elif shape_type == "triangle":
        arcade.draw_polygon_filled([x-25, y-25, x+25, y-25, x, y+25], color)
    elif shape_type == "rectangle":
        arcade.draw_rectangle_filled(x, y, 75, 50, color)

# Arcade setup
arcade.open_window(800, 600, "Random Shape Draw")
arcade.set_background_color((0, 0, 0))
arcade.start_render()

# Draw a random shape at position 400, 300
random_shape_draw(400, 300)

arcade.finish_draw()
arcade.run()                
              
Tags: