溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

怎么用C#實(shí)現(xiàn)winform版飛行棋

發(fā)布時(shí)間:2021-07-21 20:33:26 來(lái)源:億速云 閱讀:208 作者:chen 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容介紹了“怎么用C#實(shí)現(xiàn)winform版飛行棋”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

本文實(shí)例為大家分享了C#實(shí)現(xiàn)winform版飛行棋的具體代碼,供大家參考,具體內(nèi)容如下

游戲界面

怎么用C#實(shí)現(xiàn)winform版飛行棋

怎么用C#實(shí)現(xiàn)winform版飛行棋

游戲規(guī)則:

1、兩個(gè)人輪流擲骰子紅人和綠人
2、投擲出2,4,6點(diǎn)出門(mén),投擲出6點(diǎn)可以在出門(mén)后再次投擲行走
3、地圖長(zhǎng)度共100步
4、地圖中除過(guò)普通地板之外,另設(shè)六種特殊功能地板

(1) 踩到香蕉皮,退6步
(2) 踩到時(shí)空,前進(jìn)6步
(3) 踩到陷阱,暫停一回合
(4) 踩到星星,可以再投擲一次
(5) 踩到移魂大法,可以做出選擇與對(duì)方互換位置
(6) 踩到手槍?zhuān)梢該敉藢?duì)方3步
(7) 踩到大炮,可以直接將對(duì)方轟炸回家(需要重新出門(mén))

5、如果踩到對(duì)方,則對(duì)方直接回到起點(diǎn),

游戲策劃

