C#实现飞行棋(Winform)
程序员文章站
2022-06-30 10:02:45
本文实例为大家分享了c#实现飞行棋的具体代码,供大家参考,具体内容如下基于winform框架写的不足之处请大佬指教using system;using system.drawing;using sys...
本文实例为大家分享了c#实现飞行棋的具体代码,供大家参考,具体内容如下
基于winform框架写的
不足之处请大佬指教
using system; using system.drawing; using system.windows.forms; namespace 飞行棋 { public partial class form1 : form { public form1() { initializecomponent(); } //创建一个数组装游戏地板 int[] maplist = new int[390]; //创建装图片的数组 picturebox[] mappic = new picturebox[390]; //创建路的数组 int[] road = new int[100]; //创建地图 panel map = new panel(); int size = 30; //创建骰子 picturebox dice = new picturebox(); //初始位置的停放区域红色的 panel plan1 = new panel(); //初始位置的停放区域绿色的 panel plan2 = new panel(); private void form1_load(object sender, eventargs e) { this.size = new size(1200, 600); this.formborderstyle = formborderstyle.fixedsingle; this.location = new point(250, 100); this.backcolor = color.wheat; map.size = new size(30*size, 13*size); map.borderstyle = borderstyle.fixedsingle; map.location = new point(40,160); this.controls.add(map); //显示图片的方法 init(); plan1.size = new size(100, 100); plan1.location = new point(map.left, map.top - 120); plan1.backgroundimage = image.fromfile("../../img/circle.png"); plan1.backgroundimagelayout = imagelayout.stretch; this.controls.add(plan1); plan2.size = new size(100, 100); plan2.location = new point(map.left+120 ,map.top - 120); plan2.backgroundimage = image.fromfile("../../img/circle.png"); plan2.backgroundimagelayout = imagelayout.stretch; this.controls.add(plan2); picturebox redplayer = new picturebox(); redplayer.size = new size(80, 80); redplayer.image = image.fromfile("../../img/red.png"); redplayer.location = new point(10, 10); redplayer.sizemode = pictureboxsizemode.stretchimage; plan1.controls.add(redplayer); picturebox greenplayer = new picturebox(); greenplayer.size = new size(80, 80); greenplayer.image = image.fromfile("../../img/green.png"); greenplayer.location = new point(10, 10); greenplayer.sizemode = pictureboxsizemode.stretchimage; plan2.controls.add(greenplayer); //创建对话框 tall.size = new size(200, 500); tall.location = new point(map.right + 20, 50); tall.readonly = true; this.controls.add(tall); //玩家一: name.size = new size(100, 30); name.location = new point(45,10); this.controls.add(name); //玩家二: names.size = new size(100, 30); names.location = new point(160,10 ); this.controls.add(names); btn.location = new point(300,10); btn.text = "开始游戏"; btn.size = new size(100, 30); btn.backcolor = color.pink; btn.click += btn_click; this.controls.add(btn); //骰子 dice.size = new size(80, 80); dice.image = image.fromfile("../../img/roll.png"); dice.location = new point(map.right-160,map.top-120); dice.sizemode = pictureboxsizemode.stretchimage; this.controls.add(dice); dice.mouseclick += dice_mouseclick; } private void btn_click(object sender, eventargs e) { //创建初始的弹框 tall(string.format("请两人投掷骰子,{0}先手,点大的先走",name.text)); playname[0] = name.text; playname[1] = names.text; tall.focus(); } textbox name = new textbox(); textbox names = new textbox(); button btn = new button(); //创键记录的对话框 richtextbox tall = new richtextbox(); //创建随机 random r = new random(); // 轮流投掷骰子 0代表红方 1代表绿方 bool[] playstart = new bool[2] { true, false }; //记录双方当前的位置 int[] playposition = new int[2] { -1, -1 }; int[] playstand = new int[2] { -1, -1 }; // 记录骰子的点数 int[] shaizi = new int[2]; string[] playname = new string[2]; //数组存储点数 int[] num = new int[2] ; //1.轮流投掷筛子决定谁先出门 private void dice_mouseclick(object sender, mouseeventargs e) { playdice(); playgame(); } private void playdice() { //红方投掷 红方为true 投掷完之后变为false 绿方投掷 if (playstart[0]) { shaizi[0] = r.next(1,7); tall(string.format("{1}投掷出{0}点", shaizi[0],playname[0])); playstart[0] = !playstart[0]; } else { playstart[0] = !playstart[0]; } //绿方投掷 绿方为false 绿方投掷完之后变为true .轮到红方投掷 if (playstart[1]) { shaizi[1] = r.next(1, 7); tall(string.format("{1}投掷出{0}点", shaizi[1],playname[1])); playstart[1] = !playstart[1]; } else { playstart[1] = !playstart[1]; } } //决定游戏谁先手 private void outdoor() { //红方为true 绿方为false的时候一回合完了 一回合完之后对应的骰子数都不为0 if (playstart[0]&&!playstart[1]&&(shaizi[0]!=0||shaizi[1]!=0)) { if (shaizi[0] == shaizi[1]) { tall("双方点数相同,请重新投掷"); } else { //第二回合 st = false; if (shaizi[0] > shaizi[1]) { tall(string.format("{0}点数较大,先行一步,{0}投掷",name.text)); playstart[0] = true; playstart[1] = false; } if (shaizi[0] < shaizi[1]) { tall(string.format("{0}点数较大,先行一步,{0}投掷", names.text)); playstart[0] = false; playstart[1] = true; } } } } bool st = true; //控制游戏谁先走 private void playgame() { //判断游戏刚开始时候比点数先行 if (st == true) { outdoor(); //都为false的时候绿方投掷 if (!playstart[0]&&playstart[1]) { tall(string.format("请{0}投掷",names.text)); }//都为true的时候红方投掷 else if(playstart[0]&&!playstart[1]) { tall(string.format("请{0}投掷!",name.text)); } } else { if (playstart[0] && !playstart[1])//如果绿方大, 绿方为true { playreturn(1); }//红方 else if (!playstart[0] && playstart[1]) { playreturn(0); } } } /// <summary> /// 双方轮流游戏 /// </summary> /// 传入参数index0为红方 1为绿方 bool[] re = new bool[2] { false, false }; private void playreturn(int index) { //都没出门 if (playposition[index] == -1) { switch (shaizi[index]) { case 2: case 4: tall(string.format("{0}可以起步", playname[index])); playposition[index] = 0; playstand[index] = 0; //如果两个位置相等 if (playposition[1] == playposition[0]) { mappic[road[playposition[index]]].image = imagelist1.images[2]; } else { mappic[road[playposition[index]]].image = imagelist1.images[index]; } break; case 6: playstart[index] = true; playstart[1 - index] = false; tall(string.format("{0}可以起步", playname[index])); playposition[index] = 0; playstand[index] = 0; if (playposition[1] == playposition[0]) { mappic[road[playposition[index]]].image = imagelist1.images[2]; } else { mappic[road[playposition[index]]].image = imagelist1.images[index]; } tall(string.format("请{0}投掷骰子", playname[index])); break; default: tall(string.format("很遗憾,{0}投掷出{1}点无法起步,轮到{2}投掷", playname[index], shaizi[index], playname[1 - index])); break; } if (playposition[0] != -1) { plan1.controls.clear(); } if (playposition[1] != -1) { plan2.controls.clear(); } } else { //改变位置之前记录好之前的位置 playstand[index] = playposition[index]; playposition[index] += shaizi[index]; if (playposition[index] >= 99) { messagebox.show(playname[index] + "获胜"); playposition[index] = 99; //改变图片 change(index); return; } tall(string.format("{0}移动了{1}步", playname[index], shaizi[index])); //改变图片 change(index); //判断移动完成之后的位置是如何 if (playposition[index] == playposition[1 - index]) { playposition[1 - index] = 0; playstand[1 - index] = playposition[1 - index]; tall(string.format("厉害!{0}精准的将{1}踩回原点,{0}当前的位置是{2},{1}当前的位置是{3},", playname[index], playname[1 - index], playposition[index], playposition[1 - index])); mappic[road[playposition[index]]].image = imagelist1.images[index]; mappic[road[playposition[1 - index]]].image = imagelist1.images[1 - index]; tall(string.format("{0}开始投掷", playname[1 - index])); } switch (maplist[road[playposition[index]]]) { case 1: tall(string.format("{0}安全到达!当前位置是{1}", playname[index], playposition[index])); tall(string.format("{0}开始投掷!", playname[1-index])); break; case 2: tall(string.format("很不幸,{0}踩中了香蕉皮,后退6步,当前位置是{1}", playname[index], playposition[index])); playstand[index] = playposition[index]; playposition[index] -= 6; change(index); /*tall(string.format("{0}当前位置是{1}", playname[index], playposition[index]));*/ tall(string.format("{0}开始投掷", playname[1 - index])); break; case 3: tall(string.format("恭喜!{0}踩中时空隧道,前进6步!当前位置是{1}", playname[index], playposition[index])); playstand[index] = playposition[index]; playposition[index] += 6; change(index); /*tall(string.format("{0}当前位置是{1}", playname[index], playposition[index]));*/ tall(string.format("{0}开始投掷", playname[1 - index])); break; case 4: tall(string.format("好可惜,{0}踩中了陷阱,暂停一回合", playname[index])); re[index] = true; re[1 - index] =false; break; case 5: tall(string.format("真好,{0}踩中幸运星,在玩一回合!当前位置是{1}", playname[index], playposition[index])); playstart[index] = true; playstart[1 - index] = false; tall(string.format("{0}继续投掷!", playname[index])); break; case 6: tall(string.format("真好!{0}踩中秘籍,请选择措施!当前位置是{1}", playname[index], playposition[index])); dialogresult result = messagebox.show("是否与对方更换位置!", "移魂大法", messageboxbuttons.yesno); if (result == dialogresult.yes) { int temp = playposition[index]; playposition[index] = playposition[1 - index]; playposition[1 - index] = temp; playstand[index] = playposition[index]; playstand[1 - index] = playposition[1 - index]; mappic[road[playposition[index]]].image = imagelist1.images[index]; mappic[road[playposition[1 - index]]].image = imagelist1.images[1 - index]; } tall(string.format("{0}当前位置是{1},{2}的位置是{3}", playname[index], playposition[index], playname[1 - index], playposition[1 - index])); tall(string.format("{0}开始投掷。", playname[1 - index])); break; case 7: tall(string.format("幸运!{0}获得手枪,可选择击退对方3步!当前位置是{1}", playname[index], playposition[index])); dialogresult res = messagebox.show("是否选择击退对方三步!", "手枪!", messageboxbuttons.yesno); if (res == dialogresult.yes) { playstand[1 - index] = playposition[1 - index]; playposition[1 - index] -= 3; mappic[road[playposition[1 - index]]].image = imagelist1.images[1 - index]; change(1 - index); } /* tall(string.format("{0}被击退对方3步!当前位置是{1}", playname[1 - index], playposition[1 - index]));*/ tall(string.format("{0}开始投掷。", playname[1 - index])); break; default: break; } if (re[index] && !re[1 - index]) { playstart[index] = true; playstart[1 - index] = false; re[index] = false; re[1 - index] = false; } } } private void change( int index) { //如果移动完之后再同一个位置 if (playposition[1] == playposition[0]) { mappic[road[playposition[index]]].image = imagelist1.images[2]; } else {//移动完成之后显示对应玩家的图片 mappic[road[playposition[index]]].image = imagelist1.images[index]; } //原本位置图片的显示,如果两人在同一个位置站着,并且都在路上自己离开之后,留下对方的图片在原地在起点的时候 if (playstand[0]==playstand[1]&&playstand[0]!=-1&&playstand[1]!=-1&&playposition[1-index]==0) { mappic[road[playstand[index]]].image = imagelist1.images[1 - index]; mappic[road[playposition[index]]].image = imagelist1.images[ index]; } else //如果两人不再同一位置判断之前的脚下是什么 { switch (maplist[road[playstand[index]]]) { //整个地图的图片 case 0: mappic[road[playstand[index]]].image = image.fromfile("../../img/water.gif"); break; //游戏区域的路 case 1: mappic[road[playstand[index]]].image = image.fromfile("../../img/grass.png"); break; case 2: mappic[road[playstand[index]]].image = image.fromfile("../../img/sk.jpg"); break; case 3: mappic[road[playstand[index]]].image = image.fromfile("../../img/xj.jpg"); break; case 4: mappic[road[playstand[index]]].image = image.fromfile("../../img/xianjing.jpg"); break; case 5: mappic[road[playstand[index]]].image = image.fromfile("../../img/xx.jpg"); break; case 6: mappic[road[playstand[index]]].image = image.fromfile("../../img/jh.jpg"); break; case 7: mappic[road[playstand[index]]].image = image.fromfile("../../img/sq.jpg"); break; case 10: mappic[road[playstand[index]]].image = image.fromfile("../../img/start.png"); break; case 11: mappic[road[playstand[index]]].image = image.fromfile("../../img/end.bmp"); break; } } } void tall(string str) { messagebox.show(str); tall.appendtext(str+"\r\n"); } //创建一个显示所有图片的方法 void init() { //先调用地图的 createmap(); //在调用道具的 先有地图再有道具 creategear(); for (int i = 0; i < maplist.length; i++) { //创建图片每循环一次创建一个 picturebox pic = new picturebox(); //图片的大小等于30 pic.size = new size(size, size); //判断maplist索引对应的东西 switch (maplist[i]) { //整个地图的图片 case 0: pic.image = image.fromfile("../../img/water.gif"); break; //游戏区域的路 case 1: pic.image = image.fromfile("../../img/grass.png"); break; case 2: pic.image = image.fromfile("../../img/sk.jpg"); break; case 3: pic.image = image.fromfile("../../img/xj.jpg"); break; case 4: pic.image = image.fromfile("../../img/xianjing.jpg"); break; case 5: pic.image = image.fromfile("../../img/xx.jpg"); break; case 6: pic.image = image.fromfile("../../img/jh.jpg"); break; case 7: pic.image = image.fromfile("../../img/sq.jpg"); break; case 10: pic.image = image.fromfile("../../img/start.png"); break; case 11: pic.image = image.fromfile("../../img/end.bmp"); break; } //拉伸图片 pic.sizemode = pictureboxsizemode.stretchimage; mappic[i] = pic; //算出图片的坐标 pic.left = i % 30 * size; pic.top = i / 30 * size; map.controls.add(pic); } } //给整个地图添加图片 void createmap() { //调用铺路的方法 createroad(); for (int i = 0; i < road.length; i++) { maplist[road[i]] = 1; } //起始图片的索引位置 maplist[0] = 10; //结束图片对应的索引位置 maplist[maplist.length - 1] = 11; } //算出路怎么铺 void createroad() { //111111 // 1 //111111 //1 //111111 //第一行铺的路30个 for (int i = 0; i < 30; i++) { road[i] = i; } //第2个列的路 for (int i = 30; i <= 35; i++) { road[i] = road[i - 1] + 30; } //第三个路 for (int i = 36; i <65; i++) { road[i] = road[i - 1] - 1; } //第4列的路 for (int i = 65; i <=70; i++) { road[i] = road[i - 1] + 30; } //第五行的数 for (int i =71; i <100; i++) { road[i] = road[i - 1] + 1; } } //定义道具的数组 int[] back = { 7, 27, 42, 62, 73, 96 }; int[] forword = { 10, 25, 33, 65, 80, 88 }; int[] stop = { 3, 20, 35, 50, 60, 70, 90 }; int[] star = { 5, 28, 45, 71, 85 }; int[] change = { 4, 55, 75, 98 }; int[] gun = { 11, 32, 66, 83 }; //定义一个道具放置位置的方法 void creategear() { for (int i = 0; i < back.length; i++) { //将地图对应的路然后将索引换成对应的道具 maplist[road[back[i]]] = 2; } for (int i = 0; i < forword.length; i++) { maplist[road[forword[i]]] = 3; } for (int i = 0; i < stop.length; i++) { maplist[road[stop[i]]] = 4; } for (int i = 0; i <star.length; i++) { maplist[road[star[i]]] = 5; } for (int i = 0; i < change.length; i++) { maplist[road[change[i]]] = 6; } for (int i = 0; i < gun.length; i++) { maplist[road[gun[i]]] = 7; } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Bootstrap进度条实现代码解析
下一篇: 产后吃麦片可以吗?有什么作用和坏处?