Tetris -- OpenGL Help!

Started by
6 comments, last by GLwarrior85 22 years ago
Hi, im trying to begin a tetris clone in openGL, but i really have no idea where to start, can someone give me a few clues/ideas or a piece of code to go on? Or just some logic to follow Thanks alot ¤¤¤--When you lose your power to laugh, you lose your power to think--¤¤¤
¤¤¤--When you lose your power to laugh, you lose your power to think--¤¤¤
Advertisement
This is probobly the wrong forum..try the for beginiers forum.
Not sure if those are good tips, im inventing them as Im writing this but here goes...

I guess you absolutely need a timer in your program, so have a static int timer=0;
in your main Render function and increment it at each call. This will really help you out if you want difficulty level and object speed (in tetris, each time you go up a level the objects drops faster). first function you would call would be a
CreateNewObject(); which basically does a random between 0 and the number of different objects you want (cube, line, etc..), personally i would make out all these objects with cube,
ie a line would be 4 cube lined up; that will it a lot easier for you.
Back to the CreateNewObject(), you would use the random this way:
int type = 0 + rand()...
switch(type)
{
case 0: CreateCube(); break;
case 1: CreateLine(); break;
case 2: ......
}

You would also need a function LineCheck() or something, which basically checks if a line is filled, if so, you destroy it.
Have a global variable bool map[width][height] and have a function which initialize everything in it at false before the game starts. When an object collides to the bottom, you turn true the places which the object occupies, let me make a little graphic to demonstrate this :

your variable bool map[5][10] (for exemple)

f f f f f
f f f f f
f f f f f
f f f f f
f f f f f
f f f f f
f f f f f
f f f f f
f f f f f
f f f f f

so if a cube hits bottom left, you make it so it turns like that :

....
f f f f f
f f f f f
t t f f f
t t f f f

this variable will make it pretty easy to do collision and stuff like that, to translate that to your 3d scene, imagine that each cube is a 4 X 4 X 4 float value. so a vertical line would be made up of 4 cube of 4 x 4 x 4 so a height of 12, so when you use glTranslatef(...) to move the object down, always check if something is below it using your map variable.

Make it so the y value 0 would be the bottom of the "map" and that when an object comes down, it moves at 4.0f y down each 2 second or something (using your timer)

your main render function would be a bit like that

void Game()
{
if (noCurrentObjectIsBeingDropped)
CreateObject();
else
DrawCurrentObject(x, y, z); //moved by user

if (ObjectCollidedWithBottom)
noCurrentObjectIsBeingDropped= true;

if(CheckLine()) //a line filled up.
DestroyThatLine();

}


Thats how I see it....
Dont take everything I say for granted Im pretty new at this too, and I think Im gonna start working on a tetris game too HEHE kinda got me up to it writing this

hope this helps

In Construction : http://www.gdev.org
E-mail me: danman70@hotmail.com, i think i could help you out.
ya btw should have posted this in OpenGL forum =P
In Construction : http://www.gdev.org
you should get the book: "Graphic programming, a top-down aproach using opengl" by siome guy Angel i think. That''s the book i used for my first tetris clone and it is great, it uses glut and opengl
Alexandre, that was an excellent post, I now understand what i have to do. Thanks for the help! you''re a smart dude for a beginner.

¤¤¤--When you lose your power to laugh, you lose your power to think--¤¤¤
¤¤¤--When you lose your power to laugh, you lose your power to think--¤¤¤
Glad I could help, if you have any questions email me
mnok@hotmail.com
In Construction : http://www.gdev.org

This topic is closed to new replies.

Advertisement