This time I really want to start the game ···
Returns to the previous 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 = "song" FontSize = "16"> </TextBlock> <Button Name = "btnBack" Grid. row = "2" Grid. column = "1" Content = "back to list" 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 Height = "10"/> <RowDefinition/> </Grid. rowDefinitions> <Grid. columnDefinitions> <ColumnDefinition/> <ColumnDefinition Width = "10"/> <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>
Go to Game. cs.
# Region variable // <summary> // background display speed/MS // </summary> private int speed = 80; /// <summary> /// number of images displayed in the background /// </summary> private int imgMax = 30; /// <summary> /// background image Index /// </summary> private int Index = 0; /// <summary> /// timer /// </summary> private Storyboard _ timer; /// <summary> /// background painter /// </summary> private ImageBrush _ imgBrush; /// <summary> /// play sound /// </summary> private MediaElement _ mediaSound; /// <summary> /// play music /// </summary> private MediaElement _ mediaMusic; /// <summary> /// Hit timer /// </summary> private Storyboard _ tmGame; /// <summary> /// game time progress/MS /// </summary> private int gamestep = 100; /// <summary> /// music object index /// </summary> private int musicIndex = 0; /// <summary> /// rhythm object /// </summary> private XML. musicHitValue _ musicHitValue; // <summary> // MusichHit display index /// </summary> private int gmIndex = 0; /// <summary> /// MusicHit time progress /// </summary> private int gmStep = 0; /// <summary> /// Number of combos /// </summary> private int downHits = 0; /// <summary> /// total score /// </summary> private int score = 0; /// <summary> /// music key /// </summary> private static List <MusicHit> _ musicHit; /// <summary> // music loading // </summary> private static ChildMusicLoading _ MusicLoading; # endregion
2. Okay. Then I started to figure out how to implement it.
Public Game (int _ musicIndex) {InitializeComponent (); // read the music index musicIndex = _ musicIndex; // create a background painter _ imgBrush = new ImageBrush (); // create a timer _ timer = new Storyboard (); _ timer. duration = new Duration (TimeSpan. fromMilliseconds (speed); _ timer. completed + = new EventHandler (_ timer_Completed); _ timer. begin (); // Add player _ mediaSound = new MediaElement (); _ mediaMusic = new MediaElement (); this. layoutRoot. children. add (_ mediaSound); this. layoutRoot. children. add (_ mediaMusic); // create 9 music keys _ musicHit = new List <MusicHit> (); _ musicHit. clear (); for (int I = 0; I <9; I ++) _ musicHit. add (new MusicHit (); // load music _ MusicLoading = new ChildMusicLoading (musicIndex); _ MusicLoading. show (); // Add a music Click Event foreach (Canvas cvs in gcMusicHit. children) cvs. mouseLeftButtonDown + = new MouseButtonEventHandler (cvs_MouseLeftButtonDown );}
The following are background Effects:
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 is it very familiar? Yes, this is the return value of the form for loading the progress of the song. I finally found out why? Why don't I have to return a True result to him?
Then go to GameStrat ().
/// <Summary> /// start of the game /// </summary> private void GameStart () {// txtTitle of the music name. text = Common. _ MusicValue [musicIndex]. musicName; // The Playing sound Common. soundUrl (ref _ mediaSound, "Music/Btn/readygound"); _ mediaSound. play (); Common. soundUrl (ref _ mediaMusic, Common. _ MusicValue [musicIndex]. musicPath); _ mediaMusic. play (); // get the music rhythm XML. reader _ reader = new XML. reader (); _ musicHitValue = _ reader. getHisValue (Common. _ MusicValue [musicIndex]. musicHit); // create timer _ tmGame = new Storyboard (); _ tmGame. duration = new Duration (TimeSpan. fromMilliseconds (gamestep); _ tmGame. completed + = new EventHandler (_ tmGame_Completed); _ tmGame. begin ();}
The following figure shows the effect of the rhythm chart.
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(); }
This sentence should be quite troublesome ····
Canvas cvs = (Canvas) this. gcMusicHit. children. first <UIElement> (x => (x as FrameworkElement ). name. startsWith ("imgMusic" + Index. toString ()));
Amount · if I have heard my opinion before, let's take a look at LINQ ·, And I will know about it. · find out the Header element that meets the condition (which is the condition. · Query MSDN · go
Button
/// <Summary> /// move the mouse over the button to highlight the screen /// </summary> /// <param name = "sender"> </param> // <param name = "e"> </param> private void btn_MouseMove (object sender, mouseEventArgs e) {(Button) sender ). opacity = 1 ;} /// <summary> /// restore by clicking the left button /// </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()); }
Click here to determine the click position
/// <Summary> /// determine the click position /// </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 {}}
Keyboard to play
private void UserControl_KeyDown(object sender, KeyEventArgs e) { string stringKey = e.Key.ToString(); if (stringKey.Contains("NumPad")) { selectMusicHitPos(int.Parse(stringKey.Replace("NumPad", ""))); } }
Play with the mouse
void cvs_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { try { int number = int.Parse(((Canvas)sender).Name.Replace("imgMusic", "")); selectMusicHitPos(number); } catch { } }
It's almost... try it now, haha.
It is estimated that the silverlight Magic Cube is over here.
····
Next chapter?
There is also the next chapter, and there is also a concert ,. What's more, tutorials!
In the next chapter, we will attach the Le motion cube compiler ~~
Silverlight magic cube. Practical Series
Name: 5 + x
Articles and books involved:
WPF sunflower collection