IT教程 ·

C# 小游戏-拼图魔方【Game Puzzle】

1994_An Algorithm To Reconstruct Wideband Speech From Narrowband Speech Based On Codebook Mapping

  • 事情空闲之余去逛了逛CodeProject,恰好现有项目重要用到就是winform,阅读了下照片,找到上周带着蛋挞打疫苗回家的照片,因而重生一记,怎样把这些图片玩起来~
  • 80后应当都有印象,小时候有种玩具,叫做拼图魔方,90后00后的天下这类玩具应当早已灭绝了。一个塑料小板,上面分开了许多小图框,经由过程挪动这些小图框,末了拼接成完全的图片
  • 话不多说入手下手吧~ 先上一张原图

C# 小游戏-拼图魔方【Game Puzzle】 IT教程 第1张

  • 代码也很简单,重要就是经由过程BitMap分开现有(背面有时候能够优化下,让玩家自动上传图片,应当会更有意义)图片,然后Random随机打乱支解后图片的次序,经由过程点击小方格来完成图片的拼图,为了更轻易玩家,每一个小方格添加了序号,玩家也能够不参考原图,根据小方格上的序号举行拼图

C# 小游戏-拼图魔方【Game Puzzle】 IT教程 第2张

序号功用完成重假如类MyButton集成父类Button完成:

 public class MyButton : Button
    {
        private int number; 
        public int Number
        {
            get
            {
                return this.number;
            }
            set
            {
                this.Text = value.ToString();
                this.number = value;
            }
        } 
        public MyButton()
        {
        }
    }

随机分开

 Random r = new Random();
            int[] a = new int[24];
            int i = 0;
            int b;
            bool exist;
            while (i != a.Length)
            {
                exist = false;
                b = (r.Next(24) + 1);
                for (int j = 0; j < a.Length; j++)
                    if (a[j] == b) exist = true;
                if (!exist) a[i++] = b;
            }
            for (int j = 0; j < a.Length; j++)
                ButtonArray[j].Number = a[j];
            // set picture pieces as the background image
            int Number;
            int Row, Column;
            for (int k = 0; k < 5; k++)
            {
                for (int j = 0; j < 5; j++)
                {
                    if (k == 4)
                        if (j == 4) break;
                    Number = ButtonArray[k * 5 + j].Number; //Get The Number Of Button
                    Row = (Number - 1) / 5;
                    Column = (Number - 1) - (Row * 5);
                    ButtonArray[k * 5 + j].Image = CurrentBitmapImage.Clone(new Rectangle(new Point(Column * 75, Row * 75), new Size(75, 75)), System.Drawing.Imaging.PixelFormat.DontCare);
                }
            }

点击小方格,经由过程转变当前点击的小方格X,Y坐标来更新小方格的位置

    private void myButton_LocationChanged(object sender, EventArgs e)
        {
            MyButton A = sender as MyButton;
            YouWin = true;
            int ButtonNumber;
            this.NumberOfMoves++;
            if (ButtonArray == null)
            {
                this.FrmMain_Load(sender, e);
            }
            for (int i = 0; i < 5; i++)
            {
                if (YouWin == false)
                    break;
                else for (int j = 0; j < 5; j++)
                    {
                        ButtonNumber = i * 5 + j;
                        if (i == 4 && j == 4)
                            break;
                        else if (GetNumber(ButtonArray[ButtonNumber].Location.X, ButtonArray[ButtonNumber].Location.Y) == ButtonArray[ButtonNumber].Number)
                            continue;
                        else
                        {
                            YouWin = false;
                            break;
                        }
                    }
            }
            if (YouWin)
            {

                if (MessageBox.Show("You Win This Game in " + this.NumberOfMoves.ToString() + " MovesnrDo You Want To Play Another Game ?", "Congratulation", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    this.LoadNewGame();
                else
                    this.Close();
            }
        }
private void myButton_LocationChanged(object sender, EventArgs e)
        {
            MyButton A = sender as MyButton;
            YouWin = true;
            int ButtonNumber;
            this.NumberOfMoves++;
            if (ButtonArray == null)
            {
                this.FrmMain_Load(sender, e);
            }
            for (int i = 0; i < 5; i++)
            {
                if (YouWin == false)
                    break;
                else for (int j = 0; j < 5; j++)
                    {
                        ButtonNumber = i * 5 + j;
                        if (i == 4 && j == 4)
                            break;
                        else if (GetNumber(ButtonArray[ButtonNumber].Location.X, ButtonArray[ButtonNumber].Location.Y) == ButtonArray[ButtonNumber].Number)
                            continue;
                        else
                        {
                            YouWin = false;
                            break;
                        }
                    }
            }
            if (YouWin)
            {

                if (MessageBox.Show("You Win This Game in " + this.NumberOfMoves.ToString() + " MovesnrDo You Want To Play Another Game ?", "Congratulation", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    this.LoadNewGame();
                else
                    this.Close();
            }
        }

详细结果以下:

代码有许多已知的能够优化的处所,背面有空闲时候会处置惩罚,假如人人有更好的发起,无妨在下方批评区示知,在此谢谢~

写给大忙人看的操作系统

参与评论