1.地圖面積30*13
2.每個(gè)格子30像素
3.地面的代號(hào)=0,普通地板=1,香蕉皮=2,時(shí)空=3,陷阱=4,星星=5,大挪移=6,手槍=7
紅人=8,綠人=9
起點(diǎn)=10,終點(diǎn)=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(); // 創(chuàng)建游戲區(qū)對(duì)象
        int[] mapList = new int[390];//存儲(chǔ)地圖數(shù)的數(shù)組
        PictureBox[] mapImg = new PictureBox[390];// 存儲(chǔ)圖片的數(shù)組
        const int size = 30;// 格子大小
        Label jiLu = new Label();
        RichTextBox msg = new RichTextBox(); //存儲(chǔ)近況
        Random r = new Random();
        PictureBox dice = new PictureBox(); //創(chuàng)建骰子對(duì)象
        Panel redplayHome = new Panel(); // 紅方飛機(jī)
        Panel greenplayHome = new Panel(); //綠方飛機(jī)
        private void Form1_Load(object sender, EventArgs e)
        {
            // 設(shè)置不可移動(dòng)
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            // this.FormBorderStyle=FormBorderStyle.FixedToolWindow與Icon圖標(biāo)不可連用
            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);
            // 地圖對(duì)象
            map.Width = 30 * size;
            map.Height = 13 * size;
            map.Location = new Point(10, 150);
            map.BorderStyle = BorderStyle.FixedSingle;
            this.Controls.Add(map);
            InitialGame(); //調(diào)用初始化地圖圖片方法
            // 紅方的家
            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);
            //紅飛機(jī)
            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);
            //綠飛機(jī)
            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;//設(shè)置只讀
            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 = "請(qǐng)兩個(gè)人先輪流擲骰子,點(diǎn)大的先一步,紅方先手";
            ResultTell(startmsg); //調(diào)用
        }
        // 骰子點(diǎn)擊事件
        private void Dice_MouseClick(object sender, MouseEventArgs e)
        {
            PlayDice();
            PlayGame();
        }
        // 記錄誰(shuí)可以投擲   索引0代表紅方,1代表綠方
        bool[] whoCan = new bool[2] { true, false };
        int[] startNum = new int[2]; // 記錄雙方投擲的點(diǎn)數(shù)
        string[] playeName = new string[2] { "紅方", "綠方" };
        int[] playPostion = new int[2] { -1, -1 };// 記錄兩個(gè)人現(xiàn)在位置 沒(méi)有出門(mén)默認(rèn)-1
        int[] playStan = new int[2]{ -1,-1}; // 記錄上飛機(jī)上一個(gè)位置的坐標(biāo)索引默認(rèn)為-1
        private void PlayDice() // 輪流投擲的方法  此方法為投擲為一輪
        {
            //紅方先投   默認(rèn)紅方先投擲
            if (whoCan[0]) //默認(rèn)為true
            {
                startNum[0] = r.Next(1, 7);
                ResultTell(string.Format("紅方投擲出【{0}】點(diǎn)", startNum[0]));
                whoCan[0] = !whoCan[0]; // false賦給紅方
            }
            else
            {
                whoCan[0] = !whoCan[0]; //將false變?yōu)閠rue
            }
            // 綠方投擲
            if (whoCan[1]) // 默認(rèn)為false
            {
                startNum[1] = r.Next(1, 7);
                ResultTell(string.Format("綠方投擲出【{0}】點(diǎn)", startNum[1]));
                whoCan[1] = !whoCan[1]; // 將true變?yōu)閒alse
            }
            else
            {
                whoCan[1] = !whoCan[1]; //將true變?yōu)閒alse
            }
        }
        // 控制游戲剛開(kāi)始誰(shuí)先行
        bool start = true; 
        // 判斷是不是游戲剛開(kāi)始,比點(diǎn)數(shù)確定先行
        private void PlayGame()
        {
            // 判斷是否投擲完一輪,投擲完一輪后才能判斷點(diǎn)數(shù)誰(shuí)先行
            if (start)
            {
                // 雙方都投擲后,點(diǎn)數(shù)都相同的情況
                OutDoor();
                if (whoCan[0] && !whoCan[1]) // true && true
                {
                    ResultTell("請(qǐng)紅方投擲");
                }
                else if (!whoCan[0] && whoCan[1]) // false && true
                {
                    ResultTell("請(qǐng)綠方投擲");
                }
            }
            else
            {   // 判斷誰(shuí)變成false,說(shuō)明上次是誰(shuí)投擲的
                if (whoCan[0]&&!whoCan[1]) //綠方
                {
                    PlayeReturn(1);
                }
                else if (!whoCan[0]&&whoCan[1])// 紅方
                {
                    PlayeReturn(0);
                }
            }
        }
        // 是否暫停判斷
        bool[] reclick = new bool[2] { false,false };
        //雙方開(kāi)始游戲,playIndex為索引0,1來(lái)判斷是紅方還是綠方
        private void PlayeReturn(int playIndex)
        {   // 判斷此方位置為-1,則沒(méi)有出發(fā)
            if (playPostion[playIndex] == -1)
            {
                switch (startNum[playIndex]) //判斷點(diǎn)數(shù) 2,4,6 可以出發(fā)
                {
                    case 2:
                    case 4:
                        ResultTell(string.Format("{0}可以起步!", playeName[playIndex]));
                        playPostion[playIndex] = 0; //初始到開(kāi)始位置
                        playStan[playIndex] = 0; //存儲(chǔ)上一次位置
                        // 如果位置相同 換兩個(gè)飛機(jī)的圖片
                        if (playPostion[1] == playPostion[0])
                        {
                            mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[2];
                        }
                        else // 如果不同則位置對(duì)應(yīng)的圖片
                        {
                            mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[playIndex];
                        }
                        break;
                    case 6:
                        whoCan[playIndex] = true; //此方重新投擲
                        whoCan[1 - playIndex] = false;//對(duì)方不投擲
                        ResultTell(string.Format("{0}可以起步!", playeName[playIndex]));
                        playPostion[playIndex] = 0; // 初始到索引為0處
                        playStan[playIndex] = 0; // 數(shù)組存儲(chǔ)上一次位置為0
                        // 如果位置相同 換兩個(gè)飛機(jī)的圖片
                        if (playPostion[1] == playPostion[0])
                        {
                            mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[2];
                        }
                        else // 如果不同則位置對(duì)應(yīng)的圖片
                        {
                            mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[playIndex];
                        }

                        ResultTell(string.Format("請(qǐng){0}投擲骰子", playeName[playIndex]));
                        break;
                    default:
                        ResultTell(string.Format("很遺憾,{0}投擲出{1}點(diǎn)無(wú)法起步!輪到{2}投擲", playeName[playIndex], startNum[playIndex], playeName[1 - playIndex]));
                        break;
                }
                if (playPostion[0]!=-1) //紅色出門(mén)
                {
                    redplayHome.Controls.Clear(); // 紅色飛機(jī)的家中飛機(jī)消失
                }
                if (playPostion[1]!=-1) // 綠色出門(mén)
                {
                    greenplayHome.Controls.Clear(); //綠色飛機(jī)的家中飛機(jī)消失
                }
            }
            else // 不是-1則已經(jīng)出發(fā)
            {
                // 將現(xiàn)在位置賦給記錄上一次位置的數(shù)組中
                playStan[playIndex] = playPostion[playIndex];
                playPostion[playIndex] += startNum[playIndex]; //將點(diǎn)數(shù)賦給位置
                ResultTell(string.Format("{0}移動(dòng){1}步", playeName[playIndex], startNum[playIndex]));
                if (playPostion[playIndex]>=99)
                {
                    ResultTell(string.Format("{0}獲勝!", playeName[playIndex]));
                    playPostion[playIndex] = 99;
                    ChangImg(playIndex);
                    return;
                }
                ChangImg(playIndex); // 改變圖片
                // 判斷移動(dòng)完后位置
                if (playPostion[playIndex]==playPostion[1-playIndex])
                {
                    playPostion[1 - playIndex] = 0;
                    ResultTell(string.Format("厲害!{0}精準(zhǔn)踩到{1},{0}的當(dāng)前位置是{2},{1}的當(dāng)前位置是{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}開(kāi)始投擲。", playeName[1 - playIndex]));
                }
                switch (mapList[road[playPostion[playIndex]]])
                {
                    case 1:  // 走到索引為1的地圖上時(shí)
                        ResultTell(string.Format("{0}安全到達(dá)!當(dāng)前位置是{1}", playeName[playIndex],playPostion[playIndex]));
                        ResultTell(string.Format("{0}開(kāi)始投擲。", playeName[1 - playIndex]));
                        break;
                    case 2: //香蕉皮
                        ResultTell(string.Format("很不幸,{0}踩中香蕉皮,退6步!當(dāng)前位置是{1}", playeName[playIndex],playPostion[playIndex]));
                        playStan[playIndex] = playPostion[playIndex];
                        //記錄當(dāng)前的位置 到上次位置數(shù)組中
                        playPostion[playIndex] -= 6; // 現(xiàn)在的位置后退6步
                        ChangImg(playIndex); // 添加對(duì)應(yīng)的圖片
                        ResultTell(string.Format("{0}當(dāng)前的位置是{1}",playeName[playIndex],playPostion[playIndex]));
                        ResultTell(string.Format("{0}開(kāi)始投擲。", playeName[1 - playIndex]));
                        break;
                    case 3: // 時(shí)空隧道
                        ResultTell(string.Format("恭喜!{0}踩中時(shí)空隧道,前進(jìn)6步!當(dāng)前位置是{1}", playeName[playIndex],playPostion[playIndex]));
                        playStan[playIndex] = playPostion[playIndex];// 將現(xiàn)在坐標(biāo)存儲(chǔ)到記錄上位置的數(shù)組中
                        playPostion[playIndex] += 6; // 現(xiàn)在的位置后退6步
                        ChangImg(playIndex); // 添加對(duì)應(yīng)的圖片
                        ResultTell(string.Format("{0}當(dāng)前位置是{1}", playeName[playIndex], playPostion[playIndex]));
                        ResultTell(string.Format("{0}開(kāi)始投擲。", playeName[1 - playIndex]));
                        break;
                    case 4: //陷阱
                        ResultTell(string.Format("可惜!{0}踩中陷阱,暫停一回合!", playeName[playIndex]));

                        reclick[1 - playIndex] = true;
                        reclick[playIndex] = false;
                        break;
                      
                    case 5: // 幸運(yùn)星
                        ResultTell(string.Format("恭喜!踩中幸運(yùn)星,再玩一回合!當(dāng)前位置是{1}", playeName[playIndex],playPostion[playIndex]));
                        whoCan[playIndex] = true; //開(kāi)啟繼續(xù)搖骰子
                        whoCan[1 - playIndex] = false;
                        ResultTell(string.Format("{0}繼續(xù)投擲。", playeName[playIndex]));
                        break;
                    case 6: // 大羅移
                        ResultTell(string.Format("恭喜!{0}踩中大羅移,請(qǐng)選擇是否移動(dòng)!當(dāng)前位置是{1}", playeName[playIndex],playPostion[playIndex]));
                       DialogResult dr= MessageBox.Show("是否選擇移動(dòng)!","大羅移!",MessageBoxButtons.YesNo);
                        if (dr==DialogResult.Yes)
                        {   //雙方位置互換
                            int temp = playPostion[playIndex];
                            playPostion[playIndex] = playPostion[1 - playIndex];
                            playPostion[1 - playIndex] = temp;
                            playStan[playIndex] = playPostion[playIndex]; // 將此方現(xiàn)在位置賦給記錄上次位置的數(shù)組
                            playStan[1 - playIndex] = playPostion[1 - playIndex];// 將另一方現(xiàn)在的位置賦給記錄上次位置的數(shù)組
                            mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[playIndex];
                            mapImg[road[playPostion[1 - playIndex]]].Image = imageList1.Images[1-playIndex];
                        }
                        ResultTell(string.Format("{0}的當(dāng)前位置是{1},{2}的當(dāng)前位置是{3}", playeName[playIndex], playPostion[playIndex], playeName[1 - playIndex], playPostion[1 - playIndex]));
                        ResultTell(string.Format("{0}開(kāi)始投擲。", playeName[1 - playIndex]));
                        break;
                    case 7: // 手槍
                        ResultTell(string.Format("恭喜!{0}獲得手槍?zhuān)蛇x擇擊退對(duì)方3步!當(dāng)前位置是{1}", playeName[playIndex],playPostion[playIndex]));
                        DialogResult drr = MessageBox.Show("是否選擇擊退對(duì)方!", "手槍?zhuān)?quot;, MessageBoxButtons.YesNo);
                        if (drr==DialogResult.Yes)
                        {
                            playStan[1 - playIndex] = playPostion[1 - playIndex]; // 記錄對(duì)方位置
                            playPostion[1 - playIndex] -= 3; //對(duì)方現(xiàn)在位置后移3個(gè)
                            mapImg[road[playPostion[1 - playIndex]]].Image = imageList1.Images[1 - playIndex];// 添加圖片
                            ChangImg(1-playIndex);
                        }
                        ResultTell(string.Format("{0}被擊退對(duì)方3步!當(dāng)前位置是{1}", playeName[1-playIndex], playPostion[1-playIndex]));
                        ResultTell(string.Format("{0}開(kāi)始投擲。", playeName[1 - playIndex]));
                        break;
                }
                /*
                 第一輪:↓
                 紅色踩到后:     reclick[0紅色]=reclick=false; reclick[1綠色]=reclick=true;
                 此時(shí)紅色搖完,綠色搖   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;
                }
                /*
                 * 第二輪結(jié)束
                 whcan[0]=false,whcan[1]=true;
                 第三輪開(kāi)始
                 whcan[0]=false,whcan[1]=true;
                 //三輪結(jié)束
                 whcan[0]=true,whcan[1]=false;
             */

            }
        }

        private void ChangImg(int playIndex)
        {
            // 如果位置相同 換成兩個(gè)飛機(jī)的圖片
            if (playPostion[1] == playPostion[0])
            {
                mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[2];
            }
            else // 如果不同則位置對(duì)應(yīng)的圖片
            {
                mapImg[road[playPostion[playIndex]]].Image = imageList1.Images[playIndex];
            }
            // 雙方已經(jīng)出發(fā),兩個(gè)人在同一位置,此方圖片離開(kāi)后,顯示另一方圖片
            if (playStan[0]==playStan[1] &&playStan[0]!=-1&&playStan[0]!=-1) // 如果上次位置雙方相同
            {
                mapImg[road[playStan[playIndex]]].Image = imageList1.Images[1 - playIndex];
            }
            else // 上次雙方位置不同,變?yōu)橹澳J(rèn)的圖片
            {
                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: // 時(shí)空
                        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: // 開(kāi)始位置
                        mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/game-out.jpg");
                        break;
                    case 11: // 結(jié)束位置
                        mapImg[road[playStan[playIndex]]].Image = Image.FromFile("../../img/game-over.jpg");
                        break;
                    default:
                        break;
                }
            }
        }

        // 判斷投擲完一輪,相同點(diǎn)數(shù),誰(shuí)點(diǎn)數(shù)大 誰(shuí)再投擲
        private void OutDoor()
        {
            //投擲完一輪,并且雙方都存有點(diǎn)數(shù)
            if (whoCan[0] && !whoCan[1] && (startNum[0] != 0 || startNum[1] != 0))
            {   // 雙方點(diǎn)數(shù)相同
                if (startNum[0].Equals(startNum[1]))
                {
                    ResultTell("雙方點(diǎn)數(shù)相同,請(qǐng)重新投擲!");
                }
                else
                {
                    start = false; //不是第一回合
                    if (startNum[0] > startNum[1])
                    {
                        ResultTell("紅方點(diǎn)數(shù)較大,先行一步,紅方投擲");
                        whoCan[0] = true;  //紅方繼續(xù)投擲
                        whoCan[1] = false;
                    }
                    else
                    {
                        ResultTell("綠方點(diǎn)數(shù)較大,先行一步,綠方投擲");
                        whoCan[0] = false; 
                        whoCan[1] = true; //綠方繼續(xù)投擲
                    }
                }
            }
        }

        void InitialGame() //初始化地圖
        {
            for (int i = 0; i < mapImg.Length; i++)
            {
                CreateMap();// 調(diào)用存儲(chǔ)數(shù)字的圖片
                CreateGear(); // 調(diào)用創(chuàng)建機(jī)關(guān)的方法 必須向有路,才能調(diào)用機(jī)關(guān)的方法
                PictureBox picture = new PictureBox();
                picture.Size = new Size(size, size);
                mapImg[i] = picture; //圖片添加到數(shù)組
                switch (mapList[i]) // 判斷地圖數(shù)組中存儲(chǔ)的數(shù)字,添加圖片
                {
                    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: // 時(shí)空
                        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: // 開(kāi)始位置
                        picture.Image = Image.FromFile("../../img/game-out.jpg");
                        break;
                    case 11: // 結(jié)束位置
                        picture.Image = Image.FromFile("../../img/game-over.jpg");
                        break;
                    default:
                        break;
                }
                picture.SizeMode = PictureBoxSizeMode.StretchImage; //圖片適應(yīng)大小
                picture.Location= new Point(i % 30 * size,i/30*size); 
                map.Controls.Add(picture);// 圖片添加到地圖中
            }
        }
        void CreateMap() //創(chuàng)建地圖
        {
            CreateRoed();//創(chuàng)建路
            for (int i = 0; i < road.Length; i++)
            {
                mapList[road[i]] = 1; // 路 將路對(duì)應(yīng)的索引變?yōu)?
            }
            mapList[0] = 10; //開(kāi)始位置
            mapList[mapList.Length - 1] = 11; //結(jié)束位置
        }
        int[] road = new int[100];// 記錄所有路的索引位置
        int[] back = {7,27,43,63,96}; // 記錄香蕉皮的索引位置
        int[] forword = {10,25,33,65,80,89 };// 記錄時(shí)空的索引位置
        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() // 創(chuàng)建路
        {
            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() // 創(chuàng)建機(jī)關(guān)
        {
            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; // 時(shí)空
            }
            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"); // 將獲取的點(diǎn)數(shù)添加到文本框中
        }
    }
}

“怎么用C#實(shí)現(xiàn)winform版飛行棋”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI