• Advertisement
Sign in to follow this  

random block placment switch

This topic is 4871 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

ok i have this pice of code here
            for (int l = 0; l <30; l++)
              { 
                blocks[l]->SetPosition(rand()%500, rand()%450);
                      for(int s=0; s < 30; s++)
                         {
                             if( blocks[l]->TestCollision(blocks[s]) == TRUE)
                             {
                              if (blocks[s] != blocks[l])
                               blocks[l]->SetPosition(rand()%500, rand()%450);
                             };    
                          };
             };        
          for (int y = 0; y <30; y++)
          {
              blocks[y]->SetHidden(false);
          }; 
             
                      
       };
       totalblocks=29; 
   };


in my GameStart function to load the blocks randomly but when i do this
 switch (totalblocks)
    {
        case  28: 
        {
        
            for (int l = 0; l <30; l++)
              { 
                blocks[l]->SetPosition(rand()%500, rand()%450);
                      for(int s=0; s < 30; s++)
                         {
                             if( blocks[l]->TestCollision(blocks[s]) == TRUE)
                             {
                              if (blocks[s] != blocks[l])
                               blocks[l]->SetPosition(rand()%500, rand()%450);
                             };    
                          };
             };        
          for (int y = 0; y <30; y++)
          {
              blocks[y]->SetHidden(false);
          }; 
             
                      
       };
       totalblocks=29; 
   };

in my GamePaint function it dosnt work sure most of them are seprate but some of them are on top of each other and that screws up my collision detection. i cant figure out what wrong here maybe you guys can thx in advance. [smile] windows API dev-cpp 4.9.9.0

Share this post


Link to post
Share on other sites
Advertisement
You test for a collison. If there is you try again. But you dont then test to see if your second attempt failed. It might have collided again. You need a while loop to retest for collison everytime you try to place the block.

Share this post


Link to post
Share on other sites
i tryed that i put this line

while ( blocks[l]->TestCollision(blocks[s]) == TRUE)
if (blocks[s] != blocks[l])
blocks[l]->SetPosition(rand()%500, rand()%450);


but when it got time to exicute the statement the whole thing just froze up

Share this post


Link to post
Share on other sites
           for (int l = 0; l <30; l++)
{
blocks[l]->SetPosition(rand()%500, rand()%450);
bool IsNotSet=true;
while (1)
{
bool Failed=false;
for(int s=0; s < 30; s++)
{
if( blocks[l]->TestCollision(blocks[s]) == TRUE)
{
if (blocks[s] != blocks[l])
blocks[l]->SetPosition(rand()%500, rand()%450);
Failed=true;
}
}
if (!Failed) break;
}
}
for (int y = 0; y <30; y++)
{
blocks[y]->SetHidden(false);
}


}
totalblocks=29;
}



Try that.

Share this post


Link to post
Share on other sites
Sorry try putting braces round

blocks[l]->SetPosition(rand()%500, rand()%450);
Failed=true;

(otherwise it will always fail, sorry!!)

Share this post


Link to post
Share on other sites
yey it works thanks alot you guys

[edit] guss i got ahead of my self well it works even better then it did before but im still getting 1 or 2 thinks overlapped i dont know why

do you think i can just ignore it. it isnt very common it must be an exception or somthing thats throwing it off

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement