• Advertisement
Sign in to follow this  

Looking for tetris created on C, not c++.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, i'm looking for tetris sroucecode (better would be a tutorial), created with C language. Not C++. Just C. Because i'v took some courses on youtube for C and now i want to create some game. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Look up the tetris stuff in my posting history (newest 2-3), to get some idea.

Or:
http://www.gamedev.net/columns/hardcore/tetris/default.asp

Share this post


Link to post
Share on other sites
Google turned up these:
http://www.programmersheaven.com/download/16993/download.aspx
http://www.programmersheaven.com/download/1022/download.aspx

They look pretty old though.

[Edit]
This one looks a bit newer:
http://code.google.com/p/simple-tetris-clone/

Share this post


Link to post
Share on other sites
I'v tried that google one before. I like it how it's designed, that's why i wanted to get it up. It just doesn't compiles. I guess i need some external stuff or something.

Share this post


Link to post
Share on other sites
Quote:
Original post by frozen01
I've tried that Google one before. I like it how it's designed, that's why I wanted to get it up. It just doesn't compiles. I guess I need some external stuff or something.


Yes. You need SDL.

Share this post


Link to post
Share on other sites
Here is my code. I made it in 5 hours, so no optimization, or fancy stuff. No game over.

It's C, but I think it has some invalid C syntax, but they can easily been replaced with valid code.

