小編給大家分享一下C#如何實(shí)現(xiàn)拼圖魔方小游戲,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)建站專(zhuān)注于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站制作、網(wǎng)站開(kāi)發(fā)。公司秉持“客戶至上,用心服務(wù)”的宗旨,從客戶的利益和觀點(diǎn)出發(fā),讓客戶在網(wǎng)絡(luò)營(yíng)銷(xiāo)中找到自己的駐足之地。尊重和關(guān)懷每一位客戶,用嚴(yán)謹(jǐn)?shù)膽B(tài)度對(duì)待客戶,用專(zhuān)業(yè)的服務(wù)創(chuàng)造價(jià)值,成為客戶值得信賴(lài)的朋友,為客戶解除后顧之憂。
先上一張?jiān)瓐D
代碼也很簡(jiǎn)單,主要就是通過(guò)BitMap分隔現(xiàn)有(后面有時(shí)間可以?xún)?yōu)化下,讓玩家自動(dòng)上傳圖片,應(yīng)該會(huì)更有意思)圖片,然后Random隨機(jī)打亂分割后圖片的順序,通過(guò)點(diǎn)擊小方格來(lái)完成圖片的拼圖,為了更方便玩家,每個(gè)小方格添加了序號(hào),玩家也可以不參考原圖,按照小方格上的序號(hào)進(jìn)行拼圖
序號(hào)功能實(shí)現(xiàn)主要是類(lèi)MyButton集成父類(lèi)Button實(shí)現(xiàn):
public class MyButton : Button { private int number; public int Number { get { return this.number; } set { this.Text = value.ToString(); this.number = value; } } public MyButton() { } }
隨機(jī)分隔
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); } }
點(diǎn)擊小方格,通過(guò)改變當(dāng)前點(diǎn)擊的小方格X,Y坐標(biāo)來(lái)更新小方格的位置
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(); } }
具體效果如下:
以上是“C#如何實(shí)現(xiàn)拼圖魔方小游戲”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!