小女也爱c#(3)--俄罗斯方块练习数组
生活随笔
收集整理的這篇文章主要介紹了
小女也爱c#(3)--俄罗斯方块练习数组
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
學完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; }轉載于:https://www.cnblogs.com/baihelianzi/archive/2011/09/06/2169169.html
總結
以上是生活随笔為你收集整理的小女也爱c#(3)--俄罗斯方块练习数组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到井和水是什么意思
- 下一篇: 梦到撞人了有什么兆头