creating combinations of obstacles in a 2D side scroller game

1k views Asked by At

My first game in ALLEGRO 5 with c++.

It has a player that keeps moving continuously in right direction. From the right edge of the screen player faces obstacles like triangles and squares. These obstacles come alive at right edge of screen and die at left edge of screen.

Suppose X is triangle and O is square.

i want to create them in few combinations like

..{x} {xo} {xoox} {oxxo} {oxx}... And some variations(random maybe)

And after that I will randomize the occurence of these pattern.

SO I found a method to implement what i wanted. I used a switch() to randomly select various cases of combination. It works pretty well. But once in a while it overlaps even after I introduced a minimum gap xoff

Here is the code:

`//I have used srand(time(NULL)); once in main function too.


   if(state == PLAYING)
        {

            ball->Moveball();
                ball->Jumpball();

                //Camera-----------
                ball->Cameraupdate();
                al_identity_transform(&camera);
                al_translate_transform(&camera,-Cameraposition[0],-Cameraposition[1]);
                al_use_transform(&camera);



                if(rand() % 200==0)
                {

                     xoff+=200;// to introduce a minimum gap between them but this also fail once in a while.
                    int ch;
                    ch=rand()%4;
                        switch(ch)
                        {

                        case 0:
                        //T

                        triangle = new Triangle(850 + Cameraposition[0]+xoff,319);
                        objects.push_back(triangle);
                        triangle->SetAlive(true);

                        break;

                        case 1:
                        //TT

                        triangle = new Triangle(850 + Cameraposition[0]+xoff,319);
                        objects.push_back(triangle);
                        triangle->SetAlive(true);
                        triangle = new Triangle(850 +30+ Cameraposition[0]+xoff,319);
                        objects.push_back(triangle);
                        triangle->SetAlive(true);
                        break;

                        case 2:
                        //S

                        square = new Square(850 + Cameraposition[0]+xoff,310);
                        objects.push_back(square);
                        square->SetAlive(true);

                        break;

                        case 3:
                        //SS

                        square = new Square(850 + Cameraposition[0]+xoff,310);
                        objects.push_back(square);
                        square->SetAlive(true);
                        square = new Square(850 +82+ Cameraposition[0]+xoff,310);
                        objects.push_back(square);
                        square->SetAlive(true);

                        break;

                        }

                }
`

Problem solved :(For those who experienced similar doubt)The above code solves the problem of creating random combination of obstacles and generating those combinations randomly. But do check for collisions if you don't want to get overlapping combinations.

1

There are 1 answers

6
Keeler On

Just add another for loop:

void createObstacles(int numObstacles)
{
    for(int i = 0; i < numObstacles; i++)
    {
        if(rand() % 2 == 0)
        {
            // Create a triangle.
        }
        else
        {
            // Create a square.
        }
    }
}

Call this every time you want to create obstacles, controlling the number of objects released at one time by changing the parameter numObstacles.

You can now do:

if(rand() % 200 == 0)
{
    // Create anywhere from 1 to maxToCreate (which is 4) obstacles at a time.
    const int maxToCreate = 4;
    int numToCreate = (rand() % maxToCreate) + 1;

    // Call the function to make some obstacles.
    createObstacles(numToCreate);
}

Since your shapes are overlapping, either fix your initialization code to initialize them the way