码迷,mamicode.com
首页 > Windows程序 > 详细

初学WPF,做一款小游戏练习一下

时间:2016-01-16 19:05:47      阅读:378      评论:0      收藏:0      [点我收藏+]

标签:

以前做项目的时候有用过Silverlight,最近了解了一下WPF,做了一款简单的小游戏来练手,希望能和大家多多交流。

想法:最强大脑 牛人多多,对色彩的分辨更是让人目瞪口呆,于是我就想到做一个小游戏,简单一点,做网格状的色块,中间穿插一块不同颜色但是又非常相近的颜色块,点击该颜色块即为通关,进入下一关,下一关的色块数量+1,直到到达一个差不多的关卡后不再+1;

效果如图

技术分享

说做就做,新建工程,开始实现,首先准备好一组颜色相近的颜色代码,百度HTML颜色代码,随便选取,越相邻颜色色差越小。

技术分享

 1  private string[][] colors = new string[][] {
 2             new string[2]{"#7B7B7B","#8E8E8E"} ,
 3             new string[2]{"#FF5151","#ff7575"} ,
 4             new string[2]{"#ffaad5","#FFC1E0"} ,
 5             new string[2]{"#DCB5FF","#E6CAFF"} ,
 6             new string[2]{"#79FF79","#93FF93"} ,
 7             new string[2]{"#97CBFF","#ACD6FF"} ,
 8             new string[2]{"#FFFF93","#FFFFAA"} ,
 9             new string[2]{"#B766AD","#C07AB8"} ,
10             new string[2]{"#B7FF4A","#C2FF68"}
11         };

相关代码

    /// <summary>
    /// 游戏 找色差 实例
    /// </summary>
    public partial class MainWindow : Window
    {

        private int level = 2;   //当前关卡
        private double cavWidth = 0;    //容器宽度、宽高一样
        private double btnWidth = 0;    //色块宽高
        const int maxLevel = 8; //最大关卡

        //颜色数组
        private string[][] colors = new string[][] {
            new string[2]{"#7B7B7B","#8E8E8E"} ,
            new string[2]{"#FF5151","#ff7575"} ,
            new string[2]{"#ffaad5","#FFC1E0"} ,
            new string[2]{"#DCB5FF","#E6CAFF"} ,
            new string[2]{"#79FF79","#93FF93"} ,
            new string[2]{"#97CBFF","#ACD6FF"} ,
            new string[2]{"#FFFF93","#FFFFAA"} ,
            new string[2]{"#B766AD","#C07AB8"} ,
            new string[2]{"#B7FF4A","#C2FF68"}
        };
        //WPF的定时器使用DispatcherTimer类对象
        private System.Windows.Threading.DispatcherTimer dTimer = null;

        private int tmNum = 30;
        public MainWindow()
        {
            InitializeComponent();
            Load();
        }

        private void Load()
        {
            if (dTimer != null)
            {
                dTimer.Stop();
            }
            else
            {
                //Timer
                dTimer = new DispatcherTimer();
                //注:此处 Tick 为 dTimer 对象的事件( 超过计时器间隔时发生)
                dTimer.Tick += new EventHandler(dTimer_Tick);

                //设置时间:TimeSpan(时, 分, 秒)
                dTimer.Interval = new TimeSpan(0, 0, 1);
            }

            //启动 DispatcherTimer对象dTime。
            dTimer.Start();
            tmNum = 30;
            lblTime.Content = string.Format("剩余{0}秒", tmNum);

            cavWidth = cavContent.Width;
            btnWidth = cavWidth / level;
            int otherIndex = GetRandomNum(level * level); //不一样的那个

            lblLevel.Content = string.Format("第 {0} 关", level - 1);

            string[] strs = colors[GetRandomNum(colors.Length)];
            string colorBtnBg = strs[0];
            for (int i = 0; i < level; i++)
            {
                for (int j = 0; j < level; j++)
                {
                    //向容器中添加一个色块(按钮)
                    Button btn = new Button()
                    {
                        Margin = new Thickness(btnWidth * i, btnWidth * j, 0, 0),
                        Background = new SolidColorBrush((Color)System.Windows.Media.ColorConverter.ConvertFromString(colorBtnBg)),
                        BorderThickness = new Thickness(1),
                        BorderBrush = Brushes.Black,
                        Width = btnWidth,
                        Height = btnWidth
                    };
                    //随机生成的色块编号,颜色换成相近颜色,当点击该色块表示找出差异,进入游戏下一关
                    if (otherIndex == i * level + j)
                    {
                        btn.Background = new SolidColorBrush((Color)System.Windows.Media.ColorConverter.ConvertFromString(strs[1]));
                        btn.Click += btn_Click;
                    }
                    cavContent.Children.Add(btn);
                }
            }

        }

        /// <summary>
        /// 计时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dTimer_Tick(object sender, EventArgs e)
        {
            lblTime.Content = string.Format("剩余{0}秒", tmNum);
            if (tmNum < 10)
            {
                lblTime.Foreground = Brushes.Red;
            }
            if (tmNum <= 0)
            {
                dTimer.Stop();
                MessageBoxResult result = MessageBox.Show("时间到,还未完成游戏,是否继续?", "Game Over", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    level = 2;
                    Load(); //重置游戏
                }
                else
                {
                    this.Close();
                }
            }
            tmNum--;

        }

        /// <summary>
        /// 点击不同颜色的色块时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btn_Click(object sender, RoutedEventArgs e)
        {
            cavContent.Children.Clear();
            //达到最大关卡时数量不在增多,否则每次多一排一列色块
            if (level <= maxLevel)
            {
                level = level + 1;
            }
            Load();
        }


        /// <summary>
        /// 取随机数
        /// </summary>
        /// <param name="maxNum">0-最大区间</param>
        /// <returns></returns>
        public int GetRandomNum(int maxNum)
        {
            Random radom = new Random();
            return radom.Next(maxNum);
        }
    }

  

 

初学WPF,做一款小游戏练习一下

标签:

原文地址:http://www.cnblogs.com/chmc/p/5135725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!