Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Make a Tetris Clone?

I am working on coding a Tetris clone in XNA C# and am unsure of the best way to approach the data structure side of the game on a high level.

I am totally fine with the collision detection, rotations, animation etc. What I need to know the best way to do the storing of "dropped blocks" - ie blocks that are no longer under tha player's control.

I think that each Tetromino block should be stored in its own class that consists of a 4x4 array so that the block can easily be rotated. The problem is then how to I store the tetromino's final position into the game grid by then cutting the tetromino up into individual blocks(for each cell) and then set the main game grid's corresponding positions to hold these same blocks, then disappearing the tetromino once it has reached its final position. Maybe there is some drawback to my method.

Should I create a 10x20 matrix for the main game grid which can then store? or should I use stacks or queues to somehow store the dropped blocks. Or maybe there is some better method/data structure to store things?

I am sure my way would work, but I am reaching out to see if anybody knows a better way or if my way is good enough?

P.S. Not homework, this will be a project for my portfolio. Thanks.

like image 664
Brock Woolf Avatar asked Feb 19 '09 16:02

Brock Woolf


People also ask

Is it legal to make a Tetris game?

The game, name, and all associated works of Tetris are owned and trademarked by The Tetris Company. The concept of tetrominos, however, are not owned by anyone.

Is Tetris hard to code?

Tetris is a pretty easy game to implement, just a simple html file with some inline css/javascript. The only tricky part is dealing with rotation of the 7 tetrominoes.

What was Tetris coded in?

The original Tetris (Russian: Тетрис) was programmed by Alexey Pajitnov using the programming language Pascal on an Electronika 60 (Russian: Электроника 60) - an unauthorized Soviet clone of a Digital Equipment Corp. PDP-11 computer.

Are Tetris blocks Copyright?

A designer can make a falling block game with any shape of block, but the blocks that are used in Tetris are owned by Tetris. The judge also finds that a 10×20 field in a falling block game is protectable by copyright and owned by Tetris.


2 Answers

Once a block is immobile, there's nothing that distinguishes it from any other block that is now immobile. In that regard, I think it makes the most sense to store the entire grid as a matrix, where each square is either filled or not (along with the color of the block if it is).

I feel like the matrix has many advantages. It'll make collision detection simple (no having to compare with multiple objects, just locations on a matrix). Storing it as a matrix will also make it easier to determine when a full line has been created. On top of that, you don't have to worry about splicing an immobile Tetromino when a line disappears. And when one does, you can just shift the entire matrix down in one fell swoop.

like image 96
Dan Lew Avatar answered Oct 24 '22 17:10

Dan Lew


This smells like homework, but my take on an object-oriented approach to Tetris would be to have each individual square be an object, and both "blocks" (tetrominos) and the grid itself would be collections of the same square objects.

Block objects manage the rotation and position of the falling squares, and the grid handles displaying them and detroying completed rows. Each block would have a colour or texture associated with it that would be provided by the original block object it came from, but otherwise squares at the base of the grid would have no other indication that they were ever part of the same original block.

To elaborate, when you create a new block object, it creates a set of 4 squares with the same colour/texture on the grid. The grid manages their display. So when the block hits the bottom, you just forget about the block, and the squares remain referenced by the grid.

Rotations and dropping are operations only a block need deal with, and only one its four squares (though it will need to be able to query the grid to make sure the rotation can fit).

like image 34
Welbog Avatar answered Oct 24 '22 17:10

Welbog