/// <summary> /// 创建并播放填充动画 /// </summary> private void FillNull() { List <bijou> changedBijou = new List <bijou>(); Storyboard sb = new Storyboard(); sb.Completed += (object sender1, EventArgs ea1) => { var sb2 = sender1 as Storyboard; sb2.Stop(); AnimationIsPlaying = false; foreach (var tempBijou in changedBijou) { tempBijou.RenderTransform.SetValue(TranslateTransform.YProperty, 0.0); } Resources.Remove("sbFill"); var erasableBijous = GetErasablebijou(); if (erasableBijous != null) { RemoveListBijou(erasableBijous); } }; Resources.Add("sbFill", sb); for (int i = 0; i < MaxColumn; i++) { int nullNum = 0; int row = 0; for (int j = 0; j < MaxRow; j++) { if (this[i, j] == null) { if (j > row) { row = j; } nullNum++; } } for (int k = row - nullNum; k >= 0 && nullNum != 0; k--) //添加被删除Bijou上面的Bijou下落动画 { DoubleAnimation da = new DoubleAnimation(); bijou tempBijou = this[i, k]; tempBijou.RenderTransform = new TranslateTransform(); tempBijou.RenderTransform.SetValue(TranslateTransform.YProperty, (double)-64 * nullNum); AddAnimationToStoryboard(sb, tempBijou.RenderTransform, "Y", 0.0, TimeSpan.FromMilliseconds(300 * nullNum)); this[tempBijou.Column, tempBijou.Row + nullNum] = tempBijou; changedBijou.Add(tempBijou); } for (int l = nullNum - 1; l >= 0 && nullNum != 0; l--) //创建新的Bijou填充已消除的 { int randomNum = GetRandomNum(); bijou tempBijou = new bijou(randomNum, i, l); tempBijou.RenderTransform = new TranslateTransform(); tempBijou.RenderTransform.SetValue(TranslateTransform.YProperty, (double)-64 * nullNum); AddAnimationToStoryboard(sb, tempBijou.RenderTransform, "Y", 0.0, TimeSpan.FromMilliseconds(300 * nullNum)); this[tempBijou.Column, tempBijou.Row] = tempBijou; changedBijou.Add(tempBijou); } } BeginAnimation(sb); }
/// <summary> /// 初始化游戏 /// </summary> public void InitGame() { this.Visibility = Visibility.Collapsed; for (int row = 0; row < MaxRow; row++) { for (int column = 0; column < MaxColumn; column++) { int type = GetRandomNum(); var tempbijou = new bijou(type, column, row); this[column, row] = tempbijou; } } while (true) { var earsablebijous = GetErasablebijou(); if (earsablebijous != null) { foreach (var tempBijou in earsablebijous) { var random = new Random(); int type = GetRandomNum(); this.LayoutRoot.Children.Remove(this[tempBijou.Column, tempBijou.Row]); this[tempBijou.Column, tempBijou.Row] = new bijou(type, tempBijou.Column, tempBijou.Row); } } else { break; } } this.Visibility = Visibility.Visible; }
private void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (e.OriginalSource != null && e.OriginalSource.GetType() == typeof(bijou)) { bijou tempbijou = (bijou)e.OriginalSource; tempbijou.ReleaseMouseCapture(); } }
private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (e.OriginalSource.GetType() == typeof(bijou)) { this.mouseUpBeforePoint = e.GetPosition(null); bijou tempbijou = (bijou)e.OriginalSource; tempbijou.CaptureMouse(); } }
/// <summary> /// 将指定的宝石向右移动一格 /// </summary> /// <param name="bijou">要移动的宝石</param> public void Right(bijou bijou, bool isRollBack) { var near = GetbijouByDirection(bijou, Direction.Right); if (near != null) { this.ExchangeLocation(bijou, near); //调换两个宝石的位置 AddingZindexBybijou(bijou, near); //让bijou的Zindex比near大 bijou.RenderTransform = new TranslateTransform(); bijou.RenderTransform.SetValue(TranslateTransform.XProperty, -64.0); //实际位置已经往上移了一位,所以往下平移64 near.RenderTransform = new TranslateTransform(); near.RenderTransform.SetValue(TranslateTransform.XProperty, 64.0); //实际位置已经往下移了一位,所以网上平移64 EventHandler neareh = new EventHandler((object sender1, EventArgs ea1) => { var da = sender1 as DoubleAnimation; sbAll.Children.Remove(da); bijou.RenderTransform.SetValue(TranslateTransform.XProperty, 0.0); //播放完后还原平移变换 near.RenderTransform.SetValue(TranslateTransform.XProperty, 0.0); }); EventHandler bijoueh = new EventHandler((object sender1, EventArgs ea1) => { var da = sender1 as DoubleAnimation; sbAll.Children.Remove(da); bijou.RenderTransform.SetValue(TranslateTransform.XProperty, 0.0); //播放完后还原平移变换 near.RenderTransform.SetValue(TranslateTransform.XProperty, 0.0); var erasableBjous = GetErasablebijou(); if (erasableBjous == null) { Right(near, false); } else { RemoveListBijou(erasableBjous); } }); this.AddAnimationToStoryboard(near.RenderTransform, "X", 0.0, TimeSpan.FromMilliseconds(300), neareh); //添加动画 if (isRollBack) { this.AddAnimationToStoryboard(bijou.RenderTransform, "X", 0.0, TimeSpan.FromMilliseconds(300), bijoueh); //添加动画 } else { this.AddAnimationToStoryboard(bijou.RenderTransform, "X", 0.0, TimeSpan.FromMilliseconds(300), neareh); //添加动画 } this.BeginAnimation(); } }
/// <summary> /// 调换两个bijou的位置 /// </summary> /// <param name="bijou1">第一个bijou</param> /// <param name="bijou2">第二个bijou</param> public void ExchangeLocation(bijou bijou1, bijou bijou2) { int bijou1Column = bijou1.Column; int bijou1Row = bijou1.Row; int bijou2Column = bijou2.Column; int bijou2Row = bijou2.Row; this[bijou1Column, bijou1Row] = bijou2; this[bijou2Column, bijou2Row] = bijou1; //int bijouColumn = bijou1.Column; //int bijouRow = bijou1.Row; //bijou1.Column = bijou2.Column; //bijou1.Row = bijou2.Row; //bijou2.Column = bijouColumn; //bijou2.Row = bijouRow; }
/// <summary> /// 获取指定bijou的指定方向的相邻bijou /// </summary> /// <param name="movedbijou">指定的bijou</param> /// <param name="direction">方向</param> /// <returns>相邻的bijou</returns> public bijou GetbijouByDirection(bijou movedbijou, Direction direction) { bijou returnbijou = null; switch (direction) { case Direction.Up: if (movedbijou.Row != 0) { returnbijou = this[movedbijou.Column, movedbijou.Row - 1]; } break; case Direction.Down: if (movedbijou.Row != MaxColumn - 1) { returnbijou = this[movedbijou.Column, movedbijou.Row + 1]; } break; case Direction.Left: if (movedbijou.Column != 0) { returnbijou = this[movedbijou.Column - 1, movedbijou.Row]; } break; case Direction.Right: if (movedbijou.Column != MaxColumn - 1) { returnbijou = this[movedbijou.Column + 1, movedbijou.Row]; } break; } return(returnbijou); }
/// <summary> /// 让第一个bijou的Zindex值比第二个大 /// </summary> /// <param name="addedbijou">被加Zindex值的bijou</param> /// <param name="?">参考值</param> public void AddingZindexBybijou(bijou addedbijou, bijou referencebijou) { Canvas.SetZIndex(addedbijou, Canvas.GetZIndex(referencebijou) + 1); }
/// <summary> /// 初始化游戏 /// </summary> public void InitGame() { #region 初始化宝石 this.Visibility = Visibility.Collapsed; for (int row = 0; row < MaxRow; row++) { for (int column = 0; column < MaxColumn; column++) { int type = GetRandomNum(); var tempbijou = new bijou(type, column, row); this[column, row] = tempbijou; } } #endregion #region 替换可消除宝石为不可消除宝石 while (true) { var earsablebijous = GetErasablebijou(); if (earsablebijous != null) { foreach (var tempBijou in earsablebijous) { var random = new Random(); int type = GetRandomNum(); this.LayoutRoot.Children.Remove(this[tempBijou.Column, tempBijou.Row]); this[tempBijou.Column, tempBijou.Row] = new bijou(type, tempBijou.Column, tempBijou.Row); } } else { break; } } #endregion #region 开始动画 var sbDown = new Storyboard(); Resources.Add("sbDown", sbDown); sbDown.Completed += (object sender1, EventArgs ea1) => { var sb = sender1 as Storyboard; sb.Stop(); Resources.Remove("sbDown"); foreach (var tempBijou2 in bijous) { tempBijou2.RenderTransform.SetValue(TranslateTransform.YProperty, 0.0); } AnimationIsPlaying = false; }; for (int column = 0; column < MaxColumn; column++) { for (int row = 0; row < MaxRow; row++) { var tempBijou = this[column, row]; tempBijou.RenderTransform = new TranslateTransform(); tempBijou.RenderTransform.SetValue(TranslateTransform.YProperty, -1024.0); AddAnimationToStoryboard(sbDown, tempBijou.RenderTransform, "Y", 0.0, TimeSpan.FromMilliseconds(2500 - row * 200)); } } this.Visibility = Visibility.Visible; BeginAnimation(sbDown); #endregion }
/// <summary> /// 创建并播放填充动画 /// </summary> private void FillNull() { List<bijou> changedBijou = new List<bijou>(); Storyboard sb = new Storyboard(); sb.Completed += (object sender1, EventArgs ea1) => { var sb2 = sender1 as Storyboard; sb2.Stop(); AnimationIsPlaying = false; foreach (var tempBijou in changedBijou) { tempBijou.RenderTransform.SetValue(TranslateTransform.YProperty, 0.0); } Resources.Remove("sbFill"); var erasableBijous = GetErasablebijou(); if (erasableBijous != null) { RemoveListBijou(erasableBijous); } }; Resources.Add("sbFill", sb); for (int i = 0; i < MaxColumn; i++) { int nullNum = 0; int row = 0; for (int j = 0; j < MaxRow; j++) { if (this[i, j] == null) { if (j > row) row = j; nullNum++; } } for (int k = row - nullNum; k >= 0 && nullNum != 0; k--) //添加被删除Bijou上面的Bijou下落动画 { DoubleAnimation da = new DoubleAnimation(); bijou tempBijou = this[i, k]; tempBijou.RenderTransform = new TranslateTransform(); tempBijou.RenderTransform.SetValue(TranslateTransform.YProperty, (double)-64 * nullNum); AddAnimationToStoryboard(sb, tempBijou.RenderTransform, "Y", 0.0, TimeSpan.FromMilliseconds(300 * nullNum)); this[tempBijou.Column, tempBijou.Row + nullNum] = tempBijou; changedBijou.Add(tempBijou); } for (int l = nullNum - 1; l >= 0 && nullNum != 0; l--) //创建新的Bijou填充已消除的 { int randomNum = GetRandomNum(); bijou tempBijou = new bijou(randomNum, i, l); tempBijou.RenderTransform = new TranslateTransform(); tempBijou.RenderTransform.SetValue(TranslateTransform.YProperty, (double)-64 * nullNum); AddAnimationToStoryboard(sb, tempBijou.RenderTransform, "Y", 0.0, TimeSpan.FromMilliseconds(300 * nullNum)); this[tempBijou.Column, tempBijou.Row] = tempBijou; changedBijou.Add(tempBijou); } } BeginAnimation(sb); }
/// <summary> /// 获取指定bijou的指定方向的相邻bijou /// </summary> /// <param name="movedbijou">指定的bijou</param> /// <param name="direction">方向</param> /// <returns>相邻的bijou</returns> public bijou GetbijouByDirection(bijou movedbijou, Direction direction) { bijou returnbijou = null; switch (direction) { case Direction.Up: if (movedbijou.Row != 0) { returnbijou = this[movedbijou.Column, movedbijou.Row - 1]; } break; case Direction.Down: if (movedbijou.Row != MaxColumn - 1) { returnbijou = this[movedbijou.Column, movedbijou.Row + 1]; } break; case Direction.Left: if (movedbijou.Column != 0) { returnbijou = this[movedbijou.Column - 1, movedbijou.Row]; } break; case Direction.Right: if (movedbijou.Column != MaxColumn - 1) { returnbijou = this[movedbijou.Column + 1, movedbijou.Row]; } break; } return returnbijou; }
/// <summary> /// 将指定的宝石向右移动一格 /// </summary> /// <param name="bijou">要移动的宝石</param> public void Right(bijou bijou, bool isRollBack) { var near = GetbijouByDirection(bijou, Direction.Right); if (near != null) { this.ExchangeLocation(bijou, near); //调换两个宝石的位置 AddingZindexBybijou(bijou, near); //让bijou的Zindex比near大 bijou.RenderTransform = new TranslateTransform(); bijou.RenderTransform.SetValue(TranslateTransform.XProperty, -64.0); //实际位置已经往上移了一位,所以往下平移64 near.RenderTransform = new TranslateTransform(); near.RenderTransform.SetValue(TranslateTransform.XProperty, 64.0); //实际位置已经往下移了一位,所以网上平移64 EventHandler neareh = new EventHandler((object sender1, EventArgs ea1) => { var da = sender1 as DoubleAnimation; sbAll.Children.Remove(da); bijou.RenderTransform.SetValue(TranslateTransform.XProperty, 0.0); //播放完后还原平移变换 near.RenderTransform.SetValue(TranslateTransform.XProperty, 0.0); }); EventHandler bijoueh = new EventHandler((object sender1, EventArgs ea1) => { var da = sender1 as DoubleAnimation; sbAll.Children.Remove(da); bijou.RenderTransform.SetValue(TranslateTransform.XProperty, 0.0); //播放完后还原平移变换 near.RenderTransform.SetValue(TranslateTransform.XProperty, 0.0); var erasableBjous = GetErasablebijou(); if (erasableBjous == null) { Right(near, false); } else { RemoveListBijou(erasableBjous); } }); this.AddAnimationToStoryboard(near.RenderTransform, "X", 0.0, TimeSpan.FromMilliseconds(300), neareh); //添加动画 if (isRollBack) this.AddAnimationToStoryboard(bijou.RenderTransform, "X", 0.0, TimeSpan.FromMilliseconds(300), bijoueh); //添加动画 else this.AddAnimationToStoryboard(bijou.RenderTransform, "X", 0.0, TimeSpan.FromMilliseconds(300), neareh); //添加动画 this.BeginAnimation(); } }
private void LayoutRoot_MouseMove(object sender, MouseEventArgs e) { if (e.OriginalSource != null && e.OriginalSource.GetType() == typeof(bijou) && !AnimationIsPlaying) { bijou movedbijou = (bijou)e.OriginalSource; if (movedbijou.isMouseLeftButtonDown) { Point mousePoint = e.GetPosition(null); double XCha = mousePoint.X - mouseUpBeforePoint.X; double YCha = mousePoint.Y - mouseUpBeforePoint.Y; double absXCha = Math.Abs(XCha); double absYCha = Math.Abs(YCha); #region 判断移动方向 if (absXCha > 10 || absYCha > 10) { if (XCha > 10 && YCha < 10) { if (absXCha > absYCha) { this.Right(movedbijou, true); } else { this.Up(movedbijou, true); } } else if (XCha > 10 && YCha > 10) { if (absXCha > absYCha) { this.Right(movedbijou, true); } else { this.Down(movedbijou, true); } } else if (XCha < 10 && YCha > 10) { if (absXCha > absYCha) { this.Left(movedbijou, true); } else { this.Down(movedbijou, true); } } else if (XCha < 10 && YCha < 10) { if (absXCha > absYCha) { this.Left(movedbijou, true); } else { this.Up(movedbijou, true); } } } #endregion } } }