How should I do this?

Started by
4 comments, last by jakpandora 19 years, 6 months ago
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

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);
    }

}

I am sure theres probably a simple way to do this, but I cant seem to see it.
______________________________My website: Quest Networks
Advertisement
Just call rectfill() with values you choose?
You just need to read in the data from a file or from a hard-coded array instead of using rand().

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?
______________________________My website: Quest Networks
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.
ok, thanks! I already have a level editor(mappy) but perhaps ill write my own just for fun.
______________________________My website: Quest Networks

This topic is closed to new replies.

Advertisement