# OpenGL tetris/game-board

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

## Recommended Posts

sorry for this dumb question; i am looking to develop a tetris game; but wonder how do I display/draw the game board and its pieces; presently, i am working on a text version drawing the filled parts with 1 & empty spaces with 0. i have drawn primitives and other stuffs with OpenGL but wonder, how do i convert my present board status into OpenGL window; help me out.... ~ boyd

##### Share on other sites
I read somewhere once (probably these forums, do a search) that you could use a 2D array for the 'board'. For the pieces, use a smaller 2D array.

Basically, you've got your board (- = 0, # = 1)

Initially:

----------
----------
----------
----------
----------
----------
----------
----------

(10x8 2D array)

An 'L' piece might be represented by:

#--
#--
##-

(3x3 2D array)

Board after 'L' piece placed:

----------
----------
----------
----------
----------
#---------
#---------
##--------

For collision detection you just mask the piece over the next space down/sideways, if you get two #'s then it can't move.

I'm fairly sure tetris is suggested as a first project because it's harder than you'd think- remembering that you have to be able to rotate the pieces, too.

This quote says it best: "90% of game development is data structure manipulation".

##### Share on other sites
one other dumb question::

I completely agree with the way I gotto design my game-board as told above; assume I've a 2d array of (10 * 8) filled with 0's & 1's, how do i render them as - or #; how do i decide upon the positions for each value in the array; Is it something like I should increment the x by 1 for each column and then increment y for each row;

Is that correct ?

~ boyd

##### Share on other sites

int i, j,BOARD_X=10,BOARD_Y=8;
bool board[BOARD_X][BOARD_Y];

for (i=0; i

This is how you would iterate through a 2D array and visit each cell on the way.

If this has answered your question I strongly suggested you read up on 2D arrays and other pertinent topics before having a stab at tetris.

##### Share on other sites
([imwithstupid] Sorry, that was me, keep forgetting I'm not logged in and I can't edit that post now)

int i, j,BOARD_X=10,BOARD_Y=8;bool board[BOARD_X][BOARD_Y];for (i=0; i<BOARD_X; i++){    for (j=0; j<BOARD_Y; j++)    {        if (board[j]) // occupied block        {            printf("#");        }        else //empty block        {            printf("-");        }                printf("\n"); // print newline    }}

This is how you would iterate through a 2D array and visit each cell on the way. It's neither efficient (nor correct) for a tetris game.

If this has answered your question I strongly suggested you read up on 2D arrays and other pertinent topics before having a stab at tetris.

##### Share on other sites
Sorry, I got confused in the perspective of rendering them using OpenGL and was thinking in terms of glTranslate ()....

BTW, could you help me in rotating a piece; i've my piece defined as told by u:: a 3*3 2D array but I felt itz useless to have different rotation pieces as a separate piece and use them when a key event for rotation is given.

help me out....

~ boyd

##### Share on other sites
I too was wanting to write a tetris clone, only in flash, and made a similar mistake of thinking I would rotate the pieces using the graphical interface (flash's graphical movieclip rotate function) until I realized that would make collision detection and deleting full rows much harder. I was able to find a very helpful open source flash tetris clone that is well commented and based on a 2d array, that may help you out:
http://www.flashkit.com/movies/Games/Full_Game_Source/Tetris_C-Ilya_Vol-9155/index.php
jordan314

• 13
• 18
• 29
• 11
• 20