在好例子网,分享、交流、成长!
您当前所在位置:首页Sliverlight 开发实例图形图像处理 → WPF动物连连看(漂亮)实例源码

WPF动物连连看(漂亮)实例源码

图形图像处理

下载此实例
  • 开发语言:Sliverlight
  • 实例大小:1.81M
  • 下载次数:26
  • 浏览次数:362
  • 发布时间:2018-04-22
  • 实例类别:图形图像处理
  • 发 布 人:lovezyb
  • 文件格式:.zip
  • 所需积分:5
 相关标签: wpf wp 连连看

实例介绍

【实例简介】WPF动物连连看

【实例截图】

from clipboard

【核心代码】WPF

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;
using System.Windows.Media.Animation;
using System.Threading;

namespace LianLianKan
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private GameGrid gamegrid;
        private AnimalItemCtl m_PreSelAnimalItemCtl;
        private AnimalItemCtl m_CurSelAnimalItemCtl;
        private GameCtl gamectl;
        private System.Timers.Timer m_timer;
        private int m_hittimes;

        public MainWindow() {
            InitializeComponent();
            m_PreSelAnimalItemCtl = null;
            m_CurSelAnimalItemCtl = null;
            m_timer = new System.Timers.Timer();
            m_timer.Enabled = false;
            m_timer.AutoReset = true;
            
            m_timer.Elapsed  = new System.Timers.ElapsedEventHandler(m_timer_Elapsed);
            m_hittimes = 0;

            GameGrid.Columns = 18;
            GameGrid.Rows = 12;
            SetItemWidth();
            gamegrid = new GameGrid();
            gamectl = new GameCtl();
            SetNextPrePassBtnStatus();
            this.tb_curpass.Text = string.Format("第{0}关", (int)gamectl.CurPass);
            this.tb_score.Text = string.Format("{0}次", m_hittimes);
            InitGameGird();
            LoadGameGrid();
            this.GameCanvasGrid.IsEnabled = false;
        }

        void m_timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
            try {
                this.progressBar1.Dispatcher.Invoke(
                    new Action(
                        delegate {
                            this.progressBar1.Value--;
                            if (this.progressBar1.Value <= 0) {
                                m_timer.Stop();
                                this.GameCanvasGrid.Visibility = System.Windows.Visibility.Hidden;
                                this.tb_lose.Visibility = System.Windows.Visibility.Visible;
                            }
                        }
                ));
            }
            catch (Exception ex) {
                MessageBox.Show(ex.ToString());
            }
        }
        /// <summary>
        /// 设置可拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TitleGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
            this.DragMove();
        }
        /// <summary>
        /// 初始化游戏网格并计算margin
        /// </summary>
        private void InitGameGird() {
            gamegrid.SetWandH(this.GameCanvas.Width, this.GameCanvas.Height);
            do {
                gamegrid.GetAnimalGrid();
            } while (gamegrid.IsLock());
            //gamegrid.GetAnimalGrid();
            this.GameCanvasGrid.Margin = new Thickness(gamegrid.leftmargin   AnimalItem.AnimalImageWidth / 2, gamegrid.topmargin   AnimalItem.AnimalImageWidth / 2, 
                gamegrid.leftmargin   AnimalItem.AnimalImageWidth / 2, gamegrid.topmargin   AnimalItem.AnimalImageWidth / 2);
        }

        /// <summary>
        /// 设置item的宽度
        /// </summary>
        private void SetItemWidth() {
            //double itemheight = this.GameCanvas.Height / (GameGrid.Rows   1);
            //double itemwidth = this.GameCanvas.Width / (GameGrid.Columns   1);
            //AnimalItem.AnimalImageWidth = itemheight > itemwidth ? itemwidth : itemheight;
            AnimalItem.AnimalImageWidth = 35;
        }
        /// <summary>
        /// 加载网格图片
        /// </summary>
        private void LoadGameGrid() {
            int i, j;
            for (i = 0; i < GameGrid.Rows; i  ) {
                RowDefinition row = new RowDefinition();
                row.Height = new GridLength(AnimalItem.AnimalImageWidth, GridUnitType.Pixel);
                this.GameCanvasGrid.RowDefinitions.Add(row);
            }
            for (j = 0; j < GameGrid.Columns; j  ) {
                ColumnDefinition column = new ColumnDefinition();
                column.Width = new GridLength(AnimalItem.AnimalImageWidth, GridUnitType.Pixel);
                this.GameCanvasGrid.ColumnDefinitions.Add(column);
            }

            foreach(List<AnimalItem> animalrow in gamegrid.AnimalGrid){
                foreach(AnimalItem animal in animalrow){
                    AnimalItemCtl itemctl = new AnimalItemCtl();
                    itemctl.Animal = animal;
                    this.GameCanvasGrid.Children.Add(itemctl);
                    itemctl.SetValue(Grid.RowProperty, animal.Y-1);
                    itemctl.SetValue(Grid.ColumnProperty, animal.X-1);
                }
            }
        }
        /// <summary>
        /// 处理某个图片被单击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AnimalItemClicked(object sender, RoutedEventArgs e) {
            AnimalItemCtl item = e.Source as AnimalItemCtl;
            e.Handled = false;
            if (m_CurSelAnimalItemCtl != null) {
                item.SetCheckStatus(false);
                return;
            }
            if (m_PreSelAnimalItemCtl == null) {
                m_PreSelAnimalItemCtl = item;
            } else {
                if (m_PreSelAnimalItemCtl == item) {
                    return;
                } else {
                    if (m_PreSelAnimalItemCtl.Animal.AnimlNum != item.Animal.AnimlNum) {
                        m_PreSelAnimalItemCtl.SetCheckStatus(false);
                        m_PreSelAnimalItemCtl = item;
                    } else {
                        m_CurSelAnimalItemCtl = item;
                        //进行可消除判断
                        Polyline polyline = gamegrid.FindPath(m_PreSelAnimalItemCtl.Animal, m_CurSelAnimalItemCtl.Animal);

                        if (polyline != null) {
                            m_hittimes  ;
                            this.tb_score.Text = string.Format("{0}次", m_hittimes);
                            DoubleAnimation daV1 = new DoubleAnimation(1D, 0D, new Duration(TimeSpan.FromMilliseconds(500)));
                            DoubleAnimation daV2 = new DoubleAnimation(1D, 0D, new Duration(TimeSpan.FromMilliseconds(500)));
                            DoubleAnimation daV3 = new DoubleAnimation(1D, 0D, new Duration(TimeSpan.FromMilliseconds(500)));
                            daV1.Completed  = new EventHandler(daV_Completed);

                            this.GameCanvas.Children.Add(polyline);
                            polyline.BeginAnimation(UIElement.OpacityProperty, daV1);
                            m_PreSelAnimalItemCtl.BeginAnimation(UIElement.OpacityProperty, daV2);
                            m_CurSelAnimalItemCtl.BeginAnimation(UIElement.OpacityProperty, daV3);
                        } else {
                            m_PreSelAnimalItemCtl.SetCheckStatus(false);
                            m_PreSelAnimalItemCtl = m_CurSelAnimalItemCtl;
                            m_CurSelAnimalItemCtl = null;
                        }
                    }
                }
            }
        }

        void daV_Completed(object sender, EventArgs e) {
            this.GameCanvas.Children.Remove(sender as Polyline);
            this.GameCanvasGrid.Children.Remove(m_CurSelAnimalItemCtl);
            this.GameCanvasGrid.Children.Remove(m_PreSelAnimalItemCtl);
            gamegrid.Remove(m_PreSelAnimalItemCtl.Animal, m_CurSelAnimalItemCtl.Animal);
            m_CurSelAnimalItemCtl = null;
            m_PreSelAnimalItemCtl = null;
            if (this.GameCanvasGrid.Children.Count == 0) {
                this.tb_win.Visibility = System.Windows.Visibility.Visible;
                //解锁下一关
                gamectl.UnLockNextPass();
                SetNextPrePassBtnStatus();
                return;
            }
            if (gamegrid.IsLock()) {
                MessageBox.Show("无可连图片!");
                m_timer.Stop();
            }
        }

        private void btn_start_Click(object sender, RoutedEventArgs e) {
            this.GameCanvasGrid.IsEnabled = true;
            this.btn_start.IsEnabled = false;
            m_timer.Interval = gamectl.CurSpeed;
            m_timer.Start();
        }

        private void btn_new_Click(object sender, RoutedEventArgs e) {
            this.GameCanvasGrid.Children.Clear();
            this.tb_lose.Visibility = System.Windows.Visibility.Hidden;
            this.tb_win.Visibility = System.Windows.Visibility.Hidden;
            this.GameCanvasGrid.Visibility = System.Windows.Visibility.Visible;
            this.GameCanvasGrid.IsEnabled = false;
            m_hittimes = 0;
            this.tb_score.Text = string.Format("{0}次", m_hittimes);
            m_timer.Stop();
            this.progressBar1.Value = this.progressBar1.Maximum;
            InitGameGird();
            LoadGameGrid();
            this.btn_start.IsEnabled = true;
        }

        private void btn_prepass_Click(object sender, RoutedEventArgs e) {
            this.GameCanvasGrid.Children.Clear();
            this.tb_lose.Visibility = System.Windows.Visibility.Hidden;
            this.tb_win.Visibility = System.Windows.Visibility.Hidden;
            this.GameCanvasGrid.Visibility = System.Windows.Visibility.Visible;
            this.GameCanvasGrid.IsEnabled = false;
            m_hittimes = 0;
            m_timer.Stop();
            this.progressBar1.Value = this.progressBar1.Maximum;
            gamectl.IntoPrePass();
            this.tb_curpass.Text = string.Format("第{0}关", (int)gamectl.CurPass);
            this.tb_score.Text = string.Format("{0}次", m_hittimes);
            InitGameGird();
            LoadGameGrid();
            this.btn_start.IsEnabled = true;
            SetNextPrePassBtnStatus();
        }

        private void btn_nextpass_Click(object sender, RoutedEventArgs e) {
            this.GameCanvasGrid.Children.Clear();
            this.tb_lose.Visibility = System.Windows.Visibility.Hidden;
            this.tb_win.Visibility = System.Windows.Visibility.Hidden;
            this.GameCanvasGrid.Visibility = System.Windows.Visibility.Visible;
            this.GameCanvasGrid.IsEnabled = false;
            m_hittimes = 0;
            m_timer.Stop();
            this.progressBar1.Value = this.progressBar1.Maximum;
            gamectl.IntoNextPass();
            this.tb_curpass.Text = string.Format("第{0}关", (int)gamectl.CurPass);
            this.tb_score.Text = string.Format("{0}次", m_hittimes);
            InitGameGird();
            LoadGameGrid();
            this.btn_start.IsEnabled = true;
            SetNextPrePassBtnStatus();
        }

        private void btn_quit_Click(object sender, RoutedEventArgs e) {
            this.Close();           
        }

        private void SetNextPrePassBtnStatus() {
            this.btn_nextpass.IsEnabled = gamectl.NextPassStatus;
            this.btn_prepass.IsEnabled = gamectl.PrePassStatus;
        }
    }
}

标签: wpf wp 连连看

实例下载地址

WPF动物连连看(漂亮)实例源码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警