Public Group

#### Archived

This topic is now archived and is closed to further replies.

# Trying to make Tetris

This topic is 5275 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m taking the advice from another post and I''ve decided to build a Tetris game to help learn game programming. I''m not new to programming, but am new to C++ and armed ( and dangerous ) with my Sam''s books. As I am thinking it through and building a sort of task list, I''ve come up with these: 1. I need a way to draw the blocks ( I want to use DX full screen ) 2. Put them together to make basic shapes 3. Make them fall 4. Rotate shapes left or right 5. Keep track of when a row is completed 6. Remove completed rows 7. Keep score 8. Maintain levels 9. Maitain a good supply of Sam Adams While I''ve got the last item covered, any tips for outlining how to go about any of items 1-8 would be greatly appreciated. Thanks.

##### Share on other sites
Item #1 will probably be the largest. I suppose if you''ve used DirectX from VB or something, you''ll have a significantly easier time, though. If not, however, be prepared to browse through lotsa example code, online tutorials, and possibly get a DirectX book.

You could try doing it in a console window, using MSDN''s Console Reference for aid. If you keep your drawing and your game logic separated enough, then you could easily add in DirectX afterwards, and thus turn this into a two-step project: make Tetris in C++, and learn DirectX in C++.

Other than that, all your steps are fairly broken down and simple. When you decide on the general data structure you''ll use for all the blocks, then most of the other items will likely be quite obvious.

Have fun!

##### Share on other sites
(Rotation Tip)

With a 4*4 grid you can rotate 90 degrees by
For Y=0 to 3   For X=0 to 3      NewGrid[x][y] = OldGrid[4-Y][X]

Using two grids overlaid is probably the easiest way to do Tetris.

Jay

##### Share on other sites
This answer may serve no benefit, but it''s working very well for me right now.

I am actually in the process of doing this very thing right now: coding Tetris. It took me a couple of weeks to figure out a way of drawing the blocks that didn''t involve a massive series of nested switch statements. I have a 3-dimensional array that''s arraged like this...

GridCoordPair is a struct

typedef struct _GridCoordPair { int x; int y; } GridCoordPair;

Where CurrentForm is which tetriad (square, bar, etc) it is, which of the three possible orientations it is, and "n" ranges from 0 to 2: these are three "adjustments" (+0, +1, -2) from whatever piece I use to trace the tetriad as it falls thru space.

In essence:

X 0
X *
XX 12

Here we have an L piece; I track the piece by the * cell, and the drawing routine is something like

1. Draw the building block at *.
2. Loop from 0 to 2 and draw building blocks at locations off of * that you find by adding the CoordPairs stored in the TetriadModelList.

To set the list up I have a brute-force method that sets them all thus...

So if the "origin" cell of the tetriad is drawn at (GridX, GridY), then the second cell would be drawn at (GridX + TML[][][].x, GridY + TML[][][])

This may make *no* sense (I''m hastily typing this on my lunch break), but I thought it was a *much* better idea than the dozens of nested switch statements it''s replaced. Now each method is only a few lines long and just uses member_variables that keep track of what the tetriad in question is, which of the four possible orientations it''s at, and where it is on the grid. Of course you''ll need routines that "translate" from Grid-relative coordinates (the tetris-grid is like 10 wide by 24 tall) and actual pixel-relative coordinates.

Making them fall is pretty easy; just watch the system timer and each time a certain amount of time has elapsed (depending on what Level of Difficulty the player has achieved) move it down the grid one. When the player tries to drop it (they hold DOWN), let the piece fall at some MINIMUM_TIME_DELAY, and not just free of all delays. The first way is like Nintendo Gameboy. The second way is the annoying Windows 3.11 tetris way.

That''s enough to start, I guess. I''m considering writing up my efforts and seeing about "making a contribution" to this site, but I *think* there are already a few hundred "How to make Tetris" tutorials out there, so I probably won''t. I''ll help with your questions if I can though. Just let me know.

Cheers and good luck,
Steve

##### Share on other sites
quote:
Original post by Jason Zelos
(Rotation Tip)

