您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > 用C语言写俄罗斯方块 源代码
//程序名称:俄罗斯方块//编译环境:VisualC++6.0,EasyX2011惊蛰版//程序编写:krissizhaoh1987@qq.com//最后更新:2010-12-18//#includegraphics.h#includeconio.h#includetime.h///////////////////////////////////////////////定义常量、枚举量、结构体、全局变量/////////////////////////////////////////////#defineWIDTH10//游戏区宽度#defineHEIGHT22//游戏区高度#defineSIZE20//每个游戏区单位的实际像素//定义操作类型enumCTRL{CTRL_ROTATE,//方块旋转CTRL_LEFT,CTRL_RIGHT,CTRL_DOWN,//方块左、右、下移动CTRL_SINK,//方块沉底CTRL_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();//结束游戏CTRLGetControl(bool_onlyresettimer=false);//获取控制命令voidDispatchControl(CTRL_ctrl);//分发控制命令voidNewBlock();//生成新的方块boolCheckBlock(BLOCKINFO_block);//检测指定方块是否可以放下voidDrawBlock(BLOCKINFO_block,DRAW_draw=SHOW);//画方块voidOnRotate();//旋转方块voidOnLeft();//左移方块voidOnRight();//右移方块voidOnDown();//下移方块voidOnSink();//沉底方块///////////////////////////////////////////////函数定义///////////////////////////////////////////////主函数voidmain(){Init();CTRLc;while(true){c=GetControl();DispatchControl(c);//按退出时,显示对话框咨询用户是否退出if(c==CTRL_QUIT){HWNDwnd=GetHWnd();if(MessageBox(wnd,您要退出游戏吗?,提醒,MB_OKCANCEL|MB_ICONQUESTION)==IDOK)Quit();}}}//初始化游戏voidInit(){initgraph(640,480);srand((unsigned)time(NULL));//显示操作说明setfont(14,0,宋体);outtextxy(20,330,操作说明);outtextxy(20,350,上:旋转);outtextxy(20,370,左:左移);outtextxy(20,390,右:右移);outtextxy(20,410,下:下移);outtextxy(20,430,空格:沉底);outtextxy(20,450,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,游戏结束。\n您想重新来一局吗?,游戏结束,MB_YESNO|MB_ICONQUESTION)==IDYES)NewGame();elseQuit();}//获取控制命令CTRLGetControl(bool_onlyresettimer){staticDWORDoldtime=GetTickCount();//重置计时器if(_onlyresettimer){oldtime=GetTickCount();returnCTRL_DOWN;//仅仅为了重置计时器,随便返回一个值}//获取控制值while(true){//如果超时,自动下落一格DWORDnewtime=GetTickCount();if(newtime-oldtime=500){oldtime=newtime;returnCTRL_DOWN;}//如果有按键,返回按键对应的功能if(kbhit()){switch(getch()){case'w':case'W':returnCTRL_ROTATE;case'a':case'A':returnCTRL_LEFT;case'd':case'D':returnCTRL_RIGHT;case's':case'S':returnCTRL_DOWN;case27:returnCTRL_QUIT;case'':returnCTRL_SINK;case0:case0xE0:switch(getch()){case72:returnCTRL_ROTATE;case75:returnCTRL_LEFT;case77:returnCTRL_RIGHT;case80:returnCTRL_DOWN;}}}}}//分发控制命令voidDispatchControl(CTRL_ctrl){switch(_ctrl){caseCTRL_ROTATE:OnRotate();break;caseCTRL_LEFT:OnLeft();break;caseCTRL_RIGHT:OnRight();break;caseCTRL_DOWN:OnDown();break;caseCTRL_SINK:OnSink();break;caseCTRL_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);}//画方块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_CurB
本文标题:用C语言写俄罗斯方块 源代码
链接地址:https://www.777doc.com/doc-4392010 .html