Advertisement

About the Tetris game?

Started by October 30, 2001 01:33 AM
0 comments, last by neXt 23 years ago
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?
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; <br>		oldy=y;<br>	}<br>	return;<br>}<br><br>void TetrisPiece::restore()<br>{<br>	for (UINT i=0;i<4;i++)<br>	{<br>		x=oldx; <br>		y=oldy;<br>	}<br>	return;<br>}<br><br>BOOL TetrisPiece::moveright()<br>{<br>	save();<br>	for (UINT i; i<4;i++)<br>		x++;<br>	if (!validate())<br>	{<br>		restore();<br>		return FALSE;<br>	}<br>	return TRUE;<br>}<br><br>BOOL TetrisPiece::moveleft()<br>{<br>	save();<br>	for (UINT i; i<4;i++)<br>		x–;<br>	if (!validate())<br>	{<br>		restore();<br>		return FALSE;<br>	}<br>	return TRUE;<br>}<br><br>BOOL TetrisPiece::movedown()<br>{<br>	save();<br>	for (UINT i; i<4;i++)<br>		y++;<br>	if (!validate())<br>	{<br>		restore();<br>		lock–;<br>		if (lock == 0) <br>		{<br>			freeze();<br>			return FALSE;  //only return FALSE if the lock count is expended<br>		}<br>	}<br>	return TRUE;<br>}<br><br>BOOL TetrisPiece::validate()<br>{<br>	for	(UINT i=0;i<4;i++)  //Check for squares outside of the board<br>		if ((x<0) || (x>=BOARDWIDTH) || (y>=BOARDHIEGHT))<br>			return FALSE;<br>	for	(i;i<4;i++)  //Check that all the squares are clear<br>		if (board->get(x, y) != ClearSquare)<br>			return FALSE;<br>	return TRUE;<br>}<br><br>BOOL TetrisPiece::rotateleft()<br>{<br>	save();<br>	for (UINT i=1;i<4;i++)<br>	{<br>		x=x[0]-(oldy-y[0]);<br>		y=y[0]+(oldx-x[0]);<br>	}<br>	if (!validate())<br>	{<br>		restore();<br>		return FALSE;<br>	}<br>	return TRUE;<br>}<br><br>BOOL TetrisPiece::rotateright()<br>{<br>	save();<br>	for (UINT i=1;i<4;i++)<br>	{<br>		x=x[0]+(oldy-y[0]);<br>		y=y[0]-(oldx-x[0]);<br>	}<br>	if (!validate())<br>	{<br>		restore();<br>		return FALSE;<br>	}<br>	return TRUE;<br>}<br><br>void TetrisPiece::spawn(int piece)<br>{<br>	lock = 3;<br>	for (UINT i=0; i<4; i++)<br>	{<br>		x=4;<br>		y=2;<br>	}<br>	switch (piece)<br>	{<br>	case 0:  //Line<br>		y[1]-=2;<br>		y[2]–;<br>		y[3]++;<br>		break;<br>	case 1:  //Box<br>		x[1]++;<br>		y[2]++;<br>		x[3]++; y[3]++;<br>		break;<br>	case 2:  //T<br>		y[1]–;<br>		x[2]++;<br>		y[3]++;<br>		break;<br>	case 3:  //L<br>		y[1]–;<br>		y[2]++;<br>		y[3]++;x[3]++;<br>		break;<br>	case 4:  //Mirror L<br>		y[1]–;<br>		y[2]++;<br>		y[3]++; x[3]–;<br>		break;<br>	case 5:  //S<br>		y[1]–;<br>		x[2]++;<br>		y[3]++;x[3]++;<br>		break;<br>	case 6:  //Z<br>		y[1]–;x[1]++;<br>		x[2]++;<br>		y[3]++;<br>		break;<br>	}<br>	<br>}<br><br>void TetrisPiece::freeze()<br>{<br>	for (UINT i=0; i<4; i++)<br>		if (y >= 0)<br>			board->set(x,y, SolidSquare);<br>}<br>/<pre><br>  </i>   </pre> 

This topic is closed to new replies.

Advertisement