Skip to main content
gamedev.net
🔒 Locked

Programming Tetris

Started by Asheh Oct 13, 2005 at 11:14 AM 10 replies 4.9k views
Original Post
Asheh
Asheh
Ok so got bored today, started writing a simple tetris game. Or so i thought, not sure if its me but i cant seem to figure out the logic behind it. Can anyone provide me with some advice on developing it?
Asheh
Asheh
Well....

I have a class, which describes a single block. A class per block type. This makes rotation easy but the problem is collision, since every block is different it means I have to search for the lowest blocks in each piece and check on each row for the first one to collide then freeze all the blocks (?)

or somthing?
Wavarian
Wavarian
When I made my tetris clone, I too took a very object-orientated approach.

Here's some ideas that you might consider to start you off:

* Okay, you have a "Block" class which represents a single game block. Now think about adding functionality to the class so that one Block can be "attached" to another. You can also give the class RotateLeft(), RotateRight() and some movement methods. When several Blocks have been connected, you can then consider them to be "Pieces". Only one piece can be in existance at a time.

* Taking the above into account, when receiving player input, a Piece needs to know about the game Board (so that proper collision detection can be performed).

* You'll need a Timer to apply gravity to the current Piece. Once the Piece has landed, you should break the Piece up back into Blocks, and move each Block into the game Board Block array. You can automate this by giving the Piece class a BreakInto(Board& board) method.

* Don't forget your GoF design patterns, they're very useful in this sort of project.

I could give a few more tips, but most of the fun comes from working it out for yourself - or maybe even coming up with a completely different solution.
CrazyLiberal
CrazyLiberal
I actually made a tetris game a while back, which was also my first game. I did not use any tetris tutorials or anything like that (though suprisingly I did it much the same way as most people suggest).

First, I differentiate between a 'shape' and a 'block.' A block is a single square block. A shape is a group of 4 blocks and can be either a line, square, L, backwards L, S, or a backwards S. I used an array of 6 DirectDraw surfaces for the blocks, one for each color. I made a "Grid" class that would be the main playing field and was 12 cells wide and 24 cells tall @ 20 pixels per cell. If a cell was 'occupied' by a block, then I would just set grid.color equal to the index of that DirectDraw surface used for that color block.

As for the collision detection, I will admit that was a little more difficult, but basically I would first determine which direction the shape was moving. My 'block' struct was actually just a struct of two integers, a row number and a column number, to determine which grid cell it currently occupied. So, I would create 4 temp. blocks and store the location of the shape after moving. If any of these cells were already occupied, then you cannot move there.


Hope this helps and good luck!
caseyd
caseyd
Okay, basically you have a Well. Which is really just a 2d array consisting of the block size your using. Like this

// Define our well
int Well[wellx][welly];

// Define our piece array
int Piece[WhichPiece][WhichRotation][x][y];

Then..You basically insert the piece into the well after testing if it can. You can test if a piece will fit easily by:
1) Perform collision before putting the piece into the well..If it's past the initial piece entry and each time the piece moves you have to remove the piece from the well before doing collison, otherwise the piece will collide with itself.

2) Loop through the piece using the position it is going to. If the well at the new position is empty for each of the blocks in the piece,than the piece has hit something.

3) Stop moving the piece, place it into the well and create a new piece.

It's not really that hard except that the collision detection can kind of get you.

dimovich
dimovich
I'm currently working on a Tetris clone (http://netrix.sf.net)... And here are a few points:

- Why have a block class ? I have an 4x4 array for each figure type that describes the shape of the figure in each rotation position, so the problem of dynamically computing the rotation of an abstract figure is eliminated.

Ex:
0,1,0,0, 0,0,1,0, 0,1,1,0, 1,1,1,0
0,1,0,0, 1,1,1,0, 0,0,1,0, 1,0,0,0
0,1,1,0, 0,0,0,0, 0,0,1,0, 0,0,0,0
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0


- The main algorithm in some kind of pseudo-code (you can look at the Netrix source code at: "/src/game/seq.c", in function - seqProcGame:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if a new figure is generated
check if there are any complete lines
if it is not posible to place the generated figure on the screen
game lost
return

generate all other "fancy" stuff :)

else
if not posible to place the figure on the screen
if figure moved on Y dimension (down)
figure landed
generate a new figure

update the figure position
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

you should manage the periodical Y axis figure movement, and also the input. For each input you should run the above algorithm.
We're doing this for a living, George. We are not amateurs.
Hodge
Hodge
Quote:
Original post by dimovich
I'm currently working on a Tetris clone (http://netrix.sf.net).
Looks like a nice open source tetris clone dimovich. You also might want to look at quadra http://quadra.sourceforge.net/ to get an idea how a tetris like puzzle game is built in C. There are also a few books that have you make a tetris like game(I made a buggy one). It will require some game programming experience but it isn't too difficult if you stay focused on it.
You fight like a cow
mikedoty
mikedoty
Quote:
- Why have a block class ? I have an 4x4 array for each figure type that describes the shape of the figure in each rotation position, so the problem of dynamically computing the rotation of an abstract figure is eliminated.

Ex:
0,1,0,0, 0,0,1,0, 0,1,1,0, 1,1,1,0
0,1,0,0, 1,1,1,0, 0,0,1,0, 1,0,0,0
0,1,1,0, 0,0,0,0, 0,0,1,0, 0,0,0,0
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0


I used an array system when I made my Tetris clone (http://www.geocities.com/tetratris/), and it works great. However, I might recommend using a 5x5 array instead of a 4x4 array. This make it possible to rotate pieces less awkwardly, since a 5x5 array can be evenly split (centered at array[2][2]). With a 4x4, you might get pieces that rotate with a very large radius. ;)
--Mike
dimovich
dimovich
One more thing... Probably the best web resource on Tetris is this:

http://www.colinfahey.com/2003jan_tetris/2003jan_tetris.htm
We're doing this for a living, George. We are not amateurs.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.