WPF实现魔方小游戏

 更新时间:2018年02月11日 16:53:57   作者:mq_shouhug753951mq  
这篇文章主要为大家详细介绍了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":
          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;
    }
  }
}

下载地址:

魔方小游戏

希望大家会喜欢。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • c# wpf如何附加依赖项属性

    c# wpf如何附加依赖项属性

    这篇文章主要介绍了c# wpf如何附加依赖项属性,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-03-03
  • C#基础知识之Partial的使用

    C#基础知识之Partial的使用

    这篇文章主要介绍了C#基础知识之Partial的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • C#调用C++动态库接口函数和回调函数方法

    C#调用C++动态库接口函数和回调函数方法

    这篇文章主要介绍了C#调用C++动态库接口函数和回调函数方法,通过C++端编写接口展开内容,文章介绍详细具有一定的参考价值,需要的小伙伴可以参考一下
    2022-03-03
  • C# try catch代码块不起效果的解决方法

    C# try catch代码块不起效果的解决方法

    本文主要介绍了C# try catch代码块不起效果的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01
  • C# 拷贝数组的几种方法(总结)

    C# 拷贝数组的几种方法(总结)

    下面小编就为大家带来一篇C# 拷贝数组的几种方法(总结)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-08-08
  • C#如何正确实现一个自定义异常Exception

    C#如何正确实现一个自定义异常Exception

    这篇文章主要为大家详细介绍了C#如何正确实现一个自定义异常Exception,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-09-09
  • C#面向对象特征的具体实现及作用详解

    C#面向对象特征的具体实现及作用详解

    所有的面相对象思想,归根结底是为了简化代码,减少代码量,构建更符合现实生活逻辑的程序代码,从而减轻程序员的负担。不能一味地或者说刻意地去使用面相对象的思想而忽略了程序所实现的功能或者框架,要根据实际情况
    2013-10-10
  • unity实现多点触控代码

    unity实现多点触控代码

    这篇文章主要介绍了unity实现多点触控代码,我最近在学习Unity游戏引擎。先从Unity平面开始,本章介绍Unity 平面上的多点触摸。有需要的小伙伴参考下。
    2015-03-03
  • C#实现字符串倒序遍历的方法小结

    C#实现字符串倒序遍历的方法小结

    这篇文章主要为大家详细介绍了C#中实现字符串倒序遍历的常见方法,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考下
    2024-02-02
  • C#微信开发第一章

    C#微信开发第一章

    这篇文章主要为大家详细介绍了C#微信开发第一章,很有参考价值和实用性,感兴趣的小伙伴们可以参考一下
    2016-07-07

最新评论