C#完整實現消消樂

2020-06-03 17:44:35

C#課程設計作業,花了兩天時間,其中摳圖找圖都花了大半天(後悔以前沒去學PS,後悔莫及,所以最後做出來自己都看不下去的粗糙,不過只能這樣了)
第一天上午:構思整個的大體框架,要實現的功能
第一天下午:找圖,摳圖,找資源,地圖的繪製
第二天上午:被搜尋聯通塊以及方塊的消除難住了,後面參考了一些才解決主要核心的,包括判斷相鄰,執行交換,消除,執行下落,利用了3個Timer元件
第二天下午:技能體系,充值體系,修修改改,勉強做出來了

(一)課程作業:C#消消樂完整實現
利用 C#winform 實現了消消樂遊戲的製作 開發環境 VS2015,SQL sever 2008,
實現功能:
1.登錄,註冊視窗
2.主介面消消樂的實現
3.技能體系:3個技能
4.充值體系
5.揹包功能的實現
6.闖關功能的實現
(二)主要表單
1.登錄,註冊
在这里插入图片描述
在这里插入图片描述
2.主介面
在这里插入图片描述
3.充值介面,教學介面
在这里插入图片描述
在这里插入图片描述
(三)各表單核心程式碼
1.登錄,註冊

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;

namespace KillGame
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)//登錄
        {
            if(checkBox1 .Checked ==true)
            {
                if (textBox1.Text.Trim() == "123456" && textBox2.Text.Trim() == "123456")
                {
                    string sname = "老師";
                    startProgress(progressBar1);
                    this.Hide();
                    Game game = new KillGame.Game(sname);
                    game.Show();

                }
                else
                {
                    MessageBox.Show("登錄失敗,超級使用者賬號密碼都是123456,再試試");
                }
            }
            else
            {
                string id = textBox1.Text.Trim().ToString();
                string pwd = textBox2.Text.Trim().ToString();
                if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "")
                {
                    MessageBox.Show("請輸入賬號或密碼!");
                    return;
                }
                SqlConnection conn = new SqlConnection(@"Data Source=(local);Initial Catalog=MBook;Persist Security Info=True;User ID=sa;Password='zdllxf211215'");
                string strselect = "select Name from TUser where id='" + textBox1.Text.Trim().ToString() + "'and pwd='" + textBox2.Text.Trim().ToString() + "'";
                SqlCommand cmd = new SqlCommand(strselect, conn);
                conn.Open();
                string name = cmd.ExecuteScalar().ToString();
                if (name != null)
                {
                    startProgress(progressBar1);
                    this.Hide();
                    Game game = new KillGame.Game(name);
                    game.Show();

                }
                else
                {
                    MessageBox.Show("登錄失敗!");
                }
            }
           
          
        }

        private void button2_Click(object sender, EventArgs e)//取消
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)//註冊
        {
            Logon logon = new Logon();
            logon.Show();
        }
        public void SetCursor(Bitmap cursor, Point hotPoint)//自定義遊標
        {
            int hotX = hotPoint.X;
            int hotY = hotPoint.Y;
            Bitmap myNewCursor = new Bitmap(cursor.Width * 2 - hotX, cursor.Height * 2 - hotY);
            Graphics g = Graphics.FromImage(myNewCursor);
            g.Clear(Color.FromArgb(0, 0, 0, 0));
            g.DrawImage(cursor, cursor.Width - hotX, cursor.Height - hotY, cursor.Width,
            cursor.Height);
            this.Cursor = new Cursor(myNewCursor.GetHicon());
            g.Dispose();
            myNewCursor.Dispose();
        }
        private void startProgress(ProgressBar pBar1)
        {
            pBar1.Visible = true;// 顯示進度條控制元件.
            pBar1.Minimum = 1;// 設定進度條最小值.
            pBar1.Maximum = 15;// 設定進度條最大值.
            pBar1.Value = 1;// 設定進度條初始值
            pBar1.Step = 1;// 設定每次增加的步長
                           //建立Graphics物件
            Graphics g = pBar1.CreateGraphics();
            for (int x = 1; x <= 15; x++)
            {
                //執行PerformStep()函數
                pBar1.PerformStep();
                string str = Math.Round((100 * x / 15.0), 2).ToString("#0.00 ") + "%";
                Font font = new Font("Times New Roman", (float)10, FontStyle.Regular);
                PointF pt = new PointF(pBar1.Width / 2 - 17, pBar1.Height / 2 - 7);
                g.DrawString(str, font, Brushes.Black , pt);
                //每次回圈讓程式休眠300毫秒
                System.Threading.Thread.Sleep(300);
            }
            pBar1.Visible = false;
            //MessageBox.Show("success!");
        }

        private void Login_Load(object sender, EventArgs e)//表單載入
        {

            SoundPlayer bm = new SoundPlayer("Music\\bj.wav");//背景音樂
            bm.PlayLooping();
            Bitmap a = (Bitmap)Bitmap.FromFile("Image\\mouse.png");
            SetCursor(a, new Point(0, 0));

        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KillGame
{
    public partial class Logon : Form
    {
        public Logon()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)//註冊
        {
            if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "" || textBox3.Text.Trim() == "" || textBox4.Text.Trim()=="")
            {
                MessageBox.Show("請輸入完整資訊!");
                return;
            }
            if(textBox2 .Text .Trim ()==textBox3 .Text .Trim())
            {
                SqlConnection conn = new SqlConnection(@"Data Source=(local);Initial Catalog=MBook;Persist Security Info=True;User ID=sa;Password='zdllxf211215'");
                string strselect = "insert into TUser values('" + textBox1.Text.Trim() + "','" + textBox4.Text.Trim() + "','" + textBox2.Text.Trim() + "')";
                SqlCommand cmd = new SqlCommand(strselect, conn);
                conn.Open();
                int a = cmd.ExecuteNonQuery();
                if (a == 1)
                {
                    MessageBox.Show("註冊成功,快去徵戰吧,騷年!");
                }
                else
                {
                    MessageBox.Show("註冊失敗!");
                }
            }
            else
            {
                MessageBox.Show("密碼前後不一致,註冊失敗!");
            }
            
        }
        public void SetCursor(Bitmap cursor, Point hotPoint)//自定義遊標
        {
            int hotX = hotPoint.X;
            int hotY = hotPoint.Y;
            Bitmap myNewCursor = new Bitmap(cursor.Width * 2 - hotX, cursor.Height * 2 - hotY);
            Graphics g = Graphics.FromImage(myNewCursor);
            g.Clear(Color.FromArgb(0, 0, 0, 0));
            g.DrawImage(cursor, cursor.Width - hotX, cursor.Height - hotY, cursor.Width,
            cursor.Height);
            this.Cursor = new Cursor(myNewCursor.GetHicon());
            g.Dispose();
            myNewCursor.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)//取消
        {
            this.Close();
        }

        private void Logon_Load(object sender, EventArgs e)//載入
        {
            Bitmap a = (Bitmap)Bitmap.FromFile("Image\\mouse.png");
            SetCursor(a, new Point(0, 0));
        }
    }
}

