Red de conocimiento informático - Material del sitio web - Cómo escribir un juego de Tetris en lenguaje C (con comentarios)

Cómo escribir un juego de Tetris en lenguaje C (con comentarios)

#include lt;windows.hgt;

#include lt;time.hgt;

#include lt;conio.hgt;

#include lt;stdio .hgt;

const int back=34;

const int velocidad[]={0, 12, 9, 6, 3, 1}; const int número[]={0, 2, 3, 5, 7, 11, 15};

const int col[]={11, 15, 12, 12, 13, 13, 14} ;

int rango=1;

int nivel=0; //0 significa ni derecha ni izquierda, 1 significa izquierda, 2 significa derecha

int kind ;

int pri_kind;

int revolve=0; // Marca de estado de rotación

Estructura

{

int x;

int y

}actual, antes

estructura

{

int; x;

int y;

int color;

bool teniendo

}tablero[15] [25]; p>

estructura

{

int variar_x[4];

int variar_y[4]

} variar[; ]={

{{0, 2, 4, 6}, {0, 0, 0, 0}}, {{0, 0, 0, 0}, { 0, -1, - 2, -3}},

{{0, 2, 2, 0}, {0, 0, -1, -1}}, {{0, -2, -2, -4 }, {{0, 0, -1, -1}},

{{0, 0, 2, 2}, {0, -1, -1, - 2}}, {{ 0, 2, 2, 4}}, {0, 0, -1, -1}},

{{0, 0, -2, -2}, { 0, -1, - 1, -2}, {{0, 0, 2, 4}}, {0, -1, 0, 0}},

{{0, 0, 0 , 2}}, { 0,-1,-2,-2}}, {{0, 0,-2,-4}, {0,-1,-1,-1}},

{{0 , 2, 2, 2}}, {0, 0, -1, -2}}, {{0, 2, 4, 4}}, {0, 0, 0, -1} },

{{0, -2, -2, -2}}, {0, 0, -1, -2}}, {0, 0, -1, -2}}, {0, 0, - 1, -2}}, {0, 0, -1, -2}}, {0, 0, 2, 4}}, {0, -1, -1, -1}},

{{0, 0, 0,-2}, {0,-1,-2,-2}}, {{0, 2, 4, 2}}, {0, 0, 0,-1} },

{{0, 0, 2, 0}}, {0,-1,-1,-2}}, {0,-2, 0, 2 }, {0, - 1, -1, -1 }},

{{0, 0, -2, 0}, {0, -1, -1, -2}},

{{0, 0, -2, 0}, {0, -1, -1, -2}},

};<

/p>

estructura

{

int suma

int conexión_x[5]; ;

}conexión[]={

{2, {-2, 2}, {0, 1}},

{1, {0 }, {0}},

{2, {2,-2}, {0, 0}},

{2, {-2, 2}, {0 , 0}},

{4, {-2, 0, 4, -2}, {0, 0, -1, 1}},

{4, { -4, 2, -2, 4}, {0, 0, -1, 1}},

{4, {-2, 2, 0, 0}, {0, 0, 0 , 0}},

};

void gotoxy(int x, int y)

{

COORD pos; p>

pos. /p>

color vacío(int b)

{

HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE));

SetConsoleTextAttribute(hConsole, b) ;

}

void PrintGarphy()

{

color(1593);

int i, j;

gotoxy(2, 2)

for(i=0; ilt; 26; i )

printf("▓") ;

printf("▓").

gotoxy(2, 28);

for(i=0; ilt); ; 26; i )

printf("▓");

for(i=0; ilt; 26; i)

{

gotoxi(2, 2 i );

pone("▓");

gotoxi(34, 2 i);

si(i); ==8||i==16 )

printf("▓▓▓▓▓▓▓▓▓");

else

puts(" ▓");

gotoxy(52, 2 i);

puts("▓");

}

int t ;

ARCHIVO *fp=fopen("C.\els:\els.txt", "r");

fscanf (fp, "d", amp; t) ;

fclose (fp);

color(43);

gotoxy(37, 12);

printf("Más alto registro: d", t);

color(46);

gotoxy(37, 14);

puts("Puntuación actual: 0") ;

color (44);

