现在的位置: 首页 > 综合 > 正文

C#拼图游戏代码

2013年09月07日 ⁄ 综合 ⁄ 共 11099字 ⁄ 字号 评论关闭

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);
            }
        }

    }
}
 

抱歉!评论已关闭.