Give Changing Different colors to a shape

1.3k views Asked by At

Were working on a family feud game and i wanted to apply changing colors to a group of circles randomly

i tried using for loop in this given code, but i know its wrong. how do i randomize?

    //looping set1

for(x=0;x<=15;x++)
{
    setcolor(x);
    sleep(3000);
}
setfillstyle(1,1);
fillpoly(13,lyt1);
fillpoly(9,lyt2);
fillpoly(9,lyt3);
fillpoly(12,lyt4);

//looping set2

for(x=0;x<=15;x++);
{
    setcolor(x);
    sleep(3000);
}
setfillstyle(1,1);
fillpoly(11,lyt5);
fillpoly(12,lyt6);
fillpoly(13,lyt7);
fillpoly(12,lyt8);
1

There are 1 answers

1
Spektre On BEST ANSWER

I am assuming you are in MS-DOS (not sure if emulated or real one or just windows console) but the animation and randomization is done a bit differently.

Due to various restrictions (so it works on each platform and do not use any advanced stuff) the program structure of your main loop should look more like this:

// main loop
const int dt=40; // [ms] approximate loop iteration time
int col_t=0,col_T=3000; // [ms] time and period for changing the colors
int col;
randomize();
col=random(16);
for (;;)
 {
 // 1. handle keyboard,mouse,joystick... here
 //    do not forget to break; if exit button is hit like: if (Key==27) break;

 // 2. update (world objects positions, score, game logic,etc)
 col_t+=dt;
 if (col_t>=col_T)
  {
  col_t=0; 
  col=random(16);
  }

 // 3. draw you scene here
 setcolor(col);

 // 4. CPU usage and fps limiter
 sleep(dt); // 40ms -> 25fps
 }

This structure does not need any interrupts so it is easy to understand for rookies. But games need usually more speed and event handlers are faster. For that you would need to use interrupts ISR for stuff like keyboard,PIT,...

Using sleep() is not precise so if you want precise measurement of time you should either use PIT or RDTSC but that could create incompatibilities in emulated environments...

Haven't code in MS-DOS for ages so I am not sure in which lib the random and randomize routines are they might also be called Random,Randomize my bet is they are in stdio.h or conio.h. Simply type random into program place cursor at it and hit ALT+F1 to bring up context help. There you will read which lib to include. Also I am not sure if to use random(15) or random(16) so read which is correct there too.

If you are coding a game then you will probably need some menu's. Either incorporate them into main loop or have separate main loop for each game page and use goto or encode each as separate function.

Have a look at few related QA's of mine:

And setcolor doc