gotoxi(37,16)

;

puts("Niveles");

color(39);

gotoxy(37,20);

puts( " ↑ ↓ ← →");

gotoxy(37, 22);

puts("Presiona espacio para pausar"); 24 );

puts(" Presione ESC para salir");

gotoxy(37, 26);

puts(" Escritor "); p>

color(atrás);

for(i=0;ilt;15;i)

{

for(j=0; jlt; 25; j )

{

tablero[i][j].x=4 2*i;

tablero[i][j] .y =3 j;

tablero[i] [j].tener=0;

gotoxy(tablero[i][j].x, tablero[i][j] ]. y);

puts(" ");

}

}

}

color. (391

para (int i=24; igt; =0; i--)

para (int j=0; jlt; 15; j )

{

gotoxy(32-j*2, 3 i);

puts("▆");

Dormir( 5) ;

}

color(atrás);

for(int j=0; jlt; 25; j )

for( int i=0; ilt; 15; i )

{

tablero[i][j].having=0

gotoxy(4; 2* i, j 3);

puts("▆");

Dormir(5); (42, 16);

while(rango--)

printf( " "); > gotoxy (47, 14);

puts(" ");

}

void GetDepth(int *pprofundidad)

{

(*pprofundidad)=24;

for(int j=0;jlt;4;j)

{

int suma=0 ;

int n=(current.x variar[tipo].vary_x[j]-4)/2

int m=actual.y variar[tipo].vary_y[; j] -3;

if(mlt;0) m=0;

for( m;mlt;25;m)

if(tablero[ n] [m].having==0amp;amp;ngt;=0amp;amp;nlt;15amp.amp;mgt;=0amp;amp;mlt;25)

suma;

else

romper;

if(sumlt; (*pprofundidad))

(*pprofundidad)=sum

}

}

void CheckFull(int *pscore)

{

int i, j;

int suma=0

int s[]={0, 1, 3, 6, 10};

for(i=0;ilt;25;i)

p> {

bool mark=1;

for( j=0; jlt; 15; j )

{

if(tablero[j][i].teniendo==0)

{

marca=0;

tablero[j][i].color=volver

}

}

if(marca)

{

suma ;

for(int t=i;tgt;0;t--)

{

gotoxy(4, 3 t);

for(j=0; jlt; 15; j )

{

if(tablero[j][t-1].Sí)

{

color(tablero[j][t-1].color);

printf("▆");

tablero[j][t].color=tablero[j][t-1].color;

tablero[j][ t].tener=1;

tablero[j][t-1].tener=0

}

más

{

color(atrás);

printf(" ");

tablero[j][t].color=tablero[j][t]. ].color;

tablero[j][t].tener=0

tablero[j][t-1].tener=0 )

}

}

}

}

(*pscore) =s[suma];

color(46);

gotoxy(47, 14);

printf("d", *pscore

if(*pscoregt;=140); ) rango=5;

else if(*pscoregt;=90) rango=4;

else if(*pscoregt;=50) rango=3;

else if(*pscoregt;=20) rango=3;

else if(*pscoregt;=20) gt;=20) rango=2;

else if( *pscoregt; =1) rango=1;

color(44);

gotoxy(42, 16); ;rankamp;amp;*pscore;i )

printf("★");

int t;

ARCHIVO *fp=fopen("C. \els:\els.txt", "r");

fscanf (fp

, "d",&t);

fclose(fp);

if(*pscoregt;t)

