Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.
C#
<pre lang="c#">


HTML
<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>

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" removed="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <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>

    <!--Sample code showing usage of ApplicationBar-->
    <!--<phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
            <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
                <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>-->

</phone:PhoneApplicationPage>


C#
namespace PhoneApp3
{

    
    public partial class MainPage : PhoneApplicationPage
    {
        public int lives =3;
        private Accelerometer _ac;
        // Constructor
        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); 
           
            //Rect temp = new Rect((double)rectangle1.GetValue(Canvas.LeftProperty), (double)rectangle1.GetValue(Canvas.TopProperty), rectangle1.Width ,rectangle1.Height);



            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();        }    
        }   
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900