BabySmash7 – a wp7 children’s game (source code included)

BabySmash7 is a wp7 based version of Scott Hanselmans ‘BabySmash‘

“As babies smash on the phone, colored shapes, letters and numbers appear on the screen”

Go to the bottom of this article to find a link to the source code.

The MainPage.xaml is quite small and it’s dominated by the Canvas-object, where the colored shapes, letters and numbers will be added.

<Grid x:Name="LayoutRoot"> <Grid.Background> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FFA0A0A0" Offset="0"/> <GradientStop Color="Black" Offset="1"/> </LinearGradientBrush> </Grid.Background> <TextBlock x:Name="PageTitle" Opacity="0.8" Text="BabySmash7" Foreground="#FF4C4C4C" Margin="21,10,0,28" Style="{StaticResource PhoneTextTitle1Style}"/> <MediaElement x:Name="media" Width="0" Height="0" Visibility="Collapsed" /> <Canvas Name="figuresCanvas" Background="Transparent" MouseLeftButtonDown="figuresCanvas_MouseLeftButtonDown"/> </Grid>

The ‘figuresCanvas_MouseLeftButtonDown’ event is raised each time the user touches the screen.

private void figuresCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { PageTitle.Visibility = Visibility.Collapsed; addRandomUserControl(); playRandomSound(); if (figuresCanvas.Children.Count > 6) { figuresCanvas.Children.RemoveAt(0); } }

private void addRandomUserControl() { int randomNumber = Utils.RandomBetweenTwoNumbers(1, 4); //randomNumber = 1; switch (randomNumber) { case 1: addRandomLetter(); break; case 2: addRandomNumber(); break; case 3: addRandomSymbol(); break; case 4: addRandomSymbol(); break; } }

This method adds a random colored letter to the screen.

private void addRandomLetter() { UserControl userControl = new UserControl(); string[] numbers = new string[52] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; int randomNumber = Utils.RandomBetweenTwoNumbers(0, 51); userControl = new Shapes.CoolLetter(numbers[randomNumber].ToString(), Utils.GetRandomColor()); userControl.Width = 320; userControl.Height = 260; if (Orientation == PageOrientation.Landscape || Orientation == PageOrientation.LandscapeLeft || Orientation == PageOrientation.LandscapeRight) { Canvas.SetLeft(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(550))); Canvas.SetTop(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(250))); } else { Canvas.SetLeft(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(260))); Canvas.SetTop(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(550))); } figuresCanvas.Children.Add(userControl); }

This method adds a random colored number to the screen.

private void addRandomNumber() { UserControl userControl = new UserControl(); int[] numbers = new int[10] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int randomNumber = Utils.RandomBetweenTwoNumbers(0, 9); userControl = new Shapes.CoolLetter(numbers[randomNumber].ToString(), Utils.GetRandomColor()); userControl.Width = 320; userControl.Height = 260; if (Orientation == PageOrientation.Landscape || Orientation == PageOrientation.LandscapeLeft || Orientation == PageOrientation.LandscapeRight) { Canvas.SetLeft(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(600))); Canvas.SetTop(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(250))); } else { Canvas.SetLeft(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(300))); Canvas.SetTop(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(550))); } figuresCanvas.Children.Add(userControl); }

This method adds a random colored symbol to the screen.

private void addRandomSymbol() { UserControl userControl = new UserControl(); int randomNumber = Utils.RandomBetweenTwoNumbers(1, 7); Color randomColor = Utils.GetRandomColor(); switch (randomNumber) { case 1: userControl = new Shapes.CoolCircle(Utils.GetGradientBrush(randomColor)); break; case 2: userControl = new Shapes.CoolHeart(Utils.GetGradientBrush(randomColor)); break; case 3: userControl = new Shapes.CoolHexagon(Utils.GetGradientBrush(randomColor)); break; case 4: userControl = new Shapes.CoolRectangle(Utils.GetGradientBrush(randomColor)); break; case 5: userControl = new Shapes.CoolSquare(Utils.GetGradientBrush(randomColor)); break; case 6: userControl = new Shapes.CoolStar(Utils.GetGradientBrush(randomColor)); break; case 7: userControl = new Shapes.CoolTrapezoid(Utils.GetGradientBrush(randomColor)); break; case 8: userControl = new Shapes.CoolTriangle(Utils.GetGradientBrush(randomColor)); break; } userControl.Width = 100; //Utils.RandomBetweenTwoNumbers(0, 100); userControl.Height = 100; //Utils.RandomBetweenTwoNumbers(0, 100); //Canvas.SetLeft(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(this.ActualWidth - userControl.Width))); //Canvas.SetTop(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(this.ActualHeight - userControl.Height))); if (Orientation == PageOrientation.Landscape || Orientation == PageOrientation.LandscapeLeft || Orientation == PageOrientation.LandscapeRight) { Canvas.SetLeft(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(700))); Canvas.SetTop(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(300))); } else { Canvas.SetLeft(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(200))); Canvas.SetTop(userControl, Utils.RandomBetweenTwoNumbers(0, Convert.ToInt32(600))); } figuresCanvas.Children.Add(userControl); }

This method plays a random sound.

private void playRandomSound() { int randomSound = Utils.RandomBetweenTwoNumbers(1, 7); media.Source = new Uri("Audio/" + randomSound + ".wav", UriKind.Relative); media.Play(); }

Download the source code:

