65.9K
CodeProject 正在变化。 阅读更多。
Home

游戏拼图

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.40/5 (15投票s)

2011年6月29日

CPOL

4分钟阅读

viewsIcon

83332

downloadIcon

12229

这是一个简单的游戏拼图,适合初学者。

引言

这是一个简单的游戏拼图。 在这个游戏中,用户必须将图片碎片按照顺序排列到其他碎片附近,以完成一幅期望的图片。 他/她可以借助每个图片表面上显示的数字。 当用户完成图片时,游戏将向他显示赢得游戏所需的移动次数。 这个游戏可以非常复杂,例如,用户可以选择图像,或者游戏保存每个图片的最高分,或者在每个阶段,游戏变得比以前更难,或者如果它有一个计时器,并且根据游戏时间计算用户得分。 所有这些都是正确的,但我打算创建一个非常简单的游戏拼图。

Using the Code

首先,我要说的是,有很多方法可以完成每个工作,例如“拼图游戏”。 我知道这个游戏的一些部分很好,一些部分可以更好,也许有些部分是错误的。 也许,你可以做得更好。 当我制作这个游戏时,我还是一个编程初学者,如果我现在做,它可能会更好。 也许你可以修改这段代码的某些部分以使其更好地工作,没问题 :)

这个游戏将在一些部分中解释,请按照以下步骤操作

  • MyButton是一个继承自“Button”类的类,并具有一个扩展字段“Number”。 此字段确定按钮必须位于窗体表面上的哪个位置。

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

