Jump to content
  • Advertisement
Sign in to follow this  
jakpandora

How should I do this?

This topic is 5035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!