时间:2021-05-20
今天给大家带来的是一块用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": 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; } }}下载地址:
魔方小游戏
希望大家会喜欢。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
俄罗斯方块是俄罗斯人发明的一款休闲类的小游戏,这款小游戏可以说是很多人童年的主打电子游戏了,本文我们使用Python来实现这款小游戏。游戏的基本规则是:移动、旋
本文实例为大家分享了Javascript实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下前言原生JavaScript实现贪吃蛇小游戏GitHub地址直接复制
本文实例为大家分享了C++实现推箱子小游戏的具体代码,供大家参考,具体内容如下游戏效果简单易懂的推箱子闯关小游戏。游戏代码#include#include#in
本文实例为大家分享了C++实现走迷宫小游戏的具体代码,供大家参考,具体内容如下源码下载:C++实现走迷宫小游戏主程序代码:#include#include#in
微信跳一跳是最近非常火爆的一个微信小游戏,办公室或者教室里面休闲时间低着头看手机的基本是在玩这个游戏。很多朋友都表示,玩此游戏时要在魔方上面停留一会儿,会有