C++实现俄罗斯方块游戏
本文实例为大家分享了C++实现俄罗斯方块游戏的具体代码,供大家参考,具体内容如下
使用VS2017编译
思路:
1.打印出游戏地图,也就是设计游戏地图类game_interdace,包括设计游戏开始界面的函数,游戏地图界面的函数,游戏结束界面的函数,以及设计一些辅助函数如(设置光标为目标点,改变颜色,隐藏光标)来完成上述设计。
2.设计方块图形类,包括生成图形,清理图形,图形移动,图形停止的处理,图形的消失(实质是得分)等。
#include#include #include #include #include classgame_interdace { public: friendclassGraphic; game_interdace(); voidstart_interface();//游戏开始界面 voidHideCursor();//隐藏光标 intcolor(intc);//改变颜色 voidgotoxy(intx,inty);//设置光标为目标点 voidgotoprintf(intx,inty);//在目标点打印■ voidgotoclear(intx,inty);//消除目标点■ voidmap();//游戏地图界面 boolend_map();//游戏结束界面 private: intgrade; staticintgame_lv; }; intgame_interdace::game_lv{}; game_interdace::game_interdace():grade{} { }//注意这里的花括号 classGraphic { public: classgame_interdaceinterdace; Graphic(); intlocation_x;//用于判断和控制图形位置(x坐标) intlocation_y;//用于判断和控制图形位置(y坐标) intgraph_id;//图形id intnext_graph_id{};//下一图形id intrandom();//生成随机数 voidproduce_graph(intlocation_x,intlocation_y,intid);//生成图形 voidclear_graph(intlocation_x,intlocation_y,intid);//清理图形 voidgraph_moving();//图形移动 boolgraph_no_moving(intlocation_x,intlocation_y,intid);//图形不能超界和不能变形处理 intgraph_change(intid);//按‘w'键时,图形的改变 voidgraph_stop();//图形停止的处理 voidgraph_disppear(intlocation_x,intlocation_y);//图形的消失(实质是得分) boolgame_end();//游戏结束的判定 private: constintgraph[15][8];//记录每个每个图形的各点坐标 intgraph_active_pos[32][26]{};//方块活动地图的各坐标点的状态 constintgraph_with[15];//记录每个每个图形的宽度 intstop_tgraph_top;//记录所有已经下落的方块中的最高点 }; Graphic::Graphic(): graph { //两个值一组分别代表x,y坐标就比如方形图形数据可以这么来看(0,0),(2,0),(0,2),(2,2) //由于每个方块由"■"的宽度为两个字符,所以x坐标成双增长 {0,0,2,0,0,1,2,1},//方形 {2,0,4,0,0,1,2,1},{0,0,0,1,2,1,2,2},//|_ //|形 {0,0,2,0,2,1,4,1},{2,0,0,1,2,1,0,2},// {0,0,2,0,4,0,6,0},{0,0,0,1,0,2,0,3},//条形 {2,0,0,1,2,1,4,1},{0,0,0,1,2,1,0,2},{0,0,2,0,4,0,2,1},{2,0,0,1,2,1,2,2},//T形 {0,0,2,0,0,1,0,2},{0,0,0,1,2,1,4,1},{0,0,2,0,2,1,2,2},{0,0,2,0,4,0,0,1}, },//L形 graph_with{2,3,2,3,2,4,1,3,2,3,2,2,3,2,3}, location_x{14}, location_y{1}, graph_id{5}, stop_tgraph_top{26} { } intmain() { boolaganst{false}; do { game_interdaceinterdace; interdace.HideCursor(); interdace.start_interface(); interdace.map(); Graphicgraph; graph.graph_moving(); if(interdace.end_map()) { aganst=true; system("cls"); } }while(aganst); } voidgame_interdace::start_interface()//游戏开始界面 { color(2); gotoxy(30,2); std::cout<<"游戏说明"; color(3); gotoxy(30,5); std::cout<<"请在英文输入法中输入wsad控制方块"; color(4); gotoxy(30,9); std::cout<<"'w'为变形\n"; gotoxy(30,10); std::cout<<"'s'为快速下落\n"; gotoxy(30,11); std::cout<<"'a'为左移"; gotoxy(30,12); std::cout<<"'d'为右移"; gotoxy(30,14); color(5); std::cout<<"游戏等级"; gotoxy(30,16); color(5); std::cout<<"====================================================="; color(3); gotoxy(30,18); std::cout<<"游戏等级:(1)简单--(2)(困难)--(3)地狱"; gotoxy(30,20); color(5); std::cout<<"====================================================="; gotoxy(30,22); color(7); std::cout<<"等级越高,方块下落的速度越快,加油吧!"; gotoxy(30,24); color(9); std::cout<<"请输入游戏等级(1-3):"; gotoxy(70,24); std::cin>>game_lv; system("cls"); color(7); } voidgame_interdace::HideCursor() { CONSOLE_CURSOR_INFOcursor; cursor.bVisible=FALSE; cursor.dwSize=sizeof(4); HANDLEhandle=GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorInfo(handle,&cursor); } intgame_interdace::color(intc) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c); return0; } voidgame_interdace::gotoxy(intx,inty) { COORDpos; pos.X=x; pos.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos); } voidgame_interdace::gotoprintf(intx,inty) { gotoxy(x,y); printf("■"); } voidgame_interdace::gotoclear(intx,inty) { gotoxy(x,y); printf("");//"■"为两个字符,所以要覆盖两个字符 } voidgame_interdace::map() { intx,y;//边框打印 for(x=0;x<47;x+=2)//从第0列开始到第52列,两个字符一个"■",一共27个”■“ { gotoprintf(x,0); gotoprintf(x,26); } for(y=0;y<27;y++)//从第0行开始到第26行 { gotoprintf(0,y); gotoprintf(32,y); gotoprintf(46,y); }//x两格为一个前进坐标单位,y一格为一个前进单位 } boolgame_interdace::end_map() { charisno{}; system("cls"); gotoprintf(14,6); std::cout<<"游戏结束\n"; std::cout<<"输入【y】键继续游戏\n"; std::cout<<"输入【n】键结束游戏\n"; gotoxy(17,10); std::cin>>isno; if(isno=='y') { returntrue; } else { returnfalse; } } intGraphic::random() { srand((int)time(NULL)); intnumber=rand()%15; returnnumber; } voidGraphic::produce_graph(intlocation_x,intlocation_y,intid) { intX,Y; for(inti=0;i<4;i++) { X=location_x+graph[id][i*2]; Y=location_y+graph[id][i*2+1]; interdace.gotoprintf(X,Y); } } voidGraphic::clear_graph(intlocation_x,intlocation_y,intid) { intX,Y; interdace.color(random()%15+1);//使下落过程中颜色随机变化(有黑色的) for(inti=0;i<4;i++) { X=location_x+graph[id][i*2]; Y=location_y+graph[id][i*2+1]; interdace.gotoclear(X,Y); } } voidGraphic::graph_moving()//按键后,如果符号要求就消除旧图形,打印新图形 { intsign{},sleep{400}; graph_id=random(); next_graph_id=random(); produce_graph(36,3,next_graph_id); while(true) { sign=0; interdace.gotoxy(35,9); std::cout<<"游戏分数:"; interdace.gotoxy(39,11); std::cout< 30) { returntrue; } elsereturnfalse; } return0;//防止出现该路径没有返回值 } intGraphic::graph_change(intid) { switch(id) { case0:id=0;break;//方形 case1:id=2;break;//|_ case2:id=1;break;//| case3:id=4;break;// case4:id=3;break; case5:id=6;break;//条形 case6:id=5;break; case7:id=8;break;//T形 case8:id=9;break; case9:id=10;break; case10:id=7;break; case11:id=12;break;//L形 case12:id=13;break; case13:id=14;break; case14:id=11;break; default: break; } returnid; } voidGraphic::graph_stop() { intX{},Y{}; for(inti=0;i<4;i++) { X=location_x+graph[graph_id][i*2]; Y=location_y+graph[graph_id][i*2+1]; if(graph_active_pos[X][Y+1]==1||Y>=25) { for(inti=0;i<4;i++) { X=location_x+graph[graph_id][i*2]; Y=location_y+graph[graph_id][i*2+1]; graph_active_pos[X][Y]=1; } if(stop_tgraph_top>location_y) { stop_tgraph_top=location_y; } graph_disppear(location_y,Y); location_x=14;//初始化初始坐标 location_y=1; clear_graph(36,3,next_graph_id);//清除图形预览 graph_id=next_graph_id; next_graph_id=random(); produce_graph(36,3,next_graph_id);//生成新的图形预览 produce_graph(location_x,location_y,graph_id);//打印新的初始图形 } } } voidGraphic::graph_disppear(intlocation_y,intY) { intcount{0}; boolisno{false}; for(intline=location_y+graph[graph_id][7];line>location_y;line--) { count=0; isno=false; for(intcolumn=2;column<32;column+=2) { if(graph_active_pos[column][line]==1) { count++; if(count==15) { count=0; interdace.grade++; isno=true; } } if(isno) { for(intls=2;ls<32;ls+=2) { for(inti=line;i>=stop_tgraph_top;i--) { if(graph_active_pos[ls][i]) { interdace.gotoclear(ls,i); } graph_active_pos[ls][i]=graph_active_pos[ls][i-1]; if(graph_active_pos[ls][i]) { interdace.gotoprintf(ls,i); } } } } } } } boolGraphic::game_end() { if(stop_tgraph_top<=1)returntrue; elsereturnfalse; }
更多有趣的经典小游戏实现专题,分享给大家:
C++经典小游戏汇总
python经典小游戏汇总
python俄罗斯方块游戏集合
JavaScript经典游戏玩不停
javascript经典小游戏汇总
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。