C#实现winform版飞行棋
本文实例为大家分享了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(); // 创建游戏区对象 int[] mapList = new int[390];//存储地图数的数组 PictureBox[] mapImg = new PictureBox[390];// 存储图片的数组 const int size = 30;// 格子大小 Label jiLu = new Label(); RichTextBox msg = new RichTextBox(); //存储近况 Random r = new Random(); PictureBox dice = new PictureBox(); //创建骰子对象 Panel redplayHome = new Panel(); // 红方飞机 Panel greenplayHome = new Panel(); //绿方飞机 private void Form1_Load(object sender, EventArgs e) { // 设置不可移动 this.FormBorderStyle = FormBorderStyle.FixedSingle; // 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); // 地图对象 map.Width = 30 * size; map.Height = 13 * size; map.Location = new Point(10, 150); map.BorderStyle = BorderStyle.FixedSingle; this.Controls.Add(map); InitialGame(); //调用初始化地图图片方法 // 红方的家 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); // 绿方的家 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); //红飞机 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); //绿飞机 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); // 记录游戏近况框 msg.Size = new Size(260, 390); msg.ReadOnly = true;//设置只读 msg.Location = new Point(map.Right,map.Top); msg.Font = new Font("微软雅黑", 12); msg.BackColor = Color.LightPink; this.Controls.Add(msg); // 游戏近况字 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); //骰子 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); //调用 } // 骰子点击事件 private void Dice_MouseClick(object sender, MouseEventArgs e) { PlayDice(); PlayGame(); } // 记录谁可以投掷 索引0代表红方,1代表绿方 bool[] whoCan = new bool[2] { true, false }; int[] startNum = new int[2]; // 记录双方投掷的点数 string[] playeName = new string[2] { "红方", "绿方" }; int[] playPostion = new int[2] { -1, -1 };// 记录两个人现在位置 没有出门默认-1 int[] playStan = new int[2]{ -1,-1}; // 记录上飞机上一个位置的坐标索引默认为-1 private void PlayDice() // 轮流投掷的方法 此方法为投掷为一轮 { //红方先投 默认红方先投掷 if (whoCan[0]) //默认为true { startNum[0] = r.Next(1, 7); ResultTell(string.Format("红方投掷出【{0}】点", startNum[0])); whoCan[0] = !whoCan[0]; // false赋给红方 } else { whoCan[0] = !whoCan[0]; //将false变为true } // 绿方投掷 if (whoCan[1]) // 默认为false { startNum[1] = r.Next(1, 7); ResultTell(string.Format("绿方投掷出【{0}】点", startNum[1])); whoCan[1] = !whoCan[1]; // 将true变为false } else { whoCan[1] = !whoCan[1]; //将true变为false } } // 控制游戏刚开始谁先行 bool start = true; // 判断是不是游戏刚开始,比点数确定先行 private void PlayGame() { // 判断是否投掷完一轮,投掷完一轮后才能判断点数谁先行 if (start) { // 双方都投掷后,点数都相同的情况 OutDoor(); if (whoCan[0] && !whoCan[1]) // true && true { ResultTell("请红方投掷"); } else if (!whoCan[0] && whoCan[1]) // false && true { ResultTell("请绿方投掷"); } } else { // 判断谁变成false,说明上次是谁投掷的 if (whoCan[0]&&!whoCan[1]) //绿方 { PlayeReturn(1); } else if (!whoCan[0]&&whoCan[1])// 红方 { PlayeReturn(0); } } } // 是否暂停判断 bool[] reclick = new bool[2] { false,false }; //双方开始游戏,playIndex为索引0,1来判断是红方还是绿方 private void PlayeReturn(int playIndex) { // 判断此方位置为-1,则没有出发 if (playPostion[playIndex] == -1) { switch (startNum[playIndex]) //判断点数 2,4,6 可以出发 { case 2: case 4: ResultTell(string.Format("{0}可以起步!", playeName[playIndex])); playPostion[playIndex] = 0; //初始到开始位置 playStan[playIndex] = 0; //存储上一次位置 // 如果位置相同 换两个飞机的图片 if (playPostion[1] == playPostion[0]) { mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[2]; } else // 如果不同则位置对应的图片 { mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[playIndex]; } break; case 6: whoCan[playIndex] = true; //此方重新投掷 whoCan[1 - playIndex] = false;//对方不投掷 ResultTell(string.Format("{0}可以起步!", playeName[playIndex])); playPostion[playIndex] = 0; // 初始到索引为0处 playStan[playIndex] = 0; // 数组存储上一次位置为0 // 如果位置相同 换两个飞机的图片 if (playPostion[1] == playPostion[0]) { mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[2]; } else // 如果不同则位置对应的图片 { 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) //红色出门 { redplayHome.Controls.Clear(); // 红色飞机的家中飞机消失 } if (playPostion[1]!=-1) // 绿色出门 { greenplayHome.Controls.Clear(); //绿色飞机的家中飞机消失 } } else // 不是-1则已经出发 { // 将现在位置赋给记录上一次位置的数组中 playStan[playIndex] = playPostion[playIndex]; playPostion[playIndex] += startNum[playIndex]; //将点数赋给位置 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); // 改变图片 // 判断移动完后位置 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: // 走到索引为1的地图上时 ResultTell(string.Format("{0}安全到达!当前位置是{1}", playeName[playIndex],playPostion[playIndex])); ResultTell(string.Format("{0}开始投掷。", playeName[1 - playIndex])); break; case 2: //香蕉皮 ResultTell(string.Format("很不幸,{0}踩中香蕉皮,退6步!当前位置是{1}", playeName[playIndex],playPostion[playIndex])); playStan[playIndex] = playPostion[playIndex]; //记录当前的位置 到上次位置数组中 playPostion[playIndex] -= 6; // 现在的位置后退6步 ChangImg(playIndex); // 添加对应的图片 ResultTell(string.Format("{0}当前的位置是{1}",playeName[playIndex],playPostion[playIndex])); ResultTell(string.Format("{0}开始投掷。", playeName[1 - playIndex])); break; case 3: // 时空隧道 ResultTell(string.Format("恭喜!{0}踩中时空隧道,前进6步!当前位置是{1}", playeName[playIndex],playPostion[playIndex])); playStan[playIndex] = playPostion[playIndex];// 将现在坐标存储到记录上位置的数组中 playPostion[playIndex] += 6; // 现在的位置后退6步 ChangImg(playIndex); // 添加对应的图片 ResultTell(string.Format("{0}当前位置是{1}", playeName[playIndex], playPostion[playIndex])); ResultTell(string.Format("{0}开始投掷。", playeName[1 - playIndex])); break; case 4: //陷阱 ResultTell(string.Format("可惜!{0}踩中陷阱,暂停一回合!", playeName[playIndex])); reclick[1 - playIndex] = true; reclick[playIndex] = false; break; case 5: // 幸运星 ResultTell(string.Format("恭喜!踩中幸运星,再玩一回合!当前位置是{1}", playeName[playIndex],playPostion[playIndex])); whoCan[playIndex] = true; //开启继续摇骰子 whoCan[1 - playIndex] = false; ResultTell(string.Format("{0}继续投掷。", playeName[playIndex])); break; case 6: // 大罗移 ResultTell(string.Format("恭喜!{0}踩中大罗移,请选择是否移动!当前位置是{1}", playeName[playIndex],playPostion[playIndex])); DialogResult dr= MessageBox.Show("是否选择移动!","大罗移!",MessageBoxButtons.YesNo); if (dr==DialogResult.Yes) { //双方位置互换 int temp = playPostion[playIndex]; playPostion[playIndex] = playPostion[1 - playIndex]; playPostion[1 - playIndex] = temp; playStan[playIndex] = playPostion[playIndex]; // 将此方现在位置赋给记录上次位置的数组 playStan[1 - playIndex] = playPostion[1 - playIndex];// 将另一方现在的位置赋给记录上次位置的数组 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: // 手枪 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]; // 记录对方位置 playPostion[1 - playIndex] -= 3; //对方现在位置后移3个 mapImg[road[playPostion[1 - playIndex]]].Image = imageList1.Images[1 - playIndex];// 添加图片 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; //三轮结束 whcan[0]=true,whcan[1]=false; */ } } private void ChangImg(int playIndex) { // 如果位置相同 换成两个飞机的图片 if (playPostion[1] == playPostion[0]) { mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[2]; } else // 如果不同则位置对应的图片 { mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[playIndex]; } // 双方已经出发,两个人在同一位置,此方图片离开后,显示另一方图片 if (playStan[0]==playStan[1] &&playStan[0]!=-1&&playStan[0]!=-1) // 如果上次位置双方相同 { mapImg[road[playStan[playIndex]]].Image = imageList1.Images[1 - playIndex]; } else // 上次双方位置不同,变为之前默认的图片 { switch (mapList[road[playStan[playIndex]]]) //判断上一次记录的图片 { case 0: // 地板 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/floor.png"); break; case 1: // 路 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/water.gif"); break; case 2: // 香蕉皮 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/xj.jpg"); break; case 3: // 时空 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/sk.jpg"); break; case 4: // 陷阱 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/xianjing.jpg"); break; case 5: // 星星 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/xx.jpg"); break; case 6: // 交换 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/jh.jpg"); break; case 7: // 手枪 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/sq.jpg"); break; case 10: // 开始位置 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/game-out.jpg"); break; case 11: // 结束位置 mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/game-over.jpg"); break; default: break; } } } // 判断投掷完一轮,相同点数,谁点数大 谁再投掷 private void OutDoor() { //投掷完一轮,并且双方都存有点数 if (whoCan[0] && !whoCan[1] && (startNum[0] != 0 || startNum[1] != 0)) { // 双方点数相同 if (startNum[0].Equals(startNum[1])) { ResultTell("双方点数相同,请重新投掷!"); } else { start = false; //不是第一回合 if (startNum[0] > startNum[1]) { ResultTell("红方点数较大,先行一步,红方投掷"); whoCan[0] = true; //红方继续投掷 whoCan[1] = false; } else { ResultTell("绿方点数较大,先行一步,绿方投掷"); whoCan[0] = false; whoCan[1] = true; //绿方继续投掷 } } } } void InitialGame() //初始化地图 { for (int i = 0; i < mapImg.Length; i++) { CreateMap();// 调用存储数字的图片 CreateGear(); // 调用创建机关的方法 必须向有路,才能调用机关的方法 PictureBox picture = new PictureBox(); picture.Size = new Size(size, size); mapImg[i] = picture; //图片添加到数组 switch (mapList[i]) // 判断地图数组中存储的数字,添加图片 { case 0: // 地板 picture.Image = Image.FromFile("../../img/floor.png"); break; case 1: // 路 picture.Image = Image.FromFile("../../img/water.gif"); break; case 2: // 香蕉皮 picture.Image = Image.FromFile("../../img/xj.jpg"); break; case 3: // 时空 picture.Image = Image.FromFile("../../img/sk.jpg"); break; case 4: // 陷阱 picture.Image = Image.FromFile("../../img/xianjing.jpg"); break; case 5: // 星星 picture.Image = Image.FromFile("../../img/xx.jpg"); break; case 6: // 交换 picture.Image = Image.FromFile("../../img/jh.jpg"); break; case 7: // 手枪 picture.Image = Image.FromFile("../../img/sq.jpg"); break; case 10: // 开始位置 picture.Image = Image.FromFile("../../img/game-out.jpg"); break; case 11: // 结束位置 picture.Image = Image.FromFile("../../img/game-over.jpg"); break; default: break; } picture.SizeMode = PictureBoxSizeMode.StretchImage; //图片适应大小 picture.Location= new Point(i % 30 * size,i/30*size); map.Controls.Add(picture);// 图片添加到地图中 } } void CreateMap() //创建地图 { CreateRoed();//创建路 for (int i = 0; i < road.Length; i++) { mapList[road[i]] = 1; // 路 将路对应的索引变为1 } mapList[0] = 10; //开始位置 mapList[mapList.Length - 1] = 11; //结束位置 } int[] road = new int[100];// 记录所有路的索引位置 int[] back = {7,27,43,63,96}; // 记录香蕉皮的索引位置 int[] forword = {10,25,33,65,80,89 };// 记录时空的索引位置 int[] stop = { 3,20,35,50,60,70,90}; //记录陷阱的索引位置 int[] star = { 5,28,45,71,85}; //记录星星的索引位置 int[] change = { 4,55,75,98};//记录交换的索引位置 int[] gun = {22,32,66,82 };//记录手枪的索引位置 void CreateRoed() // 创建路 { for (int i = 0; i < 30; i++) // 0-29的索引 向右 { road[i] = i; } for (int i = 30; i <=35; i++) // 59,89,119,149,179 右下五格 { road[i] = road[i - 1] + 30; // i=30; road[30]===>29 // i=31; road[31-1]+30==>road[30]+30==>29+30==>59 // i=32;road[32-1]+30==>road[31]+30==>59+30==>89 } for (int i = 36; i <65 ; i++) // 向左 { road[i] = road[i - 1] - 1; // i=36; road[36-1]-1=>road[35]-1=>179-1=>178 ... } 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; } } 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; // 手枪 } } void ResultTell(string str) { MessageBox.Show(str); msg.AppendText(str + "\r\n"); // 将获取的点数添加到文本框中 } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持海外IDC网。
【文章来自:http://www.yidunidc.com/gfcdn.html 欢迎留下您的宝贵建议】