接着上一篇繼續~~~動畫
六、動畫顯示增長分數spa
/// <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); }
該動畫經過位置向上移動和透明度變化實現。code
七、移動操做
7.1 移動操做方法
每按下一次上下左右鍵,則調用相應的移動操做方法。之前實現的,代碼比較繁瑣,此次沒去精簡清理,看看就好。這是左移操做方法:orm
/// <summary> /// 左移 /// </summary> private void MoveLeft() { int score = 0; Storyboard sb1 = new Storyboard(); // 移去左側和中間的空塊(左移) 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; } } } } // 相鄰相同方塊合併後加到左側 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 (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(); } // 將合併後出現的中間空方塊移去(再次左移一次) 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 = true; } } } } sb1.Completed += Sb1_Completed; // 全部動畫完成後執行事件 sb1.Begin(); } /// <summary> /// 動畫完成後運行事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Sb1_Completed(object sender, EventArgs e) { // 檢查遊戲是否結束 if (isGameOver()) { ShowGameOver(); } else { NewNum(); ShowAllLabel(); } }
向右、向上和向下的方法相似。
blog
7.2 xaml文件中添加Window控件的keyDown事件
KeyDown="Window_KeyDown"
對應的cs代碼以下:遊戲
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; } }
大體如此。最後看看效果圖吧~~事件