飞行棋using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; u

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 _1208飞行棋自己写练习
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
            Panel map = new Panel() ;
            //这个数组是所有地板的一个数组,游戏逻辑全部取决于数字数组
            int[] maplist = new int[390];
            //这个数组是所有地图的图片根据maplist来决定每个元素的内容
            PictureBox[] mapimg = new PictureBox[390];
            const int size = 30;
            //这个数组用来记录所有路的索引位置
            int[] road = new int[100];

        private void Form1_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            //窗体模式
            //布局
            this.BackColor = Color.White;//颜色
            this.Width = 1300;//宽
            this.Height = 700;//高
            //窗体位置
            this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
            this.Top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
            //地板
            map.Width = 30 * size;
            map.Height = 13 * size;
            this.Controls.Add(map);
            //游戏区域
            map.Location = new Point(50, 250);
            map.BorderStyle = BorderStyle.FixedSingle;
            //这里还需要一个代码
            //红方绿方的家圆圈
            redplayHome.Size = new Size(100,100);
            redplayHome.BackgroundImage = Image.FromFile(@"../../\img\select_circle.png");
            redplayHome.Location = new Point(50,map.Top-120);
            redplayHome.BackgroundImageLayout = ImageLayout.Stretch;
            this.Controls.Add(redplayHome);
            InitialGame();
            greenplayHome.Size = new Size(100, 100);
            greenplayHome.BackgroundImage = Image.FromFile("../../img/select_circle.png");
            greenplayHome.Location = new Point(170, 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,600);
            msg.Location = new Point(map.Right+20,50);
            msg.ReadOnly = true;
            this.Controls.Add(msg);
            //骰子
            dice.Size = new Size(100,100);
            
            dice.Location = new Point(msg.Left-150,50);
            dice.Image = Image.FromFile("../../img/roll.png");
            dice.SizeMode = PictureBoxSizeMode.StretchImage;
            this.Controls.Add(dice);
            dice.MouseClick += Dice_MouseClick;

            string startmsg = "请两个人先轮流投掷骰子,点数大的先行一步,红方先手";
            ResultTell(startmsg);
        }
        //记录谁可以投掷
            Panel redplayHome = new Panel();
            Panel greenplayHome = new Panel();
            RichTextBox msg = new RichTextBox();
            PictureBox dice = new PictureBox();
            Random r = new Random();
        int[] startNum = new int[2];
        //索引为0代表红方,索引为1代表绿方
        bool[] whoCan = new bool[2] { true, false };
        //轮流投掷筛子
        private void playdice()
        {
            //红方先投掷
            if (whoCan[0])
            {
                startNum[0] = r.Next(1, 7);
                ResultTell(String.Format("红方投掷出【{0}点】", startNum[0]));
                whoCan[0] = !whoCan[0];
            }
            else
            {
                whoCan[0] = !whoCan[0];
            }
            //绿方投掷
            if (whoCan[1])
            {
                startNum[1] = r.Next(1, 7);
                ResultTell(String.Format("绿方投掷出【{0}】点", startNum[1]));
                whoCan[1] = !whoCan[1];
            }
            else
            {
                whoCan[1] = !whoCan[1];
            }
        }
        //轮流投掷骰子决定谁先出门
        private void Dice_MouseClick(object sender, MouseEventArgs e)
        {
            playdice();
            PlayGame();
        }
        //双方投掷出的点数
        string[] playName = new string[2] { "红方", "绿方" };
        //记录双方位置
        int[] playPostion = new int[2] { -1, -1 };
        int[] playStand = new int[2] { -1, -1 };
        //控制游戏刚开始决定谁先行
        bool start = true;
        private void PlayGame()
        {
            //先判断是否是游戏刚开始比点数确定先行
            if (start)
            {
                OutDoor();
                if (!whoCan[0] && whoCan[1])
                {
                    ResultTell("请绿方投掷");
                }
                else if (whoCan[0] && !whoCan[1])
                {
                    ResultTell("请红方投掷");
                }
            }
            else
            {
                //判断谁这次变成了false上次是谁投掷的
                if (whoCan[0] && !whoCan[1])//绿方
                {
                    PlayeReturnGame(1);
                }
                else if (!whoCan[0] && whoCan[1])//红方
                {
                    PlayeReturnGame(0);
                }
            }
        }
        bool[] reclick = new bool[2] { false, false };
        private void PlayeReturnGame(int playIndex)
        {
            //判断这次执行方的位置还是-1证明没出门,判断能否出门
            if (playPostion[playIndex] == -1)
            {
                switch (startNum[playIndex])
                {
                    //如果投掷是2或者4可以出门
                    case 2:
                    case 4:
                        ResultTell(String.Format("{0}可以起步!", playName[playIndex]));
                        playPostion[playIndex] = 0;
                        playStand[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}可以起步!", playName[playIndex]));
                        playPostion[playIndex] = 0;
                        playStand[playIndex] = 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}投掷骰子", playName[playIndex]));
                        break;
                    default:
                        ResultTell(String.Format("很遗憾,{0}投掷出{1}点无法起步!轮到{2}投掷", playName[playIndex], startNum[playIndex], playName[1 - playIndex]));
                        break;
                }
                if (playPostion[0] != -1)//红色出门
                {
                    redplayHome.Controls.Clear();
                }
                if (playPostion[1] != -1)
                {
                    greenplayHome.Controls.Clear();
                }
            }
            else//出门之后,按照你的点数计算位置
            {
                //在其位置改变之前记录好原本站立的位置索引
                playStand[playIndex] = playPostion[playIndex];
                playPostion[playIndex] += startNum[playIndex];
                if (playPostion[playIndex] >= 99)
                {
                    MessageBox.Show(playName[playIndex] + "获胜!");
                    playPostion[playIndex] = 99;
                    ChangeImg(playIndex);
                    return;
                }
                ResultTell(string.Format("{0}移动{1}步", playName[playIndex], startNum[playIndex]));
                ChangeImg(playIndex);

                //判断移动完成之后的位置是什么
                if (playPostion[playIndex] == playPostion[1 - playIndex])
                {
                    playPostion[1 - playIndex] = 0;
                    playStand[1 - playIndex] = playPostion[1 - playIndex];
                    ResultTell(string.Format("厉害!{0}精准踩到{1}将其踩回原点!{0}当前位置是{2},{1}当前的位置是{3}", playName[playIndex], playName[1 - playIndex], playPostion[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}开始投掷。", playName[1 - playIndex]));
                }

                switch (maplist[road[playPostion[playIndex]]])
                {
                    case 1:
                        ResultTell(string.Format("{0}安全到达!当前位置是{1}", playName[playIndex], playPostion[playIndex]));
                        ResultTell(string.Format("{0}开始投掷。", playName[1 - playIndex]));
                        break;
                    case 2:
                        ResultTell(string.Format("很不幸,{0}踩中香蕉皮,后退6步!当前位置是{1}", playName[playIndex], playPostion[playIndex]));
                        playStand[playIndex] = playPostion[playIndex];
                        playPostion[playIndex] -= 6;
                        ChangeImg(playIndex);
                        ResultTell(string.Format("{0}当前位置是{1}", playName[playIndex], playPostion[playIndex]));
                        ResultTell(string.Format("{0}开始投掷。", playName[1 - playIndex]));
                        break;
                    case 3:
                        ResultTell(string.Format("恭喜!{0}踩中时空隧道,前进6步!当前位置是{1}", playName[playIndex], playPostion[playIndex]));
                        playStand[playIndex] = playPostion[playIndex];
                        playPostion[playIndex] += 6;
                        ChangeImg(playIndex);
                        ResultTell(string.Format("{0}当前位置是{1}", playName[playIndex], playPostion[playIndex]));
                        ResultTell(string.Format("{0}开始投掷。", playName[1 - playIndex]));
                        break;
                    case 4:
                        ResultTell(string.Format("可惜!{0}踩中陷阱,暂停一回合!", playName[playIndex]));
                        //0=false  1=true;  0踩中了
                        //下一回合0不能执行
                        reclick[1 - playIndex] = true;
                        reclick[playIndex] = false;
                        break;
                    case 5:
                        ResultTell(string.Format("真好!{0}踩中幸运星,再玩一回合!当前位置是{1}", playName[playIndex], playPostion[playIndex]));
                        whoCan[playIndex] = true;
                        whoCan[1 - playIndex] = false;
                        ResultTell(string.Format("{0}继续投掷。", playName[playIndex]));
                        break;
                    case 6:
                        ResultTell(string.Format("真好!{0}踩中秘籍,请选择措施!当前位置是{1}", playName[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;
                            playStand[playIndex] = playPostion[playIndex];
                            playStand[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}", playName[playIndex], playPostion[playIndex], playName[1 - playIndex], playPostion[1 - playIndex]));
                        ResultTell(string.Format("{0}开始投掷。", playName[1 - playIndex]));
                        break;
                    case 7:
                        ResultTell(string.Format("幸运!{0}获得手枪,可选择击退对方3步!当前位置是{1}", playName[playIndex], playPostion[playIndex]));
                        DialogResult re = MessageBox.Show("是否选择击退对方三步!", "手枪!", MessageBoxButtons.YesNo);
                        if (re == DialogResult.Yes)
                        {
                            playStand[1 - playIndex] = playPostion[1 - playIndex];
                            playPostion[1 - playIndex] -= 3;
                            mapimg[road[playPostion[1 - playIndex]]].Image =imageList1.Images[1 - playIndex];
                            ChangeImg(1 - playIndex);
                        }
                        ResultTell(string.Format("{0}被击退对方3步!当前位置是{1}", playName[1 - playIndex], playPostion[1 - playIndex]));
                        ResultTell(string.Format("{0}开始投掷。", playName[1 - playIndex]));
                        break;
                    default:
                        break;
                }
                //判断对方现在处于暂停回合状态
                if (reclick[playIndex] && !reclick[1 - playIndex])
                {
                    whoCan[playIndex] = true;
                    whoCan[1 - playIndex] = false;
                    reclick[playIndex] = false;
                    reclick[1 - playIndex] = false;
                }
            }
        }


        private void ChangeImg(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 (playStand[0] == playStand[1] && playStand[0] != -1 && playStand[1] != -1 && playPostion[1 - playIndex] == 0)
            {
                mapimg[road[playStand[playIndex]]].Image = imageList1.Images[1 - playIndex];
                mapimg[road[playPostion[playIndex]]].Image = imageList1.Images[playIndex];
            }
            else//如果两人原来不在同一个位置
            {
                //判断原来脚下站的格子是什么
                switch (maplist[road[playStand[playIndex]]])
                {
                    case 0:
                        mapimg[road[playStand[playIndex]]].Image = Image.FromFile("../../img/floor.png");
                        break;
                    case 1:
                        mapimg[road[playStand[playIndex]]].Image = Image.FromFile("../../img/water.gif");
                        break;
                    case 2:
                        mapimg[road[playStand[playIndex]]].Image = Image.FromFile("../../img/xj.jpg");
                        break;
                    case 3:
                        mapimg[road[playStand[playIndex]]].Image = Image.FromFile("../../img/sk.jpg");
                        break;
                    case 4:
                        mapimg[road[playStand[playIndex]]].Image = Image.FromFile("../../img/xianjing.jpg");
                        break;
                    case 5:
                        mapimg[road[playStand[playIndex]]].Image = Image.FromFile("../../img/xx.jpg");
                        break;
                    case 6:
                        mapimg[road[playStand[playIndex]]].Image = Image.FromFile("../../img/jh.jpg");
                        break;
                    case 7:
                        mapimg[road[playStand[playIndex]]].Image = Image.FromFile("../../img/sq.jpg");
                        break;
                    case 10:
                        mapimg[road[playStand[playIndex]]].Image = Image.FromFile("../../img/start.png");
                        break;
                    case 11:
                        mapimg[road[playStand[playIndex]]].Image = Image.FromFile("../../img/end.bmp");
                        break;
                    case 12:
                        mapimg[road[playStand[playIndex]]].Image = Image.FromFile("../../img/double.jpg");
                        break;
                }
            }
        }
        private void OutDoor()
        {
            //满足这种情况两人投掷完成一个回合
            //0=true,1=false
            if (whoCan[0] && !whoCan[1] && (startNum[0] != 0 || startNum[1] != 0))
            {
                if (startNum[0] == 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()
        {
            CreateMap();
            CreateGear();
            for (int i = 0; i < mapimg.Length; i++)
            {
                PictureBox picture = new PictureBox();
                picture.Size = new Size(size, size);
                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 8:
                        picture.Image = Image.FromFile("../../img/red.png");
                        break;
                    case 9:
                        picture.Image = Image.FromFile("../../img/green.png");
                        break;
                    case 10:
                        picture.Image = Image.FromFile("../../img/start.png");
                        break;
                    case 11:
                        picture.Image = Image.FromFile("../../img/end.bmp");
                        break;
                    case 12:
                        picture.Image = Image.FromFile("../../img/double.jpg");
                        break;
                }
                picture.SizeMode = PictureBoxSizeMode.StretchImage;
                picture.Left = i % 30 * size;
                picture.Top = i / 30 * size;
                mapimg[i] = picture;
                map.Controls.Add(picture);
            }
        }
        //创建地图土地
        void CreateMap()
        {
            CreateRoed();
            for (int i = 0; i < road.Length; i++)
            {
                maplist[road[i]] = 1;
            }
            maplist[0] = 10;
            maplist[maplist.Length - 1] = 11;
        }
        //创建道路
        void CreateRoed()
        {
            for (int i = 0; i < 30; i++)
            {
                road[i] = i;
            }
            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;
            }
            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;
            }
        }
        void ResultTell(string str)
        {
            MessageBox.Show(str);
            msg.AppendText(str + "\r\n");
        }

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值