Public Group

# 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.

## 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

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632948
• Total Posts
3009401
• ### Who's Online (See full list)

There are no registered users currently online

×