2022年俄罗斯方块c++源代码[] .pdf
第一个 Cpp #include #include #include #include #include #pragma comment(lib,winmm.lib) /播放背景音乐的头文件#include colorConsole.h #include #define SQUARE_COLOR BACKGROUD_BLUE|BACKGROUD_RED| BACKGROUD_INTENSITY /背景颜色#define SQUARE_COLOR FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY /方块的颜色#define up 72 #define down 80 #define left 75 #define right 77 #define esc 27 #define MAPW 15 /地图的宽度#define MAPH 25 /地图的高度void initiate1(); int * build(); /创建方块/初始化工作BOOL isavailable(int a,int x,int y,int w,int h); /判定是否能放下void drawblocks(int a,int w,int h,int x,int y,WORD wColors,int nColors); void delete_cache(); /清除键盘缓冲区void revolve(int a4,int w,int h,int *x,int y); /转动方块void pro(); void end(); void delete_blocks(int *a,int w,int h,int x,int y); void gameover(); void deletefull_line(int mMAPW,int row,int w,int h); /消除一行int dx=30,dy=5; /屏幕上的偏移量int score=0,level=0; int mapMAPHMAPW; int a144=1,1,1,1; int a244=0,1,1,1,1; int a344=1,1,0,1,1; int a444=0,0,1,1,1,1; int a544=0,1,1,1,1; int a644=1,1,1,1; int a744=1,1,1,1; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 11 页 - - - - - - - - - int a44; int main() HANDLE handle; handle=initiate(); WORD wColors1=FOREGROUND_BLUE| FOREGROUND_GREEN|FOREGROUND_INTENSITY ; while(1) sndPlaySound(ResourceJust Dance.wav,SND_LOOP|SND_ASYNC);/用异步方式播放音乐,PlaySound 函数在开始播放后立即返回system(CLS); int n=0; printf( 目录 n1.开始游戏 n2.退出游戏 nnn); scanf(%d,&n); switch(n) case 1: system(CLS); textout(handle,22,6,wColors+2,1, 请选择游戏等级:); textout(handle,32,8,wColors+2,1,1. 初级 ); textout(handle,32,10,wColors+2,1,2. 中级 ); textout(handle,32,12,wColors+2,1,3. 高级 ); while(1) char choice; choice=_getch(); if(choice=1) textout(handle,22,6,wColors+2,1, 开始游戏,初级); textout(handle,32,8,wColors+2,1, ); textout(handle,32,10,wColors+2,1, ); textout(handle,32,12,wColors+2,1, ); level=0,score=0; Sleep(2000); textout(handle,22,6,wColors+2,1, ); break; else if(choice=2) textout(handle,22,6,wColors+2,1, 开始游戏,中级); textout(handle,32,8,wColors+2,1, ); textout(handle,32,10,wColors+2,1, ); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 11 页 - - - - - - - - - textout(handle,32,12,wColors+2,1, ); level=2,score=20; Sleep(2000); textout(handle,22,6,wColors+2,1, ); break; else if(choice=3) textout(handle,22,6,wColors+2,1, 开始游戏,高级); textout(handle,32,8,wColors+2,1, ); textout(handle,32,10,wColors+2,1, ); textout(handle,32,12,wColors+2,1, ); level=4,score=40; Sleep(2000); textout(handle,22,6,wColors+2,1, ); break; else if(choice!=1&choice!=2&choice!=3) continue; pro(); break; case 2: return 0; default: printf( 错误,按键继续); while(!_kbhit(); void pro() /游戏主题 initiate1(); int *b=NULL; b=build(); /创建方块int sign,blank,x,y; while(1) for(int i=0;i4;i+) /复制方块for(int j=0;j4;j+) if(aij=*(b+i*4+j) blank=i; y=1-blank;x=4; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 11 页 - - - - - - - - - delete_blocks(&a00,4,4,16,10); b=build(); HANDLE handle; handle=initiate(); WORD wColors1=FOREGROUND_BLUE| FOREGROUND_GREEN|FOREGROUND_INTENSITY ; drawblocks(b,4,4,16,10,wColors,1); wColors0=SQUARE_COLOR; drawblocks(&a00,4,4,x,y,wColors,1); delete_cache(); char string5; wColors0=FOREGROUND_RED| FOREGROUND_GREEN|FOREGROUND_INTENSITY; textout(handle,dx-10,8+dy,wColors,1,itoa(score,string,10); textout(handle,dx-10,14+dy,wColors,1,itoa(level,string,10); sign=1; while(sign) int delay=0,max_delay=100-10*level; /延迟量while(delaymax_delay) if(_kbhit() /用 if 避免按住键使方块卡住 int draw=0; int key=_getch(); switch (key) case up: delete_blocks(&a00,4,4,x,y); revolve(a,4,4,&x,y); draw=1; break; case down: delay=max_delay; break; case left: if(isavailable(&a00,x-1,y,4,4) delete_blocks(&a00,4,4,x,y); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 11 页 - - - - - - - - - x-; draw=1; break; case right: if(isavailable(&a00,x+1,y,4,4) delete_blocks(&a00,4,4,x,y); x+; draw=1; break; case 32:/32 是空格键的ASCII 码,按空格键暂停while(1) textout(handle,dx,-2+dy,wColors,1,Press any key to continue); Sleep(200); textout(handle,dx,-2+dy,wColors,1, ); Sleep(200); if(_kbhit() draw=1; break; break; case esc:/ 按键退出游戏exit(EXIT_SUCCESS); if(draw) HANDLE handle; handle=initiate(); WORD wColors1=SQUARE_COLOR; drawblocks(&a00,4,4,x,y,wColors,1); draw=0; _sleep(5);delay+; if(isavailable(&a00,x,y+1,4,4) /是否能下移 delete_blocks(&a00,4,4,x,y); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 11 页 - - - - - - - - - y+; HANDLE handle; handle=initiate(); WORD wColors1=SQUARE_COLOR; drawblocks(&a00,4,4,x,y,wColors,1); else sign=0; /标记,使跳出while(sign) 循环,产生新方块if(y=1) system(CLS); HANDLE handle; handle=initiate(); WORD wColors1=FOREGROUND_RED| FOREGROUND_GREEN; textout(handle,4+dx,6+dy,wColors,1,GAME OVER!); textout(handle,4+dx,8+dy,wColors,1, 分数: ); textout(handle,10+dx,8+dy,wColors,1,itoa(score,string,10); textout(handle,4+dx,10+dy,wColors,1, 制作者: *); delete_cache(); exit(EXIT_SUCCESS); /是否结束for(int i=0;i4;i+) /放下方块for(int j=0;j4;j+) if(aij&(i+y)MAPH-1)&(j+x)MAPW-1) mapi+yj+x=aij; int full,k=0; for( i=y;imin(y+4,MAPH-1);i+) full=1; for(int j=1;j14;j+) if(!mapij) full=0; if(full) /消掉一行 deletefull_line(map,i,MAPW,MAPH); k+; score=score+k; level=min(score/10,9); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 11 页 - - - - - - - - - void initiate1() /初始化 int i; for(i=0;i25;i+) mapi0=-2; mapi14=-2; for(i=0;i15;i+) map0i=-1; map24i=-1; map00=-3; map014=-3; map240=-3; map2414=-3; HANDLE handle; handle=initiate(); WORD wColors1=FOREGROUND_GREEN| FOREGROUND_BLUE|FOREGROUND_INTENSITY; textout(handle,dx-10,6+dy,wColors,1,SCORE); textout(handle,dx-10,12+dy,wColors,1,LEVEL); textout(handle,32+dx,8+dy,wColors,1,NEXT); wColors0=FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY; drawblocks(&map00,15,25,0,0,wColors,1); textout(handle,dx,dy,wColors,1, ); wColors0=FOREGROUND_BLUE| FOREGROUND_GREEN|FOREGROUND_INTENSITY; textout(handle,dx+8,dy+5,wColors,1, 按任意键开始); wColors0=FOREGROUND_BLUE|FOREGROUND_RED|FOREGROUND_INTENSITY; textout(handle,dx+7,dy-3,wColors,1, 制作者 :*); int x=_getch(); srand(time(NULL); textout(handle,dx+8,dy+5,wColors,1, ); int * build() /创建方块 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 11 页 - - - - - - - - - int * a=NULL; int c=rand()%7; switch(c) case 0: a=&a100;break; case 1: a=&a200;break; case 2: a=&a300;break; case 3: a=&a400;break; case 4: a=&a500;break; case 5: a=&a600;break; case 6: a=&a700;break; return a; void drawblocks(int a,int w,int h,int x,int y,WORD wColors,int nColors) /画出方块 HANDLE handle; handle = initiate(); int temp; for(int i=0;ih;i+) for(int j=0;j0) if(temp=-3) textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,); _sleep(30); else if(temp=-2) textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors, );_sleep(30); else if(temp=1) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 11 页 - - - - - - - - - textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,); else if(temp=-1) textout(handle,2*(x+j)+dx ,y+i+dy,wColors,nColors, );_sleep(30); void delete_cache() /清除缓冲区 while(_kbhit() _getch(); void delete_blocks(int *a,int w,int h,int x,int y) /覆盖方块 HANDLE handle; handle=initiate(); WORD wColors1=SQUARE_COLOR; for(int i=0;ih;i+) for(int j=0;j0) textout(handle,2*(x+j)+dx,y+i+dy,wColors,1, ); void revolve(int a4,int w,int h,int *x,int y) /转动方块 int b44=0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0; int sign=0,line=0; for(int i=h-1;i=0;i-) for(int j=0;jw;j+) if(aij) bjline=aij; sign=1; if(sign) line+; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 11 页 - - - - - - - - - sign=0; for(i=0;i4;i+) if(isavailable(&b00,*x-i,y,w,h) *x-=i; for(int k=0;kh;k+) for(int j=0;j1;i-) delete_blocks(&mi1,MAPW-2,1,1,i); for(int j=1;jMAPW-1;j+) mij=mi-1j; drawblocks(&mi1,MAPW-2,1,1,i,wColors,1); for(i=1;iMAPW-1;i+) m1i=0; BOOL isavailable(int a,int x,int y,int w,int h) for(int i=max(y,1);iy+h;i+) for(int j=x;jx+w;j+) if(mapij&aw*(i-y)+j-x) return 0; return 1; 第二个 cpp: 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 11 页 - - - - - - - - - #include colorConsole.h HANDLE initiate() HANDLE hOutput; hOutput = GetStdHandle(STD_OUTPUT_HANDLE); return hOutput; BOOL textout(HANDLE hOutput,int x,int y,WORD wColors,int nColors,LPTSTR lpszString) DWORD cWritten; BOOL fSuccess; COORD coord; coord.X = x; / start at first cell coord.Y = y; / of first row fSuccess = WriteConsoleOutputCharacter( hOutput, / screen buffer handle lpszString, / pointer to source string lstrlen(lpszString), / length of string coord, / first cell to write to &cWritten); / actual number written if (! fSuccess) couterror:WriteConsoleOutputCharacterendl; for (;fSuccess & coord.X lstrlen(lpszString)+x; coord.X += nColors) fSuccess = WriteConsoleOutputAttribute( hOutput, / screen buffer handle wColors, / pointer to source string nColors, / length of string coord, / first cell to write to &cWritten); / actual number written if (! fSuccess) couterror:WriteConsoleOutputAttributeendl; return 0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 11 页 - - - - - - - - -