You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
arix_tetris_game/俄罗斯方块终极极简版.c

266 lines
6.4 KiB

6 years ago
#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#define N 20
#define M 16
int cur_x,cur_y;
int score,mark,next,map[N][M],Gamespeed=400;
int shape[28][6]=
{
{0,-1,0,-2,1,0}, {0,1,1,0,2,0}, {-1,0,0,1,0,2}, {0,-1,-1,0,-2,0},
{0,-1,0,1,-1,0}, {0,1,1,0,-1,0}, {1,0,0,-1,0,1}, {1,0,-1,0,0,-1},
{-1,1,0,1,1,0}, {0,-1,1,0,1,1}, {-1,0,0,-1,1,-1}, {-1,-1,-1,0,0,1},
{-1,0,0,1,1,1}, {0,1,1,-1,1,0}, {-1,0,0,1,1,1}, {0,1,1,-1,1,0},
{-1,0,0,-1,0,-2}, {-1,0,-2,0,0,1}, {0,1,0,2,1,0}, {0,-1,1,0,2,0},
{0,1,1,0,1,1}, {0,-1,1,0,1,-1}, {-1,0,0,-1,-1,-1}, {-1,0,-1,1,0,1},
{0,1,0,2,0,3}, {1,0,2,0,3,0}, {0,-1,0,-2,0,-3}, {-1,0,-2,0,-3,0}
};
void Init(int now);
void PrintPrompting();
void YourScore();
void GotoxyWithFullwidth(short x, short y);//ȫ<><C8AB>
void Gameover();
void ShowMap(int now);
void GotoxyWithFullwidth(short x, short y)//<2F><>ȫ<EFBFBD>Ƕ<EFBFBD>λ<EFBFBD><CEBB>
{
static COORD cd;//<2F><><EFBFBD><EFBFBD><EAB6A8>
cd.X = x;
cd.Y = y;
HANDLE A_GConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(A_GConsoleOutput, cd);
}
void PrintPrompting()//<2F><><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>ʾ<EFBFBD><CABE>Ϣ <20><>
{
HANDLE A_GConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(A_GConsoleOutput, 0xF);
GotoxyWithFullwidth(50, 5);
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><EFBFBD><EFBFBD> <20><>");
GotoxyWithFullwidth(50, 6);
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><EFBFBD><EFBFBD> <20><>");
GotoxyWithFullwidth(50, 7);
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><EFBFBD><EFBFBD> <20><>");
GotoxyWithFullwidth(50, 8);
printf("<EFBFBD><EFBFBD>˳ʱ<EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD> <20><>");
GotoxyWithFullwidth(50, 9);
printf("<EFBFBD><EFBFBD><EFBFBD>ո<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
GotoxyWithFullwidth(50, 14);
printf("|<7C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӭ<EFBFBD><D3AD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>˹<EFBFBD><CBB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ") ;
GotoxyWithFullwidth(50,15);
printf("|<7C><><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʒ<EFBFBD><C6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߾<EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
GotoxyWithFullwidth(50, 16);
printf("|<7C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɰ<EFBFBD><C9B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD>") ;
GotoxyWithFullwidth(50, 17);
printf("|<7C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˼ҲŲ<D2B2><C5B2>Ǵ<EFBFBD><C7B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӱȣ<D3B1>");
GotoxyWithFullwidth(50, 18);
printf("|<7C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><CFB7>~~<7E><><EFBFBD><EFBFBD>~~~<7E><>ιι<CEB9><CEB9><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ˮ<EFBFBD><CBAE>Ҳ̫<D2B2><CCAB><EFBFBD><EFBFBD><EFBFBD>˰ȣ<CBB0><C8A3><EFBFBD>");
GotoxyWithFullwidth(75, 28);
printf("<EFBFBD><EFBFBD>By: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һֻ<D2BB><D6BB><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>С<EFBFBD>˼<EFBFBD>");
}
void Gameover()//<2F><><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>Ϸ<EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
int i,j,flag=0;
for(j=1;j<M-1;j++)
{
if(map[1][j]!=0)
{
flag=1;break;
}
}
if(flag==1)
{
system("cls");
GotoxyWithFullwidth(9,9);
printf("GAME OVER");
GotoxyWithFullwidth(1,N+1);
exit(0);
}
}
void ShowMap(int now)//<2F><>ҳ<EFBFBD><EFBFBD>ֺͷ<D6BA><CDB7><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
CONSOLE_CURSOR_INFO cursorInfo = { 1, FALSE};
int i,j;
GotoxyWithFullwidth(0,0);
if(now!=-1)
{
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
if(i==0&&j==0 || i==0&&j==M-1 || j==0&&i==N-1 || j==M-1&&i==N-1)printf(" ");
else if( i==N-1)printf("==");
else if(j==0 || j==M-1)printf("|");
else if(map[i][j]==2) printf("<EFBFBD><EFBFBD>");
else if(i==cur_x+shape[now][0] && j==cur_y+shape[now][1] ||
i==cur_x+shape[now][2] && j==cur_y+shape[now][3] ||
i==cur_x+shape[now][4] && j==cur_y+shape[now][5] ||
i==cur_x && j==cur_y)
printf("<EFBFBD><EFBFBD>"); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ǵ<EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
else if(map[i][j]==0) printf(" "); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6>ĺۼ<C4BA>
}
if(i==1)printf(" <20><>һ<EFBFBD><D2BB>:");
if(i==11)printf(" <20><><EFBFBD><EFBFBD> : %d",score);
if(i==19)printf(" <20><>ǰ<EFBFBD>ٶ<EFBFBD> : %d",score/100+1);
puts(" ");
}
}
else//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
mark=1;
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
if(i==0&&j==0 || i==0&&j==M-1 || j==0&&i==N-1 || j==M-1&&i==N-1)printf(" ");
else if(i==N-1)printf("==");
else if(j==0 || j==M-1)printf("|");
else if(map[i][j]==2) printf("<EFBFBD><EFBFBD>");
else if(map[i][j]==0) printf(" ");
}
if(i==1)printf(" <20><>һ<EFBFBD><D2BB>:");
if(i==11)printf(" <20><><EFBFBD><EFBFBD> : %d",score);
if(i==19)printf(" <20><>ǰ<EFBFBD>ٶ<EFBFBD> : %d",score/100+1);
puts(" ");
}
}
for(i=0;i<6;i=i+2)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD>ʾ
{
GotoxyWithFullwidth(36+2*shape[now][i+1],6+shape[now][i]); printf(" ");
}
GotoxyWithFullwidth(36,6); printf("<EFBFBD><EFBFBD>");
for(i=0;i<6;i=i+2)
{
GotoxyWithFullwidth(36+2*shape[next][i+1],6+shape[next][i]); printf("<EFBFBD><EFBFBD>");
}
Sleep(Gamespeed);
}
void Init(int now)//<2F><><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
{
int i,j;
memset(map,0,sizeof(map));
for( i=0;i<N;i++)
{
for( j=0;j<M;j++)
if(i==N-1 || j==0 || j==M-1)
map[i][j]=-1;
}
cur_x=0; cur_y=5;
ShowMap(now);
}
int Judge(int x,int y,int now)
{
if(map[x][y]!=0)
return 0;
int i;
for( i=0;i<6;i=i+2)
{
if(map[ x+shape[now][i] ][ y+shape[now][i+1] ]!=0)
return 0;
}
return 1;
}
void YourScore()
{//<2F>÷֣<C3B7><D6A3><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD>ͼ<EFBFBD>ε<EFBFBD><CEB5><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD>
CONSOLE_CURSOR_INFO cursorInfo = { 1, FALSE};
int i,j,p,q;
for(i=1;i<N-1;i++)
{
int flag=0;
for( j=1;j<M-1;j++)
{
if(map[i][j]!=2)
{
flag=1;break;
}
}
if(flag==0)
{
int k=3;
while(k--)
{
GotoxyWithFullwidth(2,i+1);
for( p=1;p<M-1;p++)
{
if(map[i][p]==2)
{
if(k%2==1)
printf(" ");
}
}
Sleep(10);
}
for( p=i;p>1;p--)
{
for( q=1;q<M-1;q++) map[p][q]=map[p-1][q];
}
ShowMap(-1); score+=20;
if(score%100==0 && score!=0 && Gamespeed!=0) Gamespeed-=50;
}
}
}
int main(void)
{
CONSOLE_CURSOR_INFO cursorInfo = { 1, FALSE};//<2F>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD>
GetStdHandle(STD_OUTPUT_HANDLE);//<2F><><EFBFBD>DZ<EFBFBD>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
HANDLE A_GConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); //<2F><>ֵ<EFBFBD><D6B5>׼<EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>
SetConsoleCursorInfo(A_GConsoleOutput, &cursorInfo);
SetConsoleTitleA("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˹<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨<EFBFBD><EFBFBD><EFBFBD>By<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)<29>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ŮQAQ");//<2F>˼<EFBFBD><CBBC><EFBFBD><EFBFBD><EFBFBD>qwq
do
{
PrintPrompting();//<2F><>ʾ<EFBFBD><CABE>ʾ<EFBFBD><CABE>Ϣ
YourScore();
int i,now,set=1;
srand(time(NULL));
now=rand()%28;
next=rand()%28;
Init(now);
while(1)
{
Here: mark=0;
if(set==0)
{
now=next;
next=rand()%28;
cur_x=0;cur_y=5;
set=1;
}
while(!kbhit())
{
Gameover();//<2F><>Ϸ<EFBFBD><CFB7>û<EFBFBD>н<EFBFBD><D0BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>һ<EFBFBD><D2BB>
if(Judge(cur_x+1,cur_y,now)==1) cur_x++;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
else
{
map[cur_x][cur_y]=2;
for(i=0;i<6;i=i+2)
map[ cur_x+shape[now][i] ][ cur_y+shape[now][i+1] ]=2;
YourScore();//<2F>÷֣<C3B7><D6A3><EFBFBD><EFBFBD>л<EFBFBD><D0BB><EFBFBD>ͼ<EFBFBD>ε<EFBFBD><CEB5><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD>
set=0;
}
if(mark!=1) ShowMap(now);
goto Here;
}
char key;
key=getch();
if(key==72)
{
int temp=now;
now++;
if( now%4==0 && now!=0 )now=now-4;//<2F><><EFBFBD><EFBFBD>ͬһ<CDAC><D2BB><EFBFBD><EFBFBD><EFBFBD>͵ķ<CDB5><C4B7><EFBFBD>
if(Judge(cur_x,cur_y,now)!=1)now=temp;
}//<2F><>ת
else if(key==80 && Judge(cur_x+1,cur_y,now)==1)cur_x++;//<2F><>
else if(key==75 && Judge(cur_x,cur_y-1,now)==1)cur_y--;//<2F><>
else if(key==77 && Judge(cur_x,cur_y+1,now)==1)cur_y++; //<2F><>
else if(key==32 && Judge(cur_x,cur_y,now)==1){cur_x=cur_x-1;}//<2F>ո<EFBFBD><D5B8><EFBFBD><EFBFBD><EFBFBD>ȥ
else continue;
}
}
while (1);
GotoxyWithFullwidth(0, 0);
CloseHandle(A_GConsoleOutput);
return 0;
}