Then continue on a ~ ~ ~
6, increase the score animation
/// <summary> /// 动画显示增加得分 /// </summary> /// <param name="addScore"></param> private void ShowAddScore(int addScore) { lblAddScore.Content = "+" + addScore.ToString(); DoubleAnimation top = new DoubleAnimation(); DoubleAnimation opacity = new DoubleAnimation(); opacity.AutoReverse = true; opacity.From = 0; opacity.To = 1; top.From = 0; top.To = -40; Duration duration = new Duration(TimeSpan.FromMilliseconds(500)); top.Duration = duration; opacity.Duration = duration; tt.BeginAnimation(TranslateTransform.YProperty, top); lblAddScore.BeginAnimation(Label.OpacityProperty, opacity); }
The animation is achieved by upward movement of the position and changes in the transparency.
7, operation of the mobile
7.1 mobile operating method
every depression of arrow keys, a corresponding method for operating the mobile call. Previously implemented, the code more complicated, this did not go to clean up the streamlined look like. This is the left shift method:
/// <Summary> /// left /// </ Summary> Private void MoveLeft () { int Score = 0 ; your storyboard SB1 = new new your storyboard (); // remove left and middle empty block (L shift) for ( int Y = 0 ; Y < . 4 ; Y ++ ) { for ( int X = 0 ; X < . 4 ; X ++ ) { for ( int I = X + . 1 ; I < . 4 ; I ++) { if (gridData[y, i] != 0 && gridData[y, x] == 0) { gridData[y, x] = gridData[y, i]; if (lblArray[y, i] == null) { lblArray[y, i] = new Label(); lblArray[y, i].SetValue(Canvas.LeftProperty, lblPadding * ((i) + 1) + (double)((i) * lblWidth)); lblArray[y, i].SetValue(Canvas.TopProperty, lblPadding * (y + 1) + (double)(y * lblWidth)); lblArray[y, i].SetValue(Label.ContentProperty, gridData[y, i].ToString()); lblArray[y, i].SetValue(Label.BackgroundProperty, SetBackground(gridData[y, i])); lblArray[y, i].SetValue(Button.FontSizeProperty, (double)SetFontSize(gridData[y, i])); } // 左移方块动画 DoubleAnimation da1 = null; double from = (double)lblArray[y, i].GetValue(Canvas.LeftProperty); double to = (x + 1) * lblPadding + x * lblWidth; da1 = new DoubleAnimation( from, to, new Duration(TimeSpan.FromMilliseconds(300))); da1.AccelerationRatio = 0.1; da1.DecelerationRatio = 0.1; Storyboard.SetTarget(da1, lblArray[y, i]); Storyboard.SetTargetProperty(da1, new PropertyPath("(Canvas.Left)")); sb1.Children.Add (DA1); gridData [Y, I] = 0 ; } } } } // adjacent to the same block merger added to the left for ( int Y = 0 ; Y < . 4 ; Y ++ ) { for ( int X = 0 ; X < . 4 ; ++ X ) { IF (X + . 1 < . 4 && gridData [Y, X] == gridData [Y, X + . 1 ]) { // if not timely generating the right block IF (gridData [Y, X + . 1] != 0)// && gridData[y,x]!=0) { if (lblArray[y, x + 1] == null) { lblArray[y, x + 1] = new Label(); lblArray[y, x + 1].SetValue(Canvas.LeftProperty, lblPadding * ((x + 1) + 1) + (double)((x + 1) * lblWidth)); lblArray[y, x + 1].SetValue(Canvas.TopProperty, lblPadding * (y + 1) + (double)(y * lblWidth)); lblArray[y, x + 1].SetValue(Label.ContentProperty, gridData[y, x + 1].ToString()); lblArray[y, x + 1].SetValue(Label.BackgroundProperty, SetBackground(gridData[y, x + 1])); lblArray[y, x + 1].SetValue(Button.FontSizeProperty, (double)SetFontSize(gridData[y, x + 1])); } // 左移动画 DoubleAnimation da2 = null; double from = (double)lblArray[y, x + 1].GetValue(Canvas.LeftProperty); double to = from - lblWidth - lblPadding; da2 = new DoubleAnimation( from, to, new Duration(TimeSpan.FromMilliseconds(200))); da2.AccelerationRatio = 0.1; da2.DecelerationRatio = 0.1; Storyboard.SetTarget(da2, lblArray[y, x + 1]); Storyboard.SetTargetProperty(da2, new PropertyPath("(Canvas.Left)")); sb1.Children.Add(da2); } gridData[y, x] *= 2; gridData[y, x + 1] = 0; score += gridData[y, x]; } } } if (score != 0) { ShowAddScore (Score); currScore + = Score; lblCurrScore.Content = currScore.ToString (); } // after the combined intermediate empty squares removed (once again left) for ( int Y = 0 ; Y < . 4 ; Y ++ ) { for ( int X = 0 ; X < . 4 ; X ++ ) { for ( int I = X + . 1 ; I < . 4 ; I ++ ) { IF ! (gridData [Y, I] = 0 && gridData[y, x] == 0) { gridData[y, x] = gridData[y, i]; if (lblArray[y, i] == null) { lblArray[y, i] = new Label(); lblArray[y, i].SetValue(Canvas.LeftProperty, lblPadding * ((i) + 1) + (double)((i) * lblWidth)); lblArray[y, i].SetValue(Canvas.TopProperty, lblPadding * (y + 1) + (double)(y * lblWidth)); lblArray[y, i].SetValue(Label.ContentProperty, gridData[y, i].ToString()); lblArray[y, i].SetValue(Label.BackgroundProperty, SetBackground(gridData[y, i])); lblArray[y, i].SetValue(Button.FontSizeProperty, (double)SetFontSize(gridData[y, i])); } // 左移动画 DoubleAnimation da = null; double from = (double)lblArray[y, i].GetValue(Canvas.LeftProperty); double to = (x + 1) * lblPadding + x * lblWidth; da = new DoubleAnimation( from, to, new Duration(TimeSpan.FromMilliseconds(200))); da.AccelerationRatio = 0.1; da.DecelerationRatio = 0.1; Storyboard.SetTarget(da, lblArray[y, i]); Storyboard.SetTargetProperty(da, new PropertyPath("(Canvas.Left)")); sb1.Children.Add(da); gridData[y, i] = 0; // isMove = to true; } } } } sb1.Completed + = Sb1_Completed; // perform all the event animation completes sb1.Begin (); } /// <Summary> /// after completion event operating /// < / Summary> /// <param name = "SENDER"> </ param> /// <param name = "E"> </ param> Private void Sb1_Completed ( Object SENDER, EventArgs E) { // check the game is ended IF (isGameOver ()) { ShowGameOver (); } else { NewNum(); ShowAllLabel(); } }
Similarly to the right, up and down method.
Add Window controls 7.2 xaml file keyDown event
KeyDown = "Window_KeyDown"
corresponding cs code is as follows:
private void Window_KeyDown(object sender, KeyEventArgs e) { if (!isStarted) return; switch (e.Key) { case Key.Left: if (!isGameOver()) MoveLeft(); else ShowGameOver(); break; case Key.Right: if (!isGameOver()) MoveRight(); else ShowGameOver(); break; case Key.Up: if (!isGameOver()) MoveUp(); else ShowGameOver(); break; case Key.Down: if (!isGameOver()) MoveDown(); else ShowGameOver(); break; } }
Much the same. Finally, take a look at it ~ ~ renderings