Sign in to follow this  
jakpandora

How should I do this?

Recommended Posts

jakpandora    112
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.

Share this post


Link to post
Share on other sites
fractoid    703
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

Share this post


Link to post
Share on other sites
fractoid    703
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this