Tetris

Started by
8 comments, last by AliasNotFound 21 years, 7 months ago
As suggested in one of the articles in the beginner''s section I have started writing a Tetris clone as my first attempt at creating a complete game, but I''m not sure about a certain design aspect. I''m trying to decide if it''s better to use bitmaped blocks or blocks that are generated in code. I have to be able to figure out when the blocks are touching, and when they make a complete line across the bottom so that the line can be removed, etc, but I''m not clear on how this is done. Do I have to use some kind of per-pixel collision detection or is it easier than this? I''m thinking maybe if I make the blocks a specific size, then i could use that in figuring out wher to place a block. A little push in the right direction would be appreciated. Thanks, Phillip
Advertisement
Hmmm. What I did, and I''m not sure if this is the right method, but it works, was make a 10x20 array of short integers, representing the field (the field is 20 blocks high and 10 blocks wide). The integers represented the colors of the block (so 1 is red, 2 is blue, etcetera. 0 meant that there was no block in that position.)

Then, everytime the block that was currently dropping couldn''t go any further, I checked all rows of the array to see if there were 0''s in it. If there weren''t any zero''s in the array, that meant there was a complete line.

Hope this helped a bit. Good luck!
quote:Original post by Bas Paap
What I did, and I''m not sure if this is the right method...


If it works then its right!

I did tetris in a very similar way a couple of months back.



pan narrans
Study + Hard Work + Loud Profanity = Good Code
Minister of Propaganda : leighstringer.com : Nobody likes the man who brings bad news - Sophocles (496 BC - 406 BC), Antigone
Do look at the "Hands on interactive game development" forum. There Teej covers making a tetris game as a tutorial. I haven''t read it myself, but it hopefully covers the aspects your wondering about. There''s also an Windows assembly totorial somewhere among the featured articles that is about a tetris clone. You probably don''t want to dive into assembly, but the design considerations could be interesting. I''m sure there is alot to find about making a tetris clone elsewhere too.

Some tips from me:
- Build all blocks of its four parts; not as a whole block.
- Make one array, like Bas Paap suggested. Only fill it with bricks that has fallen down and are stuck.
- Have the currently falling block separate. Store its position, type and rotation.
- Before moving the falling block one slot down, check each of its four parts against the array. If any position in the array is busy, the block can''t go down anymore, and you fill in the array with it.
- You can also use the same algorithm as above to determine if the block can move to the sides.
- Don''t try to implement "smooth falling" blocks. That is, they are going down pixel-by-pixel. Instead, let them bump a whole step/slot at a time. It can be extremely frustrating, and can ruin your project if you fail, so take the easy way.
im working on a tetris clone right now too..i can send you my beta code if you like..(its done in opengl and c++)

daniel_rong@hotmail.com

[edited by - branhield on September 23, 2002 12:55:23 PM]
I used the array method for my tetris clone as well. and as said before, I made the falling block a separate entity, as well. there''s prolly other ways, but this one seemed simplest, and it worked, so can''t go wrong there.
The separate falling block is definately the way to go, otherwise you''d have to empty out the array at certain points each time you move the block.

How to specify what a certain block looks like is harder. I used a bit pattern. It was a bit awkward, but it learned me to work with bitshifts and logical operators.
Hello,

A while back I started a tetris clone too. I used the same method of having two arrays, one for the currently moving block, and the other to store the actual blocks.

I ran into a problem with showing different shapes that rotate. I didn''t know how to rotate them. Anyone have any little tricks, or methods on how to rotate and store the different shapes?

Mike
Michael RhodesTiger Studios Web Designhttp://tigerstudios.net
Have a way to describe a block. You could have 4x4 array, or have 4 X/Y values that "plots" them relative to a 4x4 array (I used the latter).
Code says more than 1000 words:
typedef struct _POINT {    short        x, y;} POINT; typedef struct _BLOCK_ROT {    POINT        point[4];} BLOCK_ROT; typedef struct _BLOCK {    BLOCK_ROT    block[4];} BLOCK; //BLOCK block_type[7] = {    // Type 1    {        // Rotation CW: 0        { {0, 0}, {0, 0}, {0, 0}, {0, 0} },        // Rotation CW: 1 ...    },    // Type 2 ...};
I had a not-so-stylish brute force method: My current block was represented by an unsigned short integer. I imagined each block to fit in a 4x4 matrix. I filled in the block in the matrix, and placed all the rows next to each other. Then, when a square of the matrix is empty, I put down a 0, and if it contained part of the block, I put down a 1. This gives you one 16 bit binary number; an short integer.

To make it more clear, here's what a t-shaped block looks like:


  [ ][ ][ ][ ][x][x][x][ ][ ][x][ ][ ][ ][ ][ ][ ]Next to each other, that's:[ ][ ][ ][ ] [x][x][x][ ] [ ][x][ ][ ] [ ][ ][ ][ ] 0  0  0  0   1  1  1  0   0  1  0  0   0  0  0  0  


0000 1110 0100 0000 == 0x0E40, and voila: there's my block.

My method of rotation was very large and unclean, but fast at the same time. I just used a boolean value to determine if it rotated left (true) or right (false), and then made two gigantic switch statements:


    if (direction) //left{  switch (blockID)  {  case 0x0F00:    return 0x2222;  case 0x2222:    return // etcetera  }}else //right{  switch (blockID)  {  case 0x0F00:    return 0x4444;  case 0x4444:    return // etcetera  }}    


Big and unclean, but fast, and it works. Also, I learned a good deal about bit shifting and logical operators when determining if a block fitted someplace and actually placing it in the array.


[edited by - Bas Paap on September 25, 2002 4:00:57 AM]

This topic is closed to new replies.

Advertisement