void setupdebris()
{
int n,x,y,size,color;
//fill the battlefield with random debris
for (n = 0; n < BLOCKS; n++)
{
x = BLOCKSIZE + rand() % (SCREEN_W-BLOCKSIZE*2);
y = BLOCKSIZE + rand() % (SCREEN_H-BLOCKSIZE*2);
size = (10 + rand() % BLOCKSIZE)/2;
color = makecol(rand()%255, rand()%255, rand()%255);
rectfill(screen, x-size, y-size, x+size, y+size, color);
}
}
How should I do this?
In the following code, which I took from the book Game programming all in one, 2nd ed." blocks are randomly generated on terrain. I want to make it so they arent randomly generated, And I can just specify there exact location. heres the code
I am sure theres probably a simple way to do this, but I cant seem to see it.
You just need to read in the data from a file or from a hard-coded array instead of using rand().
with blocks.dat containing rows of
x y size color_r color_g color_b
void setupdebris(){ int n,x,y,size,r,g,b,color; FILE *file = fopen("blocks.dat"); //fill the battlefield with random debris while (6 == fscanf(file, "%d %d %d %d %d %d", &x, &y, &size, &r, &g, &b)) { if (x < BLOCKSIZE) x = BLOCKSIZE; if (x > SCREEN_W-BLOCKSIZE) x = SCREEN_W-BLOCKSIZE; if (y < BLOCKSIZE) y = BLOCKSIZE; if (y > SCREEN_H-BLOCKSIZE) y = SCREEN_H-BLOCKSIZE; if (size < 10) size = 10; if (size > BLOCKSIZE+10) size = BLOCKSIZE+10; size /= 2; color = makecol(rand()%255, rand()%255, rand()%255); rectfill(screen, x-size, y-size, x+size, y+size, color); } fclose(file);}
with blocks.dat containing rows of
x y size color_r color_g color_b
ok, thanks! so I essentialy just create a file with numbers numbers in a certain order to get the level I choose?
Yep.
The next bit, that makes it all worth while, is that you can now have multiple level files - your task now is to write a level editor! :) Of course, for a simple format like this you can probably get away with just editing files by hand.
The next bit, that makes it all worth while, is that you can now have multiple level files - your task now is to write a level editor! :) Of course, for a simple format like this you can probably get away with just editing files by hand.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement