Advertisement

Advice on Tetris--- Newbie here

Started by April 04, 2001 10:50 AM
3 comments, last by amreshr 23 years, 10 months ago
Hi, Just started OpenGL two weeks back and would like to get the fundamentals down, so I have decided to develop a game. I am right now trying to develop a version of the popular game tetris, but I am not sure if my approach is correct and there is no one nearby who has got stuff! I am using OpenGL and VC++. I don''t even know if I am on the right track. I am also not sure of the type of shapes in Tetris, been abt years since I played it. My approach is like this: I have used matrices to represent the shapes I have a 20x10 matrix which is mapped to the screen. All the shapes are denoted using a 4x4 matrix. e.g: The matrix below represents an inverted T. 0 0 0 0 0 0 1 0 0 1 1 1 The problem that I am facing is along the boundaries. If you look at the above matrix, the 1''s are donot have a symmetry and at the edges, when I rotate the matrix, the shape goes beyond the edge. The first thing that came to my mind was to shift the coordinates, but the problem here is that the each one in the matrix denotes a square on the screen and in the loops, where I map the ones to the screen, I can check only the left most one for the left edge and I can shift. The same problem exists on the right hand side and I reverse the maping of the matrix. The other complex part (for me) is when the shapes reach the bottom, the large matrix is not filled evenly and I have to scan the matrix each time a shape reaches the end and have to keep track of another matrix which shows the level of filling in each column. I still haven''t taken care of this. The only thing that is bugging me is that this seems too complicated for a game like this. There must be some other way to do this. Please help. It would be really nice if you could mail me at ramresh@dsqsoft.com coz I do not have a net connection nearby! Tia Amresh R Amresh ramresh@dsqsoft.com Amresh ramresh@dsqsoft.com
dArkteMplaR of Delphi
This forum is for writing stories for games. You want general programming. I''l move this thread there in a day or two (to give you time to see this message).

I want to help design a "sandpark" MMO. Optional interactive story with quests and deeply characterized NPCs, plus sandbox elements like player-craftable housing and lots of other crafting. If you are starting a design of this type, please PM me. I also love pet-breeding games.

Advertisement
I am sorry that the message is posted here. I didn''t know.
Please move it.
Thanks


