PDA

View Full Version : Randomizing Colors



Chidori
12-05-2009, 11:24 PM
Hi, I'm making a simple 2D game in it I have three rows of of fifteen circles that need to be randomly generated as blue, green or red. I was wondering if you all would be able to explain how to randomize the three colors, or maybe point me in the direction of a good tutorial?

Thank you,
Chi

ZbuffeR
12-06-2009, 05:27 AM
Not sure I see the connection to opengl.
Do something like :



#include "stdlib.h"

...
// for each circle :

// modulo division
int randomValue = rand() % 3;
if (randomValue == 0) {
glColor3ub(0,0,255);
} else if (randomValue == 1) {
glColor3ub(0,255,0);
} else {
glColor3ub(255,0,0);
}

Chidori
12-08-2009, 09:49 PM
Thanks! I ended up with this:



int random_generator(int min,int max)
{

randomValue = min + random()%(max - min);
return randomValue;

}


then


glPushMatrix();
glTranslatef(250+aim,102,1);
random_generator(0,3);
if (randomValue == 0)
{
glColor3f(0,.5,0);


}
else if (randomValue == 1)
{
glColor3f(1,0,0);


}
else
{
glColor3f(0,0,.5);

}

glLineWidth(1.0);
drawCircle(14,200);
glPopMatrix();


though since I'm calling it in my draw function it's being called repeatedly, thus flashing all three colors o.o

so I'm working on a for loop to draw one row of fifteen circles, so I can then use a pointer in my main function which only gets called once. I'm getting errors though so I'll make a new thread I suppose since this topic's been closed. If this is against policy do let me know.

And I'm using the opengl library, to draw the shapes so I thought this was were my question belonged. If you still don't see the connection in my new thread should I try a C programming forum? Since this is a combination of OpenGL and C?

Oz1571
12-08-2009, 10:19 PM
Yeah, these issues really seem like they belong in a general programming forum. You probably want to store the colour variable in the circle class and have it randomized upon circle creation, and then, in the drawing code, have your program use each specific circle's colour as you render them.