wpf 消消看(附源代码)
偶然间看到的消消看,就是三个或者以上的会消去,就随手写了写,当时还是汕头,那一天没事儿,刚好有些想法,就写了写,
只是一个初稿,没有积分或者下一关
大概思路:
整个界面9*9 81个格子,通过9行9列进行判定,消去还是重新生成,内部有两个线程,一个控制界面消去,另外一个控制图片从上往下落
//64位数组,存储64个图片 private ImageShow[,] imageList = new ImageShow[8, 8]; //图片固定大小尺寸 private const int ImageSize = 94; //两个已经被选中的图片 bool FirClick = false; bool SecClick = false; bool IsFir = false; int FirRow = 0; int FirCol = 0; int SecRow = 0; int SecCol = 0; Random rd = new Random(); //控制消失动画线程 DispatcherTimer dispathcertimer=new DispatcherTimer(); //控制新产生的图片动画 DispatcherTimer dispatchCreate = new DispatcherTimer();
因为有7个图片,所以随即选择图片,用自带的random就行,在图片类里有这两个方法
public void Hide() { DoubleAnimation shadeOut = new DoubleAnimation(0, new Duration(TimeSpan.FromSeconds(0.3))); shadeOut.Completed += new EventHandler(shadeOut_Completed); image.BeginAnimation(Image.OpacityProperty, shadeOut); } void shadeOut_Completed(object sender, EventArgs e) { DoubleAnimationUsingKeyFrames animation = new DoubleAnimationUsingKeyFrames(); animation.Duration = new Duration(TimeSpan.FromSeconds(0.3)); animation.KeyFrames.Add(new LinearDoubleKeyFrame(1.1, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.1)))); animation.KeyFrames.Add(new LinearDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.4)))); this.BeginAnimation(Image.OpacityProperty,animation, HandoffBehavior.SnapshotAndReplace); }
用动画控制图片的消去
主界面上主要是这个算法:
/// <summary> /// 后台启动动画填充事件 /// </summary> /// <param name="obj"></param> /// <param name="e"></param> private void dispatchCreate_Tick(object obj, EventArgs e) { //启动动画板 Storyboard sb = new Storyboard(); //ImageSize #region 以前写的算法 for (int i = 0; i < 8; i++) { //初始为第行,如果遇到状态是1的,则开始计数 int temp = 0; for (int j = 0; j < 8; j++) { if (imageList[j, i].GetImageState == 1) { temp++; if (j == 7) { //找到最顶端没有消失的图片,只用关注行就可以了 int judgy = j - temp - 1; for (int m = j; m >= 0; m--) { DoubleAnimation da; if (judgy >= 0) { imageList[m, i] = imageList[judgy, i]; da = new DoubleAnimation(ImageSize * judgy, ImageSize * m, new Duration (TimeSpan.FromMilliseconds(200 * temp))); //imageList[j-m-1, i].BeginAnimation(Canvas.TopProperty, da); } else { ImageShow imageshow = new ImageShow(rd.Next(1, 7), 0, 0); carr.Children.Add(imageshow); Canvas.SetLeft(imageshow, i * ImageSize); Canvas.SetTop(imageshow, judgy * ImageSize); imageList[m, i] = imageshow; da = new DoubleAnimation(ImageSize * judgy, ImageSize * m, new Duration (TimeSpan.FromMilliseconds(200 * temp))); //imageList[j - m - 1, i].BeginAnimation(Canvas.TopProperty, da); } Storyboard.SetTarget(da, imageList[m, i]); Storyboard.SetTargetProperty(da, new PropertyPath("(Canvas.Top)")); sb.Children.Add(da); judgy--; } temp = 0; } } else if (temp > 0 && imageList[j, i].GetImageState == 0) { //找到最顶端没有消失的图片,只用关注行就可以了 int judgy = j - temp - 1; for (int m = j - 1; m >= 0; m--) { DoubleAnimation da; if (judgy >= 0) { imageList[m, i] = imageList[judgy, i]; da = new DoubleAnimation(ImageSize * judgy, ImageSize * m, new Duration (TimeSpan.FromMilliseconds(200 * temp))); //imageList[j-m-1, i].BeginAnimation(Canvas.TopProperty, da); } else { ImageShow imageshow = new ImageShow(rd.Next(1, 7), 0, 0); carr.Children.Add(imageshow); Canvas.SetLeft(imageshow, i * ImageSize); Canvas.SetTop(imageshow, judgy * ImageSize); imageList[m, i] = imageshow; da = new DoubleAnimation(ImageSize * judgy, ImageSize * m, new Duration (TimeSpan.FromMilliseconds(200 * temp))); //imageList[j - m - 1, i].BeginAnimation(Canvas.TopProperty, da); } Storyboard.SetTarget(da, imageList[m, i]); Storyboard.SetTargetProperty(da, new PropertyPath("(Canvas.Top)")); sb.Children.Add(da); judgy--; } temp = 0; } } } #endregion sb.Begin(); dispatchCreate.Stop(); for (int l = 0; l < 8; l++) { for (int k = 0; k < 8; k++) { imageList[l, k].GetImageState = 0; } } //dispathcertimer.Tick += new EventHandler(dispathcertimer_Tick); //dispathcertimer.Interval = TimeSpan.FromMilliseconds(60); dispathcertimer.Start(); }
通过9*9行列判断,判断哪个位置空缺,然后重新生成几个图片,从上往下用动画控制落下,没有专门写一个控制动画类
单纯为了功能而实现功能
消去和补充加上了,基本就是相当于一个消消看的雏形,至于积分或者下一关之类的,如果朋友有兴趣,可以继续弄,只是提供一个思路,代码风格或者冗余方法都不在考虑之中,毕竟只是一天时间突然奇想写的,会有bug和不足,仅作参考,若有兴趣,可以讨论
源码:files.cnblogs.com/fish124423/Dip.rar