2021-07-21 20:00:07
本文实例为大家分享了C#实现winform版飞行棋的具体代码,供大家参考,具体内容如下
1、两个人轮流掷骰子红人和绿人
2、投掷出2,4,6点出门,投掷出6点可以在出门后再次投掷行走
3、地图长度共100步
4、地图中除过普通地板之外,另设六种特殊功能地板
(1) 踩到香蕉皮,退6步
(2) 踩到时空,前进6步
(3) 踩到陷阱,暂停一回合
(4) 踩到星星,可以再投掷一次
(5) 踩到移魂大法,可以做出选择与对方互换位置
(6) 踩到手枪,可以击退对方3步
(7) 踩到大炮,可以直接将对方轰炸回家(需要重新出门)
5、如果踩到对方,则对方直接回到起点,
1.地图面积30*13
2.每个格子30像素
3.地面的代号=0,普通地板=1,香蕉皮=2,时空=3,陷阱=4,星星=5,大挪移=6,手枪=7
红人=8,绿人=9
起点=10,终点=11
两人在同一位置=12
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 飞行棋 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Panel map = new Panel(); https:// 创建游戏区对象 int[] mapList = new int[390];https://存储地图数的数组 PictureBox[] mapImg = new PictureBox[390];https:// 存储图片的数组 const int size = 30;https:// 格子大小 Label jiLu = new Label(); RichTextBox msg = new RichTextBox(); https://存储近况 Random r = new Random(); PictureBox dice = new PictureBox(); https://创建骰子对象 Panel redplayHome = new Panel(); https:// 红方飞机 Panel greenplayHome = new Panel(); https://绿方飞机 private void Form1_Load(object sender, EventArgs e) { https:// 设置不可移动 this.FormBorderStyle = FormBorderStyle.FixedSingle; https:// this.FormBorderStyle=FormBorderStyle.FixedToolWindow与Icon图标不可连用 this.BackColor =Color.Plum; this.Size = new Size(1200, 600); this.Location = new Point(Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2, Screen.PrimaryScreen.WorkingArea.Height / 2 - this.Height / 2); https:// 地图对象 map.Width = 30 * size; map.Height = 13 * size; map.Location = new Point(10, 150); map.BorderStyle = BorderStyle.FixedSingle; this.Controls.Add(map); InitialGame(); https://调用初始化地图图片方法 https:// 红方的家 redplayHome.Size = new Size(100, 100); redplayHome.BackgroundImage = Image.FromFile("../../img/select_circle.png"); redplayHome.Location = new Point(20, map.Top - 120); redplayHome.BackgroundImageLayout = ImageLayout.Stretch; this.Controls.Add(redplayHome); https:// 绿方的家 greenplayHome.Size = new Size(100, 100); greenplayHome.BackgroundImage = Image.FromFile("../../img/select_circle.png"); greenplayHome.Location = new Point(map.Left+map.Width-greenplayHome.Width, map.Top - 120); greenplayHome.BackgroundImageLayout = ImageLayout.Stretch; this.Controls.Add(greenplayHome); https://红飞机 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; redplayHome.Controls.Add(redPlayer); https://绿飞机 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; greenplayHome.Controls.Add(greenPlayer); https:// 记录游戏近况框 msg.Size = new Size(260, 390); msg.ReadOnly = true;https://设置只读 msg.Location = new Point(map.Right,map.Top); msg.Font = new Font("微软雅黑", 12); msg.BackColor = Color.LightPink; this.Controls.Add(msg); https:// 游戏近况字 jiLu.Size = new Size(100, 30); jiLu.Text = "游戏近况"; jiLu.Font = new Font("楷体", 16); jiLu.ForeColor = Color.Maroon; jiLu.BackColor = Color.Aquamarine; jiLu.Location = new Point(msg.Left+msg.Width/2-jiLu.Width/2, map.Top-jiLu.Height); this.Controls.Add(jiLu); https://骰子 dice.Size = new Size(100, 100); dice.Image = Image.FromFile("../../img/roll.png"); dice.Location = new Point(map.Left+map.Width/2-dice.Width/2,map.Top-dice.Height); dice.SizeMode = PictureBoxSizeMode.StretchImage; this.Controls.Add(dice); dice.MouseClick += Dice_MouseClick; string startmsg = "请两个人先轮流掷骰子,点大的先一步,红方先手"; ResultTell(startmsg); https://调用 } https:// 骰子点击事件 private void Dice_MouseClick(object sender, MouseEventArgs e) { PlayDice(); PlayGame(); } https:// 记录谁可以投掷 索引0代表红方,1代表绿方 bool[] whoCan = new bool[2] { true, false }; int[] startNum = new int[2]; https:// 记录双方投掷的点数 string[] playeName = new string[2] { "红方", "绿方" }; int[] playPostion = new int[2] { -1, -1 };https:// 记录两个人现在位置 没有出门默认-1 int[] playStan = new int[2]{ -1,-1}; https:// 记录上飞机上一个位置的坐标索引默认为-1 private void PlayDice() https:// 轮流投掷的方法 此方法为投掷为一轮 { https://红方先投 默认红方先投掷 if (whoCan[0]) https://默认为true { startNum[0] = r.Next(1, 7); ResultTell(string.Format("红方投掷出【{0}】点", startNum[0])); whoCan[0] = !whoCan[0]; https:// false赋给红方 } else { whoCan[0] = !whoCan[0]; https://将false变为true } https:// 绿方投掷 if (whoCan[1]) https:// 默认为false { startNum[1] = r.Next(1, 7); ResultTell(string.Format("绿方投掷出【{0}】点", startNum[1])); whoCan[1] = !whoCan[1]; https:// 将true变为false } else { whoCan[1] = !whoCan[1]; https://将true变为false } } https:// 控制游戏刚开始谁先行 bool start = true; https:// 判断是不是游戏刚开始,比点数确定先行 private void PlayGame() { https:// 判断是否投掷完一轮,投掷完一轮后才能判断点数谁先行 if (start) { https:// 双方都投掷后,点数都相同的情况 OutDoor(); if (whoCan[0] && !whoCan[1]) https:// true && true { ResultTell("请红方投掷"); } else if (!whoCan[0] && whoCan[1]) https:// false && true { ResultTell("请绿方投掷"); } } else { https:// 判断谁变成false,说明上次是谁投掷的 if (whoCan[0]&&!whoCan[1]) https://绿方 { PlayeReturn(1); } else if (!whoCan[0]&&whoCan[1])https:// 红方 { PlayeReturn(0); } } } https:// 是否暂停判断 bool[] reclick = new bool[2] { false,false }; https://双方开始游戏,playIndex为索引0,1来判断是红方还是绿方 private void PlayeReturn(int playIndex) { https:// 判断此方位置为-1,则没有出发 if (playPostion[playIndex] == -1) { switch (startNum[playIndex]) https://判断点数 2,4,6 可以出发 { case 2: case 4: ResultTell(string.Format("{0}可以起步!", playeName[playIndex])); playPostion[playIndex] = 0; https://初始到开始位置 playStan[playIndex] = 0; https://存储上一次位置 https:// 如果位置相同 换两个飞机的图片 if (playPostion[1] == playPostion[0]) { mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[2]; } else https:// 如果不同则位置对应的图片 { mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[playIndex]; } break; case 6: whoCan[playIndex] = true; https://此方重新投掷 whoCan[1 - playIndex] = false;https://对方不投掷 ResultTell(string.Format("{0}可以起步!", playeName[playIndex])); playPostion[playIndex] = 0; https:// 初始到索引为0处 playStan[playIndex] = 0; https:// 数组存储上一次位置为0 https:// 如果位置相同 换两个飞机的图片 if (playPostion[1] == playPostion[0]) { mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[2]; } else https:// 如果不同则位置对应的图片 { mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[playIndex]; } ResultTell(string.Format("请{0}投掷骰子", playeName[playIndex])); break; default: ResultTell(string.Format("很遗憾,{0}投掷出{1}点无法起步!轮到{2}投掷", playeName[playIndex], startNum[playIndex], playeName[1 - playIndex])); break; } if (playPostion[0]!=-1) https://红色出门 { redplayHome.Controls.Clear(); https:// 红色飞机的家中飞机消失 } if (playPostion[1]!=-1) https:// 绿色出门 { greenplayHome.Controls.Clear(); https://绿色飞机的家中飞机消失 } } else https:// 不是-1则已经出发 { https:// 将现在位置赋给记录上一次位置的数组中 playStan[playIndex] = playPostion[playIndex]; playPostion[playIndex] += startNum[playIndex]; https://将点数赋给位置 ResultTell(string.Format("{0}移动{1}步", playeName[playIndex], startNum[playIndex])); if (playPostion[playIndex]>=99) { ResultTell(string.Format("{0}获胜!", playeName[playIndex])); playPostion[playIndex] = 99; ChangImg(playIndex); return; } ChangImg(playIndex); https:// 改变图片 https:// 判断移动完后位置 if (playPostion[playIndex]==playPostion[1-playIndex]) { playPostion[1 - playIndex] = 0; ResultTell(string.Format("厉害!{0}精准踩到{1},{0}的当前位置是{2},{1}的当前位置是{3}", playeName[playIndex], playeName[1 - playIndex], playPostion[playIndex], playPostion[1 - playIndex])); playStan[1 - playIndex] = playPostion[1 - playIndex]; mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[1 - playIndex]; mapImg[road[playPostion[1 - playIndex]]].Image = imageList1.Images[1 - playIndex]; ResultTell(string.Format("{0}开始投掷。", playeName[1 - playIndex])); } switch (mapList[road[playPostion[playIndex]]]) { case 1: https:// 走到索引为1的地图上时 ResultTell(string.Format("{0}安全到达!当前位置是{1}", playeName[playIndex],playPostion[playIndex])); ResultTell(string.Format("{0}开始投掷。", playeName[1 - playIndex])); break; case 2: https://香蕉皮 ResultTell(string.Format("很不幸,{0}踩中香蕉皮,退6步!当前位置是{1}", playeName[playIndex],playPostion[playIndex])); playStan[playIndex] = playPostion[playIndex]; https://记录当前的位置 到上次位置数组中 playPostion[playIndex] -= 6; https:// 现在的位置后退6步 ChangImg(playIndex); https:// 添加对应的图片 ResultTell(string.Format("{0}当前的位置是{1}",playeName[playIndex],playPostion[playIndex])); ResultTell(string.Format("{0}开始投掷。", playeName[1 - playIndex])); break; case 3: https:// 时空隧道 ResultTell(string.Format("恭喜!{0}踩中时空隧道,前进6步!当前位置是{1}", playeName[playIndex],playPostion[playIndex])); playStan[playIndex] = playPostion[playIndex];https:// 将现在坐标存储到记录上位置的数组中 playPostion[playIndex] += 6; https:// 现在的位置后退6步 ChangImg(playIndex); https:// 添加对应的图片 ResultTell(string.Format("{0}当前位置是{1}", playeName[playIndex], playPostion[playIndex])); ResultTell(string.Format("{0}开始投掷。", playeName[1 - playIndex])); break; case 4: https://陷阱 ResultTell(string.Format("可惜!{0}踩中陷阱,暂停一回合!", playeName[playIndex])); reclick[1 - playIndex] = true; reclick[playIndex] = false; break; case 5: https:// 幸运星 ResultTell(string.Format("恭喜!踩中幸运星,再玩一回合!当前位置是{1}", playeName[playIndex],playPostion[playIndex])); whoCan[playIndex] = true; https://开启继续摇骰子 whoCan[1 - playIndex] = false; ResultTell(string.Format("{0}继续投掷。", playeName[playIndex])); break; case 6: https:// 大罗移 ResultTell(string.Format("恭喜!{0}踩中大罗移,请选择是否移动!当前位置是{1}", playeName[playIndex],playPostion[playIndex])); DialogResult dr= MessageBox.Show("是否选择移动!","大罗移!",MessageBoxButtons.YesNo); if (dr==DialogResult.Yes) { https://双方位置互换 int temp = playPostion[playIndex]; playPostion[playIndex] = playPostion[1 - playIndex]; playPostion[1 - playIndex] = temp; playStan[playIndex] = playPostion[playIndex]; https:// 将此方现在位置赋给记录上次位置的数组 playStan[1 - playIndex] = playPostion[1 - playIndex];https:// 将另一方现在的位置赋给记录上次位置的数组 mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[playIndex]; mapImg[road[playPostion[1 - playIndex]]].Image = imageList1.Images[1-playIndex]; } ResultTell(string.Format("{0}的当前位置是{1},{2}的当前位置是{3}", playeName[playIndex], playPostion[playIndex], playeName[1 - playIndex], playPostion[1 - playIndex])); ResultTell(string.Format("{0}开始投掷。", playeName[1 - playIndex])); break; case 7: https:// 手枪 ResultTell(string.Format("恭喜!{0}获得手枪,可选择击退对方3步!当前位置是{1}", playeName[playIndex],playPostion[playIndex])); DialogResult drr = MessageBox.Show("是否选择击退对方!", "手枪!", MessageBoxButtons.YesNo); if (drr==DialogResult.Yes) { playStan[1 - playIndex] = playPostion[1 - playIndex]; https:// 记录对方位置 playPostion[1 - playIndex] -= 3; https://对方现在位置后移3个 mapImg[road[playPostion[1 - playIndex]]].Image = imageList1.Images[1 - playIndex];https:// 添加图片 ChangImg(1-playIndex); } ResultTell(string.Format("{0}被击退对方3步!当前位置是{1}", playeName[1-playIndex], playPostion[1-playIndex])); ResultTell(string.Format("{0}开始投掷。", playeName[1 - playIndex])); break; } /* 第一轮:↓ 红色踩到后: reclick[0红色]=reclick=false; reclick[1绿色]=reclick=true; 此时红色摇完,绿色摇 whcan[0]=false,whcan[1]=true; reclick[playIndex] && !reclick[1-playIndex]=true:false; 第二轮↓ reclick[1绿色]=reclick=true;reclick[0红色]=reclick=false; whcan[0]=true,whcan[1]=false; reclick[playIndex==绿色]&&!reclick[1-playIndex==红色]=true:false; */ if (reclick[playIndex]&&!reclick[1-playIndex]) { whoCan[playIndex] = true; whoCan[1 - playIndex] = false; reclick[playIndex] = false; reclick[playIndex] = false; } /* * 第二轮结束 whcan[0]=false,whcan[1]=true; 第三轮开始 whcan[0]=false,whcan[1]=true; https://三轮结束 whcan[0]=true,whcan[1]=false; */ } } private void ChangImg(int playIndex) { https:// 如果位置相同 换成两个飞机的图片 if (playPostion[1] == playPostion[0]) { mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[2]; } else https:// 如果不同则位置对应的图片 { mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[playIndex]; } https:// 双方已经出发,两个人在同一位置,此方图片离开后,显示另一方图片 if (playStan[0]==playStan[1] &&playStan[0]!=-1&&playStan[0]!=-1) https:// 如果上次位置双方相同 { mapImg[road[playStan[playIndex]]].Image = imageList1.Images[1 - playIndex]; } else https:// 上次双方位置不同,变为之前默认的图片 { switch (mapList[road[playStan[playIndex]]]) https://判断上一次记录的图片 { case 0: https:// 地板 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/floor.png"); break; case 1: https:// 路 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/water.gif"); break; case 2: https:// 香蕉皮 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/xj.jpg"); break; case 3: https:// 时空 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/sk.jpg"); break; case 4: https:// 陷阱 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/xianjing.jpg"); break; case 5: https:// 星星 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/xx.jpg"); break; case 6: https:// 交换 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/jh.jpg"); break; case 7: https:// 手枪 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/sq.jpg"); break; case 10: https:// 开始位置 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/game-out.jpg"); break; case 11: https:// 结束位置 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/game-over.jpg"); break; default: break; } } } https:// 判断投掷完一轮,相同点数,谁点数大 谁再投掷 private void OutDoor() { https://投掷完一轮,并且双方都存有点数 if (whoCan[0] && !whoCan[1] && (startNum[0] != 0 || startNum[1] != 0)) { https:// 双方点数相同 if (startNum[0].Equals(startNum[1])) { ResultTell("双方点数相同,请重新投掷!"); } else { start = false; https://不是第一回合 if (startNum[0] > startNum[1]) { ResultTell("红方点数较大,先行一步,红方投掷"); whoCan[0] = true; https://红方继续投掷 whoCan[1] = false; } else { ResultTell("绿方点数较大,先行一步,绿方投掷"); whoCan[0] = false; whoCan[1] = true; https://绿方继续投掷 } } } } void InitialGame() https://初始化地图 { for (int i = 0; i < mapImg.Length; i++) { CreateMap();https:// 调用存储数字的图片 CreateGear(); https:// 调用创建机关的方法 必须向有路,才能调用机关的方法 PictureBox picture = new PictureBox(); picture.Size = new Size(size, size); mapImg[i] = picture; https://图片添加到数组 switch (mapList[i]) https:// 判断地图数组中存储的数字,添加图片 { case 0: https:// 地板 picture.Image = Image.FromFile("../../img/floor.png"); break; case 1: https:// 路 picture.Image = Image.FromFile("../../img/water.gif"); break; case 2: https:// 香蕉皮 picture.Image = Image.FromFile("../../img/xj.jpg"); break; case 3: https:// 时空 picture.Image = Image.FromFile("../../img/sk.jpg"); break; case 4: https:// 陷阱 picture.Image = Image.FromFile("../../img/xianjing.jpg"); break; case 5: https:// 星星 picture.Image = Image.FromFile("../../img/xx.jpg"); break; case 6: https:// 交换 picture.Image = Image.FromFile("../../img/jh.jpg"); break; case 7: https:// 手枪 picture.Image = Image.FromFile("../../img/sq.jpg"); break; case 10: https:// 开始位置 picture.Image = Image.FromFile("../../img/game-out.jpg"); break; case 11: https:// 结束位置 picture.Image = Image.FromFile("../../img/game-over.jpg"); break; default: break; } picture.SizeMode = PictureBoxSizeMode.StretchImage; https://图片适应大小 picture.Location= new Point(i % 30 * size,i/30*size); map.Controls.Add(picture);https:// 图片添加到地图中 } } void CreateMap() https://创建地图 { CreateRoed();https://创建路 for (int i = 0; i < road.Length; i++) { mapList[road[i]] = 1; https:// 路 将路对应的索引变为1 } mapList[0] = 10; https://开始位置 mapList[mapList.Length - 1] = 11; https://结束位置 } int[] road = new int[100];https:// 记录所有路的索引位置 int[] back = {7,27,43,63,96}; https:// 记录香蕉皮的索引位置 int[] forword = {10,25,33,65,80,89 };https:// 记录时空的索引位置 int[] stop = { 3,20,35,50,60,70,90}; https://记录陷阱的索引位置 int[] star = { 5,28,45,71,85}; https://记录星星的索引位置 int[] change = { 4,55,75,98};https://记录交换的索引位置 int[] gun = {22,32,66,82 };https://记录手枪的索引位置 void CreateRoed() https:// 创建路 { for (int i = 0; i < 30; i++) https:// 0-29的索引 向右 { road[i] = i; } for (int i = 30; i <=35; i++) https:// 59,89,119,149,179 右下五格 { road[i] = road[i - 1] + 30; https:// i=30; road[30]===>29 https:// i=31; road[31-1]+30==>road[30]+30==>29+30==>59 https:// i=32;road[32-1]+30==>road[31]+30==>59+30==>89 } for (int i = 36; i <65 ; i++) https:// 向左 { road[i] = road[i - 1] - 1; https:// i=36; road[36-1]-1=>road[35]-1=>179-1=>178 ... } for (int i = 65; i <=70; i++) https:// 左下五格 { road[i] = road[i - 1] + 30; } for (int i = 71; i <100; i++) https:// 向右 { road[i] = road[i - 1] + 1; } } void CreateGear() https:// 创建机关 { for (int i = 0; i < back.Length; i++) { mapList[road[back[i]]] = 2; https:// 香蕉皮 } for (int i = 0; i < forword.Length; i++) { mapList[road[forword[i]]] = 3; https:// 时空 } for (int i = 0; i < stop.Length; i++) { mapList[road[stop[i]]] = 4; https:// 陷阱 } for (int i = 0; i < star.Length; i++) { mapList[road[star[i]]] = 5;https:// 星星 } for (int i = 0; i < change.Length; i++) { mapList[road[change[i]]] = 6; https:// 交换 } for (int i = 0; i < gun.Length; i++) { mapList[road[gun[i]]] = 7; https:// 手枪 } } void ResultTell(string str) { MessageBox.Show(str); msg.AppendText(str + "\r\n"); https:// 将获取的点数添加到文本框中 } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:http://www.yuepc.com/a/144893.html
http://www.yuepc.com 为 “沈一博客” 唯一官方服务平台,请勿相信其他任何渠道。
2021-05-17
2021-07-08
2021-06-07
2021-05-17
2021-06-10
2021-05-14
2021-05-16
2021-05-19
2021-06-08
2021-05-12