6 Replies - 5303 Views - Last Post: 24 January 2012 - 03:26 PM Rate Topic: -----

#1 mastrgamr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 30-December 10

how to go about making a tetris clone?

Posted 22 January 2012 - 09:53 PM

I'm somewhat new to game programming and trying to learn how to make a tetris clone. Problem is, I don't have a plan of attack. I'm not sure how to start creating it.
I started a project already and created a grid for the blocks to fall into. Only thing is I don't know how to get pieces to fall into that grid or get collision detection for each block.

So far I found a way t draw the blocks made of 4 segments. What I do is draw each segment and for collision detection I plan to create a rectangle for each segment. I don't think that will be efficient when the grid is almost full. Also I don't know how I would get rid of specific rows when a line is complete and move the rows above down one segment.

It's all just confusing to me, I looked at tons of source code for the game, and apparently there's a different method for making the game. And i don't understand most of what the code is trying to do. Is anyone willing to help? :helpsmilie:

P.S. I'm programming with the Java swing API

This post has been edited by mastrgamr: 22 January 2012 - 09:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: how to go about making a tetris clone?

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,288
  • Joined: 12-June 08

Re: how to go about making a tetris clone?

Posted 22 January 2012 - 09:57 PM

What language are you using? Any specific api or sdk? The joy of tetris is there's not a whole lot of moving parts on the board.. sure there's the blocks but if you throw a scan after a block 'lands' to see if a row is formed that shouldn't present a problem.
Was This Post Helpful? 0
  • +
  • -

#3 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 411
  • Joined: 08-January 12

Re: how to go about making a tetris clone?

Posted 22 January 2012 - 10:20 PM

Well thinking about the logic in how the game has to delete rows, you will have to create squares that make up the Tetris pieces and code the pieces together.You would really only need one square. From there you could create a class for the pieces to link squares together, or create placements.
I really do not know Java at all therefore all I can do is suggest a few things.
Use containers or a structure, like a multidimensional array for example. (rows x columns)
Figure out how many blocks each piece should be by standard, so you know how big to make the array.
You will need to test for lines after every collision. You would only be check rows here...
I think that covers most of the logic. I hope this gives you some ideas
Was This Post Helpful? 0
  • +
  • -

#4 mastrgamr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 30-December 10

Re: how to go about making a tetris clone?

Posted 22 January 2012 - 10:25 PM

That's what I'm trying to tackle now, so far I have a multidim array for the blocks in tetris:

