Public Group

#### Archived

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

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

## Recommended Posts

I was reading through one of the articles here and it said you should really start makiing a tetris game. Now I don''t have any programming knowlege and I wont really be able to concentrate on learning it until I finish high school in a year. So I just wanted to get the basic idea. I wanted to know if there are any actual guided just to making the tetris game so I can just try it out and get the hang of the basics?

##### Share on other sites
The way I implemented it is as follows, you have 2 objects, a piece (4 squares), and the board (10x20 squares). The piece has basic movement functions. To move the piece, you save the current position, then move to and new coordinates. If the new move is invalid (off the board or into a solid square) then you restore the old position. The board only needs to check if it is full and remove any solid lines. The game itself is relatively simple and straight forward (you can easily write it on a TI calculator), but a good learning lesson. You can start with basic tetris, move on to tetris with special items, then multiplayer, 3d graphics, ect. It makes a good programming lesson for many different things.

Here is some of my old tetris code. Hope it helps.

// TetrisBoard.cpp: implementation of the TetrisBoard class.
//
//////////////////////////////////////////////////////////////////////

#include "TetrisBoard.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

TetrisBoard::TetrisBoard()
{

}

TetrisBoard::~TetrisBoard()
{

}

void TetrisBoard::set(int x, int y, int type)
{
square[x][y] = type;
}

INT TetrisBoard::get(int x, int y)
{
return square[x][y];
}

BOOL TetrisBoard::full()
{
BOOL isfull = false;
for (UINT x = 0; x < BOARDWIDTH; x++)
for (UINT y = 0; y < BOARDHIEGHT; y++)
if (square[x][y]!=ClearSquare) return true;
return false;
}

BOOL TetrisBoard::solidline(int y)
{
for (UINT x=0; x if (square[x][y]==ClearSquare) return false;
return true;
}

void TetrisBoard::collapseline(int line)
{
for (UINT y=line; y>0;y--)
for (UINT x=0;x square[x][y]=square[x][y-1];
for (UINT x=0;x square[x][0]=ClearSquare;
}

BOOL TetrisBoard::update()
{
for (UINT y=0; y if (solidline(y)) collapseline (y);
return full ();
}

// TetrisPiece.cpp: implementation of the TetrisPiece class.
//
//////////////////////////////////////////////////////////////////////

#include "TetrisPiece.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

TetrisPiece::TetrisPiece(TetrisBoard *pboard)
{
board = pboard;
}

TetrisPiece::~TetrisPiece()
{

}

void TetrisPiece::save()
{
for (UINT i=0;i<4;i++)
{
oldx=x[i];
oldy[i]=y[i];
}
return;
}

void TetrisPiece::restore()
{
for (UINT i=0;i<4;i++)
{
x[i]=oldx[i];
y[i]=oldy[i];
}
return;
}

BOOL TetrisPiece::moveright()
{
save();
for (UINT i; i<4;i++)
x[i]++;
if (!validate())
{
restore();
return FALSE;
}
return TRUE;
}

BOOL TetrisPiece::moveleft()
{
save();
for (UINT i; i<4;i++)
x[i]--;
if (!validate())
{
restore();
return FALSE;
}
return TRUE;
}

BOOL TetrisPiece::movedown()
{
save();
for (UINT i; i<4;i++)
y[i]++;
if (!validate())
{
restore();
lock--;
if (lock == 0)
{
freeze();
return FALSE; //only return FALSE if the lock count is expended
}
}
return TRUE;
}

BOOL TetrisPiece::validate()
{
for (UINT i=0;i<4;i++) //Check for squares outside of the board
if ((x[i]<0) || (x[i]>=BOARDWIDTH) || (y[i]>=BOARDHIEGHT))
return FALSE;
for (i;i<4;i++) //Check that all the squares are clear
if (board->get(x[i], y[i]) != ClearSquare)
return FALSE;
return TRUE;
}

BOOL TetrisPiece::rotateleft()
{
save();
for (UINT i=1;i<4;i++)
{
x[i]=x[0]-(oldy[i]-y[0]);
y[i]=y[0]+(oldx[i]-x[0]);
}
if (!validate())
{
restore();
return FALSE;
}
return TRUE;
}

BOOL TetrisPiece::rotateright()
{
save();
for (UINT i=1;i<4;i++)
{
x[i]=x[0]+(oldy[i]-y[0]);
y[i]=y[0]-(oldx[i]-x[0]);
}
if (!validate())
{
restore();
return FALSE;
}
return TRUE;
}

void TetrisPiece::spawn(int piece)
{
lock = 3;
for (UINT i=0; i<4; i++)
{
x[i]=4;
y[i]=2;
}
switch (piece)
{
case 0: //Line
y[1]-=2;
y[2]--;
y[3]++;
break;
case 1: //Box
x[1]++;
y[2]++;
x[3]++; y[3]++;
break;
case 2: //T
y[1]--;
x[2]++;
y[3]++;
break;
case 3: //L
y[1]--;
y[2]++;
y[3]++;x[3]++;
break;
case 4: //Mirror L
y[1]--;
y[2]++;
y[3]++; x[3]--;
break;
case 5: //S
y[1]--;
x[2]++;
y[3]++;x[3]++;
break;
case 6: //Z
y[1]--;x[1]++;
x[2]++;
y[3]++;
break;
}

}

void TetrisPiece::freeze()
{
for (UINT i=0; i<4; i++)
if (y[i] >= 0)
board->set(x[i],y[i], SolidSquare);
}
/[CODE]

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
631775
• Total Posts
3002278
×