1、新建“.h”头文件,将“头文件”代码粘贴至其中,2、新建“.c”源文件,将“源代码”代码粘贴到其中。3、新建空白工程,将头文件和源代码添加进去,调试使用。//头文件//1.自定义枚举类型,定义7种形态的游戏方块typedefenumtetris_shape{ZShape=0,SShape,LineShape,TShape,SquareShape,LShape,MirroredLShape}shape;//2.函数声明//(1)操作方块函数intmaxX();//取得当前方块的最大x坐标intminX();//取得当前方块的最小x坐标voidturn_left();//当前方块逆时针旋转90度voidturn_right();intout_of_table();voidtransform();intleftable();intrightable();intdownable();voidmove_left();voidmove_right();//(2)操作游戏桌面的函数intadd_to_table();voidremove_full();//(3)控制游戏函数voidnew_game();voidrun_game();voidnext_shape();intrandom(intseed);//(4)绘图函数voidpaint();voiddraw_table();//(5)其他功能函数voidkey_down(WPARAMwParam);voidresize();voidinitialize();voidfinalize();//(6)回调函数,用来处理Windows消息LRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);//源代码//1.文件包含#includewindows.h#includetime.h#includestdio.h#includetetris.h//2.常量定义#defineAPP_NAMETETRIS#defineAPP_TITLETetrisGame#defineGAMEOVERGAMEOVER#defineSHAPE_COUNT7#defineBLOCK_COUNT4#defineMAX_SPEED5#defineCOLUMS10#defineROWS20#defineREDRGB(255,0,0)#defineYELLOWRGB(255,255,0)#defineGRAYRGB(128,128,128)#defineBLACKRGB(0,0,0)#defineWHITERGB(255,255,255)#defineSTONERGB(192,192,192)#defineCHARS_IN_LINE14#defineSCORESCORE%4d//3.全局变量定义//(1)charscore_char[CHARS_IN_LINE]={0};//(2)char*press_enter=PressEnterkey...;//(3)帮助提示信息char*help[]={pressspaceorupkeytotransformshape.,Pressleftorrightkeytomovershape.,Pressdownkeytospeedup.,Pressenterkeytopausegame.,Enjoyit.:-),0};//(4)枚举游戏的状态enumgame_state{game_start,game_run,game_pause,game_over,}state=game_start;//(5)定义方块的颜色COLORREFshape_color[]={RGB(255,0,0),RGB(0,255,0),RGB(0,0,255),RGB(255,255,0),RGB(0,255,255),RGB(255,0,255),RGB(255,255,255)};//(6)方块的7中类型intshape_coordinate[SHAPE_COUNT][BLOCK_COUNT][2]={{{0,1},{0,0},{-1,0},{-1,1}},{{0,-1},{0,0},{1,0},{1,1}},{{0,-1},{0,0},{0,1},{0,2}},{{-1,0},{0,0},{1,0},{0,1}},{{0,0},{1,0},{0,1},{1,1}},{{-1,-1},{0,-1},{0,0},{0,1}},{{1,-1},{0,-1},{0,0},{0,1}}};//(7)得分intscore=0;//(8)下一个方块shapenext=0;//(9)当前方块shapecurrent=0;//(10)当前方块的每一部分坐标intcurrent_coordinate[4][2]={0};//(11)游戏桌面inttable[ROWS][COLUMS]={0};//(12)当前方块的x坐标intshapex=0;//(13)当前方块的\y坐标intshapey=0;//(14)方块下移速度intspeed=0;//(15)每一帧开始时间clock_tstart=0;//(16)每一帧结束时间clock_tfinish=0;//(17)windows绘图用变量HWNDgameWND;HBITMAPmemBM;HBITMAPmemBMOld;HDCmemDC;RECTclientRC;HBRUSHblackBrush;HBRUSHstoneBrush;HBRUSHshapeBrush[SHAPE_COUNT];HPENgrayPen;HFONTbigFont;HFONTsmallFont;//4.主要处理函数//(1)取最大坐标intmaxX(){inti=0;intx=current_coordinate[i][0];intm=x;for(i=1;iBLOCK_COUNT;i++){x=current_coordinate[i][0];if(mx){m=x;}}returnm;}//(2)取最小坐标intminX(){inti=0;intx=current_coordinate[i][0];intm=x;for(i=1;iBLOCK_COUNT;i++){x=current_coordinate[i][0];if(mx){m=x;}}returnm;}//(3)逆时针转动方块voidturn_left(){inti=0;intx,y;for(i=0;i4;i++){x=current_coordinate[i][0];y=current_coordinate[i][1];current_coordinate[i][0]=y;current_coordinate[i][1]=-x;}}//(4)顺时针旋转方块voidturn_right(){inti=0;intx,y;for(i=0;i4;i++){x=current_coordinate[i][0];y=current_coordinate[i][1];current_coordinate[i][0]=-y;current_coordinate[i][1]=x;}}//(5)检查方块是否越界intout_of_table(){inti=0;intx,y;for(i=0;i4;i++){x=shapex+current_coordinate[i][0];y=shapey+current_coordinate[i][1];if(x0||x(COLUMS-1)||y(ROWS-1)){return1;}if(table[y][x]){return1;}}return0;}//(6)旋转方块voidtransform(){if(current==SquareShape){return;}turn_right();if(out_of_table()){turn_left();}}//(7)判断方块是否向左移动intleftable(){inti=0;intx,y;for(i=0;i4;i++){x=shapex+current_coordinate[i][0];y=shapey+current_coordinate[i][1];if(x=0||table[y][x-1]==1){return0;}}return1;}//(8)判断方块是否向右移动intrightable(){inti=0;intx,y;for(i=0;i4;i++){x=shapex+current_coordinate[i][0];y=shapey+current_coordinate[i][1];if(x=(COLUMS-1)||table[y][x+1]==1){return0;}}return1;}//(9)判断方块是否向下移动intdownable(){inti=0;intx,y;for(i=0;i4;i++){x=shapex+current_coordinate[i][0];y=shapey+current_coordinate[i][1];if(y=(ROWS-1)||table[y+1][x]==1){return0;}}return1;}//(10)向左移动当前方块voidmove_left(){if(leftable()){shapex--;}}//(11)向右移动当前方块voidmove_right(){if(rightable()){shapex++;}}//(12)向下移动当前方块voidmove_down(){if(downable()){shapey++;}else{if(add_to_table()){remove_full();next_shape();}else{state=game_over;}}}//(13)将当前方块固定到桌面上intadd_to_table(){inti=0;intx,y;for(i=0;i4;i++){x=shapex+current_coordinate[i][0];y=shapey+current_coordinate[i][1];if(y0||table[y][x]==1){return0;}table[y][x]=1;}return1;}//(14)删除填满的行voidremove_full(){intc=0;inti,j;for(i=ROWS-1;i0;i--){c=0;for(j=0;jCOLUMS;j++){c+=table[i][j];}if(c==COLUMS){memmove(table[1],table[0],sizeof(int)*COLUMS*i);memset(table[0],0,sizeof(int)*COLUMS);score++;speed=(score/100)%MAX_SPEED;i++;}elseif(c==0){break;}}}//(15)创建新游戏voidnew_game(){memset(table,0,sizeof(int)*COLUMS*ROWS);start=clock();next=random(SHAPE_COUNT);score=0;speed=0;}//(16)运行游戏voidrun_game(){finish=clock();if((finish-start)(MAX_SPEED-speed)*100){move_down();start=clock();InvalidateRect(gameWND,NULL,TRUE);}}//(17)操作当前方块voidnext_shape(){current=next;memcpy(current_coordinate,shape_coordinate[next],sizeof(int)*BLOCK_COUNT*2);shapex=(COLUMS-((maxX(current)-minX(current))))/2;s