private static final int[/*blockType*/][/*rotation*/][][] piece =
		{
		{ //blockType = square = 0
			{ //rotation
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0},
				{0, 0, 1, 1, 0},
				{0, 0, 1, 1, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0},
				{0, 0, 1, 1, 0},
				{0, 0, 1, 1, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0},
				{0, 0, 1, 1, 0},
				{0, 0, 1, 1, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0},
				{0, 0, 1, 1, 0},
				{0, 0, 1, 1, 0},
				{0, 0, 0, 0, 0}
			}
		},
		{ //Straight = 2
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0},
				{0, 1, 1, 1, 1},
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 1, 0, 0},
				{0, 0, 1, 0, 0},
				{0, 0, 1, 0, 0},
				{0, 0, 1, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0},
				{1, 1, 1, 1, 0},
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 1, 0, 0},
				{0, 0, 1, 0, 0},
				{0, 0, 1, 0, 0},
				{0, 0, 1, 0, 0},
				{0, 0, 0, 0, 0}
			}
		},
		{ //L = 3
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 1, 0, 0},
				{0, 0, 1, 0, 0},
				{0, 0, 1, 1, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0},
				{0, 0, 1, 1, 1},
				{0, 0, 1, 0, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0},
				{0, 1, 1, 0, 0},
				{0, 0, 1, 0, 0},
				{0, 0, 1, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 1, 0, 0},
				{1, 1, 1, 0, 0},
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0}
			}
		},
		{ //L-mirror = 4
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 1, 0, 0},
				{0, 0, 1, 0, 0},
				{0, 1, 1, 0, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 1, 0, 0, 0},
				{0, 1, 1, 1, 0},
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 1, 1, 0},
				{0, 0, 1, 0, 0},
				{0, 0, 1, 0, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0},
				{0, 1, 1, 1, 0},
				{0, 0, 0, 1, 0},
				{0, 0, 0, 0, 0}
			}
		},
		{ //T = 5
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0},
				{0, 0, 1, 0, 0},
				{0, 1, 1, 1, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 1, 0, 0, 0},
				{0, 1, 1, 0, 0},
				{0, 1, 0, 0, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 1, 1, 1, 0},
				{0, 0, 1, 0, 0},
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 0, 1, 0},
				{0, 0, 1, 1, 0},
				{0, 0, 0, 1, 0},
				{0, 0, 0, 0, 0}
			}
		},
		{ //S = 6
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0},
				{0, 0, 1, 1, 0},
				{0, 1, 1, 0, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 1, 0, 0, 0},
				{0, 1, 1, 0, 0},
				{0, 0, 1, 0, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0},
				{0, 0, 1, 1, 0},
				{0, 1, 1, 0, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 1, 0, 0, 0},
				{0, 1, 1, 0, 0},
				{0, 0, 1, 0, 0},
				{0, 0, 0, 0, 0}
			}
		},
		{ //Z = 7
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0},
				{0, 1, 1, 0, 0},
				{0, 0, 1, 1, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 0, 1, 0},
				{0, 0, 1, 1, 0},
				{0, 0, 1, 0, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 0, 0, 0},
				{0, 1, 1, 0, 0},
				{0, 0, 1, 1, 0},
				{0, 0, 0, 0, 0}
			},
			{ 
				{0, 0, 0, 0, 0},
				{0, 0, 0, 1, 0},
				{0, 0, 1, 1, 0},
				{0, 0, 1, 0, 0},
				{0, 0, 0, 0, 0}
			}
		}
	};


basically when i run the game and load a block i access this array and say for each "1" in the array, draw a square, that acts as 1 of the 4 segments of the tetris block. The same idea will go behind the collision detections, create a rectangle for each segment.

This post has been edited by mastrgamr: 22 January 2012 - 10:26 PM

Was This Post Helpful? 0
  • +
  • -

#5 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: how to go about making a tetris clone?

Posted 24 January 2012 - 02:11 PM

This really is not a good solution. What you should be doing is defining each shape efficiently, and algorithmically determine the shape rotations. For example, if we have an 'L' shape we know that its shape is preserved no matter where it is on the grid. Assuming the top-left hand corner of any shape to be the origin, we have

*   r0c0 (origin)
*   r1c0
**  r2c0, r2c1



These are the 'model coordinates' for the shape. Now comes the problem of interpreting the shape's position on the grid in order to be able to translate it around the world (grid). Now the grid also has its own coordinate system, of course, so it is a trivial issue to issue to move the shape around:

  • Increment row value when 'right' is pressed.
  • Decrement row when 'left' is pressed.
  • Increment column value over time to preserve shape falling.


Rotations are also trivial, but I will leave you to work out that one.

To check for collisions you only need to check the bottom blocks that form the shape - or more accurately, off to the bottom and side of the blocks that form the shape for collision.
Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 1011
  • View blog
  • Posts: 4,215
  • Joined: 14-February 08

Re: how to go about making a tetris clone?

Posted 24 January 2012 - 03:12 PM

Why the oversized arrays as well? Tetris blocks are at most 4x4 yet you are using 5x5, reason?
Was This Post Helpful? 0
  • +
  • -

#7 mastrgamr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 18
  • Joined: 30-December 10

Re: how to go about making a tetris clone?

Posted 24 January 2012 - 03:26 PM

View Poststayscrisp, on 24 January 2012 - 05:12 PM, said:

Why the oversized arrays as well? Tetris blocks are at most 4x4 yet you are using 5x5, reason?

Yes i took a look at it and noticed it myself, only reason I did 5x5 is because of the way i set the rotation for the straight tetris block. I was thinking of changing it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1