Share via


Silverlight Games: Add Images and use the mouse to move images

Moving images in Silverlight is pretty simple, the code works on the phone as well as on the web. Here is the simplest code I could come up with:

  1. Create a canvas
  2. Create a border for each image (for simplicity)
  3. Add images
  4. Add mouse events (for both phone and web, but it won’t work on a real phone)
  5. Add variables
  6. Add C# events for the mouse events

Step 1 and 2, Phone Silverlight:

For phone Silverlight, copy and paste the code below into the initial XAML Page, this assumes a single page of XAML which is default named MainPage.XAML, this creates a canvas and three images, but you haven’t added the Images yet. This effort completes steps 1 and 2, make sure that the line (2)

x:Class=”PaddleMove.MainPage” stays the same as the namespace in your project or it will throw an error:

Code Snippet

  1. <phone:PhoneApplicationPage
  2.     x:Class="PaddleMove.MainPage"
  3.     xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4.     xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
  7.     xmlns:d="https://schemas.microsoft.com/expression/blend/2008"
  8.     xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006"
  9.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
  10.     FontFamily="{StaticResource PhoneFontFamilyNormal}"
  11.     FontSize="{StaticResource PhoneFontSizeNormal}"
  12.     Foreground="{StaticResource PhoneForegroundBrush}"
  13.     SupportedOrientations="Portrait" Orientation="Portrait"
  14.     shell:SystemTray.IsVisible="True">
  15.  
  16.     <Canvas x:Name="Layout"
  17.             Background="White">
  18.  
  19.         <Border x:Name="paddle1"
  20.                 Canvas.Top="100"
  21.                 Canvas.Left="10"
  22.                 MouseMove="paddle1_MouseMove"
  23.                 MouseLeftButtonDown="paddle1_MouseLeftButtonDown"
  24.                 MouseLeftButtonUp="paddle1_MouseLeftButtonUp"
  25.                 >
  26.             <Image x:Name="Paddle1" Source="images/paddle1.png" Stretch="Uniform" ></Image>
  27.         </Border>
  28.  
  29.         <Border x:Name="Paddle2"
  30.                 Canvas.Top="100"
  31.                 Canvas.Left="100"
  32.                 MouseMove="paddle2_MouseMove"
  33.                 MouseLeftButtonDown="paddle2_MouseLeftButtonDown"
  34.                 MouseLeftButtonUp="paddle2_MouseLeftButtonUp"
  35.                 >
  36.             <Image x:Name="paddle2"
  37.                    Source="images/paddle2.png"
  38.                    Stretch="Uniform" >
  39.             </Image>
  40.         </Border>
  41.  
  42.         <Border x:Name="Ball"
  43.                 Canvas.Top="100"
  44.                 Canvas.Left="150"
  45.                 
  46.                 >
  47.             <Image x:Name="ball"
  48.                    Source="images/ball.png"
  49.                    Stretch="Uniform" >
  50.             </Image>
  51.         </Border>
  52.     </Canvas>
  53.  
  54. </phone:PhoneApplicationPage>

Step 1 and 2, Web based Silverlight:

For Web Based Silverlight, copy and paste all of the code below into the initial XAML Page, replacing all of the code, except for the first line, if you change the name of the class then the namespace has to be changed as well.

Code Snippet

  1. <UserControl x:Class="DragAndMove.Page"
  2.     xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.     xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  4.     Width="300" Height="200">
  5.  
  6.     <Canvas x:Name="Layout"
  7.             Background="White">
  8.         
  9.         <Border x:Name="paddle1"
  10.                 Canvas.Top="100"
  11.                 Canvas.Left="10"
  12.                 MouseMove="paddle1_MouseMove"
  13.                 MouseLeftButtonDown="paddle1_MouseLeftButtonDown"
  14.                 MouseLeftButtonUp="paddle1_MouseLeftButtonUp"
  15.                 >            
  16.                         <Image x:Name="Paddle1" Source="images/paddle1.png" Stretch="Uniform" ></Image>
  17.         </Border>
  18.  
  19.         <Border x:Name="Paddle2"
  20.                 Canvas.Top="100"
  21.                 Canvas.Left="100"
  22.                 MouseMove="paddle2_MouseMove"
  23.                 MouseLeftButtonDown="paddle2_MouseLeftButtonDown"
  24.                 MouseLeftButtonUp="paddle2_MouseLeftButtonUp"
  25.                 >
  26.             <Image x:Name="paddle2"
  27.                    Source="images/paddle2.png"
  28.                    Stretch="Uniform" >                
  29.             </Image>
  30.         </Border>
  31.  
  32.     </Canvas>