Amresh
ramresh@dsqsoft.com
dArkteMplaR of Delphi
You need not use a 4x4 matrix for this, since you will always have only 4 pieces filled© ¥If you do want to use a matrix, use a 5x5 and center the piece¤© I use 2 arrays of 4 ints to hold the x and y coordinates of the 4 squares of the tetris pieces:
int x[4], y[4];
I use x[0], y[0] to store the center square of the piece ¥this is the one that the other squares rotate about¤© For any moving or rotating function I do the following steps:
1¤ Call a save function to store the old coordinates of the piece©
2¤ Perform the transformation ¥ie© add 1 to all y''s to move the piece down, or add 1 to all x''s to move right¤©
3¤ Check if the move is valid ¥are all squares under my new position clear?¤©
4¤ If the move is not valid, call restore to put the piece back at its old position©
To rotate, you first calculate the relative position of the square by subtracting x[0] and y[0]: int dx = x-x[0]; int dy = y-y[0];<br> Then add dx to y[0] and subtract dy from x[0]: y = y[0]+dx; x = x[0]+dy© <br> Or reverse the sign of the add/subtract to go the other way© This is based on the formula x=y*sin¥a¤+x*cos¥a¤; y=y*cos¥a¤-x*sin¥a¤; which simplifies since you are using 90 degree angles©<br> Anyways, I know it sounds complicated, but look at the code, and you will see its really simple©<br> </i> <!–STARTSCRIPT–><center><table border=1 cellspacing=0 cellpadding=8 bgcolor="#FFFFFF" width="90%"><tr><td><font color=black face="Courier New"><pre> <br>TetrisPiece::TetrisPiece¥TetrisBoard *pboard¤<br>{<br> board = pboard;<br>}<br><br>TetrisPiece::~TetrisPiece¥¤<br>{<br><br>}<br><br><font color="blue">void</font> TetrisPiece::save¥¤<br>{<br> <font color="blue">for</font> ¥U<font color="blue">INT</font> i=0;i&lt;4;i++¤<br> {<br> oldx[<font color="purple">i</font>]=x[<font color="purple">i</font>]; <br> oldy[<font color="purple">i</font>]=y[<font color="purple">i</font>];<br> }<br> return;<br>}<br><br><font color="blue">void</font> TetrisPiece::restore¥¤<br>{<br> <font color="blue">for</font> ¥U<font color="blue">INT</font> i=0;i&lt;4;i++¤<br> {<br> x[<font color="purple">i</font>]=oldx[<font color="purple">i</font>]; <br> y[<font color="purple">i</font>]=oldy[<font color="purple">i</font>];<br> }<br> return;<br>}<br><br><font color="blue">BOOL</font> TetrisPiece::moveright¥¤<br>{<br> save¥¤;<br> <font color="blue">for</font> ¥U<font color="blue">INT</font> i; i&lt;4;i++¤<br> x[<font color="purple">i</font>]++;<br> <font color="blue">if</font> ¥!validate¥¤¤<br> {<br> restore¥¤;<br> <font color="blue">return</font> FALSE;<br> }<br> <font color="blue">return</font> TRUE;<br>}<br><br><font color="blue">BOOL</font> TetrisPiece::moveleft¥¤<br>{<br> save¥¤;<br> <font color="blue">for</font> ¥U<font color="blue">INT</font> i; i&lt;4;i++¤<br> x[<font color="purple">i</font>]–;<br> <font color="blue">if</font> ¥!validate¥¤¤<br> {<br> restore¥¤;<br> <font color="blue">return</font> FALSE;<br> }<br> <font color="blue">return</font> TRUE;<br>}<br><br><font color="blue">BOOL</font> TetrisPiece::movedown¥¤<br>{<br> save¥¤;<br> <font color="blue">for</font> ¥U<font color="blue">INT</font> i; i&lt;4;i++¤<br> y[<font color="purple">i</font>]++;<br> <font color="blue">if</font> ¥!validate¥¤¤<br> {<br> restore¥¤;<br> lock–;<br> <font color="blue">if</font> ¥lock == 0¤ <br> {<br> freeze¥¤;<br> <font color="blue">return</font> FALSE; <font color="gray">//only return FALSE if the lock count is expended<br></font><br> }<br> }<br> <font color="blue">return</font> TRUE;<br>}<br><br><font color="blue">BOOL</font> TetrisPiece::validate¥¤<br>{<br> <font color="blue">for</font> ¥U<font color="blue">INT</font> i=0;i&lt;4;i++¤ <font color="gray">//Check for squares outside of the board<br></font><br> <font color="blue">if</font> ¥¥x[<font color="purple">i</font>]&lt;0¤ || ¥x[<font color="purple">i</font>]&gt;=BOARDWIDTH¤ || ¥y[<font color="purple">i</font>]&gt;=BOARDHIEGHT¤¤<br> <font color="blue">return</font> FALSE;<br> <font color="blue">for</font> ¥i;i&lt;4;i++¤ <font color="gray">//Check that all the squares are clear<br></font><br> <font color="blue">if</font> ¥board-&gt;get¥x[<font color="purple">i</font>], y[<font color="purple">i</font>]¤ != ClearSquare¤<br> <font color="blue">return</font> FALSE;<br> <font color="blue">return</font> TRUE;<br>}<br><br><font color="blue">BOOL</font> TetrisPiece::rotateleft¥¤<br>{<br> save¥¤;<br> <font color="blue">for</font> ¥U<font color="blue">INT</font> i=1;i&lt;4;i++¤<br> {<br> x[<font color="purple">i</font>]=x[<font color="purple">0</font>]-¥oldy[<font color="purple">i</font>]-y[<font color="purple">0</font>]¤;<br> y[<font color="purple">i</font>]=y[<font color="purple">0</font>]+¥oldx[<font color="purple">i</font>]-x[<font color="purple">0</font>]¤;<br> }<br> <font color="blue">if</font> ¥!validate¥¤¤<br> {<br> restore¥¤;<br> <font color="blue">return</font> FALSE;<br> }<br> <font color="blue">return</font> TRUE;<br>}<br><br><font color="blue">BOOL</font> TetrisPiece::rotateright¥¤<br>{<br> save¥¤;<br> <font color="blue">for</font> ¥U<font color="blue">INT</font> i=1;i&lt;4;i++¤<br> {<br> x[<font color="purple">i</font>]=x[<font color="purple">0</font>]+¥oldy[<font color="purple">i</font>]-y[<font color="purple">0</font>]¤;<br> y[<font color="purple">i</font>]=y[<font color="purple">0</font>]-¥oldx[<font color="purple">i</font>]-x[<font color="purple">0</font>]¤;<br> }<br> <font color="blue">if</font> ¥!validate¥¤¤<br> {<br> restore¥¤;<br> <font color="blue">return</font> FALSE;<br> }<br> <font color="blue">return</font> TRUE;<br>}<br><br><font color="blue">void</font> TetrisPiece::spawn¥<font color="blue">int</font> piece¤<br>{<br> lock = 3;<br> <font color="blue">for</font> ¥U<font color="blue">INT</font> i=0; i&lt;4; i++¤<br> {<br> x[<font color="purple">i</font>]=4;<br> y[<font color="purple">i</font>]=2;<br> }<br> <font color="blue">switch</font> ¥piece¤<br> {<br> <font color="blue">case</font> 0: <font color="gray">//Line<br></font><br> y[<font color="purple">1</font>]-=2;<br> y[<font color="purple">2</font>]–;<br> y[<font color="purple">3</font>]++;<br> break;<br> <font color="blue">case</font> 1: <font color="gray">//Box<br></font><br> x[<font color="purple">1</font>]++;<br> y[<font color="purple">2</font>]++;<br> x[<font color="purple">3</font>]++; y[<font color="purple">3</font>]++;<br> break;<br> <font color="blue">case</font> 2: <font color="gray">//T<br></font><br> y[<font color="purple">1</font>]–;<br> x[<font color="purple">2</font>]++;<br> y[<font color="purple">3</font>]++;<br> break;<br> <font color="blue">case</font> 3: <font color="gray">//L<br></font><br> y[<font color="purple">1</font>]–;<br> y[<font color="purple">2</font>]++;<br> y[<font color="purple">3</font>]++;x[<font color="purple">3</font>]++;<br> break;<br> <font color="blue">case</font> 4: <font color="gray">//Mirror L<br></font><br> y[<font color="purple">1</font>]–;<br> y[<font color="purple">2</font>]++;<br> y[<font color="purple">3</font>]++; x[<font color="purple">3</font>]–;<br> break;<br> <font color="blue">case</font> 5: <font color="gray">//S<br></font><br> y[<font color="purple">1</font>]–;<br> x[<font color="purple">2</font>]++;<br> y[<font color="purple">3</font>]++;x[<font color="purple">3</font>]++;<br> break;<br> <font color="blue">case</font> 6: <font color="gray">//Z<br></font><br> y[<font color="purple">1</font>]–;x[<font color="purple">1</font>]++;<br> x[<font color="purple">2</font>]++;<br> y[<font color="purple">3</font>]++;<br> break;<br> }<br> <br>}<br><br><font color="blue">void</font> TetrisPiece::freeze¥¤<br>{<br> <font color="blue">for</font> ¥U<font color="blue">INT</font> i=0; i&lt;4; i++¤<br> <font color="blue">if</font> ¥y[<font color="purple">i</font>] &gt;= 0¤<br> board-&gt;set¥x[<font color="purple">i</font>],y[<font color="purple">i</font>], SolidSquare¤;<br>}<br> </pre></font></td></tr></table></center><!–ENDSCRIPT–>
Tanx a lot invective.
I will try it out! Thanks for the sample piece of code too.
dArkteMplaR of Delphi

This topic is closed to new replies.

Advertisement