这次真的要··开始游戏了···
回归上次Game.xaml
<UserControl x:Class="SilverlightMusicHit.Game"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="800" KeyDown="UserControl_KeyDown">
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="435"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Name="txtTitle" Grid.Row="0" Grid.Column="0" Text="歌曲" FontSize="16"></TextBlock>
<Button Name="btnBack" Grid.Row="2" Grid.Column="1" Content="返回列表" Opacity="0.5" Click="btnBack_Click" MouseLeave="btn_MouseLeave" MouseMove="btn_MouseMove"></Button>
<Grid Name="gcMusicHit" Grid.Row="1" Grid.Column="1" Height="435" Width="435" Opacity="0.9">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="10"/>
<RowDefinition/>
<RowDefinition Height="10"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="10"/>
<ColumnDefinition/>
<ColumnDefinition Width="10"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Canvas Background="White" Grid.Row="0" Grid.Column="1"/>
<Canvas Background="White" Grid.Row="0" Grid.Column="3"/>
<Canvas Background="White" Grid.Row="1" Grid.Column="0"/>
<Canvas Background="White" Grid.Row="1" Grid.Column="1"/>
<Canvas Background="White" Grid.Row="1" Grid.Column="2"/>
<Canvas Background="White" Grid.Row="1" Grid.Column="3"/>
<Canvas Background="White" Grid.Row="1" Grid.Column="4"/>
<Canvas Background="White" Grid.Row="2" Grid.Column="1"/>
<Canvas Background="White" Grid.Row="2" Grid.Column="3"/>
<Canvas Background="White" Grid.Row="3" Grid.Column="0"/>
<Canvas Background="White" Grid.Row="3" Grid.Column="1"/>
<Canvas Background="White" Grid.Row="3" Grid.Column="2"/>
<Canvas Background="White" Grid.Row="3" Grid.Column="3"/>
<Canvas Background="White" Grid.Row="3" Grid.Column="4"/>
<Canvas Background="White" Grid.Row="4" Grid.Column="1"/>
<Canvas Background="White" Grid.Row="4" Grid.Column="3"/>
<Canvas Background="Black" Name="imgMusic7" Grid.Row="0" Grid.Column="0"/>
<Canvas Background="Black" Name="imgMusic8" Grid.Row="0" Grid.Column="2"/>
<Canvas Background="Black" Name="imgMusic9" Grid.Row="0" Grid.Column="4"/>
<Canvas Background="Black" Name="imgMusic4" Grid.Row="2" Grid.Column="0"/>
<Canvas Background="Black" Name="imgMusic5" Grid.Row="2" Grid.Column="2"/>
<Canvas Background="Black" Name="imgMusic6" Grid.Row="2" Grid.Column="4"/>
<Canvas Background="Black" Name="imgMusic1" Grid.Row="4" Grid.Column="0"/>
<Canvas Background="Black" Name="imgMusic2" Grid.Row="4" Grid.Column="2"/>
<Canvas Background="Black" Name="imgMusic3" Grid.Row="4" Grid.Column="4"/>
</Grid>
<TextBox Grid.Column="1" Name="txtScore" Text="0" FontSize="40" Opacity="0.5" Foreground="#FF97AFC4" FontFamily="Comic Sans MS" Margin="0,0,0,0" />
<TextBox Grid.Column="2" Grid.Row="1" HorizontalAlignment="Left" Text="Hit" Width="182" Name="txtHit" FontFamily="Comic Sans MS" FontSize="32" FontWeight="Bold" Foreground="#FF2626B7" BorderBrush="#FF1747DE" Margin="0,200,0,200" Visibility="Collapsed" />
</Grid>
</UserControl>
再进入Game.cs
#region 变量
/// <summary>
/// 背景显示速度 /ms
/// </summary>
private int speed = 80;
/// <summary>
/// 背景显示图片数量
/// </summary>
private int imgMax = 30;
/// <summary>
/// 背景图片索引
/// </summary>
private int Index = 0;
/// <summary>
/// 定时器
/// </summary>
private Storyboard _timer;
/// <summary>
/// 背景画刷
/// </summary>
private ImageBrush _imgBrush;
/// <summary>
/// 播放声音
/// </summary>
private MediaElement _mediaSound;
/// <summary>
/// 播放音乐
/// </summary>
private MediaElement _mediaMusic;
/// <summary>
/// Hit定时器
/// </summary>
private Storyboard _tmGame;
/// <summary>
/// 游戏时间进度 /ms
/// </summary>
private int gamestep = 100;
/// <summary>
/// 音乐对象索引
/// </summary>
private int musicIndex = 0;
/// <summary>
/// 节奏对象
/// </summary>
private XML.MusicHitValue _musicHitValue;
/// <summary>
/// MusichHit显示索引
/// </summary>
private int gmIndex = 0;
/// <summary>
/// MusicHit时间进度
/// </summary>
private int gmStep = 0;
/// <summary>
/// 连击数
/// </summary>
private int downHits = 0;
/// <summary>
/// 总分
/// </summary>
private int score = 0;
/// <summary>
/// 音乐键
/// </summary>
private static List<MusicHit> _musicHit;
/// <summary>
/// 音乐加载
/// </summary>
private static ChildMusicLoading _MusicLoading;
#endregion
2、好了··接着开始想怎样实现了······· 不过有之前的铺垫,应该也不难理解的
public Game(int _musicIndex)
{
InitializeComponent();
//读取音乐索引
musicIndex = _musicIndex;
//创建背景画刷
_imgBrush = new ImageBrush();
//创建定时器
_timer = new Storyboard();
_timer.Duration = new Duration(TimeSpan.FromMilliseconds(speed));
_timer.Completed += new EventHandler(_timer_Completed);
_timer.Begin();
//添加播放器
_mediaSound = new MediaElement();
_mediaMusic = new MediaElement();
this.LayoutRoot.Children.Add(_mediaSound);
this.LayoutRoot.Children.Add(_mediaMusic);
//创建9个音乐键
_musicHit = new List<MusicHit>();
_musicHit.Clear();
for (int i = 0; i < 9; i++)
_musicHit.Add(new MusicHit());
//加载音乐
_MusicLoading = new ChildMusicLoading(musicIndex);
_MusicLoading.Show();
//新增音乐单击事件
foreach (Canvas cvs in gcMusicHit.Children)
cvs.MouseLeftButtonDown += new MouseButtonEventHandler(cvs_MouseLeftButtonDown);
}
以下是背景效果
void _timer_Completed(object sender, EventArgs e)
{
if (_MusicLoading.DialogResult == true)
{
GameStart();
_MusicLoading.DialogResult = false;
}
bool isHit = false;
string url = string.Format("{0}{1}{2}", "Image/picBackGround/background", Index, ".jpg");
_imgBrush.ImageSource = Common.GetGif(ref Index, ref imgMax, ref isHit, url, 1);
this.LayoutRoot.Background = _imgBrush;
_timer.Begin();
}
_MusicLoading.DialogResult 这个是不是很面熟? 没错,这个是加载歌曲进度窗体的返回值,终于发现为啥··要无端端返回个True给他吧····呵呵
接着进入GameStrat()了
/// <summary>
/// 游戏开始
/// </summary>
private void GameStart()
{
//音乐名
txtTitle.Text = Common._MusicValue[musicIndex].MusicName;
//播放声音
Common.SoundUrl(ref _mediaSound, "Music/Btn/readyGo.mp3");
_mediaSound.Play();
Common.SoundUrl(ref _mediaMusic, Common._MusicValue[musicIndex].MusicPath);
_mediaMusic.Play();
//获取音乐节奏
XML.Reader _reader = new XML.Reader();
_musicHitValue = _reader.GetHisValue(Common._MusicValue[musicIndex].MusicHit);
//创建定时器
_tmGame = new Storyboard();
_tmGame.Duration = new Duration(TimeSpan.FromMilliseconds(gamestep));
_tmGame.Completed += new EventHandler(_tmGame_Completed);
_tmGame.Begin();
}
以下是节奏图效果
void _tmGame_Completed(object sender, EventArgs e)
{
try
{
if (gmStep.Equals(_musicHitValue.MusicTimer[gmIndex]))
{
MusicHit musicHit = new MusicHit();
int Index = _musicHitValue.MusicHits[gmIndex];
Canvas cvs = (Canvas)this.gcMusicHit.Children.First<UIElement>(x => (x as FrameworkElement).Name.StartsWith("imgMusic" + Index.ToString()));
cvs.Children.Add(musicHit);
_musicHit[Index - 1] = musicHit;
gmIndex++;
}
}
catch { }
gmStep++;
_tmGame.Begin();
}
应该是这句比较麻烦了····
Canvas cvs = (Canvas)this.gcMusicHit.Children.First<UIElement>(x => (x as FrameworkElement).Name.StartsWith("imgMusic" + Index.ToString()));
额··这个如果之前有听我意见,看一下LINQ··,就会知道大概了··· 找出满足条件的头元素(里面就是条件了)额。···查MSDN····去
按钮
/// <summary>
/// 鼠标移过按钮高亮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_MouseMove(object sender, MouseEventArgs e)
{
((Button)sender).Opacity = 1;
}
/// <summary>
/// 鼠标离开按钮恢复
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_MouseLeave(object sender, MouseEventArgs e)
{
((Button)sender).Opacity = 0.5;
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
((App)(Application.Current)).RedirectTo(new Music());
}
以下判断点击位置
/// <summary>
/// 判断点击位置
/// </summary>
/// <param name="num"></param>
private void selectMusicHitPos(int num)
{
Hit hit = new Hit();
hit.HitStyle = 0;
try
{
if (_musicHit[num - 1].isMusicHit)
{
hit.hitDisIndex = 1;
downHits++;
txtHit.Visibility = System.Windows.Visibility.Visible;
}
else
{
hit.hitDisIndex = 0;
downHits = 0;
txtHit.Visibility = System.Windows.Visibility.Collapsed;
}
Canvas cvs = (Canvas)this.gcMusicHit.Children.First<UIElement>(x => (x as FrameworkElement).Name.StartsWith("imgMusic" + num.ToString()));
cvs.Children.Add(hit);
score += downHits * 10;
txtHit.Text = "Hit:" + downHits.ToString();
txtScore.Text = score.ToString();
}
catch { }
}
键盘来玩
private void UserControl_KeyDown(object sender, KeyEventArgs e)
{
string stringKey = e.Key.ToString();
if (stringKey.Contains("NumPad"))
{
selectMusicHitPos(int.Parse(stringKey.Replace("NumPad", "")));
}
}
鼠标来玩
void cvs_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
try
{
int number = int.Parse(((Canvas)sender).Name.Replace("imgMusic", ""));
selectMusicHitPos(number);
}
catch { }
}
差不多了····现在试试吧,哈。
估计silverlight乐动魔方 就在这里结束了
····
下章?
还有下章, 哈·····演唱会也有加场,。···何况 教程···嘻嘻!
下章将会附加 乐动魔方 编曲器~~
silverlight 乐动魔方 .实战系列
name:5+x
参与文章与书籍:
WPF葵花宝典