Jump to content
  • Advertisement
Sign in to follow this  
wkiffer

Tetris Clone Collision Problem

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

My first attempt at a 2d game is a Tetris Clone (rather cliche isn't it?). Anyways, I'm not very far however I just implemented the Falling Shape function and I'm having a little problem with it. First off, however, you should probably know how I'm going about making the tetris game, I divided the playing grid into a multidimension array of an object I made representing every individual square. Each square has a bool variable telling if it is empty or not as well as a pointer to the image of the block it contains if it is occupied. The grid starts at the bottom left so [0][0] is the bottom left hand corner and [19][9] is the top right. To display the grid I just run through every block and if it is occupied I display the block. Now for the falling shape function I take an argument saying which shape it is, I then another multidimension array called coordinates, depending on which shape is passed to the function I assign where the starting coordinates on the grid would be so if is the straight shape I assign coordinates as {{19,5},{18,5},{17,5},{16,5}}. To make the shape fall each y value by 1, then check to see if there is a collision. I do this by sending the array to the checkcollision function, it loops through the values of the array and sees if the corresponding blocks in the grid are already occupied, if it is it returns true, the coordinates are reincrimented and the loop stops. However when I run it the blocks just stay in their starting positions indicating that a collision was detected the first time through the loop which shouldn't happened. I've already checked to make sure that my program doesn't automatically initialize the occupied variable of every block to true by default. I really can't see anythign wrong with the collision function and I was hoping you guys could help.
void ShapeFall(int shape)
{
              //Assign the appropriate color block for the shape in order to                           
              //display it correctly
              SDL_Surface *blockcolor=blocks[shape].block;
              
             
              
              
              
              
              int coordinates[4][2];
              switch(shape)
              {
             //
            //Apply starting coordinates to shape, left out because its so long
            //
}


bool collision=false;



Timer timer;


while (!collision)
{
                 if(SDL_PollEvent(&event))
                 {if(event.type==SDL_QUIT)
                 quit=true;
                 break;
                 }
timer.start();

//Subtract one from the current y coordinate to simulate it falling
for(int i=0;i<4;i++)
{
        coordinates[0]--;
}

//Check to see if the shapes new position collides with something
//if it does return the coordinates to their original position and stop the loop
if(checkcollision(coordinates))
{              for(int i=0;i<4;i++)
                  {

                          coordinates[0]++;
                  }
                  collision=true;
}

//If it doesn't change the blocks it just occupied to empty and then
//set the new blocks to occupied and store its image to be displayed
else {
     for(int h=0;h<4;h++)
                  {
                          coordinates[h][0]++;
                          myGrid.grid[(coordinates[h][0])][(coordinates[h][1])].occupied=false;
                          coordinates[h][0]--;
                  }
for(int i=0;i<4;i++)
{
        
       
        myGrid.grid[(coordinates[0])][(coordinates[1])].occupied=true;
        myGrid.grid[(coordinates[0])][(coordinates[1])].block=blockcolor;
         
        
}
}
//Draw the screen
DrawScreen();
//Wait a second before doing it again
while(timer.get_ticks()<1000)
{}


}

}




//This is where I'm having a problem
//It returns true no matter what
bool checkcollision(int coordinates[4][2])
{

for(int i=0;i<4;i++)
{
//If the grid position the coordinates point to is already occupied
//or it is below the bottom of the grid return true
       if(myGrid.grid[(coordinates[0])][(coordinates[1])].occupied||coordinates[0]<0)
        return true;
}

return false;
}


I know this was a long explanation, and I thank anyone who read it all the way through, hopefully you guys can help, Will

Share this post


Link to post
Share on other sites
Advertisement
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!