With a 4*4 grid you can rotate 90 degrees by

Jay

So the first grid would be the main screen and the second would be falling block. Making a shape would be just a matter of filling in selected areas of the grid. If this is true, how would I go about keeping track of the hight of blocks that have fallen. Would that be saved in a lookup table or something?

##### Share on other sites
1. I need a way to draw the blocks ( I want to use DX full screen )
I'd use the word tile there. And block for the combination of 4 tiles.

2. Put them together to make basic shapes
You make arrays that hold bool or interger variables.
The field is an array too ... just bigger. And the blocks need offset variables that store where the block is on the map.

3. Make them fall
You change the y offset variable. If you mean dropping them ...
I used a function Move( int iDirecton )
that changed the offset. Before the block actually moves it check if there is a collision (compare block with sub array of map)
When the block is dropped a bool is set to true.
When there is a collision before moving down it is set to false again.
Also a new block appears. And the block tiles are added to the map.

4. Rotate shapes left or right
You loop through all the rows and columns of the block.
You rearrange content (I think you'll need a buffer block)

5. Keep track of when a row is completed
You loop through all the rows and columns of the map before a new block appears.
If all the columns of a row hold a tile:

6. Remove completed rows
overwrite the row ...
move the rows above that row down.

7. Keep score
You can use global variables.

8. Maintain levels
A bit much maybe? You mean like saving the game or only across gamestates?
Or importing map arrays from a file? Or how fast the blocks are depending on the score?

I guess I don't know what you mean when you say levels ...

[edited by - Clueless on June 1, 2004 3:09:26 PM]

[edited by - Clueless on June 1, 2004 3:13:09 PM]

##### Share on other sites
quote:
Original post by Clueless

8. Maintain levels
A bit much maybe? You mean like saving the game or only across gamestates?
Or importing map arrays from a file? Or how fast the blocks are depending on the score?

I guess I don''t know what you mean when you say levels ...

[edited by - Clueless on June 1, 2004 3:09:26 PM]

Basically just make the tiles fall faster as the user hits higher score plateaus. Using what you''ve mentioned, perhaps I could do this by increasing the y offset?

##### Share on other sites
I wouldn't use the offset. You wouldn't have enough control.
I'd pass the time of the last frame and add it to a waitTime variable.

waitTime += DeltaTime;if( waitTime >= minTime ){Move( 0 ); // if 0 is downwaitTime = 0;}

min time would depend on the level. Something like that.
There might be errors ... hopefully you get what I'm trying to say ...

One error fixed

The offset should be in tiles ... or do you want per pixel movement vertically?

[edited by - Clueless on June 1, 2004 3:20:45 PM]

##### Share on other sites
quote:
Original post by vinb
quote:
Original post by Jason Zelos
(Rotation Tip)

With a 4*4 grid you can rotate 90 degrees by

Jay

So the first grid would be the main screen and the second would be falling block. Making a shape would be just a matter of filling in selected areas of the grid. If this is true, how would I go about keeping track of the hight of blocks that have fallen. Would that be saved in a lookup table or something?

quote:
Original post by clueless
The field is an array too ... just bigger. And the blocks need offset variables that store where the block is on the map.

Exactly as quoted, just AND the two arrays (with game item offset) and where there is a match you have a collision. Then reverse the last drop and copy from the game item array to the main field array. (then check for lines etc, you can use integer values in your array for different colors)

Jay

##### Share on other sites
Instead of this "offset" idea, what I''m doing is checking for completed lines whenever a block touches down, using the vertical dimensions of the block as bounds for the check. So if you drop a "L" piece the long way - it''s three cells tall - wherever it stops determines where you run the check. If it completes one or more lines, it''s going to happen where it landed =) So you don''t have to explicitly monitor the height of heighest piece of the grid for clearing lines. Only for two things, really:

1. You need to know when it overflows (when you can''t fit any more pieces on screen and the player loses).
2. If you''re getting really fancy, the music accelerates when a certain height has been reached.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633298
• Total Posts
3011256
• ### Who's Online (See full list)

There are no registered users currently online

×