为了正常的体验网站,请在浏览器设置里面开启Javascript功能!
首页 > c语言俄罗斯方块

c语言俄罗斯方块

2018-05-30 22页 doc 43KB 9阅读

用户头像

is_180829

暂无简介

举报
c语言俄罗斯方块c语言俄罗斯方块 #include #include #include #include const int back=34; const int speed[]={0,12,9,6,3,1}; const int number[]={0,2,3,5,7,11,15}; const int col[]={11,15,12,12,13,13,14}; int rank=1; int level=0; //0?íÊ??È??ÊÇÏòÓÒÒ???ÊÇÏò×ó??1?íÊ?Ïò×ó??2?íÊ?ÏòÓÒ i...
c语言俄罗斯方块
c语言俄罗斯方块 #include #include #include #include const int back=34; const int speed[]={0,12,9,6,3,1}; const int number[]={0,2,3,5,7,11,15}; const int col[]={11,15,12,12,13,13,14}; int rank=1; int level=0; //0?íÊ??È??ÊÇÏòÓÒÒ???ÊÇÏò×ó??1?íÊ?Ïò×ó??2?íÊ?ÏòÓÒ int kind; int pri_kind; // 7?óÀà??ÑÕÉ??ê?Ç??µ?ÓÃcol[col_kind]; int revolve=0; // Ðýת×?Ì??ê?Ç struct { int x; int y; }current,before; struct { int x; int y; int color; bool having; }board[15][25]; struct { int vary_x[4]; int vary_y[4]; } vary[]={ {{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, 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}}, }; struct { int sum; int connection_x[5]; int connection_y[5]; }connection[]={ {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; pos.X = x; pos.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } void color(int b) { HANDLE hConsole = GetStdHandle((STD_OUTPUT_HANDLE)) ; SetConsoleTextAttribute(hConsole, b) ; } void PrintGarphy() { color(1593); int i,j; gotoxy(2,2); for(i=0;i<26;i++) printf("?ˆ"); gotoxy(2,28); for(i=0;i<26;i++) printf("?ˆ"); for(i=0;i<26;i++) { gotoxy(2,2+i); puts("?ˆ"); gotoxy(34,2+i); if(i==8||i==16) printf("?ˆ?ˆ?ˆ?ˆ?ˆ?ˆ?ˆ?ˆ?ˆ"); else puts("?ˆ"); gotoxy(52,2+i); puts("?ˆ"); } int t; FILE *fp=fopen("C:\\els.txt","r"); fscanf (fp,"%d",&t); fclose(fp); color(43); gotoxy(37,12); printf("×î?ß?ÍÂ?: %d",t); color(46); gotoxy(37,14); puts("µ?Ç?µÃ?Ö: 0"); color(44); gotoxy(37,16); puts("??Êý "); color(39); gotoxy(37,20); puts(" ?ü ?ý ?û ?ú"); gotoxy(37,22); puts(" ???Õ?ñ ÔÝÍ?"); gotoxy(37,24); puts(" ?? ESC ÍË?ö"); gotoxy(37,26); puts(" ?àÐ?ÈË ÕÅì?"); color(back); for(i=0;i<15;i++) { for(j=0;j<25;j++) { board[i][j].x=4+2*i; board[i][j].y=3+j; board[i][j].having=0; gotoxy(board[i][j].x,board[i][j].y); puts(" "); } } } void GameOver() { color(391); for(int i=24;i>=0;i--) for(int j=0;j<15;j++) { gotoxy(32-j*2,3+i); puts("?}"); Sleep(5); } color(back); for(int j=0;j<25;j++) for(int i=0;i<15;i++) { board[i][j].having=0; gotoxy(4+2*i,j+3); puts("?}"); Sleep(5); } gotoxy(42,16); while(rank--) printf(" "); color(46); gotoxy(47,14); puts(" "); } void GetDepth(int *pdepth) { (*pdepth)=24; for(int j=0;j<4;j++) { int sum=0; int n=(current.x+vary[kind].vary_x[j]-4)/2; int m=current.y+vary[kind].vary_y[j]-3; if(m<0) m=0; for(++m;m<25;m++) if(board[n][m].having==0&&n>=0&&n<15&&m>=0&&m<25) sum++; else break; if(sum<(*pdepth)) (*pdepth)=sum; } } void CheckFull(int *pscore) { int i,j; int sum=0; int s[]={0,1,3,6,10}; for(i=0;i<25;i++) { bool mark=1; for(j=0;j<15;j++) { if(board[j][i].having==0) { mark=0; board[j][i].color=back; } } if(mark) { sum++; for(int t=i;t>0;t--) { gotoxy(4,3+t); for(j=0;j<15;j++) { if(board[j][t-1].having) { color(board[j][t-1].color); printf("?}"); board[j][t].color=board[j][t-1].color; board[j][t].having=1; board[j][t-1].having=0; } else { color(back); printf(" "); board[j][t].color=board[j][t].color; board[j][t].having=0; board[j][t-1].having=0; } } } } } (*pscore)+=s[sum]; color(46); gotoxy(47,14); printf("%d",*pscore); if(*pscore>=140) rank=5; else if(*pscore>=90) rank=4; else if(*pscore>=50) rank=3; else if(*pscore>=20) rank=2; else if(*pscore>=1) rank=1; color(44); gotoxy(42,16); for(i=0;it) { fp=fopen("C:\\els.txt","w"); fprintf (fp,"%d",*pscore); fclose(fp); color(43); gotoxy(47,12); printf("%d",*pscore); } fclose(fp); } void PrintNext(int pkind) { int i; int x=43,y=6; color(34); for(i=0;i<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--; else pkind=0,x-=3; color(col[pri_kind]); for(i=0;i<4;i++) { gotoxy(x+vary[pkind].vary_x[i],y+vary[pkind].vary_y[i]); puts("?}"); } } void CheckBoundary() { int i,n,m; for(i=0;i<4;i++) { m=current.x+vary[kind].vary_x[i]; if(m<4||m>32) { current.x+=(m<4?2:-2); i=0; } } for(i=0;i<4;i++) // ?ÔËÄ?ö???é?Ö?ð?ì?é { m=(current.x+vary[kind].vary_x[i]-4)/2; n=current.y+vary[kind].vary_y[i]-3; if(n<0||n>24||board[m][n].having) { if(level==1) // ×óÒÆ current.x+=2; //??Ô, else if(level==2) // ÓÒÒÆ current.x-=2; //??Ô, level=0; break; } } } void LevelMove() { CheckBoundary(); if(level==0) return; int i; color(back); for(i=0;i<4;i++) { if(current.y+vary[kind].vary_y[i]>2) { gotoxy(before.x+vary[kind].vary_x[i],before.y+vary[kind].vary_y [i]); puts(" "); } } color(col[pri_kind]); for(i=0;i<4;i++) { if(current.y+vary[kind].vary_y[i]>2) { gotoxy(current.x+vary[kind].vary_x[i],current.y+vary[kind].vary _y[i]); puts("?}"); } } before.x=current.x; } void Revolve(int **prev_count) //Ðýתº?Êý { int i; before.x=current.x; before.y=current.y; (**prev_count)++; (**prev_count)%=connection[pri_kind].sum; current.x=current.x+connection[pri_kind].connection_x[**prev_co unt]; current.y=current.y+connection[pri_kind].connection_y[**prev_co unt]; CheckBoundary(); for(i=0;i<4;i++) { int m=(current.x-4+vary[number[pri_kind]+**prev_count].vary_x[i])/2; int n=current.y+vary[number[pri_kind]+**prev_count].vary_y[i]-3; if(board[m][n].having||m>14||m<0||n>24) { current.x=current.x-connection[pri_kind].connection_x[**prev_co unt]; current.y=current.y-connection[pri_kind].connection_y[**prev_co unt]; revolve=0; (**prev_count)--; before.x=current.x; before.y=current.y; return ; } } color(back); for(i=0;i<4;i++) { if(before.y+vary[kind].vary_y[i]>2) { gotoxy(before.x+vary[kind].vary_x[i],before.y+vary[kind].vary_y [i]); puts(" "); } } kind=number[pri_kind]+**prev_count; color(col[pri_kind]); for(i=0;i<4;i++) { if(current.y+vary[kind].vary_y[i]>2) { gotoxy(current.x+vary[kind].vary_x[i],current.y+vary[kind].vary _y[i]); puts("?}"); } } Sleep(speed[rank]); before.x=current.x; before.y=current.y; } void ChoiceDirection(int *prev_count) { int t=50/rank; while(t--) { if(kbhit()) { char c=getch(); switch(c) { case 0x48:// up revolve=1; Revolve(&prev_count); break; case 0x50: //down level=0; return; case 0x4b: //left before.x=current.x; current.x-=2; level=1; LevelMove(); break; case 0x4d: //right before.x=current.x; current.x+=2; level=2; LevelMove(); break; case ' ': _getch(); break; case 27: color(back); exit(1); default:break; } } Sleep(speed[rank]); } } void StraightFall(int depth) { int i; int rev_count=0; before.x=current.x; before.y=current.y; while(GetDepth(&depth),depth--) { ChoiceDirection(&rev_count); GetDepth(&depth); if(depth==0) { revolve=0; before.x=current.x; before.y=current.y; break; } color(back); for(i=0;i<4;i++) { if(before.y+vary[kind].vary_y[i]>2) { gotoxy(before.x+vary[kind].vary_x[i],before.y+vary[kind].vary_y [i]); puts(" "); } } current.y++; color(col[pri_kind]); for(i=0;i<4;i++) { if(current.y+vary[kind].vary_y[i]>2) { gotoxy(current.x+vary[kind].vary_x[i],current.y+vary[kind].vary _y[i]); puts("?}"); } } revolve=0; before.x=current.x; before.y=current.y; } ChoiceDirection(&rev_count); if(level||revolve) { level=0; revolve=0; ChoiceDirection(&rev_count); StraightFall(depth); //µÝ?éµ?Óà } } int main() { srand((unsigned long)time(0)); system("mode con cols=56 lines=30"); system("color 24"); int score=0; FILE *fp=fopen("C:\\els.txt","r"); if(fp==NULL) { fp=fopen("C:\\els.txt","w"); fprintf (fp,"%d",score); fclose(fp); } PrintGarphy(); kind=rand()%7; int depth; while(1) { int pkind=rand()%7; PrintNext(pkind); current.x=18; current.y=2; // ???é?õÊ??öÏÖµÄ×ø?ê GetDepth(&depth); // ?õÊ?Éî?È if(depth==0) { GameOver(); score=0; rank=1; } pri_kind=kind; if(kind==6) kind=15,current.x-=2; else if(kind==5) kind=11; else if(kind==4) kind=7; else if(kind==3) kind=5; else if(kind==2) kind=3,current.x+=2; else if(kind==1) kind=2; else kind=0,current.x-=2; StraightFall(depth); for(int i=0;i<4;i++) { int m=(current.x-4+vary[kind].vary_x[i])/2; int n=current.y-3+vary[kind].vary_y[i]; board[m][n].having=1; board[m][n].color=col[pri_kind]; } CheckFull(&score); kind=pkind; } return 0; }
/
本文档为【c语言俄罗斯方块】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索