I posted, because I'm curious about feedback.
(I hope I don't violate a rule with this.)

#include <windows.h>
#include <stdio.h>
#include <GL/gl.h>
#include "glut.h"

#define SQUARE_SIZE 0.05
#define X_OFFSET -0.5
#define Y_OFFSET -0.7

int Field[15][30]; //0 empty, 1 fixed, 2 current moving, 3 new position
int c_i, c_k;

int LineCount = 0;

void DrawSqare(float x, float y)
{
glBegin(GL_QUADS);
glVertex2f(X_OFFSET+(x+0)*SQUARE_SIZE, Y_OFFSET+(y+0)*SQUARE_SIZE);
glVertex2f(X_OFFSET+(x+0.9)*SQUARE_SIZE, Y_OFFSET+(y+0)*SQUARE_SIZE);
glVertex2f(X_OFFSET+(x+0.9)*SQUARE_SIZE, Y_OFFSET+(y+0.9)*SQUARE_SIZE);
glVertex2f(X_OFFSET+(x+0)*SQUARE_SIZE, Y_OFFSET+(y+0.9)*SQUARE_SIZE);
glEnd();
}

bool MoveBrickDown()
{
int i, k;

for( i = 0; i < 15; i++ )
{
k = 29;

while( k >=0 && Field[k] != 2 )
k--;

if( k == 29 || Field[k+1] == 1 )
return true;
}

c_k ++;

for( i = 0; i < 15; i++ )
{ k = 0;

while( k < 30 && Field[k++] != 2 );
if( k == 30 )
continue;
Field[k-1] = 0;
while( Field[k++] == 2 );
Field[k-1] = 2;
}

return false;
}

void MoveBrickRight()
{
int i, k;

for( k = 0; k < 30; k++ )
{
i = 14;

while( i >= 0 && Field[k] != 2 )
i--;

if( i != -1 && (i == 14 || Field[i+1][k] == 1) )
return;
}

c_i++;

for( k = 0; k < 30; k++ )
{ i = 0;

while( i < 15 && Field[i++][k] != 2 );
if( i == 15 )
continue;
Field[i-1][k] = 0;
while( Field[i++][k] == 2 );
Field[i-1][k] = 2;
}
}

void MoveBrickLeft()
{
int i, k;

for( k = 0; k < 30; k++ )
{
i = 0;

while( i <= 14 && Field[k] != 2 )
i++;

if( i != 15 && (i == 0 || Field[i-1][k] == 1) )
return;
}

c_i--;

for( k = 0; k < 30; k++ )
{ i = 14;

while( i > -1 && Field[i--][k] != 2 );
if( i == -1 )
continue;
Field[i+1][k] = 0;
while( Field[i--][k] == 2 );
Field[i+1][k] = 2;
}
}

void RotateBrick(int direction)
{
int i, k;
int di, dk;

for( i = 0; i < 15; i++ )
{
for( k = 0; k < 30; k++ )
{
if( Field[k] == 2 || Field[k] == 4 )
{
di = direction*(i - c_i);
dk = direction*(k - c_k);

if( c_i+dk < 0 || c_i+dk > 14 || c_k-di < 0 || c_k-di > 29 ||
Field[c_i+dk][c_k-di] == 1 )
return;
}
}
}

for( i = 0; i < 15; i++ )
{
for( k = 0; k < 30; k++ )
{
if( Field[k] & 2 )
{
di = direction*(i - c_i);
dk = direction*(k - c_k);

Field[k] &= ~2;

Field[c_i+dk][c_k-di] |= 4;
}
}
}

for( i = 0; i < 15; i++ )
{
for( k = 0; k < 30; k++ )
{ if( Field[k] == 4 )
Field[k] = 2;
}
}
}

void Display(void);

void DestroyLine()
{
int counter;

for( int k = 0; k < 30; k++ )
{
counter = 0;

for( int i = 0; i < 15; i++ )
if( Field[k] == 1 )
counter ++;

if( counter < 15 )
continue;

LineCount ++;

for( int i = 0; i < 15; i++ )
{ Field[k] = 0;
Sleep(30);
Display();
}

for( int i = 0; i < 15; i++ )
{
for( int m = k-1; m >= 0; m-- )
{
Field[m+1] = Field[m];
}
}
}
}

void MakeFieldStatic()
{
for( int i = 0; i < 15; i++ )
for( int k = 0; k < 30; k++ )
if( Field[k] == 2 )
Field[k] = 1;
}

void SpawnBrick()
{
srand(timeGetTime());

switch( rand()%7 )
{
case 0:
Field[6][0] = 2;
Field[7][0] = 2;
Field[8][0] = 2;
Field[9][0] = 2;
break;
case 1:
Field[6][0] = 2;
Field[7][0] = 2;
Field[8][0] = 2;
Field[6][1] = 2;
break;
case 2:
Field[6][0] = 2;
Field[7][0] = 2;
Field[8][0] = 2;
Field[8][1] = 2;
break;
case 3:
Field[6][0] = 2;
Field[7][0] = 2;
Field[8][0] = 2;
Field[7][1] = 2;
break;
case 4:
Field[7][0] = 2;
Field[8][0] = 2;
Field[6][1] = 2;
Field[7][1] = 2;
break;
case 5:
Field[6][0] = 2;
Field[7][0] = 2;
Field[7][1] = 2;
Field[8][1] = 2;
break;
case 6:
Field[6][0] = 2;
Field[7][0] = 2;
Field[6][1] = 2;
Field[7][1] = 2;
break;
}
c_i = 7;
c_k = 0;
}

void Init(void)
{
glDisable(GL_CULL_FACE);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);

memset(&Field[0][0],0,sizeof(Field));

SpawnBrick();
}

void PrintLines()
{
int val = LineCount;
float text_pos = -0.7;

glColor3f(1,1,1);

do
{
glRasterPos2f(text_pos, 0);

glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_24,
(char)(val%10)+'0' );
val /= 10;
text_pos -= 0.05;

}while( val > 0 );
}

void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,1,1);

for( int i = 0; i < 15; i++ )
for( int k = 0; k < 30; k++ )
{ if( Field[k] == 2 )
glColor3f(0,0,1);
else if( Field[k] == 1 )
glColor3f(1,1,1);
else
glColor3f(0.5,0,0);
DrawSqare(i,k);
}

//glColor3f(0,1,0);
//DrawSqare(c_i,c_k);

PrintLines();

glutSwapBuffers();
}

void Reshape(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION_MATRIX);

glLoadIdentity();
glOrtho(0,w,h,0,-1, 1);

glMatrixMode(GL_MODELVIEW_MATRIX);

glLoadIdentity();
glScalef(1,-1,1);

glutPostRedisplay();
}