2.主介面視窗

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;
using System.Media;

namespace KillGame
{
    public partial class Game : Form
    {
        Star[,] getmap = new Star[15, 12];//地圖資訊
        Point topleft = new Point(60, 5);//座標
        int gamestart = 0;//遊戲開始
        int gamestop = 0;
        int ispic = 0;//記錄是否選中方塊
        int pickRow = 0;//記錄選中的行
        int pickCol = 0;//記錄選中的列
        int dropRow = 0;
        int dropCol = 0;
        int changeFlag = 0;//記錄是否交換成功
        int size = 60;//方塊的大小
        string news;//訊息列表
        int height = 10;//地圖大小
        int width = 10;

        int time = 60;//時間
        int money = 0;//金幣
        int score = 0;//積分
        int gnum = 1;//關卡數
        int step = 60;

        int skill1num = 0;//技能1使用次數
        int skill2num = 0;//技能2使用次數
        int skill3num = 0;//技能3使用次數

        Star s1 = new Star(starcolor., 60, 0, new Point(0, 0));//用於臨時交換的兩個變數
        Star s2 = new Star(starcolor., 60, 0, new Point(0, 0));
        int x1 = 0;
        int x2 = 0;
        int y1 = 0;
        int y2 = 0;

        public Game()
        {
            InitializeComponent();
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
        }
        
