1. 程式人生 > >C# 小遊戲-拼圖魔方【Game Puzzle】

C# 小遊戲-拼圖魔方【Game Puzzle】

 

  • 工作閒暇之餘去逛了逛CodeProject,剛好現有專案主要用到就是winform,瀏覽了下照片,找到上週帶著蛋撻打疫苗回家的照片,於是新生一記,如何把這些圖片玩起來~
  • 80後應該都有印象,小時候有種玩具,叫做拼圖魔方,90後00後的世界這種玩具應該早已滅絕了。一個塑料小板,上面分隔了很多小圖框,通過移動這些小圖框,最後拼接成完整的圖片
  • 話不多說開始吧~ 先上一張原圖

   

  • 程式碼也很簡單,主要就是通過BitMap分隔現有(後面有時間可以優化下,讓玩家自動上傳圖片,應該會更有意思)圖片,然後Random隨機打亂分割後圖片的順序,通過點選小方格來完成圖片的拼圖,為了更方便玩家,每個小方格添加了序號,玩家也可以不參考原圖,按照小方格上的序號進行拼圖

  

  序號功能實現主要是類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() + " Moves\n\rDo 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() + " Moves\n\rDo You Want To Play Another Game ?", "Congratulation", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    this.LoadNewGame();
                else
                    this.Close();
            }
        }

  具體效果如下:

  

  程式碼有很多已知的可以優化的地方,後面有閒暇時間會處理,如果大家有更好的建議,不妨在下方評論區告知,在此感謝~

  【點選下載原始碼】

  【打個小廣告】最近申請了個微信公眾號,主要用於個人隨筆記錄,工作/生活中專案中用到的技術或娛樂的小遊戲,俗話說得好好記性不如爛鍵盤!後續娛樂的小遊戲原始碼也會分享到公眾號中(部分會同步到部落格園)。

  又有俗話說了, 您的支援就是我寫作的動力,公眾號點個關注什麼的也是舉手之勞,還請您高抬貴手~