void Keyboard(unsigned char key, int x, int y)
{
if(key==27)
exit(0);

if( key == 'A' || key == 'a' )
MoveBrickLeft();

if( key == 'D' || key == 'd' )
MoveBrickRight();

if( key == 'S' || key == 's' )
if( MoveBrickDown() )
{
MakeFieldStatic();
DestroyLine();
SpawnBrick();
}

if( key == 'Q' || key == 'q' )
RotateBrick(+1);

if( key == 'E' || key == 'e' )
RotateBrick(-1);

glutPostRedisplay();
}

void TimerFunc(int dummy)
{
if( MoveBrickDown() )
{
MakeFieldStatic();
DestroyLine();
SpawnBrick();
}

glutPostRedisplay();

glutTimerFunc(450,TimerFunc,0);
}


void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(640, 512);
glutCreateWindow("Shadow Mapping");
Reshape(640, 512);

Init();

glutTimerFunc(450,TimerFunc,0);
glutReshapeFunc(Reshape);
glutDisplayFunc(Display);
glutKeyboardFunc(Keyboard);
glutMainLoop();
}

Share this post


Link to post
Share on other sites
Quote:
Original post by szecs
It's C, but I think it has some invalid C syntax, but they can easily been replaced with valid code.


You get a cookie. Unfortunately, in C, there's no boolean type; replace with int, and return 0 and 1 in place of false and true respectively. Also, variable declarations can only exist at the start of the function. And one other nit pick: Isn't glut's header <GL/glut.h> and not "glut.h"? Google seems to say the former is correct.

Share this post


Link to post
Share on other sites
Quote:
Original post by _fastcall
Quote:
Original post by szecs
It's C, but I think it has some invalid C syntax, but they can easily been replaced with valid code.


You get a cookie. Unfortunately, in C, there's no boolean type; replace with int, and return 0 and 1 in place of false and true respectively. Also, variable declarations can only exist at the start of the function. And one other nit pick: Isn't glut's header <GL/glut.h> and not "glut.h"? Google seems to say the former is correct.

If you put it in the GL directory. But it isn't included in Windows by default, so "" means it's in the same directory, as the main.c. And since I don't use glut, just there was in a tutorial directory, it was simpler to copy near main.c.

But thanks for the cookie, whatever that means, I'm not a native English speaker (which is quite obvious, I think).

Share this post


Link to post
Share on other sites
"You get a cookie" taken literally would mean that he wants to give you a cookie (being a baked good that is flat, circular, and often chewy with something optional added like pieces of chocolate, raisins, nuts, or other bits of things added for flavor) as a reward for your help.

Given that he is not going to literally give you a cookie the meaning would then become something like "I'm not going to actually give you a cookie, but what you did was great and it would be worthy of a cookie if there was a reasonable way of me getting one to you without having to go terribly out of my way".

Share this post


Link to post
Share on other sites
Quote:
Original post by _fastcall
Unfortunately, in C, there's no boolean type; replace with int, and return 0 and 1 in place of false and true respectively.


Huh? Every C compiler I've used since the early 90's has had a bool type. I despise C++ and have only written C code. Only recently have I begun to use C++ compilers (mainly due to the fact that there are no good straight C compilers anymore), although I still write code in C. Never had a problem with bools.

Share this post


Link to post
Share on other sites
Quote:
Original post by maspeir
Huh? Every C compiler I've used since the early 90's has had a bool type.
bool wasn't introduced into C until the 1999 standard. It may have been available before then as a non-standard compiler extension.

Share this post


Link to post
Share on other sites
Quote:
Original post by nobodynews
"You get a cookie" taken literally would mean that he wants to give you a cookie (being a baked good that is flat, circular, and often chewy with something optional added like pieces of chocolate, raisins, nuts, or other bits of things added for flavor) as a reward for your help.

Given that he is not going to literally give you a cookie the meaning would then become something like "I'm not going to actually give you a cookie, but what you did was great and it would be worthy of a cookie if there was a reasonable way of me getting one to you without having to go terribly out of my way".
Cookie meant rate points in this case. But thanks for the definition of the "cookie" :P

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement