![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Hobbyist Programmer
|
I have never coded a Tetris game although I think I can help with your question about stopping the blocks from moving. As you know, in Tetris, each piece is made up of a number of smaller blocks. Here are some ASCII examples....
|_| |_| |_||_| |_| |_|_|_| |_|_| |_|_| Anyways. I think you should divide up your entire playing board into X, Y coordinates. Each block (section of a piece) will take up a position. 4 3 |_| 2 |_| |_| 1 |_|_| |_|_|_| 0 1 2 3 4 5 6 As a peice falls you will subtract (pos--) all of the blocks that make up the piece from the Y axis in order to move them down. Before a piece moves down however, you check to see if ANY of the spaces are already filled with a piece. If so, then you stop it from moving... function CanMove()
{
// subtract 1 from the coordinates of each block
// as part of the piece. Then check to see if all
// of the spaces are free for the piece to move down.
if( isSpaceFree(--piece1.Block1_X) &&
isSpaceFree(--piece1.Block1_Y) &&
isSpaceFree(--piece1.Block2_X) &&
................)
return true;
}
if( peice1.CanMove() == true)
{
piece1.MoveDown();
} else {
piece1.StopMoving();
}I hope I have explained this well enough so that you can understand. If not, feel free to ask questions about the possible solution that I have presented. As I have said... I have never programmed a Tetris game before although if I were too, this would probebly be the route that I would take. |
|
|
|
|
|
#12 |
|
Newbie
Join Date: Oct 2005
Posts: 7
Rep Power: 0
![]() |
Thats a good Idea and I was kind of thinking about doing that, but I never could really figure out how to check if a point on my 15x32 grid was open. Could you help me with this?
|
|
|
|
|
|
#13 |
|
Hobbyist Programmer
|
Should be simple enough. First, when you start a new game, you will assume that the grid is empty. Now before I continue, let me make sure you understand that when I refer to "piece" I am talking about the entire Tetris shape that is moving. "Block" is the smaller squares that make up a piece.
You will create your piece at the top center of the grid. Depending on which piece is currently in play, the exact coordinates for the blocks will vary. But that is the key.... to record the coordinates of the blocks and to keep track of them. This is how you know if a space is open or not. Lets assume we have an 'L' piece. Made up of 4 blocks total. When we first create this, it will have 4 sets of coordinates. (5,5), (5,6), (5,7), (6,7) As the piece falls, you will subtract 1 from the Y values. As the player moves the piece left or right with the arrow keys, you will add/subtract 1 to/from the X values. Before a piece can move however, you need to check and see if a block is already occupying one of those coordinates. If so, then you must stop it from moving. Now the question becomes... Where are these X, Y values stored? I personally would use an array of objects. Each type of piece (L piece, Z piece, ect...) will be an object. When a new piece is created, you will create a new instance of this object. Since you will have a lot of pieces on the grid, it would be wise to use an array. (I dont know Java, so my syntax might be off) public class L_Piece
{
int Block1_X; int Block1_Y;
int Block2_X; int Block2_Y;
int Block3_X; int Block3_Y;
.....
}
public class Z_Piece
{
.....
}
L_Piece LP[10] = new L_Piece();
Z_Piece ZP[10] = new Z_Piece();So lets review... 1.) Start with an empty grid 2.) Create a new piece at the top center. It will be an element of an array depending on the type of piece it is 3.) Store its coordinates inside the object of the piece 4.) Add/Subtract from the X/Y axis depending on movement 5.) Check if to make sure ALL coordinates are open by looping through the existing set from the other pieces |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|