How do I Create a Random Grid of Sprites?

From the Asset Store
Snap to visible grid - perfect solution for any game genre
  • I'm trying to get my head round the best approach (that doesn't involve creating a grid of spawners etc).

    I'd like to create a grid, for instance a checkerboard... made up of random different color sprites (squares for now for ease).

    So Yellow, black, red, blue, green, orange sprites etc. etc.

    However I need to make sure that the array always includes at least 3 of a specific color sprite such as red.

    I would randomise the grid of sprites each time.

    Any advice to automate this please? I'm thinking it's an array, but can't get my head around it currently.

    Thanks for any help

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Incidentally... I should add that if it makes it easier... I could just have a grid made up of 3 x red, 3 x green, 3 x yellow etc. that randomise the order.

    So i've been looking at shuffling arrays, and converting the array into a grid of sprites??

  • The easy way is create a pattern with 3 each of them.

    Then you add randomly in the grid and remove the index of pattern that you just added.

  • here is a sample of how to do something along the lines of what Maverick was suggesting...

    https://www.rieperts.com/games/forum/ColorSquares.c3p

    it doesn't limit how many times other colors can be added, but you are guaranteed 3 of the color chosen in the dropdown list.

    if you don't mind more than 3 of the chosen color then just delete the while block in the BuildBoard function

    essentially, it builds an array of colors to use for the squares. it doesn't need to sort or do anything to the array because it just randomly chooses an index for the next square and then deletes that index so it can't be picked again.

  • Wow thanks so much, that looks exactly what i'm looking to do.

    I'm just working my way through blocks to make sure I understand how it's working... it makes perfect sense, though I could never have come up with this myself.

    I think this will be the perfect foundation for what I need.

    Thank you again.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)