C#实现经典飞行棋游戏的脚本怎么写(云服务器、云主机、高防IP、高防服务器、香港服务器、美国服务器,开发技术)

时间:2024-05-10 09:29:16 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

效果展示

C#实现经典飞行棋游戏的脚本怎么写

主函数

staticvoidMain(string[]args){intw=50;inth=30;ConsoleInit(w,h);E_SceneTypenowSceneType=E_SceneType.Begin;while(true){switch(nowSceneType){caseE_SceneType.Begin:Console.Clear();GameEndOrBegin(w,h,refnowSceneType);break;caseE_SceneType.Game:Console.Clear();GameScene(w,h,refnowSceneType);break;caseE_SceneType.End:Console.Clear();GameEndOrBegin(w,h,refnowSceneType);break;default:break;}}}

场景类型枚举

enumE_SceneType{Begin,Game,End,}

控制台基础设置

staticvoidConsoleInit(intw,inth){//控制台设置Console.CursorVisible=false;Console.SetWindowSize(w,h);Console.SetBufferSize(w,h);}

开始及结束场景逻辑

staticvoidGameEndOrBegin(intw,inth,refE_SceneTypenowSceneType){Console.ForegroundColor=ConsoleColor.White;Console.SetCursorPosition(nowSceneType==E_SceneType.Begin?w/2-3:w/2-4,8);Console.Write(nowSceneType==E_SceneType.Begin?"飞行棋":"游戏结束");//当前选项的编号intcount=0;boolIsOver=false;while(true){Console.SetCursorPosition(nowSceneType==E_SceneType.Begin?w/2-4:w/2-5,11);Console.ForegroundColor=count==0?ConsoleColor.Red:ConsoleColor.White;Console.Write(nowSceneType==E_SceneType.Begin?"游戏开始":"回到主菜单");Console.SetCursorPosition(w/2-4,13);Console.ForegroundColor=count==1?ConsoleColor.Red:ConsoleColor.White;Console.Write("退出游戏");switch(Console.ReadKey(true).Key){caseConsoleKey.W:--count;if(count<0){count=0;}break;caseConsoleKey.S:++count;if(count>1){count=1;}break;caseConsoleKey.J:if(count==0){nowSceneType=nowSceneType==E_SceneType.Begin?E_SceneType.Game:E_SceneType.Begin;IsOver=true;}else{Environment.Exit(0);}break;}if(IsOver)break;}}

游戏场景逻辑

staticvoidGameScene(intw,inth,refE_SceneTypenowSceneType){DrawWall(w,h);Mapmap=newMap(14,3,80);map.Draw();Playerplayer=newPlayer(0,E_Player_Type.Player);Playercomputer=newPlayer(0,E_Player_Type.Computer);DrawPlayer(map,player,computer);while(true){if(PlayerRandomMove(w,h,refplayer,refcomputer,map,refnowSceneType)){break;}if(PlayerRandomMove(w,h,refcomputer,refplayer,map,refnowSceneType)){break;}}}staticboolPlayerRandomMove(intw,inth,refPlayerp,refPlayerotherP,Mapmap,refE_SceneTypenowSceneType){//之后的游戏逻辑//玩家扔色子逻辑//检测输入Console.ReadKey(true);//扔色子的逻辑boolisGameOver=RandomMove(w,h,refp,refotherP,map);//绘制地图map.Draw();//绘制玩家DrawPlayer(map,p,otherP);//判断是否要结束游戏if(isGameOver){//卡住程序让玩家按任意键Console.ReadKey(true);nowSceneType=E_SceneType.End;}returnisGameOver;}

固定打印的信息

staticvoidDrawWall(intw,inth){Console.ForegroundColor=ConsoleColor.Red;//横着的墙for(inti=0;i<w;i+=2){//最上面一行Console.SetCursorPosition(i,0);Console.Write("■");//中间一行Console.SetCursorPosition(i,h-6);Console.Write("■");Console.SetCursorPosition(i,h-11);Console.Write("■");//最下面一行Console.SetCursorPosition(i,h-1);Console.Write("■");}//竖着的墙for(inti=0;i<h;i++){//左边的墙Console.SetCursorPosition(0,i);Console.Write("■");//右边的墙Console.SetCursorPosition(w-2,i);Console.Write("■");}Console.SetCursorPosition(2,h-5);Console.ForegroundColor=ConsoleColor.White;Console.Write("按任意键开始扔色子");Console.SetCursorPosition(2,h-10);Console.Write("□:普通格子");Console.SetCursorPosition(2,h-9);Console.ForegroundColor=ConsoleColor.Blue;Console.Write("■:暂停,一回合不动");Console.SetCursorPosition(22,h-9);Console.ForegroundColor=ConsoleColor.Red;Console.Write("●:炸弹,倒退5格");Console.SetCursorPosition(2,h-8);Console.ForegroundColor=ConsoleColor.White;Console.Write("×:时空隧道,随机倒退,暂停,交换位置");Console.SetCursorPosition(2,h-7);Console.ForegroundColor=ConsoleColor.Cyan;Console.Write("★:玩家");Console.SetCursorPosition(11,h-7);Console.ForegroundColor=ConsoleColor.Magenta;Console.Write("▲:电脑");Console.SetCursorPosition(20,h-7);Console.ForegroundColor=ConsoleColor.Blue;Console.Write("◎:玩家电脑重合");}

格子类型枚举和格子结构体

enumE_Grid_Type{Normal,Boom,Pause,Tunnel,}///<summary>///位置信息结构体///</summary>structVector2{publicintx;publicinty;publicVector2(intx,inty){this.x=x;this.y=y;}}structGrid{//格子的类型publicE_Grid_Type_type;//格子的位置publicVector2pos;//构造函数publicGrid(intx,inty,E_Grid_Typetype){pos.x=x;pos.y=y;_type=type;}//画一个格子publicvoidDraw(){Console.SetCursorPosition(pos.x,pos.y);switch(_type){caseE_Grid_Type.Normal:Console.ForegroundColor=ConsoleColor.White;Console.Write("□");break;caseE_Grid_Type.Boom:Console.ForegroundColor=ConsoleColor.Red;Console.Write("●");break;caseE_Grid_Type.Pause:Console.ForegroundColor=ConsoleColor.Blue;Console.Write("■");break;caseE_Grid_Type.Tunnel:Console.ForegroundColor=ConsoleColor.White;Console.Write("×");break;}}}

地图结构体

structMap{publicGrid[]grids;publicMap(intx,inty,intnum){grids=newGrid[num];intindexX=0;intindexY=0;intstepNum=2;Randomr=newRandom();intrandomNum;for(inti=0;i<num;i++){randomNum=r.Next(0,101);if(randomNum<85||i==0||i==num-1){//普通格子grids[i]._type=E_Grid_Type.Normal;}elseif(randomNum<90&&randomNum>=85){//炸弹grids[i]._type=E_Grid_Type.Boom;}elseif(randomNum<95&&randomNum>=90){//暂停grids[i]._type=E_Grid_Type.Pause;}else{//时空隧道grids[i]._type=E_Grid_Type.Tunnel;}grids[i].pos=newVector2(x,y);if(indexX==10){y+=1;indexY++;if(indexY==2){indexX=0;indexY=0;stepNum=-stepNum;}}else{x+=stepNum;indexX++;}}}publicvoidDraw(){for(inti=0;i<grids.Length;i++){grids[i].Draw();}}}

玩家和电脑结构体

enumE_Player_Type{Player,Computer,}structPlayer{publicE_Player_Typetype;publicintnowIndex;//是否暂停的标识publicboolisPause;publicPlayer(intindex,E_Player_Typetype){nowIndex=index;this.type=type;isPause=false;}publicvoidDraw(MapmapInfo){//从传入的地图中得到格子信息Gridgrid=mapInfo.grids[nowIndex];Console.SetCursorPosition(grid.pos.x,grid.pos.y);switch(type){caseE_Player_Type.Player:Console.ForegroundColor=ConsoleColor.Cyan;Console.Write("★");break;caseE_Player_Type.Computer:Console.ForegroundColor=ConsoleColor.Magenta;Console.Write("▲");break;}}}

绘制玩家

staticvoidDrawPlayer(Mapmap,Playerplayer,Playercomputer){//重合时if(player.nowIndex==computer.nowIndex){//得到重合的位置Gridgrid=map.grids[player.nowIndex];Console.SetCursorPosition(grid.pos.x,grid.pos.y);Console.ForegroundColor=ConsoleColor.DarkGreen;Console.Write("◎");}//不重合时else{player.Draw(map);computer.Draw(map);}}

扔骰子逻辑

//擦除提示的函数staticvoidClearInfo(inth){Console.SetCursorPosition(2,h-5);Console.Write("");Console.SetCursorPosition(2,h-4);Console.Write("");Console.SetCursorPosition(2,h-3);Console.Write("");Console.SetCursorPosition(2,h-2);Console.Write("");}///<summary>///扔色子函数///</summary>>///<paramname="w">窗口的宽</param>///<paramname="h">窗口的高</param>///<paramname="p">扔色子的对象</param>///<paramname="map">地图信息</param>///<returns>默认返回false代表没有结束</returns>staticboolRandomMove(intw,inth,refPlayerp,refPlayerotherP,Mapmap){//擦除之前显示的提示信息ClearInfo(h);//根据扔色子的玩家类型,决定信息的颜色Console.ForegroundColor=p.type==E_Player_Type.Player?ConsoleColor.Cyan:ConsoleColor.Magenta;//扔色子之前判断玩家是否处于暂停状态if(p.isPause){Console.SetCursorPosition(2,h-5);Console.Write("处于暂停状态,{0}需要暂停一回合",p.type==E_Player_Type.Player?"你":"电脑");Console.SetCursorPosition(2,h-4);Console.Write("请按任意键,让{0}开始扔色子",p.type==E_Player_Type.Player?"电脑":"你");//停止暂停p.isPause=false;returnfalse;}//扔色子目的是改变玩家或电脑的位置计算位置的变化//扔色子随机一个1到6的数字,加上去Randomr=newRandom();intrandomNum=r.Next(1,7);p.nowIndex+=randomNum;//打印扔的点数Console.SetCursorPosition(2,h-5);Console.Write("{0}扔出的点数为:{1}",p.type==E_Player_Type.Player?"你":"电脑",randomNum);//首先判断是否到终点了if(p.nowIndex>=map.grids.Length-1){p.nowIndex=map.grids.Length-1;Console.SetCursorPosition(2,h-4);if(p.type==E_Player_Type.Player){Console.Write("恭喜你,率先到达了终点");}else{Console.Write("很遗憾,电脑率先到达了终点");}Console.SetCursorPosition(2,h-3);Console.Write("请按任意键结束");returntrue;}else{//没有到终点就判断当前对象到了一个什么类型的格子Gridgrid=map.grids[p.nowIndex];switch(grid._type){caseE_Grid_Type.Normal:Console.SetCursorPosition(2,h-4);Console.Write("{0}到达了一个安全位置",p.type==E_Player_Type.Player?"你":"电脑");Console.SetCursorPosition(2,h-3);Console.Write("请按任意键,让{0}开始扔色子",p.type==E_Player_Type.Player?"电脑":"你");break;caseE_Grid_Type.Boom:p.nowIndex-=5;if(p.nowIndex<0){p.nowIndex=0;}Console.SetCursorPosition(2,h-4);Console.Write("{0}踩到了炸弹,退后5格",p.type==E_Player_Type.Player?"你":"电脑");Console.SetCursorPosition(2,h-3);Console.Write("请按任意键,让{0}开始扔色子",p.type==E_Player_Type.Player?"电脑":"你");break;caseE_Grid_Type.Pause:p.isPause=true;Console.SetCursorPosition(2,h-4);Console.Write("{0}到达了暂停点,你需要暂停一回合",p.type==E_Player_Type.Player?"你":"电脑");Console.SetCursorPosition(2,h-3);Console.Write("请按任意键,让{0}开始扔色子",p.type==E_Player_Type.Player?"电脑":"你");break;caseE_Grid_Type.Tunnel:Console.SetCursorPosition(2,h-4);Console.Write("{0}踩到了时空隧道",p.type==E_Player_Type.Player?"你":"电脑");//随机randomNum=r.Next(1,91);if(randomNum<=30){//倒退p.nowIndex-=5;if(p.nowIndex<0){p.nowIndex=0;}Console.SetCursorPosition(2,h-5);Console.Write("触发倒退5格");}elseif(randomNum<=60){p.isPause=true;Console.SetCursorPosition(2,h-3);Console.Write("触发暂停一回合");}else{inttmp=p.nowIndex;p.nowIndex=otherP.nowIndex;otherP.nowIndex=tmp;Console.SetCursorPosition(2,h-3);Console.Write("惊喜,双方交换位置");}Console.SetCursorPosition(2,h-2);Console.Write("请按任意键,让{0}开始扔色子",p.type==E_Player_Type.Player?"电脑":"你");break;}}//默认没有结束returnfalse;}
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:C#实现经典飞行棋游戏的脚本怎么写的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Linux volname命令有什么作用下一篇:

8 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18