using System; using System.Collections.Generic; using System.Linq; 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 System.Windows.Threading; using System.Windows.Resources; // StreamResourceInfo using System.Windows.Media.Imaging; // BitmapImage // IDOUDO - a simple simon game in silverlight // Rob 2008 // 24/06/2008 - updated to silverlight 2 beta2 namespace slOverclick { public partial class Page : UserControl { private bool usermode = false; private Random random = new Random(); private List sequence = new List(); private DispatcherTimer timer = new DispatcherTimer(); private int interval; private bool doneDelay; private int curr; private bool mute = false; private int highscore = 0; public Page() { InitializeComponent(); } /// /// button in this context is the centered button for starting a new game /// /// /// private void Button_Click(object sender, RoutedEventArgs e) { startbutton.MinHeight = 0; startbutton.Height = 0; startbutton.MinWidth = 0; startbutton.Width = 0; // startbutton.Visibility = Visibility.Collapsed; crashes silverlight 2 beta 1 levelUp(); levelUp(); interval = 500; clearAll(); playBack(); } /// /// starts a new thread which can sleep which processes each element of the sequence /// private void playBack() { Seq.Text = ""; curr = 0; usermode = false; // user can not interact (accept with mute) timer = new DispatcherTimer(); // this one line missing stopped the system working in random ways !! Silverlight 2 Beta2 timer.Interval = new TimeSpan(0,0,0,0,interval); timer.Tick += new EventHandler(playBackSeq); doneDelay = false; timer.Start(); } /// /// playback of an individual element, including delays between each /// void playBackSeq(object sender, EventArgs e) { if (curr >= sequence.Count) { timer.Stop(); curr = 0; usermode = true; // allow user interaction Seq.Text = String.Format("Sequence {0} of {1}",curr+1,sequence.Count); return; } if (!doneDelay) { doneDelay = true; playSound(sequence[curr]); switch (sequence[curr]) { case 0: switchImg(redOn, redOff); break; case 1: switchImg(greenOn, greenOff); break; case 2: switchImg(yellowOn, yellowOff); break; case 3: switchImg(blueOn, blueOff); break; } } else { // clear all clearAll(); curr++; doneDelay = false; } } /// /// changes the visibility of two images, effectively switching one on and one off /// /// image to switch on /// image to switch off private void switchImg(Image imgOn, Image imgOff) { imgOn.SetValue(Canvas.ZIndexProperty, 2); imgOff.SetValue(Canvas.ZIndexProperty, 1); } /// /// sets all images to background /// private void clearAll() { switchImg(redOff, redOn); switchImg(greenOff, greenOn); switchImg(yellowOff, yellowOn); switchImg(blueOff, blueOn); } /// /// increase the level by adding a new element to the sequence /// private void levelUp() { sequence.Add(random.Next(0,4)); // random 'next' Level.Text = "Level " + (sequence.Count-1).ToString(); interval -= interval / 10; // game get faster the better you are } /// /// always sets images to show background only /// /// /// private void Image_MouseLeave(object sender, MouseEventArgs e) { if (!usermode) return; Image i = (Image)sender; switch (i.Name) { case "redOn": switchImg(redOff, redOn); break; case "greenOn": switchImg(greenOff, greenOn); break; case "yellowOn": switchImg(yellowOff, yellowOn); break; case "blueOn": switchImg(blueOff, blueOn); break; } } /// /// handle mouse button down /// /// /// private void MouseLeftButton_Down(object sender, MouseButtonEventArgs e) { if (!usermode) return; Image i = (Image)sender; switch (i.Name) { case "redOff": switchImg(redOn, redOff); break; case "greenOff": switchImg(greenOn, greenOff); break; case "yellowOff": switchImg(yellowOn, yellowOff); break; case "blueOff": switchImg(blueOn, blueOff); break; } } /// /// check the users selection is in the sequence /// then handles high scores /// /// users choice private void checkSeq(int cho) { if (cho == sequence[curr]) { curr++; Seq.Text = String.Format("Sequence {0} of {1}", curr + 1, sequence.Count); if (curr >= sequence.Count) { clearAll(); levelUp(); playBack(); } } else { usermode = false; Seq.Text = String.Format("Sequence failure at {0} of {1}, GAME OVER", curr + 1, sequence.Count); if (sequence.Count-1 > highscore) { Highscore.Text = String.Format ("High Score: {0} - NEW HIGH SCORE !! ", (sequence.Count-1).ToString()); highscore = sequence.Count-1; } else { Highscore.Text = String.Format ("High Score: {0}", highscore.ToString()); } sequence.Clear(); startbutton.Height = 30; startbutton.Width = 90; } } /// /// handle clicks, check if sequence is OK, advance sequence /// /// /// private void MouseLeftButton_Up(object sender, MouseButtonEventArgs e) { if (!usermode) return; Image i = (Image)sender; switch (i.Name) { case "redOn": switchImg(redOff, redOn); playSound(0); checkSeq(0); break; case "greenOn": switchImg(greenOff, greenOn); playSound(1); checkSeq(1); break; case "yellowOn": switchImg(yellowOff, yellowOn); playSound(2); checkSeq(2); break; case "blueOn": switchImg(blueOff, blueOn); playSound(3); checkSeq(3); break; } } /// /// plays one of the tones from the resource collection /// /// 0-3 tone private void playSound(int sel) { if (mute ) return; StreamResourceInfo sr = Application.GetResourceStream(new Uri("slOverclick;component/" + sel.ToString() + ".mp3", UriKind.Relative)); MediaElement me = new MediaElement(); me.SetSource(sr.Stream); me.AutoPlay = true; } /// /// either mutes or unmutes sound /// /// /// private void Mute_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { Canvas c = (Canvas)sender; if (c.Name == "ear") { mute = true; earmute.Visibility = Visibility.Visible; } else { mute = false; earmute.Visibility = Visibility.Collapsed; } } } }