• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Random color assignment - never twice the same

M

MisterBeeBoy

Guest
Hi everyone.
So I wanted to make a simple game for my 4 yo daughter, basically an ice cream cone and colorful balls of ice to put on it.
As I'm a real beginner in GML I gave me a little challenge : giving a random color to each of 6 balls of ice, but never twice the same color.
I tried image_blend = choose(c_yellow, c_red,...) but it gives me twice the same color

Then I tried ds_queue with the following code (with six instances of ice ball on the room) :
Object ball -create event- :
with (ds_couleur)

//{if (!ds_queue_empty(couleur)) {
image_blend = ds_queue_dequeue(couleur);
//}}

Object ds_couleur -create event-:
couleur = ds_queue_create();
ds_queue_enqueue(couleur, c_lime);
ds_queue_enqueue(couleur, c_aqua);
ds_queue_enqueue(couleur, c_maroon);
ds_queue_enqueue(couleur, c_red);
ds_queue_enqueue(couleur, c_white);

Object ds_couleur -destroy event-:
ds_queue_destroy(couleur);

...And it doesn't work at all. Don't know where I'm messing things.
Can anyone advise me or suggest a better method?

Thanks to you all
 

RujiK

Member
Code:
//in room_start code or anywhere that is before ball creation.
global.list = ds_list_create();


ds_list_add(global.list,c_red);
ds_list_add(global.list,c_blue);
ds_list_add(global.list,c_yellow);
ds_list_add(global.list,c_green);
ds_list_add(global.list,c_aqua);
ds_list_add(global.list,c_black);

randomize(); //make it different every time you run the game.
ds_list_shuffle(global.list); //randomize the list.
Code:
//in ball creation code:
color = ds_list_find_value(global.list,0);
ds_list_delete(global.list,0); //prevent the same color from being used again.
Untested but it should work.
 
M

MisterBeeBoy

Guest
Thanks! It works fine.
I just put image_blend instead of color and it's perfect.
Thanks again!
Code:
//in room_start code or anywhere that is before ball creation.

global.list = ds_list_create();
ds_list_add(global.list,c_red);
ds_list_add(global.list,c_blue);
ds_list_add(global.list,c_yellow);
ds_list_add(global.list,c_green);
ds_list_add(global.list,c_aqua);
ds_list_add(global.list,c_black);

randomize(); //make it different every time you run the game.
ds_list_shuffle(global.list); //randomize the list.
Code:
//in ball creation code:
image_blend = ds_list_find_value(global.list,0);
ds_list_delete(global.list,0); //prevent the same color from being used again.
 
Last edited by a moderator:
Top