        public Game (string name)//建構函式
        {
            InitializeComponent();
            Name = name;
        }
       
        public void SkillReload()//重新整理技能
        {

        }
        public void GetMap()//隨機生成地圖
        {
            Random rnd = new Random(DateTime.Now.Second);
            for (int i=1;i<=height ;i++)
                for(int j=1;j<=width ; j++)
                {
                    getmap[i, j] = new KillGame.Star((starcolor)rnd.Next(1, 7), size, 0, new Point(topleft.X + (j - 1) * size, topleft.Y + (i - 1) * size));
                }
        }
        public void ReGetMap(Graphics g)//繪製地圖
        {
            for (int i = 1; i <= height ; i++)
                for (int j = 1; j <= width ; j++)
                {                    
                    getmap[i, j].Drawing(g);
                }

        }
        public bool GetRowandClo(Point point,out int row,out int clo)//判斷點選是否有效,並且輸出點選的行列座標
        {
            int srow = (point.Y - topleft.Y) / size + 1;
            int sclo = (point.X - topleft.X) / size + 1;
            if (srow <=10&&srow >=1&&sclo <=10&&sclo >= 1)
            {
                row = srow;
                clo = sclo;
                return true;//點選有效,超出地圖範圍
            }
            else
            {
                row = -1;
                clo = -1;
                return false;//點選無效,超出地圖範圍
            }
        }
        public void SwapStar(int x1,int y1,int x2,int y2)//交換星星,只是交換了兩個星星的顏色
        {
            starcolor scolor = getmap[x1, y1].Color;
            getmap[x1, y1].Color = getmap[x2, y2].Color;
            getmap[x2, y2].Color = scolor;
        }
       
        public bool KillStar()
        {

            int a = 0;
            bool isclean = false;
            //初始化消除標記
            for (int i = 1; i <= height; i++)
                for (int j = 1; j <= width; j++)
                {
                    getmap[i, j].Killed  = 0;
                }

            //遍歷每個方格,每個都向右向下搜尋
            for (int i = 1; i <= height; i++)
                for (int j = 1; j <= width; j++)
                {
                    if (getmap[i, j].Color != starcolor.)//如果有顏色則搜尋
                    {
                        //縱向搜
                        int cnt = 1;//記錄相連的同色方塊個數   
                        int x = i + 1;
                        while (x <= height)
                        {
                            if (getmap[x, j].Color == getmap[i, j].Color)//如果顏色相同記錄個數
                            { x++; cnt++; }
                            else break;
                        }
                        if (cnt >= 3)//如果大於等於3個,標記將消除
                        {
                            isclean = true;
                            for (int k = i; k < i + cnt; k++)
                                getmap[k, j].Killed  = 1;
                            a += cnt;
                        }
                        //橫向搜
                        cnt = 1;
                        int y = j + 1;
                        while (y <= width)
                        {
                            if (getmap[i, y].Color == getmap[i, j].Color)//如果顏色相同記錄個數
                            { y++; cnt++; }
                            else break;
                        }
                        if (cnt >= 3)//如果大於等於3個,標記將消除
                        {
                            isclean = true;
                            for (int k = j; k < j + cnt; k++)
                                getmap[i, k].Killed  = 1;
                            a  += cnt;
                        }
                    }
                }
            //注意這裏一定要先標記再同意消除,如果直接在上面消除,會導致十字不可消
            for (int i = 1; i <= height; i++)//將待消除的方塊消除
                for (int j = 1; j <= width; j++)
                {
                    if (getmap[i, j].Killed  == 1)
                        getmap[i, j].Color = starcolor.;
                }
            time += a  /= 3;//獎勵時間
            time = Math.Min(time, 40);//時間不能超過最大的40

            score  += a  * 10;//獎勵分數
            money += a ;//獎勵金錢

            timelable .Text = time.ToString() + "s";//更新標籤
            moneylable.Text = money.ToString();
           scorelable.Text = score .ToString();
           if(money >money )
                Music("Music\\1.wav");
            if (score >=500+gnum * 250)//晉級成功
            {
                timer1.Enabled = false;
                time +=30;//獎勵時間
                time = Math.Min(time, 40);//時間不能超過最大的40
                timelable.Text = time.ToString() + "s";//更新標籤
                money += gnum * 40;
                moneylable.Text = money.ToString();//獎勵金幣
                score = 0;//積分清零
                scorelable.Text = score.ToString();
                gnum++;
                snumlable.Text = gnum.ToString();
                textBox1.Text = "訊息:恭喜您晉級成功," + Name + "閣下";
                Music("Music\\12902.wav");
                GetMap();
                timer3.Enabled = true;
                timer1.Enabled = true;
                pictureBox4.Invalidate();

                

            }
            return isclean;//返回是否可消
        }
        private void button4_Click(object sender, EventArgs e)//開始遊戲
        {
            Bitmap a = (Bitmap)Bitmap.FromFile("Image\\mouse.png");
            SetCursor(a, new Point(0, 0));
            GetMap();
            gamestart = 1;
            gamestop = 0;
            time = 60;
            score = 0;
            money = 0;
            gnum = 1;
            snumlable.Text = gnum.ToString();
            namelable.Text = Name;
            textBox1.Text = "";
            textBox1.AppendText("訊息:歡迎您來到KillGame!" + Name + "閣下!");
            pictureBox4.Invalidate();//重繪圖片
            timer3.Enabled = true;
            timer1.Enabled = true;//開始計時

        }        

                


