上接游戏人生Silverlight(2) - 趣味钢琴[Silverlight 2.0(c#)]
生活随笔
收集整理的這篇文章主要介紹了
上接游戏人生Silverlight(2) - 趣味钢琴[Silverlight 2.0(c#)]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
3、樂譜提示動畫
AnimationMusicBook.xaml.cs using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using YYPiano.Controls.Parts;
using System.Threading;
namespace YYPiano.Controls
{
????????/// <summary>
????????/// 樂譜動畫
????????/// </summary>
????????public partial class AnimationMusicBook : UserControl
????????{
????????????????/// <summary>
????????????????/// 當前進入到目標區域的按鍵集合(先進先出)
????????????????/// </summary>
????????????????private List<KeyHitModel> _currentKeys = new List<KeyHitModel>();
????????????????public AnimationMusicBook()
????????????????{
????????????????????????InitializeComponent();
????????????????}
????????????????/// <summary>
????????????????/// 啟動樂譜動畫
????????????????/// </summary>
????????????????/// <param name="code">樂譜編碼</param>
????????????????/// <returns>是否成功地啟動了樂譜動畫</returns>
????????????????public bool Start(string code)
????????????????{
????????????????????????code = code.ToUpper().Trim();
????????????????????????// 清除已有的 AnimationKey 控件
????????????????????????foreach (var c in root.Children)
????????????????????????{
????????????????????????????????var ak = c as AnimationKey;
????????????????????????????????ak.Stop();
????????????????????????}
????????????????????????root.Children.Clear();
????????????????????????_currentKeys.Clear();
????????????????????????// 把樂譜編碼解析為樂譜實體類(用于描述樂譜的每一音階)集合
????????????????????????var musicBook = new List<MusicBookModel>();
????????????????????????var countDelay = 0;
????????????????????????try
????????????????????????{
????????????????????????????????foreach (var s in code.Split(','))
????????????????????????????????{
????????????????????????????????????????var delay = int.Parse(s.Trim().Substring(1));
????????????????????????????????????????var key = Convert.ToChar(s.Trim().Substring(0, 1)).ToKey();
????????????????????????????????????????musicBook.Add(new MusicBookModel() { Length = countDelay, Key = key });
????????????????????????????????????????countDelay += delay;
????????????????????????????????}
????????????????????????}
????????????????????????catch (Exception)
????????????????????????{
????????????????????????????????return false;
????????????????????????}
????????????????????????// 在容器內放置相應的 AnimationKey 控件
????????????????????????for (int i = 0; i < musicBook.Count; i++)
????????????????????????{
????????????????????????????????AnimationKey key = new AnimationKey();
????????????????????????????????key.TargetIndex = i % 3;
????????????????????????????????key.Key = musicBook[i].Key;
????????????????????????????????key.BeginTime = TimeSpan.FromMilliseconds(musicBook[i].Length);
????????????????????????????????key.Inside += new EventHandler<PianoKeyEventArgs>(key_Inside);
????????????????????????????????key.Outside += new EventHandler<PianoKeyEventArgs>(key_Outside);
????????????????????????????????key.Start();
????????????????????????????????root.Children.Add(key);
????????????????????????}
????????????????????????return true;
????????????????}
????????????????/// <summary>
????????????????/// 按鍵進入目標區
????????????????/// </summary>
????????????????/// <param name="sender"></param>
????????????????/// <param name="e"></param>
????????????????void key_Inside(object sender, PianoKeyEventArgs e)
????????????????{
????????????????????????_currentKeys.Add(new KeyHitModel { Key = e.Key, Hit = false });
????????????????}
????????????????/// <summary>
????????????????/// 按鍵離開目標區
????????????????/// </summary>
????????????????/// <param name="sender"></param>
????????????????/// <param name="e"></param>
????????????????void key_Outside(object sender, PianoKeyEventArgs e)
????????????????{
????????????????????????// 獲取此次離開目標區的按鍵(進入到目標區域的按鍵集合的第一個成員)
????????????????????????var key = _currentKeys.First();
????????????????????????if (!key.Hit)
????????????????????????????????OnLost();
????????????????????????_currentKeys.RemoveAt(0);
????????????????}
????????????????/// <summary>
????????????????/// 指定的鍵值被敲擊后所執行的方法
????????????????/// </summary>
????????????????/// <param name="key">鍵值</param>
????????????????public void Play(Key key)
????????????????{
????????????????????????if (key >= Key.A && key <= Key.Z && _currentKeys.Where(p => !p.Hit).Count() > 0)
????????????????????????{
????????????????????????????????var validKey = _currentKeys.Where(p => !p.Hit && p.Key == key).FirstOrDefault();
????????????????????????????????if (validKey != null)
????????????????????????????????{
????????????????????????????????????????OnScore();
????????????????????????????????????????validKey.Hit = true;
????????????????????????????????}
????????????????????????????????else
????????????????????????????????{
????????????????????????????????????????OnLost();
????????????????????????????????}
????????????????????????}
????????????????}
????????????????/// <summary>
????????????????/// 按鍵敲擊正確的事件
????????????????/// </summary>
????????????????public event EventHandler<EventArgs> Score;
????????????????public void OnScore()
????????????????{
????????????????????????if (Score != null)
????????????????????????{
????????????????????????????????Score(this, new EventArgs());
????????????????????????}
????????????????}
????????????????/// <summary>
????????????????/// 按鍵敲擊錯誤或未及時敲擊的事件
????????????????/// </summary>
????????????????public event EventHandler<EventArgs> Lost;
????????????????public void OnLost()
????????????????{
????????????????????????if (Lost != null)
????????????????????????{
????????????????????????????????Lost(this, new EventArgs());
????????????????????????}
????????????????}
????????}
}
OK
[×××]
AnimationMusicBook.xaml.cs using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using YYPiano.Controls.Parts;
using System.Threading;
namespace YYPiano.Controls
{
????????/// <summary>
????????/// 樂譜動畫
????????/// </summary>
????????public partial class AnimationMusicBook : UserControl
????????{
????????????????/// <summary>
????????????????/// 當前進入到目標區域的按鍵集合(先進先出)
????????????????/// </summary>
????????????????private List<KeyHitModel> _currentKeys = new List<KeyHitModel>();
????????????????public AnimationMusicBook()
????????????????{
????????????????????????InitializeComponent();
????????????????}
????????????????/// <summary>
????????????????/// 啟動樂譜動畫
????????????????/// </summary>
????????????????/// <param name="code">樂譜編碼</param>
????????????????/// <returns>是否成功地啟動了樂譜動畫</returns>
????????????????public bool Start(string code)
????????????????{
????????????????????????code = code.ToUpper().Trim();
????????????????????????// 清除已有的 AnimationKey 控件
????????????????????????foreach (var c in root.Children)
????????????????????????{
????????????????????????????????var ak = c as AnimationKey;
????????????????????????????????ak.Stop();
????????????????????????}
????????????????????????root.Children.Clear();
????????????????????????_currentKeys.Clear();
????????????????????????// 把樂譜編碼解析為樂譜實體類(用于描述樂譜的每一音階)集合
????????????????????????var musicBook = new List<MusicBookModel>();
????????????????????????var countDelay = 0;
????????????????????????try
????????????????????????{
????????????????????????????????foreach (var s in code.Split(','))
????????????????????????????????{
????????????????????????????????????????var delay = int.Parse(s.Trim().Substring(1));
????????????????????????????????????????var key = Convert.ToChar(s.Trim().Substring(0, 1)).ToKey();
????????????????????????????????????????musicBook.Add(new MusicBookModel() { Length = countDelay, Key = key });
????????????????????????????????????????countDelay += delay;
????????????????????????????????}
????????????????????????}
????????????????????????catch (Exception)
????????????????????????{
????????????????????????????????return false;
????????????????????????}
????????????????????????// 在容器內放置相應的 AnimationKey 控件
????????????????????????for (int i = 0; i < musicBook.Count; i++)
????????????????????????{
????????????????????????????????AnimationKey key = new AnimationKey();
????????????????????????????????key.TargetIndex = i % 3;
????????????????????????????????key.Key = musicBook[i].Key;
????????????????????????????????key.BeginTime = TimeSpan.FromMilliseconds(musicBook[i].Length);
????????????????????????????????key.Inside += new EventHandler<PianoKeyEventArgs>(key_Inside);
????????????????????????????????key.Outside += new EventHandler<PianoKeyEventArgs>(key_Outside);
????????????????????????????????key.Start();
????????????????????????????????root.Children.Add(key);
????????????????????????}
????????????????????????return true;
????????????????}
????????????????/// <summary>
????????????????/// 按鍵進入目標區
????????????????/// </summary>
????????????????/// <param name="sender"></param>
????????????????/// <param name="e"></param>
????????????????void key_Inside(object sender, PianoKeyEventArgs e)
????????????????{
????????????????????????_currentKeys.Add(new KeyHitModel { Key = e.Key, Hit = false });
????????????????}
????????????????/// <summary>
????????????????/// 按鍵離開目標區
????????????????/// </summary>
????????????????/// <param name="sender"></param>
????????????????/// <param name="e"></param>
????????????????void key_Outside(object sender, PianoKeyEventArgs e)
????????????????{
????????????????????????// 獲取此次離開目標區的按鍵(進入到目標區域的按鍵集合的第一個成員)
????????????????????????var key = _currentKeys.First();
????????????????????????if (!key.Hit)
????????????????????????????????OnLost();
????????????????????????_currentKeys.RemoveAt(0);
????????????????}
????????????????/// <summary>
????????????????/// 指定的鍵值被敲擊后所執行的方法
????????????????/// </summary>
????????????????/// <param name="key">鍵值</param>
????????????????public void Play(Key key)
????????????????{
????????????????????????if (key >= Key.A && key <= Key.Z && _currentKeys.Where(p => !p.Hit).Count() > 0)
????????????????????????{
????????????????????????????????var validKey = _currentKeys.Where(p => !p.Hit && p.Key == key).FirstOrDefault();
????????????????????????????????if (validKey != null)
????????????????????????????????{
????????????????????????????????????????OnScore();
????????????????????????????????????????validKey.Hit = true;
????????????????????????????????}
????????????????????????????????else
????????????????????????????????{
????????????????????????????????????????OnLost();
????????????????????????????????}
????????????????????????}
????????????????}
????????????????/// <summary>
????????????????/// 按鍵敲擊正確的事件
????????????????/// </summary>
????????????????public event EventHandler<EventArgs> Score;
????????????????public void OnScore()
????????????????{
????????????????????????if (Score != null)
????????????????????????{
????????????????????????????????Score(this, new EventArgs());
????????????????????????}
????????????????}
????????????????/// <summary>
????????????????/// 按鍵敲擊錯誤或未及時敲擊的事件
????????????????/// </summary>
????????????????public event EventHandler<EventArgs> Lost;
????????????????public void OnLost()
????????????????{
????????????????????????if (Lost != null)
????????????????????????{
????????????????????????????????Lost(this, new EventArgs());
????????????????????????}
????????????????}
????????}
}
OK
[×××]
轉載于:https://blog.51cto.com/webabcd/345629
總結
以上是生活随笔為你收集整理的上接游戏人生Silverlight(2) - 趣味钢琴[Silverlight 2.0(c#)]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 当摄影师的山寨版奥巴马(转载)
- 下一篇: 多线程编程(15) - 多线程同步之 W