Step 3 Add Images:

To add the images do the following, select “Existing Item…” and go to the location where your images are located, this completes step 3:

image

Step 4: Add the “Code behind”, code is the same for both projects:

Cut and paste the code below, in the Web Silverlight you will need to change the “PhoneApplicationPage” to “UserControl”. Make sure that the namespace matches.

Bear in mind that the Phone App won’t be able to use the mouse on the real application, but for now, the mouse gives you feedback that you have operational code.

Code Snippet

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using Microsoft.Phone.Controls;
  13.  
  14. namespace PaddleMove
  15. {
  16.     public partial class MainPage : PhoneApplicationPage
  17.     {
  18.         private bool moving = false;
  19.  
  20.         private double offSetX;
  21.         private double offSetY;
  22.         // Constructor
  23.         public MainPage()
  24.         {
  25.             InitializeComponent();
  26.         }
  27.  
  28.         private void paddle1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  29.         {
  30.             // Left mouse button clicked within border. start moving.
  31.             moving = true;
  32.  
  33.             Point offset = e.GetPosition(paddle1);
  34.             offSetX = offset.X;
  35.             offSetY = offset.Y;
  36.         }
  37.  
  38.         private void paddle1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  39.         {
  40.             // Left mouse button release. Stop moving.
  41.             moving = false;
  42.         }
  43.  
  44.         private void paddle1_MouseMove(object sender, MouseEventArgs e)
  45.         {
  46.             if (moving)
  47.             {
  48.                 // Get the new mouse pointer position
  49.                 Canvas parent = (Canvas)this.paddle1.Parent;
  50.                 Point p = e.GetPosition(parent);
  51.                 double x = p.X - offSetX;
  52.                 double y = p.Y - offSetY;
  53.  
  54.                 // Set the new position for the border control.
  55.                 this.paddle1.SetValue(Canvas.LeftProperty, x);
  56.                 this.paddle1.SetValue(Canvas.TopProperty, y);
  57.             }
  58.         }
  59.  
  60.         private void paddle2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  61.         {
  62.             // Left mouse button clicked within border. start moving.
  63.             moving = true;
  64.  
  65.             Point offset = e.GetPosition(Paddle2);
  66.             offSetX = offset.X;
  67.             offSetY = offset.Y;
  68.         }
  69.  
  70.         private void paddle2_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  71.         {
  72.             // Left mouse button release. Stop moving.
  73.             moving = false;
  74.         }
  75.  
  76.         private void paddle2_MouseMove(object sender, MouseEventArgs e)
  77.         {
  78.             if (moving)
  79.             {
  80.                 // Get the new mouse pointer position
  81.                 Canvas parent = (Canvas)this.Paddle2.Parent;
  82.                 Point p = e.GetPosition(parent);
  83.                 double x = p.X - offSetX;
  84.                 double y = p.Y - offSetY;
  85.  
  86.                 // Set the new position for the border control.
  87.                 this.Paddle2.SetValue(Canvas.LeftProperty, x);
  88.                 this.Paddle2.SetValue(Canvas.TopProperty, y);
  89.             }
  90.         }
  91.     }
  92. }

That’s it, if you run the Windows Phone 7 App in the emulator, it should work. The same for the Web Silverlight.

If it doesn’t let me know. Leave a comment.