        private void button1_Click(object sender, EventArgs e)//技能1購買
        {

            if (gamestart  == 1 &&gamestop  == 0)
            {
                timer1.Enabled = false;
                timelable.Text = "暫停";
                if (MessageBox.Show("是否花費30金幣購買火舞旋風?", "購買", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    if (money >= 30)
                    {
                        money -= 30;
                        skill1num += 1;
                        s1lable.Text = skill1num.ToString();
                    }
                    else
                    {
                        MessageBox.Show("金幣不足,騷年,開啓鈔能力吧!點選左下角充值系統,你值得擁有!", "購買失敗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                moneylable .Text = money.ToString();
                timer1.Enabled = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)//技能2購買
        {

            if (gamestart == 1 && gamestop == 0)
            {
                timer1.Enabled = false;
                timelable.Text = "暫停";
                if (MessageBox.Show("是否花費30金幣購買萬仞浮生?", "購買", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    if (money >= 30)
                    {
                        money -= 30;
                        skill2num += 1;
                        s2lable.Text = skill2num.ToString();
                    }
                    else
                    {
                        MessageBox.Show("金幣不足,騷年,開啓鈔能力吧!點選左下角充值系統,你值得擁有!", "購買失敗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                moneylable.Text = money.ToString();
                timer1.Enabled = true;
            }
        }

        private void button3_Click(object sender, EventArgs e)//技能3購買
        {

            if (gamestart == 1 && gamestop == 0)
            {
                timer1.Enabled = false;
                timelable.Text = "暫停";
                if (MessageBox.Show("是否花費30金幣購買時間掌控?", "購買", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    if (money >= 30)
                    {
                        money -= 30;
                        skill3num += 1;
                        s3lable.Text = skill3num.ToString();
                    }
                    else
                    {
                        MessageBox.Show("金幣不足,騷年,開啓鈔能力吧!點選左下角充值系統,你值得擁有!", "購買失敗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                moneylable.Text = money.ToString();
                timer1.Enabled = true;
            }
        }

        
        private void button5_Click(object sender, EventArgs e)//退出遊戲
        {
            Application.Exit();
        }

        private void button6_Click(object sender, EventArgs e)//新手教學
        {
            About about = new About();
            about.Show();
        }

        private void pictureBox1_Click(object sender, EventArgs e)//技能1火舞旋風釋放
        {
            if(gamestart ==1&&gamestop == 0)
            {
                if(skill1num > 0)
                {
                    skill1num--;
                    s1lable.Text = skill1num.ToString();
                    textBox1.Text = "訊息:偉大的"+Name +"閣下,您使用了火舞旋風!";
                    Music("Music\\skill.wav");
                    Random myRand = new Random(DateTime.Now.Second);//隨機選擇一種顏色
                    int x = myRand.Next(1, 7);

                    for (int i = 1; i <= height; i++)//搜尋所有方塊,若是選擇的顏色則消除
                        for (int j = 1; j <= width; j++)
                        {
                            if ((int)getmap[i, j].Color == x)
                                getmap[i, j].Color = starcolor.;
                        }
                    timer3.Enabled = true;
                    pictureBox4.Invalidate();
                }
                else
                {
                    timer1.Enabled = false;
                    timelable .Text = "暫停";
                    MessageBox.Show("騷年,你的魔法不足,不氪金怎麼變強,試試你的鈔能力吧", "使用失敗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    timer1.Enabled = true;

                }
            }
        }

        private void pictureBox2_Click(object sender, EventArgs e)//技能2萬仞浮生釋放
        {

            if (gamestart == 1 && gamestop == 0)
            {
                if (skill2num > 0)
                {
                    timer1.Enabled = false;
                    skill2num--;
                    s2lable.Text = skill2num.ToString();
                    textBox1.Text = "訊息:偉大的" + Name + "閣下,您使用了萬物浮生!";
                    Music("Music\\skill.wav");
                    GetMap();  //重新重新整理地圖                                     
                    timer3.Enabled = true;
                    timer1.Enabled = true;
                    pictureBox4.Invalidate();
                }
                else
                {
                    timer1.Enabled = false;
                    timelable.Text = "暫停";
                    MessageBox.Show("騷年,你的魔法不足,不氪金怎麼變強,試試你的鈔能力吧", "使用失敗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    timer1.Enabled = true;

                }
            }
        }

        private void pictureBox3_Click(object sender, EventArgs e)//技能3時間掌控釋放
        {

            if (gamestart == 1 && gamestop == 0)
            {
                if (skill3num > 0)
                {
                    skill3num--;
                    s3lable.Text = skill3num.ToString();
                    time += 10; //時間增加
                    timelable.Text = time.ToString() + "s"; //更新標籤
                    textBox1.Text ="訊息:偉大的" + Name + "閣下,您使用了時間掌控!";
                    Music("Music\\skill.wav");
                }
                else
                {
                    timer1.Enabled = false;
                    timelable.Text = "暫停";
                    MessageBox.Show("騷年,你的魔法不足,不氪金怎麼變強,試試你的鈔能力吧", "使用失敗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    timer1.Enabled = true;

                }
            }
        }
        private void Music(string path)
        {
            SoundPlayer player = new SoundPlayer();
            player.SoundLocation = path;
            player.Load();
            player.Play();

        }

        private void timer1_Tick(object sender, EventArgs e)//記錄時間
        {
            if(time == 0)
            {
                gamestart = 0;
                timer1.Enabled = false;
                MessageBox.Show("遊戲結束!");
            }
            timelable.Text = time.ToString() + "s";
            time--;

        }

        private void pictureBox4_MouseDown(object sender, MouseEventArgs e)//滑鼠點選
        {
            if (timer3.Enabled == false && gamestart == 1 && gamestop == 0)
            {
                Music("Music\\click.wav");
                if (ispic == 0)
                {
                    int row, col;
                    bool value = GetRowandClo(new Point(e.X, e.Y), out row, out col);//第一次點選,得到點選位置的行列
                    if (value == true)
                    {
                        if (getmap[row, col].Color != starcolor.)
                        {
                            pickRow = row;
                            pickCol = col;
                            ispic = 1;
                            getmap[pickRow, pickCol].Selected();

                            //Music("Music\\12596.wav");

                        }


                    }
                }
                else
                {

                    int row, col;
                    bool value = GetRowandClo(new Point(e.X, e.Y), out row, out col);//第二次點選,得到點選位置的行列
                    if (value == true)
                    {
                        if (getmap[row, col].Color != starcolor.)
                        {
                            if (row == pickRow && Math.Abs(col - pickCol) == 1 || col == pickCol && Math.Abs(row - pickRow) == 1)//判斷是否相鄰
                            {
                                dropCol = col;
                                dropRow = row;
                                getmap[pickRow, pickCol].Select = 0;
                                s1 = new Star(getmap[pickRow, pickCol].Color, getmap[pickRow, pickCol].Size, getmap[pickRow, pickCol].Select, getmap[pickRow, pickCol].Top);
                                s2 = new Star(getmap[dropRow, dropCol].Color, getmap[dropRow, dropCol].Size, getmap[dropRow, dropCol].Select, getmap[dropRow, dropCol].Top);
                                getmap[pickRow, pickCol].Color = starcolor.;
                                getmap[dropRow, dropCol].Color = starcolor.;
                                pictureBox4.Invalidate();
                                x1 = s1.Top.X;
                                y1 = s1.Top.Y;
                                x2 = s2.Top.X;
                                y2 = s2.Top.Y;
                                changeFlag = 0;
                                timer1.Enabled = false;
                                timer2.Enabled = true;//執行交換


                            }
                            else//重新標記
                            {
                                getmap[pickRow, pickCol].Select = 0;
                                pickRow = row;
                                pickCol = col;
                                getmap[pickRow, pickCol].Selected();
                                ispic = 1;
                            }
                        }

                    }

                }

            }
            pictureBox4.Invalidate();
        }
        private void timer2_Tick(object sender, EventArgs e)//交換
        {
            pictureBox4.Invalidate();
            int tax = 0;
            if (x1 == x2)
            {
                if(y1<y2)
                {
                    s1.Top = new Point(s1.Top.X, s1.Top.Y + step);
                    s2.Top = new Point(s2.Top.X, s2.Top.Y - step);
                    if (s1.Top.Y >= y2)
                        tax = 1;
                    
                }
                else
                {
                    s1.Top = new Point(s1.Top.X, s1.Top.Y - step);
                    s2.Top = new Point(s2.Top.X, s2.Top.Y + step);
                    if (s1.Top.Y <= y2)
                        tax = 1;
                }
            }
            else if (y1 == y2)
            {
                if(x1<x2)
                {
                    s1.Top = new Point(s1.Top.X + step, s1.Top.Y);
                    s2.Top = new Point(s2.Top.X - step, s2.Top.Y);
                    if (s1.Top.X >= x2)
                        tax = 1;
                   

                }
                else
                {
                    s1.Top = new Point(s1.Top.X - step, s1.Top.Y);
                    s2.Top = new Point(s2.Top.X + step, s2.Top.Y);
                    if (s1.Top.X <= x2)
                        tax = 1;
                }
            }
            if(tax == 1)
            {
                timer2.Enabled = false;//
                timer1.Enabled = true;//計時器繼續工作
                getmap[pickRow, pickCol].Color = s2.Color;
                getmap[dropRow, dropCol].Color = s1.Color;//交換顏色
                pictureBox4.Invalidate();
               if(changeFlag == 0)
                {
                    if (!KillStar())//判斷是否可以消除
                    {
                        //繼續交換回來
                        s1 = new Star(getmap[pickRow, pickCol].Color, getmap[pickRow, pickCol].Size, getmap[pickRow, pickCol].Select, getmap[pickRow, pickCol].Top);
                        s2 = new Star(getmap[dropRow, dropCol].Color, getmap[dropRow, dropCol].Size, getmap[dropRow, dropCol].Select, getmap[dropRow, dropCol].Top);
                        getmap[pickRow, pickCol].Color = starcolor.;
                        getmap[dropRow, dropCol].Color = starcolor.;
                        pictureBox4.Invalidate();
                        timer1.Enabled = false;
                        timer2.Enabled = true;//重新交換
                        getmap[pickRow, pickCol].Select = 0;
                        ispic = 0;
                        changeFlag = 1;//這次是交換成功了

                    }
                    else
                    {
                        timer3.Enabled = true;//執行下落
                        pickRow = 0;
                        pickCol = 1;
                        ispic = 0;
                    }
                }
            }

        }

        private void timer3_Tick(object sender, EventArgs e)//下落
        {
            for (int j=1; j<=width;j++)
            {
                for (int i = height; i >= 2; i--)
                {
                    if (getmap[i, j].Color == starcolor.)
                    {
                        for (int c = i; c >= 2; c--)
                            SwapStar(c, j, c - 1, j);
                        break;
                    }
                }
            }
            Random myRand = new Random();
            for (int j = 1; j <= width; j++)
            {
                if (getmap[1, j].Color == starcolor.)
                    getmap[1, j].Color = (starcolor)myRand.Next(1, 7);
            }
            pictureBox4.Invalidate();
            int tax = 0;
            for (int i = 1; i <= height; i++)
            {
                for (int j = 1; j <= width; j++)
                {
                    if(getmap [i,j].Color ==starcolor.)
                    {
                        tax = 1;
                        break;
                    }
                }
                if (tax == 1)
                    break;
            }
            if(tax == 0)
            {
                if(!KillStar ())
                {
                    timer3.Enabled = false;
                }
            }



        }

        private void s3lable_Click(object sender, EventArgs e)
        {

        }
        public void SetCursor(Bitmap cursor, Point hotPoint)//自定義遊標
        {
            int hotX = hotPoint.X;
            int hotY = hotPoint.Y;
            Bitmap myNewCursor = new Bitmap(cursor.Width * 2 - hotX, cursor.Height * 2 - hotY);
            Graphics g = Graphics.FromImage(myNewCursor);
            g.Clear(Color.FromArgb(0, 0, 0, 0));
            g.DrawImage(cursor, cursor.Width - hotX, cursor.Height - hotY, cursor.Width,
            cursor.Height);
            this.Cursor = new Cursor(myNewCursor.GetHicon());
            g.Dispose();
            myNewCursor.Dispose();
        }

        private void pictureBox4_Paint(object sender, PaintEventArgs e)//繪製地圖函數
        {
            if(gamestart == 1)
            {
                ReGetMap(e.Graphics);
                if(timer2 .Enabled ==true)
                {
                    s1.Drawing(e.Graphics);
                    s2.Drawing(e.Graphics);
                }
            }
        }

        private void Game_Load(object sender, EventArgs e)
        {
            Music("Music\\bj.wav");
            time = 0;
            score = 0;
            money = 0;
            gnum = 0;
            moneylable.Text = money.ToString();
            scorelable.Text = score.ToString();
            timelable.Text = time.ToString();
            snumlable.Text = gnum.ToString();
        }

        private void pictureBox5_Click(object sender, EventArgs e)//揹包
        {

        }

        private void pictureBox6_Click(object sender, EventArgs e)//商城
        {
            Money m = new Money();
            //m.Show();
            if(m.ShowDialog()==DialogResult.OK)
            {
                this.money += m.Num;
                
                moneylable.Text = money.ToString();

            }
           
        }
    }
}

3.充值,揹包,教學介面

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 KillGame
{
    public partial class Money : Form
    {
        public Money()
        {
            InitializeComponent();
        }
        private int num;
        public int Num
        {
            get { return num; }
            set { num = value; }
        }


        //private void label1_Click(object sender, EventArgs e)
        //{
        //    DialogResult reult=MessageBox.Show("是否要花費20元購買200枚金幣?", "充值提醒", MessageBoxButtons.OKCancel);
        //    if(reult ==DialogResult.OK)
        //    {
        //        num = 200;
        //        MessageBox.Show("充值成功!");
        //    }
        //    else
        //    {
        //        this.Close();
        //    }
        //}

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult reult = MessageBox.Show("是否要花費20元購買200枚金幣?", "充值提醒", MessageBoxButtons.OKCancel);
            if (reult == DialogResult.OK)
            {
                num = 200;
                MessageBox.Show("充值成功!");
            }
            else
            {
                this.Close();
            }
        }
    }
}

時間緊湊,略顯粗糙,歡迎指出錯誤!