This is a ball game that contains an animation, a ball, and a rectangle, I need to have a life counter in the game but I don't know how to programme it, each time the ball hits the rectangle the lives decrease by 1.
<pre lang="c#">
<phone:PhoneApplicationPage
x:Class="PhoneApp3.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.Resources>
<Storyboard x:Name="MyStory"
Storyboard.TargetName="RecTrans">
<DoubleAnimationUsingKeyFrames
Storyboard.TargetProperty="Y">
<EasingDoubleKeyFrame Value="200" KeyTime="0:0:1"></EasingDoubleKeyFrame>
<EasingDoubleKeyFrame Value="400" KeyTime="0:0:0.5"></EasingDoubleKeyFrame>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</phone:PhoneApplicationPage.Resources>
<!--
<Grid x:Name="LayoutRoot" removed="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12">
<TextBlock x:Name="ApplicationTitle" Text="MoveBallDemo" Style="{StaticResource PhoneTextNormalStyle}"/>
</StackPanel>
<Button Content="Start" Height="72" HorizontalAlignment="Left" x:Name="btnStart" VerticalAlignment="Top" Width="160" Click="btnStart_Click" Margin="8,537,0,0" Grid.Row="1" d:LayoutOverrides="HorizontalAlignment" />
<Button Content="Stop" Height="72" HorizontalAlignment="Left" x:Name="btnStop" VerticalAlignment="Top" Width="160" Click="btnStop_Click" Margin="168,537,0,0"
Grid.Row="1" />
<Canvas x:Name="ContentGrid" Margin="0,8,8,0" Grid.Row="1" HorizontalAlignment="Right" Width="472" Height="479" VerticalAlignment="Top">
<Rectangle Canvas.Left="384" Canvas.Top="0" Fill="#FF00B6FF" Height="483" Name="rectangle7" Stroke="Black" StrokeThickness="1" Width="96" />
<Rectangle Canvas.Left="100" Canvas.Top="0" Fill="#FF0065FF" Height="483" Name="rectangle5" Stroke="Black" StrokeThickness="1" Width="108" />
<Rectangle Canvas.Left="196" Canvas.Top="0" Fill="DeepSkyBlue" Height="483" Name="rectangle6" Stroke="Black" StrokeThickness="1" Width="112" />
<Rectangle Canvas.Left="298" Canvas.Top="0" Fill="#FF0065FF" Height="483" Name="rectangle8" Stroke="Black" StrokeThickness="1" Width="96" />
<Rectangle Fill="blue" Canvas.Left="115" Canvas.Top="182" Height="36" Name="rectangle1" Stroke="Black" StrokeThickness="1" Width="279" />
<Rectangle x:Name="test" Fill="Green" Height="49" Canvas.Left="220" Stroke="Black" Canvas.Top="-135" Width="46" Visibility="Collapsed">
<Rectangle.RenderTransform>
<TranslateTransform x:Name="RecTrans">
</TranslateTransform>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle Canvas.Left="0" Canvas.Top="0" Fill="DeepSkyBlue" Height="483" Name="rectangle2" Stroke="Black" StrokeThickness="1" Width="112" />
<Ellipse x:Name="ball" Canvas.Left="144" Fill="#FF963C3C" HorizontalAlignment="Left" Height="47" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="46" Canvas.Top="266" />
</Canvas>
</Grid>
<!--
<!--
</phone:PhoneApplicationPage>
namespace PhoneApp3
{
public partial class MainPage : PhoneApplicationPage
{
public int lives =3;
private Accelerometer _ac;
public MainPage()
{
InitializeComponent();
SupportedOrientations = SupportedPageOrientation.Portrait;
ball.SetValue(Canvas.LeftProperty, ContentGrid.Width / 2);
ball.SetValue(Canvas.TopProperty, ContentGrid.Height / 2);
_ac = new Accelerometer();
_ac.ReadingChanged += ac_ReadingChanged;
}
protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
{
test.Visibility = Visibility.Visible;
MyStory.Begin();
base.OnManipulationStarted(e);
}
private void ac_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(() => MyReadingChanged(e));
}
private void MyReadingChanged(AccelerometerReadingEventArgs e)
{
double distanceToTravel = 2;
double accelerationFactor = Math.Abs(e.Z) == 0 ? 0.1 : Math.Abs(e.Z);
double ballX = (double)ball.GetValue(Canvas.LeftProperty) + distanceToTravel * e.X / accelerationFactor;
double ballY = (double)ball.GetValue(Canvas.TopProperty) - distanceToTravel * e.Y / accelerationFactor;
Point topLeft = new Point(Convert.ToDouble(rectangle1.GetValue(Canvas.LeftProperty)), Convert.ToDouble(rectangle1.GetValue(Canvas.TopProperty)));
Point bottomRight = new Point(Convert.ToDouble(rectangle1.GetValue(Canvas.LeftProperty)) + rectangle1.Width, Convert.ToDouble(rectangle1.GetValue(Canvas.TopProperty)) + rectangle1.Height);
Rect temp = new Rect(topLeft, bottomRight);
if (temp.Contains(new Point(ballX, ballY)) || temp.Contains(new Point(ballX + ball.Height, ballY)) || temp.Contains(new Point(ballX, ballY + ball.Width)) || temp.Contains(new Point(ballX + ball.Height, ballY + ball.Width)))
{
ballX = 0 ;
ballY=0;
lives--;
}
else
{
if (ballX < 0) { ballX = 0; }
else if (ballX > ContentGrid.Width)
{ ballX = ContentGrid.Width; }
if (ballY < 0) { ballY = 0; }
else if (ballY > ContentGrid.Height)
{ ballY = ContentGrid.Height; }
}
ball.SetValue(Canvas.LeftProperty, ballX);
ball.SetValue(Canvas.TopProperty, ballY);
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{ if (_ac == null)
_ac = new Accelerometer();
_ac.Start();
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{ if (_ac == null)
_ac = new Accelerometer();
_ac.Stop(); }
}
}