Problem in tetris game using sdl

Started by
3 comments, last by Toadhead 18 years, 8 months ago
I'm getting a problem in my tetris game (made using SDL). The code below need to remove full lines and let all blocks above that line fall one down. - BlockLocked is true when a falling shape just hit the ground or another block - Blocks checks the amount of blocks in every line, if its 10 the line is full - Fline is the amount of full lines (can be 1,2,3 and 4 since I only have 4 block shapes) - Above: all blocks above this number need to fall one down. Grid[0][0] is the top left tile. All tiles are 32*32 and the playfield is 10*20 (Grid[10][20]) tiles. The code for removing the lines is below. The executables can be found here: http://www.freewebs.com/toadds/Block Rain.zip (The code below is ofcourse in the gameloop of the code, just before the part that draws the blocks on the gamefield). Thanks in advance, Toadhead

//Only check for full lines if a block is locked
if(BlockLocked)
{

//Nessacary variables
int Blocks = 0;
int Fline = 0;
int Above = 0;
bool FullLines[20] = {0};
GridClass GridTemp[10][20] = {0};


//Checking for full lines
for(int yindex = 0; yindex < 20 ; yindex++)
{
   for(int xindex = 0; xindex < 10 ; xindex++)
   {
     if (Grid[xindex][yindex].block == true) Blocks++;
   }   
if(Blocks == 10) {FullLines[yindex] = true; Fline++;}          
}
//End of checking for full lines



//If there is atleast one ful line
if(Fline > 0)
{


//Copying Grid array to GridTemp (only all block above "Above" and with y: +Fline)
for(int yindex = 0 ; yindex < Above ; yindex++)
for(int xindex = 0 ; xindex < 10 ; xindex++)
  {
    GridTemp[xindex][yindex].block = Grid[xindex][yindex + Fline].block;
  }
//End of copying Grid  
  
       
        
//Checking which lines need to fall down
for(int yindex = 0 ; yindex < 20 - Fline ; yindex++)
if(FullLines[yindex] == true)
{
Above = yindex;
yindex = 20;
}
//End of checking, all lines above "Above" need to fall down Fline-times



//Removing Full lines in Temp class
for(int yindex = 0 ; yindex < 20 ; yindex++)
{
  if(FullLines[yindex] == true)
  {             
    for(int xindex = 0 ; xindex < 10 ; xindex++) 
      {
        GridTemp[xindex][yindex].block = false;
      }
  }        
}
//End of removing full lines in GridTemp



//Removing all blocks in Grid array above "Above"
for(int yindex = 0 ; yindex < Above ; yindex++)
for(int xindex = 0 ; xindex < 10 ; xindex++) 
      {
        GridTemp[xindex][yindex].block = false;
      }
//End of removing all blocks in Grid array



//Copying all GridTemp blocks to the real Grid array
for(int yindex = 0;  yindex < Above ; yindex++)
for(int xindex = 0;  xindex < 10 ; xindex++)
   {
      Grid[xindex][yindex].block = GridTemp[xindex][yindex].block;
   }
//End of updating Grid using GridTemp

}
//End of if there's atleast one full line


//Resetting everything
Above = 0;
Fline = 0;
Blocks = 0;
//End of resetting


}
//End of checking if the block is locked





Advertisement
This may or may not help, but here's the method I used in my tetris clone.

I had an array of 200 Block objects, and 20 SDL_Rects as rows.

Whenever a Piece object was created, It'd point to 4 Block objects from the array of 200 and set them to active.

When the piece stopped, I would go through the rows.

When I checked a row, I would add the stationary Block objects that collided with the row to an array that pointed the Blocks in the row. If the number of Blocks in pointed to by the array was 10, I would set the Block objects' status to shattered.

After I was done checking all the rows, I would check if there were any Block objects being shattered. If there were, I do a little flashing animation, then set the blocks to inactive.

After I was done going through the rows, I would carve out a new Piece object.

Learn to make games with my SDL 2 Tutorials

Yeah but than I would need to recode everything while my game is almost completed.

I just need to find the problem in this code or how I can make it work on another way without changing more than just this full-line system part of my code.

It looks to me like only the grid array is getting updated. Are you remembering to update the screen when the lines are found or do you just use the grid array to generate a new graphics screen?
Further in my code it will draw all blocks to the screen and update the screen with SDL_Flip(screen) etc.

There is something wrong in this code and I can't find the problem so I hoped someone else could find it. It's probably just some real stupid mistake but just can't find it :(

The first part were it get's all stats seems to work. So it's probably a mistake in the part were it copies Grid to GridTemp :|

Does anyone know the problem?


Greetings,
Rob

This topic is closed to new replies.

Advertisement