Bounding ball : Ball « Animations « Silverlight






Bounding ball

Bounding ball
    

<UserControl x:Class='SilverlightApplication3.MainPage'
    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:DesignWidth='640' 
    d:DesignHeight='480'>
  <UserControl.Resources>
    <Storyboard x:Name="storyBoard1" BeginTime="0:00:00" Duration="0:00:00" />
  </UserControl.Resources>
  <Grid x:Name="LayoutRoot" Background="LightBlue">
    <Canvas x:Name="Arena" Width="400" Height="300">
      <Ellipse x:Name="Ball" Fill="DarkBlue" Width="50" Height="50" Canvas.Left="175" Canvas.Top="125" />
    </Canvas>
  </Grid>
</UserControl>


//File: Page.xaml.cs

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;

namespace SilverlightApplication3
{
    public partial class MainPage : UserControl
    {
        private Vector v;
        private double Velocity;

        public MainPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {

            Velocity = 5;
            Random r = new Random(DateTime.Now.Second);

            v = new Vector();
            v.X = r.NextDouble();
            v.Y = r.NextDouble();

            v.Velocity = Velocity;
            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
        }

        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            double currentX = Convert.ToDouble(Ball.GetValue(Canvas.LeftProperty));
            double currentY = Convert.ToDouble(Ball.GetValue(Canvas.TopProperty));

            if (v.Velocity < 10)
            {
                v.Velocity++;
            }

            if ((currentX + Ball.Width) >= Arena.Width)
            {
                v.X = -1;
                v.Velocity = 5;
            }

            if (currentX < 0)
            {
                v.X = 1;
                v.Velocity = 5;
            }

            if ((currentY + Ball.Height) >= Arena.Height)
            {
                v.Y = -1;
                v.Velocity = 5;
            }

            if (currentY < 0)
            {
                v.Y = 1;
                v.Velocity = 5;
            }

            Ball.SetValue(Canvas.LeftProperty, currentX + (v.X * v.Velocity));
            Ball.SetValue(Canvas.TopProperty, currentY + (v.Y * v.Velocity));
        }
    }

    public class Vector
    {
        public double X { get; set; }
        public double Y { get; set; }

        public double Velocity { get; set; }
    }
}

   
    
    
    
  








Related examples in the same category

1.Bouncing Ball with DoubleAnimationUsingKeyFrames
2.Bouncing Ball with DoubleAnimation
3.Ball moves in a constant speedBall moves in a constant speed
4.Ball moves following spline key framesBall moves following spline key frames