学完c了,总算是有点收获了,自己写了个俄罗斯方块练习数组,在vc6.0控制台环境下编译通过.
makehead.h
int Makepanel(char Ary_Panel[][10]);
void gotoxy(int x, int y);
void BrushsScreen(char ary[][20]);
int initializationShape();
void ShowShape(char ary_shapel[][3][3],int nType,int *x, int *y,char Arry_recordXY);
int isNotLandDown(char ary_Panel[][20],char ary_shapel[][3][3],int nType,int *x,int *y);
void SetDownLandStatus(char ary_shapel[][3][3],int nType);
void RemoveRow(char ary_Panel[][23]);
void BrushsScreen2(char Arry_recordXY[][3][2]);
void Cls_Arry_recordXY(char Arry_recordXY[][3][2]);
int isCollision_left(char ary_Panel[][23],char ary_shapel[][3][3] ,int x, int y,int nType);
void Block_Turn(char ary_shapel[][3][3],int nType);
void DownRow(char ary_Panel[][20],int k);
char key(int s);
int isBlock_Turn(char ary_Panel[][20],char ary_shapel[][3][3],int nType,int x,int y);
int isCollision_right(char ary_Panel[][20],char ary_shapel[][3][3] ,int x, int y,int nType);
void GameOver();
int isGameOver(int x);
void Stop();
int maxDown(char ary_Panel[][20],int x,int y);
#define ShapeType 10
#include <windows.h>
#include<stdio.h>
#include<conio.h>
#define Nop 0
#define Wall 1
#define Solid 2
#define Landing 3//函数名&功能:显示主界面显示
//参数:主界面的二维数组
//返回值:intint Makepanel(char Ary_Panel[][20])
{int i=0,j=0;for(i=0,j=0;i<23;i++){for(j=0;j<20;j++){if(j==0||j==19||i==22){Ary_Panel[i][j]=1;}else{Ary_Panel[i][j]=0; }if(Ary_Panel[i][j]==Wall){printf("⊙");}else if(Ary_Panel[i][j]==Nop){printf(" ");}if(j==19){printf("\r\n");}}}printf("上下左右->wsad,暂停为空格键");return 0;
}//函数名&功能:移动到x,y点
//参数:x,y为坐标
//返回值:voidvoid gotoxy(int x, int y)
{COORD point;point.X = x; point.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point);
}//函数名&功能:刷新屏目
//参数:x,y为坐标
//返回值:voidvoid BrushsScreen(char ary[][20],int x,int y)
{int i=0,j=0;for(i=0;i<23;i++){for(j=1;j<19;j++){gotoxy(j+j,i);if(ary[i][j]==Nop){printf(" ");}}}
}//函数名&功能:刷新屏目
//参数:x,y为坐标
//返回值:voidvoid BrushsScreen2(char Arry_recordXY[][3][2])
{int i=0,j=0;for(i=0;i<3;i++){for(j=0;j<3;j++){ if(Arry_recordXY[i][j][0]!=0 || Arry_recordXY[i][j][1]!=0){gotoxy(Arry_recordXY[i][j][1],Arry_recordXY[i][j][0]);printf(" ");Arry_recordXY[i][j][0]=0x00;Arry_recordXY[i][j][1]=0x00;}}}
}//函数名&功能:方块初始化
//参数:x,y为坐标
//返回值:voidint initializationShape()
{int nType=0,i=0,j=0;srand( (unsigned)time( NULL ) );nType = rand() % 10;return nType;
}//函数名&功能:刷新显示方块边上的区域
//参数:x,y为坐标
//返回值:voidvoid ShowShape(char ary_shapel[][3][3],int nType,int *x, int *y,char Arry_recordXY[][3][2])
{int i=0,j=0;for(i=0;i<3;i++){for(j=0;j<3;j++){if(ary_shapel[nType][i][j]==Solid){if(i==0 && j==0 && (*x-1)>=0){//打印原点左边上方的点gotoxy((*y+*y-2),*x-1);printf("■");Arry_recordXY[i][j][0]=*x-1;//记录要清除的点Arry_recordXY[i][j][1]=*y+*y-2;}else if(i==0 && j==1 && (*x-1)>=0){//打印原点上方的点gotoxy((*y+*y),*x-1);printf("■");Arry_recordXY[i][j][0]=*x-1;//记录要清除的点Arry_recordXY[i][j][1]=*y+*y;}else if(i==0 && j==2 && (*x-1)>=0){//打印原点右边上方的点gotoxy((*y+*y+2),*x-1);printf("■");Arry_recordXY[i][j][0]=*x-1;//记录要清除的点Arry_recordXY[i][j][1]=*y+*y+2;}else if(i==1 && j==0 && *x>=0){//打印原点左边的点gotoxy((*y+*y-2),*x);printf("■");Arry_recordXY[i][j][0]=*x;//记录要清除的点Arry_recordXY[i][j][1]=*y+*y-2;}else if(i==1 && j==1 && *x>=0){gotoxy((*y+*y),*x);//打印原点printf("■");Arry_recordXY[i][j][0]=*x;//记录要清除的点Arry_recordXY[i][j][1]=*y+*y;}else if(i==1 && j==2 && *x>=0){//打印原点右边的点gotoxy((*y+*y+2),*x);printf("■");Arry_recordXY[i][j][0]=*x;//记录要清除的点Arry_recordXY[i][j][1]=*y+*y+2;}else if(i==2 && j==0 && (*x+1)>=0){//打印原点左边下方的点gotoxy((*y+*y-2),*x+1);printf("■");Arry_recordXY[i][j][0]=*x+1;//记录要清除的点Arry_recordXY[i][j][1]=*y+*y-2;}else if(i==2 && j==1 && (*x+1)>=0){//打印原点下边的点gotoxy((*y+*y),*x+1);printf("■");Arry_recordXY[i][j][0]=*x+1;//记录要清除的点Arry_recordXY[i][j][1]=*y+*y;}else if(i==2 && j==2 && (*x+1)>=0){//打印原点右边下方的点gotoxy((*y+*y)+2,*x+1);printf("■");Arry_recordXY[i][j][0]=*x+1;//记录要清除的点Arry_recordXY[i][j][1]=*y+*y+2;}}}}
}//函数名&功能:是否落地
//参数:x,y为坐标
//返回值:1为不落地,0为落地int isNotLandDown(char ary_Panel[][20],char ary_shapel[][3][3],int nType,int *x,int *y)
{int i=0,j=0;for(i=0;i<3;i++){for(j=0;j<3;j++){if(ary_shapel[nType][i][j]==Solid)//是否是实体{if(i==0 && j==0 ){//打印原点左边上方的点if(ary_Panel[*x-1+1][(*y-1)]==Wall || ary_Panel[*x-1+1][(*y-1)]==Landing){return 0;}}else if(i==0 && j==1 ){//打印原点上方的点if(ary_Panel[*x-1+1][(*y)]==Wall || ary_Panel[*x-1+1][(*y)]==Landing){return 0;}}else if(i==0 && j==2 ){//打印原点右边上方的点if(ary_Panel[*x-1+1][(*y+1)]==Wall || ary_Panel[*x-1+1][(*y+1)]==Landing){return 0;}}else if(i==1 && j==0 ){//打印原点左边的点if(ary_Panel[*x+1][(*y-1)]==Wall || ary_Panel[*x+1][(*y-1)]==Landing){return 0;}}else if(i==1 && j==1){if(ary_Panel[*x+1][(*y)]==Wall || ary_Panel[*x+1][(*y)]==Landing){return 0;}}else if(i==1 && j==2 ){//打印原点右边的点if(ary_Panel[*x+1][(*y+1)]==Wall || ary_Panel[*x+1][(*y+1)]==Landing){return 0;}}if(i==2 && j==0 ){//打印原点左边下方的点if(ary_Panel[*x+1+1][(*y-1)]==Wall || ary_Panel[*x+1+1][(*y-1)]==Landing){return 0;}}else if(i==2 && j==1 ){//打印原点下边的点if(ary_Panel[*x+1+1][*y]==Wall || ary_Panel[*x+1+1][*y]==Landing){return 0;}}else if(i==2 && j==2 ){//打印原点右边下方的点if(ary_Panel[*x+1+1][*y+1]==Wall || ary_Panel[*x+1+1][*y+1]==Landing){return 0;}}}}}return 1;
}//函数名&功能:设置落地的状态
//参数:
//返回值:voidvoid SetDownLandStatus(char ary_Panel[][20],char ary_shapel[][3][3],int nType,int x,int y)
{int i=0,j=0;for(i=0;i<3;i++){for(j=0;j<3;j++){if(ary_shapel[nType][i][j]==Solid)//是否是实体{if(i==0&&j==0){ary_Panel[x-1][y-1]=Landing;}else if(i==0&&j==1){ary_Panel[x-1][y]=Landing;}else if(i==0&&j==2){ary_Panel[x-1][y+1]=Landing;}else if(i==1&&j==0){ary_Panel[x][y-1]=Landing;}else if(i==1&&j==1){ary_Panel[x][y]=Landing;}else if(i==1&&j==2){ary_Panel[x][y+1]=Landing;}else if(i==2&&j==0){ary_Panel[x+1][y-1]=Landing;}else if(i==2&&j==1){ary_Panel[x+1][y]=Landing;}else if(i==2&&j==2){ary_Panel[x+1][y+1]=Landing;}}}}
}//函数名&功能:掉下
//参数:k为当前消除的行
//返回值:voidvoid DownRow(char ary_Panel[][20],int k)
{int i=0,j=0;for(k=k-1;k>=0;k--){for(j=1;j<19;j++){if(ary_Panel[k][j]==Landing){ary_Panel[k+1][j]=ary_Panel[k][j];ary_Panel[k][j]=Nop;}}}
}//函数名&功能:消除填满的行
//参数:
//返回值:1返回消行,0int RemoveRow(char ary_Panel[][20],int x,int y)
{int i=0,j=0,isSolidRow=0;for(i=0;i<22;i++){for(j=1;j<19;j++){if(ary_Panel[i][j]==Nop)//是否满足消行?{isSolidRow=0;goto gotoNOP;}}for(j=1;j<19;j++){if(ary_Panel[i][j]==Landing)//是否满足消行?{isSolidRow=1;ary_Panel[i][j]=Nop;//消行;}}DownRow(ary_Panel,i);BrushsScreen(ary_Panel,x,y);//刷新屏gotoNOP:;}
}//函数名&功能:清记录的坐标
//参数:
//返回值:voidvoid Cls_Arry_recordXY(char Arry_recordXY[][3][2])
{int i=0,j=0;for(i=0;i<3;i++){for(j=0;j<3;j++){Arry_recordXY[i][j][0]=0x00;Arry_recordXY[i][j][1]=0x00;}}
}//函数名&功能:left是否发生碰撞
//参数:
//返回值:0发生碰撞int isCollision_left(char ary_Panel[][20],char ary_shapel[][3][3] ,int x, int y,int nType)
{int i=0,j=0;for(i=0;i<3;i++){for(j=0;j<3;j++){if(ary_shapel[nType][i][j]==Solid)//是否是实体{if(i==0&&j==0){if(ary_Panel[x-1][y-1-1]==Landing || ary_Panel[x-1][y-1-1]==Wall){return 0;}}else if(i==0&&j==1){if(ary_Panel[x-1][y-1]==Landing || ary_Panel[x-1][y-1]==Wall){return 0;}}else if(i==0&&j==2){if(ary_Panel[x-1][y+1-1]==Landing || ary_Panel[x-1][y+1-1]==Wall){return 0;}}else if(i==1&&j==0){if(ary_Panel[x][y-1-1]==Landing || ary_Panel[x][y-1-1]==Wall){return 0;}}else if(i==1&&j==1){if(ary_Panel[x][y-1]==Landing || ary_Panel[x][y-1]==Wall){return 0;}}else if(i==1&&j==2){if(ary_Panel[x][y+1-1]==Landing || ary_Panel[x][y+1-1]==Wall){return 0;}}else if(i==2&&j==0){if(ary_Panel[x+1][y-1-1]==Landing || ary_Panel[x+1][y-1-1]==Wall){return 0;}}else if(i==2&&j==1){if(ary_Panel[x+1][y-1]==Landing || ary_Panel[x+1][y-1]==Wall){return 0;}}else if(i==2&&j==2){if(ary_Panel[x+1][y+1-1]==Landing || ary_Panel[x+1][y+1-1]==Wall){return 0;}}}}}return 1;
}//函数名&功能:翻转
//参数:
//返回值:void
//02=00 12=01
//22=02 21=12
//20=22 10=21
//00=20 01=10void Block_Turn(char ary_shapel[][3][3],int nType)
{char kk=0;int i=0,j=0,k=0;char cstr=0x00;cstr=ary_shapel[nType][0][2];ary_shapel[nType][0][2]=ary_shapel[nType][0][0];ary_shapel[nType][0][0]=ary_shapel[nType][2][0];ary_shapel[nType][2][0]=ary_shapel[nType][2][2];ary_shapel[nType][2][2]=cstr;cstr=ary_shapel[nType][1][2];ary_shapel[nType][1][2]=ary_shapel[nType][0][1];ary_shapel[nType][0][1]=ary_shapel[nType][1][0];ary_shapel[nType][1][0]=ary_shapel[nType][2][1];ary_shapel[nType][2][1]=cstr;
}//函数名&功能:key
//参数:
//返回值:voidchar key(int s)
{long t1, t2;char c;t1 = clock();dot2 = clock();while (((t2 - t1) < s) && !kbhit());if (kbhit()) {c = getch();} else {c = 0;}return c;
}//函数名&功能:是否能翻转
//参数:
//返回值:1能翻,0反之int isBlock_Turn(char ary_Panel[][20],char ary_shapel[][3][3],int nType,int x,int y)
{int i=0,j=0;for(i=0;i<3;i++){for(j=0;j<3;j++){if(i==0&&j==0){if(ary_Panel[x-1][y-1]==Landing||ary_Panel[x-1][y-1]==Wall){return 0;}}else if(i==0&&j==1){if(ary_Panel[x-1][y]==Landing||ary_Panel[x-1][y]==Wall){return 0;}}else if(i==0&&j==2){if(ary_Panel[x-1][y+1]==Landing||ary_Panel[x-1][y+1]==Wall){return 0;}}else if(i==1&&j==0){if(ary_Panel[x][y-1]==Landing||ary_Panel[x][y-1]==Wall){return 0;}}else if(i==1&&j==1){if(ary_Panel[x][y]==Landing||ary_Panel[x][y]==Wall){return 0;}}else if(i==1&&j==2){if(ary_Panel[x][y+1]==Landing||ary_Panel[x][y+1]==Wall){return 0;}}else if(i==2&&j==0){if(ary_Panel[x+1][y-1]==Landing||ary_Panel[x+1][y-1]==Wall){return 0;}}else if(i==2&&j==1){if(ary_Panel[x+1][y]==Landing||ary_Panel[x+1][y]==Wall){return 0;}}else if(i==2&&j==2){if(ary_Panel[x+1][y+1]==Landing||ary_Panel[x+1][y+1]==Wall){return 0;}}}}return 1;
}//函数名&功能:right是否发生碰撞
//参数:
//返回值:0发生碰撞int isCollision_right(char ary_Panel[][20],char ary_shapel[][3][3] ,int x, int y,int nType)
{int i=0,j=0;for(i=0;i<3;i++){for(j=0;j<3;j++){if(ary_shapel[nType][i][j]==Solid)//是否是实体{if(i==0&&j==0){if(ary_Panel[x-1][y-1+1]==Landing || ary_Panel[x-1][y-1+1]==Wall){return 0;}}else if(i==0&&j==1){if(ary_Panel[x-1][y+1]==Landing || ary_Panel[x-1][y+1]==Wall){return 0;}}else if(i==0&&j==2){if(ary_Panel[x-1][y+1+1]==Landing || ary_Panel[x-1][y+1+1]==Wall){return 0;}}else if(i==1&&j==0){if(ary_Panel[x][y-1+1]==Landing || ary_Panel[x][y-1+1]==Wall){return 0;}}else if(i==1&&j==1){if(ary_Panel[x][y+1]==Landing || ary_Panel[x][y+1]==Wall){return 0;}}else if(i==1&&j==2){if(ary_Panel[x][y+1+1]==Landing || ary_Panel[x][y+1+1]==Wall){return 0;}}else if(i==2&&j==0){if(ary_Panel[x+1][y-1+1]==Landing || ary_Panel[x+1][y-1+1]==Wall){return 0;}}else if(i==2&&j==1){if(ary_Panel[x+1][y+1]==Landing || ary_Panel[x+1][y+1]==Wall){return 0;}}else if(i==2&&j==2){if(ary_Panel[x+1][y+1+1]==Landing || ary_Panel[x+1][y+1+1]==Wall){return 0;}}}}}return 1;
}//函数名&功能:游戏是否结束
//参数:
//返回值:1为结束,0反之int isGameOver(int x)
{if((x-1)<0){return 1;}return 0;
}//函数名&功能:游戏结束
//参数:
//返回值:1为结束,0反之void GameOver()
{system("cls");printf("Game over");
}//函数名&功能:暂停
//参数:
//返回值:void Stop()
{while(1){int chKey=0;chKey=key(200);if(chKey==0x20){break;}}
}//函数名&功能:可以下落的最大值
//参数:
//返回值:int maxDown(char ary_Panel[][20],char ary_shapel[][3][3],int nType,int x,int y)
{int x1=0;int i=0,j=0,k=0,z=0;while(1){if(ary_shapel[nType][2][0]==2){if(ary_Panel[x+1+1+x1][y-1]==0){i++;x1++;}else{break;}}else{break;}}x1=0;while(1){if(ary_shapel[nType][2][1]==2){if(ary_Panel[x+1+1+x1][y]==0){j++;x1++;}else{break;}}else{break;}}x1=0;while(1){if(ary_shapel[nType][2][2]==2){if(ary_Panel[x+1+1+x1][y+1]==0){k++;x1++;}else{break;}}else{break;}}z=i<=j?i:j;z=z<=k?z:k;return z;
}
#include<stdio.h>
#include"makehead.h"
#include<Windows.h>
#include<conio.h>int main()
{char ary_Panel[23][20]={0};char ary_shapel[ShapeType][3][3] = {//长条{{0, 2, 0},{0, 2, 0},{0, 2, 0}},//田字格{{2, 2, 2},{2, 2, 2},{2, 2, 2}},//L形{{0, 2, 0},{0, 2, 0},{0, 2, 2}},//L2形{{0, 2, 0},{0, 2, 0},{2, 2, 0}},//Z形{{0, 0, 0},{2, 2, 0},{0, 2, 2}},//Z2形{{0, 0, 0},{0, 2, 2},{2, 2, 0}},//T形{{2, 2, 2},{0, 2, 0},{0, 2, 0}},//小M形{{0, 0, 0},{2, 2, 2},{2, 0, 2}},//大M形{{2, 2, 2},{2, 0, 2},{2, 0, 2}},//大I形{{2, 3, 2},{2, 2, 2},{2, 0, 2}}};int i=0,j=0,chKey=0,nType=0;//初始化坐标int x=0,y=10;//方块的初始化坐标char Arry_recordXY[3][3][2]={0};
Gamestrat:i=Makepanel(ary_Panel) ;//显示主界面nType=initializationShape();//初始化方块的形状//nType=3;while(1){BrushsScreen2(Arry_recordXY);ShowShape(ary_shapel,nType,&x,&y,Arry_recordXY);//刷新显示方块边上的区域chKey=key(200);//if(_kbhit())if(chKey){//得到键盘输入//chKey= _getch() ;switch(chKey){case 'w'://翻转if(isBlock_Turn(ary_Panel,ary_shapel,nType,x,y)){Block_Turn(ary_shapel,nType);}break;case 's'://向下if(isNotLandDown(ary_Panel,ary_shapel,nType,&x,&y)){//x=x+maxDown(ary_Panel,ary_shapel,nType,x,y);x++;}break;case 'a'://向左if(isCollision_left(ary_Panel,ary_shapel,x,y,nType)){y--;}break;case 'd'://向右if(isCollision_right(ary_Panel,ary_shapel,x,y,nType)){y++;}break;case 0x20://暂停Stop();break;}chKey=0;}BrushsScreen2(Arry_recordXY);ShowShape(ary_shapel,nType,&x,&y,Arry_recordXY);//刷新显示方块边上的区域if(isNotLandDown(ary_Panel,ary_shapel,nType,&x,&y))//是否落地{x++;//方块自然下落Sleep(100);}else{if(isGameOver(x))//游戏是否失败{GameOver();goto gotoGameOver;}Cls_Arry_recordXY(Arry_recordXY);//清除记录的坐标SetDownLandStatus(ary_Panel,ary_shapel,nType,x,y);//设置落地的状态nType=initializationShape();//初始化方块的形状//nType=3;x=0;y=10;//方块的初始化坐标}RemoveRow(ary_Panel);//消除填满的行}
gotoGameOver:return 0;
}