该项目的主窗体具有以下成员

 private int CrrentPictureNumber;
 private Bitmap CurrentBitmapImage;
 private Point EmptyLocation;
 private MyButton[] ButtonArray;
 private bool YouWin;
 private int NumberOfMoves;
 private FrmShowCurrentImage MyFrmShowCurrentImage;
 public event EventHandler CurrentImageChanged;
    • CurentPictureNumber保存一个介于 1 和 7 之间的数字,该数字确定当前加载并位于主窗体表面上的图片编号。
    • CurrentBitmapImage是一个位图类,用于保存位于窗体上的当前图像。
    • Emptylocation是一个点,用于确定窗体表面上空白正方形的位置。 最好说我们在窗体表面上有 25 个正方形,24 个正方形的表面被选定的图片绘制,一个正方形是空白的,用于移动其他正方形。
    • ButtonArrayMyButton 类的一个二维数组,当游戏要计算每个正方形的位置以赢得用户或继续游戏时,会使用此数组。
    • NumberOfMoves保存用户完成图像并赢得游戏所完成的移动次数。 每次用户移动一个正方形,该字段都会增加 1。
  • 该游戏在资源中有 7 个static图像,这些图像始终是固定的,用户无法更改它们。 当一个新游戏开始时,一张图片从项目的资源中加载,并被分成 25 块。 位于图片右下角的碎片被移除,并且这个位置被称为“EmptyLocation”,然后这些碎片随机地位于游戏表面上。 所有这些以及其他一些都在这些代码部分中完成。

    private void LoadNewGame()
    {
        this.myButton1.Location = new System.Drawing.Point(20, 40);
        this.myButton2.Location = new System.Drawing.Point(95, 40);
        this.myButton3.Location = new System.Drawing.Point(170, 40);
        this.myButton4.Location = new System.Drawing.Point(245, 40);
        this.myButton5.Location = new System.Drawing.Point(320, 40);
        this.myButton6.Location = new System.Drawing.Point(20, 115);
        this.myButton7.Location = new System.Drawing.Point(95, 115);
        this.myButton8.Location = new System.Drawing.Point(170, 115);
        this.myButton9.Location = new System.Drawing.Point(245, 115);
        this.myButton10.Location = new System.Drawing.Point(320, 115);
        this.myButton11.Location = new System.Drawing.Point(20, 190);
        this.myButton12.Location = new System.Drawing.Point(95, 190);
        this.myButton13.Location = new System.Drawing.Point(170, 190);
        this.myButton14.Location = new System.Drawing.Point(245, 190);
        this.myButton15.Location = new System.Drawing.Point(320, 190);
        this.myButton16.Location = new System.Drawing.Point(20, 265);
        this.myButton17.Location = new System.Drawing.Point(95, 265);
        this.myButton18.Location = new System.Drawing.Point(170, 265);
        this.myButton19.Location = new System.Drawing.Point(245, 265);
        this.myButton20.Location = new System.Drawing.Point(320, 265);
        this.myButton21.Location = new System.Drawing.Point(20, 340);
        this.myButton22.Location = new System.Drawing.Point(95, 340);
        this.myButton23.Location = new System.Drawing.Point(170, 340);
        this.myButton24.Location = new System.Drawing.Point(245, 340);
    
        if (this.CrrentPictureNumber == 7)
            this.CrrentPictureNumber = 1;
        else
            this.CrrentPictureNumber++;
        switch (this.CrrentPictureNumber)
        {
            case 1: CurrentBitmapImage = new Bitmap
    		(GamePuzzle.Properties.Resources._1, new Size(375, 375));
            break;
            case 2: CurrentBitmapImage = new Bitmap
    		(GamePuzzle.Properties.Resources._2, new Size(375, 375));
            break;
            case 3: CurrentBitmapImage = new Bitmap
    		(GamePuzzle.Properties.Resources._3, new Size(375, 375));
            break;
            case 4: CurrentBitmapImage = new Bitmap
    		(GamePuzzle.Properties.Resources._4, new Size(375, 375));
            break;
            case 5: CurrentBitmapImage = new Bitmap
    		(GamePuzzle.Properties.Resources._5, new Size(375, 375));
            break;
            case 6: CurrentBitmapImage = new Bitmap
    		(GamePuzzle.Properties.Resources._6, new Size(375, 375));
            break;
            case 7: CurrentBitmapImage = new Bitmap
    		(GamePuzzle.Properties.Resources._7, new Size(375, 375));
            break;
        }
        this.EmptyLocation = new Point(320, 340);
        this.NumberOfMoves = 0;
        this.ButtonArray = new MyButton[24];
    
        this.ButtonArray[0] = this.myButton1;
        this.ButtonArray[1] = this.myButton2;
        this.ButtonArray[2] = this.myButton3;
        this.ButtonArray[3] = this.myButton4;
        this.ButtonArray[4] = this.myButton5;
        this.ButtonArray[5] = this.myButton6;
        this.ButtonArray[6] = this.myButton7;
        this.ButtonArray[7] = this.myButton8;
        this.ButtonArray[8] = this.myButton9;
        this.ButtonArray[9] = this.myButton10;
        this.ButtonArray[10] = this.myButton11;
        this.ButtonArray[11] = this.myButton12;
        this.ButtonArray[12] = this.myButton13;
        this.ButtonArray[13] = this.myButton14;
        this.ButtonArray[14] = this.myButton15;
        this.ButtonArray[15] = this.myButton16;
        this.ButtonArray[16] = this.myButton17;
        this.ButtonArray[17] = this.myButton18;
        this.ButtonArray[18] = this.myButton19;
        this.ButtonArray[19] = this.myButton20;
        this.ButtonArray[20] = this.myButton21;
        this.ButtonArray[21] = this.myButton22;
        this.ButtonArray[22] = this.myButton23;
        this.ButtonArray[23] = this.myButton24;
    
        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);
         }
         if (this.CurrentImageChanged != null)
         this.CurrentImageChanged(this, EventArgs.Empty);
     } 
  • 当用户点击一个按钮(一张图片碎片)时,游戏程序必须检查这一点:“空白位置是否在点击的按钮旁边?” 如果是,则当前按钮和“EmptyLocation”必须相互交换位置。 所有这些都在这段代码中完成。

    private void myButton_Click(object sender, EventArgs e)
    {
        Button b = (Button)sender;
        if ((b.Location.X - 75 == EmptyLocation.X) && 
    			(b.Location.Y == EmptyLocation.Y))
        {
            b.Location = EmptyLocation;
            EmptyLocation.X += 75;
            this.Focus();
        }
        else if (b.Location.X + 75 == EmptyLocation.X && 
    			(b.Location.Y == EmptyLocation.Y))
        {
            b.Location = EmptyLocation;
            EmptyLocation.X -= 75;
            this.Focus();
        }
        else if (b.Location.Y - 75 == EmptyLocation.Y && 
    			(b.Location.X == EmptyLocation.X))
        {
            b.Location = EmptyLocation;
            EmptyLocation.Y += 75;
            this.Focus();
        }
    
        else if (b.Location.Y + 75 == EmptyLocation.Y && 
    			(b.Location.X == EmptyLocation.X))
        {
            b.Location = EmptyLocation;
            EmptyLocation.Y -= 75;
            this.Focus();
        }
    }
  • 当用户移动一个正方形时,游戏程序必须检查这一点:“当前图片是否完整?” 或者“用户已经赢得了游戏还是必须继续尝试? 所有这些工作必须在每个按钮的“LocationChanged”事件中完成

    private void myButton_LocationChanged(object sender, EventArgs e)
    {
        MyButton A = sender as MyButton;
        YouWin = true;
        int ButtonNumber;
        this.NumberOfMoves++;
        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 int GetNumber(int x, int y)
    {
        int a, b;
        if (y == 40) // y-->Row
            a = 0;
        else
            a = (y - 40) / 75;
    
        if (x == 20) // x-->Column
            b = 0;
        else
            b = (x - 20) / 75;
        int Number = a * 5 + b + 1;
        return Number;
    }

但是游戏拼图程序如何意识到每个正方形都位于其真实位置还是不是?

这个游戏有 24 个正方形,每个正方形都有一个从 1 到 24 的数字,这些正方形的编号就像这张图片

这些正方形就像一个二维数组,每个正方形都具有一个 X 维度和一个 Y 维度,例如标记为 14 的正方形,其 X 维度是 4,其 Y 维度是 3。 因此,我们可以说位于(4,3)中的正方形的数字必须是 14,如果其数字是 14,则其位置为true,否则其位置为false

历史

  • 2011 年 6 月 29 日:初始帖子
© . All rights reserved.