您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > C语言版俄罗斯方块源代码
#includegraphics.h#includeconio.h#includetime.h///////////////////////////////////////////////定义常量、枚举量、结构体、全局变量/////////////////////////////////////////////#defineWIDTH10//游戏区宽度#defineHEIGHT22//游戏区高度#defineSIZE20//每个游戏区单位的实际像素//定义操作类型enumCMD{CMD_ROTATE,//方块旋转CMD_LEFT,CMD_RIGHT,CMD_DOWN,//方块左、右、下移动CMD_SINK,//方块沉底CMD_QUIT//退出游戏};//定义绘制方块的方法enumDRAW{SHOW,//显示方块HIDE,//隐藏方块FIX//固定方块};//定义七种俄罗斯方块structBLOCK{WORDdir[4];//方块的四个旋转状态COLORREFcolor;//方块的颜色}g_Blocks[7]={{0x0F00,0x4444,0x0F00,0x4444,RED},//I{0x0660,0x0660,0x0660,0x0660,BLUE},//口{0x4460,0x02E0,0x0622,0x0740,MAGENTA},//L{0x2260,0x0E20,0x0644,0x0470,YELLOW},//反L{0x0C60,0x2640,0x0C60,0x2640,CYAN},//Z{0x0360,0x4620,0x0360,0x4620,GREEN},//反Z{0x4E00,0x4C40,0x0E40,0x4640,BROWN}};//T//定义当前方块、下一个方块的信息structBLOCKINFO{byteid;//方块IDcharx,y;//方块在游戏区中的坐标bytedir:2;//方向}g_CurBlock,g_NextBlock;//定义游戏区BYTEg_World[WIDTH][HEIGHT]={0};///////////////////////////////////////////////函数声明voidInit();//初始化游戏voidQuit();//退出游戏voidNewGame();//开始新游戏voidGameOver();//结束游戏CMDGetCmd();//获取控制命令voidDispatchCmd(CMD_cmd);//分发控制命令voidNewBlock();//生成新的方块boolCheckBlock(BLOCKINFO_block);//检测指定方块是否可以放下voidDrawBlock(BLOCKINFO_block,DRAW_draw=SHOW);//画方块voidOnRotate();//旋转方块voidOnLeft();//左移方块voidOnRight();//右移方块voidOnDown();//下移方块voidOnSink();//沉底方块///////////////////////////////////////////////函数定义//主函数voidmain(){Init();CMDc;while(true){c=GetCmd();DispatchCmd(c);//按退出时,显示对话框咨询用户是否退出if(c==CMD_QUIT){HWNDwnd=GetHWnd();if(MessageBox(wnd,_T(您要退出游戏吗?),_T(提醒),MB_OKCANCEL|MB_ICONQUESTION)==IDOK)Quit();}}}//初始化游戏voidInit(){initgraph(640,480);srand((unsigned)time(NULL));//显示操作说明setfont(14,0,_T(宋体));outtextxy(20,330,_T(操作说明));outtextxy(20,350,_T(上:旋转));outtextxy(20,370,_T(左:左移));outtextxy(20,390,_T(右:右移));outtextxy(20,410,_T(下:下移));outtextxy(20,430,_T(空格:沉底));outtextxy(20,450,_T(ESC:退出));//设置坐标原点setorigin(220,20);//绘制游戏区边界rectangle(-1,-1,WIDTH*SIZE,HEIGHT*SIZE);rectangle((WIDTH+1)*SIZE-1,-1,(WIDTH+5)*SIZE,4*SIZE);//开始新游戏NewGame();}//退出游戏voidQuit(){closegraph();exit(0);}//开始新游戏voidNewGame(){//清空游戏区setfillstyle(BLACK);bar(0,0,WIDTH*SIZE-1,HEIGHT*SIZE-1);ZeroMemory(g_World,WIDTH*HEIGHT);//生成下一个方块g_NextBlock.id=rand()%7;g_NextBlock.dir=rand()%4;g_NextBlock.x=WIDTH+1;g_NextBlock.y=HEIGHT-1;//获取新方块NewBlock();}//结束游戏voidGameOver(){HWNDwnd=GetHWnd();if(MessageBox(wnd,_T(游戏结束。\n您想重新来一局吗?),_T(游戏结束),MB_YESNO|MB_ICONQUESTION)==IDYES)NewGame();elseQuit();}//获取控制命令DWORDm_oldtime;CMDGetCmd(){//获取控制值while(true){//如果超时,自动下落一格DWORDnewtime=GetTickCount();if(newtime-m_oldtime=500){m_oldtime=newtime;returnCMD_DOWN;}//如果有按键,返回按键对应的功能if(kbhit()){switch(getch()){case'w':case'W':returnCMD_ROTATE;case'a':case'A':returnCMD_LEFT;case'd':case'D':returnCMD_RIGHT;case's':case'S':returnCMD_DOWN;case27:returnCMD_QUIT;case'':returnCMD_SINK;case0:case0xE0:switch(getch()){case72:returnCMD_ROTATE;case75:returnCMD_LEFT;case77:returnCMD_RIGHT;case80:returnCMD_DOWN;}}}//延时(降低CPU占用率)Sleep(20);}}//分发控制命令voidDispatchCmd(CMD_cmd){switch(_cmd){caseCMD_ROTATE:OnRotate();break;caseCMD_LEFT:OnLeft();break;caseCMD_RIGHT:OnRight();break;caseCMD_DOWN:OnDown();break;caseCMD_SINK:OnSink();break;caseCMD_QUIT:break;}}//生成新的方块voidNewBlock(){g_CurBlock.id=g_NextBlock.id,g_NextBlock.id=rand()%7;g_CurBlock.dir=g_NextBlock.dir,g_NextBlock.dir=rand()%4;g_CurBlock.x=(WIDTH-4)/2;g_CurBlock.y=HEIGHT+2;//下移新方块直到有局部显示WORDc=g_Blocks[g_CurBlock.id].dir[g_CurBlock.dir];while((c&0xF)==0){g_CurBlock.y--;c=4;}//绘制新方块DrawBlock(g_CurBlock);//绘制下一个方块setfillstyle(BLACK);bar((WIDTH+1)*SIZE,0,(WIDTH+5)*SIZE-1,4*SIZE-1);DrawBlock(g_NextBlock);//设置计时器,用于判断自动下落m_oldtime=GetTickCount();}//画方块voidDrawBlock(BLOCKINFO_block,DRAW_draw){WORDb=g_Blocks[_block.id].dir[_block.dir];intx,y;intcolor=BLACK;switch(_draw){caseSHOW:color=g_Blocks[_block.id].color;break;caseHIDE:color=BLACK;break;caseFIX:color=g_Blocks[_block.id].color/3;break;}setfillstyle(color);for(inti=0;i16;i++){if(b&0x8000){x=_block.x+i%4;y=_block.y-i/4;if(yHEIGHT){if(_draw!=HIDE)bar3d(x*SIZE+2,(HEIGHT-y-1)*SIZE+2,(x+1)*SIZE-4,(HEIGHT-y)*SIZE-4,3,true);elsebar(x*SIZE,(HEIGHT-y-1)*SIZE,(x+1)*SIZE-1,(HEIGHT-y)*SIZE-1);}}b=1;}}//检测指定方块是否可以放下boolCheckBlock(BLOCKINFO_block){WORDb=g_Blocks[_block.id].dir[_block.dir];intx,y;for(inti=0;i16;i++){if(b&0x8000){x=_block.x+i%4;y=_block.y-i/4;if((x0)||(x=WIDTH)||(y0))returnfalse;if((yHEIGHT)&&(g_World[x][y]))returnfalse;}b=1;}returntrue;}//旋转方块voidOnRotate(){//获取可以旋转的x偏移量intdx;BLOCKINFOtmp=g_CurBlock;tmp.dir++;if(CheckBlock(tmp)){dx=0;gotorotate;}tmp.x=g_CurBlock.x-1;if(CheckBlock(tmp)){dx=-1;gotorotate;}tmp.x=g_CurBlock.x+1;if(CheckBlock(tmp)){dx=1;gotorotate;}tmp.x=g_CurBlock.x-2;if(CheckBlock(tmp)){dx=-2;gotorotate;}tmp.x=g_CurBlock.x+2;if(CheckBlock(tmp)){dx=2;gotorotate;}return;rotate://旋转DrawBlock(g_CurBlock,HIDE);g_CurBlock.dir++;g_CurBlock.x+=dx;DrawBlock(g_CurBlock);}//左移方块voidOnLeft(){BLOCKINFOtmp=g_CurBlock;tmp.x--;if(CheckBlock(tmp)){DrawBlock(g_CurBlock,HIDE);g_CurBlock.x--;DrawBlock(g_CurBlock);}}//右移方块voidOnRight(){BLOCKINFOtmp=g_CurBlock;tmp.x++;if(CheckBlock(
本文标题:C语言版俄罗斯方块源代码
链接地址:https://www.777doc.com/doc-3392311 .html