{

fp =fopen("C:\els.txt", "w");

fprintf (fp, "d", *pscore

fclose(fp); p>

color(43);

gotoxy(47, 12);

printf("d", *pscore); /p>

fclose(fp);

}

void PrintNext(int pkind)

{

int i; <

int x=43, y=6;

color(34

for(i=0; ilt; 4; i )

{

gotoxy(x-3, y i-2

puts(" ");

}

pri_ kind=pkind;

if(pkind==6) pkind=15,x--;

else if(pkind==5) pkind=11,x --;

else if(pkind==4) pkind=7,x--;

else if(pkind==3) pkind=5,x-= 2;

else if(pkind==2) pkind=3, x =2;

else if(pkind==1) pkind=2, x--;

p>

else pkind=0, x-=3;

color( col[pri_kind]);

for(i=0;ilt;4; i)

{

gotoxy(x variar[pkind].vary_x[i], y variar[pkind].vary_y[i]);

pone ("▆ ");

}

}

void CheckBoundary()

{

int i, n, m;

for(i=0; ilt; 4; i )

{

m=current.var_x[i ];

if(mlt;4||mgt;32)

{

current.x =(mlt;4?2:-2);

i=0;

}

}

for(i=0; ilt; 4; i ) // Marca cuatro cuadrados Cada cuadrado en

{

m=current.x variar[tipo].vary_x[i];

n=actual.y variar[tipo] .vary_y[i];

m=(m-4)/2;

n-=3;

if(nlt;0| |ngt ;24||board[m][n].having)

{

if(level==1) // desplazamiento hacia la izquierda

actual .x =2; //Restaurar

si no(nivel==2

) // desplazamiento hacia la derecha

current.x-=2; // restaurar

nivel=0

romper

}

;

}

}

void LevelMove()

{

CheckBoundary();

si (nivel==0) retorno;

int i;

color(volver

for(i=0; ilt; 4; i )

{

if(current.y variar[tipo].vary_y[i]gt; 2 )

{

gotoxy(antes .x variar[tipo].vary_x[i], antes.y variar[tipo].vary_y[i]);

puts(" ");

}

}

}

color( col[pri_kind]

for(i=0; ilt; 4; i )

{

if(current.y variar[tipo].vary_y[i]gt; 2)

{

gotoxy(current.x variar[tipo].vary_x[i], actual.y variar[tipo].vary_y[i]);

puts("▆");

}

}

before.x=current.x;

}

void Revolve(int **prev_count) //Función de rotación

{

int i;

antes.x=actual.x;

}

antes.x=actual. x;

before.x=current.x; int m=(current.x-4 variar[número[pri_kind] **prev_count].vary_x[i])/2;

int n=current.y variar[número[pri_kind] **prev_count].vary_y[i]-3;

if(tablero[m][n].if(tablero[m][ n]. teniendo|mgt;14||mlt;0||ngt;24)

{

current.x=current.x-connection[pri_kind].y=current. .y;

retorno

}

}

color(volver); =0; ilt; 4; i )

{

si(antes.y variar[tipo].vary_y[i]gt;2)

{

gotoxy(antes.x variar[tipo].vary_x[i], antes.y variar[tipo].var_y[i]);

puts(" ");

}

}

tipo=número[pr

i_kind] **prev_count;

color(col[pri_kind]);

for( i=0; ilt; 4; i)

{

if(current.y varia[kind].var_x[i]); break;

case 0x50: //down

nivel=0;

return;

caso 0x4b: //izquierda

before.x=current.x;

current.x-=2; p>

nivel=1;

LevelMove();

descanso

caso 0x4d://derecha

antes de .x=actual.x;

actual.x =2;

nivel=2

nivel=2; > LevelMove ();

romper

caso ' ':

_getch(); p> caso 27:

color(volver);

salir(1);

valor predeterminado:

}

}

Dormir(velocidad[rango]);

}

}

void StraightFall(int profundidad)

{

int i;

int rev_count=0;

antes.x=current.x

antes. y= actual.yy;

descanso;

}

color(volver);

for(i=0; ilt; 4; i )

{

if(antes.y variar[tipo].vary_y[i]gt; 2)

{

gotoxy(antes.x variar[tipo].vary_x[i], antes.y variar[tipo].vary_y[i]);

puts(" ");

}

}

actual.y;

color(col[pri_kind]); ; 4 ; i )

{

if(current.y variar[tipo].vary_y[i]gt; 2)

{

gotoxy(current.x variar[tipo].vary_x[i], actual.y variar[tipo].vary_y[i]);

puts("▆");

}

}

revolución= 0;

antes.x=actual.x

antes.y=actual; .y ;

}

int m=(current.x-4 variar[tipo].vary_x[i])/2

;

int n=current.y-3 variar[tipo].vary_y[i];

tablero[m][n].having=1;

tablero[m][n].color=col[pri_kind];

}

CheckFull(amp;score);

kind=pkind;

}

devuelve 0;

}