您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“WPF如何實現(xiàn)魔方小游戲”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“WPF如何實現(xiàn)魔方小游戲”這篇文章吧。
先看一下效果圖
代碼如下,先寫一個類,用來判斷是否可以移動
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _0705 { class ClassWay { public ClassWay(int num) { if (num < 9 || (num > 17 && num < 27) || num > 35) { if (num % 3 == 0) { IsT1 = true; } if (num % 3 == 1) { IsT2 = true; } if (num % 3 == 2) { IsT3 = true; } } if (num > 8 && num < 36) { if (num < 12 || (num > 17 && num < 21) || (num > 26 && num < 30)) { IsL1 = true; } else if ((num > 11 && num < 15) || (num > 20 && num < 24) || (num > 29 && num < 33)) { IsL2 = true; } else { IsL3 = true; } } } int num; public int Num { get { return num; } set { num = value; } } bool isT1; public bool IsT1 { get { return isT1; } set { isT1 = value; } } bool isT2; public bool IsT2 { get { return isT2; } set { isT2 = value; } } bool isT3; public bool IsT3 { get { return isT3; } set { isT3 = value; } } bool isL1; public bool IsL1 { get { return isL1; } set { isL1 = value; } } bool isL2; public bool IsL2 { get { return isL2; } set { isL2 = value; } } bool isL3; public bool IsL3 { get { return isL3; } set { isL3 = value; } } } }
下面是主函數(shù)的代碼
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace _0705 { /// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow : Window { Random r = new Random(); int[] num = new int[45]; ClassWay[] cW = new ClassWay[45]; Image[] imgAll = new Image[45]; List<Image> imgmMove = new List<Image>(); public MainWindow() { InitializeComponent(); this.Width = SystemParameters.FullPrimaryScreenWidth; this.Height = SystemParameters.FullPrimaryScreenHeight; this.Left = 0; this.Top = 0; this.WindowStyle = System.Windows.WindowStyle.None; this.AllowsTransparency = true; this.Background = Brushes.Transparent; for (int i = 0; i < 45; i++) { num[i] = r.Next(1, 6); int temp = 0; for (int j = 0; j < i; j++) { if (num[i] == num[j]) { temp++; if (temp == 9) { i--; break; } } } } for (int i = 0; i < 45; i++) { cW[i] = new ClassWay(i); cW[i].Num = num[i]; //Image img = new Image(); imgAll[i] = new Image(); imgAll[i].Tag = i; imgAll[i].Width = 50; imgAll[i].Height = 50; imgAll[i].Source = new BitmapImage(new Uri("images/" + num[i] + ".png", UriKind.Relative)); back.Children.Add(imgAll[i]); if (i < 9) { Canvas.SetLeft(imgAll[i], 480 + i % 3 * imgAll[i].Width); Canvas.SetTop(imgAll[i], 50 + i / 3 * imgAll[i].Height); } else if (i < 18) { Canvas.SetLeft(imgAll[i], 300 + (i - 9) % 3 * imgAll[i].Width); Canvas.SetTop(imgAll[i], 230 + (i - 9) / 3 * imgAll[i].Height); } else if (i < 27) { Canvas.SetLeft(imgAll[i], 480 + (i - 18) % 3 * imgAll[i].Width); Canvas.SetTop(imgAll[i], 230 + (i - 18) / 3 * imgAll[i].Height); } else if (i < 36) { Canvas.SetLeft(imgAll[i], 660 + (i - 27) % 3 * imgAll[i].Width); Canvas.SetTop(imgAll[i], 230 + (i - 27) / 3 * imgAll[i].Height); } else { Canvas.SetLeft(imgAll[i], 480 + (i - 36) % 3 * imgAll[i].Width); Canvas.SetTop(imgAll[i], 410 + (i - 36) / 3 * imgAll[i].Height); } } for (int i = 0; i < 12; i++) { Button btn = new Button(); if (i < 3 || i > 8) { btn.Width = 50; btn.Height = 30; if (i < 3) { btn.Content = "上"; Canvas.SetLeft(btn, 480 + i * btn.Width); Canvas.SetTop(btn, 200); } else { btn.Content = "下"; Canvas.SetLeft(btn, 480 + (i - 9) * btn.Width); Canvas.SetTop(btn, 380); } } else { btn.Width = 30; btn.Height = 50; if (i > 2 && i < 6) { btn.Content = "左"; Canvas.SetLeft(btn, 450); Canvas.SetTop(btn, 230 + (i - 3) * btn.Height); } else { btn.Content = "右"; Canvas.SetLeft(btn, 630); Canvas.SetTop(btn, 230 + (i - 6) * btn.Height); } } btn.Tag = i; btn.Click += new RoutedEventHandler(btn_Click); back.Children.Add(btn); } } void btn_Click(object sender, RoutedEventArgs e) { imgmMove.Clear(); Button btn = (Button)sender; switch (btn.Tag.ToString()) { case "0": foreach (Image img in imgAll) { if (cW[(int)img.Tag].IsT1) { imgmMove.Add(img); } } //MessageBox.Show(imgmMove.Count.ToString()); Move(); break; case "1": foreach (Image img in imgAll) { if (cW[(int)img.Tag].IsT2) { imgmMove.Add(img); } } //MessageBox.Show(imgmMove.Count.ToString()); Move(); break; case "2": foreach (Image img in imgAll) { if (cW[(int)img.Tag].IsT3) { imgmMove.Add(img); } } //MessageBox.Show(imgmMove.Count.ToString()); Move(); break; case "3": foreach (Image img in imgAll) { if (cW[(int)img.Tag].IsL1) { imgmMove.Add(img); } } //MessageBox.Show(imgmMove.Count.ToString()); Move(); break; case "4": foreach (Image img in imgAll) { if (cW[(int)img.Tag].IsL2) { imgmMove.Add(img); } } //MessageBox.Show(imgmMove.Count.ToString()); Move(); break; case "5": foreach (Image img in imgAll) { if (cW[(int)img.Tag].IsL3) { imgmMove.Add(img); } } //MessageBox.Show(imgmMove.Count.ToString()); Move(); break; case "6": foreach (Image img in imgAll) { if (cW[(int)img.Tag].IsL1) { imgmMove.Add(img); } } //MessageBox.Show(imgmMove.Count.ToString()); imgmMove.Reverse(); Move(); break; case "7": foreach (Image img in imgAll) { if (cW[(int)img.Tag].IsL2) { imgmMove.Add(img); } } //MessageBox.Show(imgmMove.Count.ToString()); imgmMove.Reverse(); Move(); break; case "8": foreach (Image img in imgAll) { if (cW[(int)img.Tag].IsL3) { imgmMove.Add(img); } } //MessageBox.Show(imgmMove.Count.ToString()); imgmMove.Reverse(); Move(); break; case "9": foreach (Image img in imgAll) { if (cW[(int)img.Tag].IsT1) { imgmMove.Add(img); } } //MessageBox.Show(imgmMove.Count.ToString()); imgmMove.Reverse(); Move(); break; case "10": foreach (Image img in imgAll) { if (cW[(int)img.Tag].IsT2) { imgmMove.Add(img); } } //MessageBox.Show(imgmMove.Count.ToString()); imgmMove.Reverse(); Move(); break; case "11": foreach (Image img in imgAll) { if (cW[(int)img.Tag].IsT3) { imgmMove.Add(img); } } //MessageBox.Show(imgmMove.Count.ToString()); imgmMove.Reverse(); Move(); break; } bool isSucess = true; for (int i = 18; i < 26; i++) { if(cW[i].Num!=cW[i+1].Num) { isSucess = false; break; } } if(isSucess==true) { MessageBox.Show("成功"); } } private void Move() { Image imgTemp = new Image(); imgTemp.Source = imgmMove[0].Source; int temp=cW[(int)imgmMove[0].Tag].Num; for (int i = 0; i < 8; i++) { imgmMove[i].Source = imgmMove[i + 1].Source; cW[(int)imgmMove[i].Tag].Num = cW[(int)imgmMove[i+1].Tag].Num; } imgmMove[8].Source = imgTemp.Source; cW[(int)imgmMove[8].Tag].Num = temp; } } }
以上是“WPF如何實現(xiàn)魔方小游戲”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。