扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
这篇文章主要为大家展示了“WPF如何实现魔方小游戏”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“WPF如何实现魔方小游戏”这篇文章吧。
创新互联建站主要从事成都做网站、网站建设、外贸营销网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务岚皋,10多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575先看一下效果图
代码如下,先写一个类,用来判断是否可以移动
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 { ////// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { Random r = new Random(); int[] num = new int[45]; ClassWay[] cW = new ClassWay[45]; Image[] imgAll = new Image[45]; ListimgmMove = new List (); 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如何实现魔方小游戏”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联成都做网站行业资讯频道!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流