WPF实现魔方小游戏
今天给大家带来的是一块用WPF 实现魔方的小游戏,先看一下效果图
代码如下,先写一个类,用来判断是否可以移动
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; } } } }
下面是主函数的代码
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": 【文章出处:美国cn2站群服务器 欢迎转载】 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; } } }
下载地址:
魔方小游戏
希望大家会喜欢。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持海外IDC网。