C#拼圖遊戲代碼

來源:互聯網
上載者:User

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form9 : Form
    {
        public static int aa;
        public static int bb;
        private System.Windows.Forms.PictureBox[,] t;
        public Form9()
        {
            InitializeComponent();
        }

        private void Form9_Load(object sender, EventArgs e)
        {
            t = new PictureBox[3, 3]{{pictureBox1 ,pictureBox2 ,pictureBox3 },
           {pictureBox4 ,pictureBox5 ,pictureBox6 },
           {pictureBox7 ,pictureBox8 ,pictureBox9 }
           };
            for (int i = 0; i <= 2; i++)
            {
                for (int j = 0; j <= 2; j++)
                {
                    t[i, j].Enabled = false;
                }
            }
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            bb++;
            label2.Text = bb.ToString();
            if (pictureBox2.Image == null)
            {
                pictureBox2.Image = pictureBox1.Image;
                pictureBox1.Image = null;
            }
            else if (pictureBox4.Image == null)
            {
                pictureBox4.Image = pictureBox1.Image;
                pictureBox1.Image = null;
            }
        
        }
        private void pictureBox2_Click(object sender, EventArgs e)
        {
            bb++;
            label2.Text = bb.ToString();
            if (pictureBox1.Image == null)
            {
                pictureBox1.Image = pictureBox2.Image;
                pictureBox2.Image = null;
            }
            else if (pictureBox3.Image == null)
            {
                pictureBox3.Image = pictureBox2.Image;
                pictureBox2.Image = null;
            }
            else if (pictureBox5.Image == null)
            {
                pictureBox5.Image = pictureBox2.Image;
                pictureBox2.Image = null;
            }
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            bb++;
            label2.Text = bb.ToString();
            if (pictureBox2.Image == null)
            {
                pictureBox2.Image = pictureBox3.Image;
                pictureBox3.Image = null;
            }
            else if (pictureBox6.Image == null)
            {
                pictureBox6.Image = pictureBox3.Image;
                pictureBox3.Image = null;
            }
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            bb++;
            label2.Text = bb.ToString();
            if (pictureBox1.Image == null)
            {
                pictureBox1.Image = pictureBox4.Image;
                pictureBox4.Image = null;
            }
            else if (pictureBox5.Image == null)
            {
                pictureBox5.Image = pictureBox4.Image;
               pictureBox4.Image = null;
            }
            else if (pictureBox7.Image == null)
            {
                pictureBox7.Image = pictureBox4.Image;
                pictureBox4.Image = null;
            }
        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {
            bb++;
            label2.Text = bb.ToString();
            if (pictureBox5.Image == null)
            {
                pictureBox5.Image = pictureBox6.Image;
                pictureBox6.Image = null;
            }
            else if (pictureBox3.Image == null)
            {
                pictureBox3.Image = pictureBox6.Image;
                pictureBox6.Image = null;
            }
            else if (pictureBox8.Image == null)
            {
                pictureBox8.Image = pictureBox6.Image;
                pictureBox6.Image = null;
            }
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            bb++;
            label2.Text = bb.ToString();
            if (pictureBox2.Image == null)
            {
                pictureBox2.Image = pictureBox5.Image;
                pictureBox5.Image = null;
            }
            else if (pictureBox4.Image == null)
            {
                pictureBox4.Image = pictureBox5.Image;
                pictureBox5.Image = null;
            }
            else if (pictureBox6.Image == null)
            {
                pictureBox6.Image = pictureBox5.Image;
                pictureBox5.Image = null;
            }
            else if (pictureBox9.Image ==null)
            {
                pictureBox9.Image = pictureBox5.Image;
                pictureBox5.Image = null;
            }
        }

        private void pictureBox7_Click(object sender, EventArgs e)
        {
            bb++;
            label2.Text = bb.ToString();
            if (pictureBox4.Image == null)
            {
                pictureBox4.Image = pictureBox7.Image;
                pictureBox7.Image = null;
            }
            else if (pictureBox9.Image == null)
            {
                pictureBox9.Image = pictureBox7.Image;
                pictureBox7.Image = null;
            }
        }

        private void pictureBox9_Click(object sender, EventArgs e)
        {
            bb++;
            label2.Text = bb.ToString();
            if (pictureBox8.Image == null)
            {
                pictureBox8.Image = pictureBox9.Image;
                pictureBox9.Image = null;
            }
            else if (pictureBox7.Image == null)
            {
                pictureBox7.Image = pictureBox9.Image;
                pictureBox9.Image = null;
            }
            else if (pictureBox5.Image == null)
            {
                pictureBox5.Image = pictureBox9.Image;
                pictureBox9.Image = null;
            }
        }

        private void pictureBox8_Click(object sender, EventArgs e)
        {
            bb++;
            label2.Text = bb.ToString();
            if (pictureBox6.Image == null)
            {
                pictureBox6.Image = pictureBox8.Image;
                pictureBox8.Image = null;
            }
            else if (pictureBox9.Image == null)
            {
                pictureBox9.Image = pictureBox8.Image;
                pictureBox8.Image = null;
            }
        }

        private void label2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (int.Parse(label2.Text) >= 200)
                {
                    MessageBox.Show("你已經超過了遊戲所限定的最大步數,歡迎再來!", "注意",

MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Application.Exit();
                }
            }
            catch { }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button2.Enabled = true;
            pictureBox9.Image = null;
            aa = aa + 1;
            for (int m = 0; m <= 2; m++)
            {
                for (int n = 0; n <= 2; n++)
                {
                    t[m, n].Enabled = true;
                }
            }
        ss:
            switch (aa)
            {
                case 1:
                    {
                        Clipboard.SetImage(pictureBox1.Image);
                        pictureBox1.Image = pictureBox5.Image;
                        pictureBox5.Image = Clipboard.GetImage();
                        Clipboard.Clear();
                        Clipboard.SetImage(pictureBox2.Image);
                        pictureBox2.Image = pictureBox4.Image;
                        pictureBox4.Image = Clipboard.GetImage();
                        Clipboard.Clear();
                        Clipboard.SetImage(pictureBox3.Image);
                        pictureBox3.Image = pictureBox8.Image;
                        pictureBox8.Image = Clipboard.GetImage();
                        Clipboard.Clear();
                        button1.Enabled = false;
                        break;
                    }
                case 2:
                    {
                        Clipboard.SetImage(pictureBox1.Image);
                        pictureBox1.Image = pictureBox6.Image;
                        pictureBox6.Image = Clipboard.GetImage();
                        Clipboard.Clear();
                        Clipboard.SetImage(pictureBox2.Image);
                        pictureBox2.Image = pictureBox8.Image;
                        pictureBox8.Image = Clipboard.GetImage();
                        Clipboard.Clear();
                        Clipboard.SetImage(pictureBox3.Image);
                        pictureBox3.Image = pictureBox5.Image;
                        pictureBox5.Image = Clipboard.GetImage();
                        Clipboard.Clear();
                        button1.Enabled = false;
                        break;
                    }
                case 3:
                    {
                        Clipboard.SetImage(pictureBox2.Image);
                        pictureBox2.Image = pictureBox5.Image;
                        pictureBox5.Image = Clipboard.GetImage();
                        Clipboard.Clear();
                        Clipboard.SetImage(pictureBox4.Image);
                        pictureBox4.Image = pictureBox6.Image;
                        pictureBox6.Image = Clipboard.GetImage();
                        Clipboard.Clear();
                        Clipboard.SetImage(pictureBox3.Image);
                        pictureBox3.Image = pictureBox8.Image;
                        pictureBox8.Image = Clipboard.GetImage();
                        Clipboard.Clear();
                        button1.Enabled = false;
                        break;
                    }
                case 4:
                    {
                        Clipboard.SetImage(pictureBox1.Image);
                        pictureBox1.Image = pictureBox8.Image;
                        pictureBox8.Image = Clipboard.GetImage();
                        Clipboard.Clear();
                        Clipboard.SetImage(pictureBox3.Image);
                        pictureBox3.Image = pictureBox5.Image;
                        pictureBox5.Image = Clipboard.GetImage();
                        Clipboard.Clear();
                        Clipboard.SetImage(pictureBox6.Image);
                        pictureBox6.Image = pictureBox9.Image;
                        pictureBox9.Image = Clipboard.GetImage();
                        Clipboard.Clear();
                        button1.Enabled = false;
                        break;
                    }
                default:
                    {
                        aa = 1;
                        goto ss;
                    }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = pictureBox10.Image;
            pictureBox2.Image = pictureBox12.Image;
            pictureBox3.Image = pictureBox13.Image;
            pictureBox4.Image = pictureBox14.Image;
            pictureBox5.Image = pictureBox15.Image;
            pictureBox6.Image = pictureBox16.Image;
            pictureBox7.Image = pictureBox17.Image;
            pictureBox8.Image = pictureBox18.Image;
            pictureBox9.Image = null;
           label2.Text = "";
            bb = 0;
            button2.Enabled = false;
            button1.Enabled = true;
            for (int m = 0; m <= 2; m++)
            {
                for (int n = 0; n <= 2; n++)
                {
                    t[m, n].Enabled = false;
                }
            }
        }

        private void Form9_Activated(object sender, EventArgs e)
        {
            if (pictureBox1.Image == pictureBox10.Image &&
          pictureBox2.Image == pictureBox12.Image &&
          pictureBox3.Image == pictureBox13.Image &&
          pictureBox4.Image == pictureBox14.Image &&
          pictureBox5.Image == pictureBox15.Image &&
          pictureBox6.Image == pictureBox16.Image &&
          pictureBox7.Image == pictureBox17.Image &&
          pictureBox8.Image == pictureBox18.Image &&
          pictureBox9.Image == null)
            {
                MessageBox.Show("恭喜你已經完成拼圖!", "提示", MessageBoxButtons.OK,

MessageBoxIcon.Information);
            }
        }

    }
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.