<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Coding4Fun : gaming</title><link>http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx</link><description>Tags: gaming</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Creating a Pinball Game in Silverlight: Using the Physics Helper Library + Farseer Physics</title><link>http://blogs.msdn.com/coding4fun/archive/2009/11/30/9896181.aspx</link><pubDate>Mon, 30 Nov 2009 22:20:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9896181</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9896181.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9896181</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9896181</wfw:comment><description>&lt;p&gt;In this tutorial, we'll create a Silverlight pinball game using &lt;u&gt;Behaviors&lt;/u&gt;&lt;b&gt;, &lt;/b&gt;a new addition to Expression Blend 3 &amp;amp; Silverlight that allows you to create interactivity with little or no coding. &lt;/p&gt;  &lt;p&gt;Below you'll find a video and step-by-step walkthrough. &lt;/p&gt;  &lt;table border="0" cellspacing="0" cellpadding="0"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="157"&gt;Andy Beaulieu          &lt;br /&gt;&lt;a title="http://www.andybeaulieu.com/" href="http://www.andybeaulieu.com/"&gt;http://www.andybeaulieu.com/&lt;/a&gt;&amp;#160;&lt;/td&gt;        &lt;td valign="top" width="481"&gt;&lt;strong&gt;Run It: &lt;/strong&gt;&lt;a href="http://www.andybeaulieu.com/silverlight/3.0/pinballgame/Coding4FunPinball.htm"&gt;View The Demo&lt;/a&gt;           &lt;br /&gt;&lt;strong&gt;Code It: &lt;/strong&gt;&lt;a href="http://physicshelper.codeplex.com/Release/ProjectReleases.aspx"&gt;Download the Source&lt;/a&gt;           &lt;br /&gt;          &lt;br /&gt;&lt;strong&gt;Difficulty: &lt;/strong&gt;Intermediate           &lt;br /&gt;&lt;b&gt;Time Required:&lt;/b&gt; 4 Hour           &lt;br /&gt;&lt;strong&gt;Cost: &lt;/strong&gt;Free!           &lt;br /&gt;&lt;strong&gt;Software Needed: &lt;/strong&gt;&lt;a href="http://www.microsoft.com/expression/products/Blend_Overview.aspx"&gt;Microsoft Expression Blend 3&lt;/a&gt; (Free Trial), Microsoft Visual Studio 2008 or &lt;a href="http://www.microsoft.com/express/vcsharp/"&gt;Visual C# Express&lt;/a&gt;, &lt;a href="http://www.silverlight.net/GetStarted"&gt;Visual Studio Tools for Silverlight&lt;/a&gt;, &lt;a href="http://physicshelper.codeplex.com"&gt;Physics Helper Library&lt;/a&gt;.&amp;#160; To help get setup quicker, try &lt;a href="http://www.microsoft.com/web"&gt;Web Platform Installer&lt;/a&gt;.&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt; &lt;iframe height="500" src="http://www.coding4fun.net/media/slpinball/slpinball.html" frameborder="0" width="100%"&gt;&lt;/iframe&gt;&lt;a href="http://www.andybeaulieu.com/silverlight/3.0/pinballgame/coding4funtutorial.htm#_Toc237838546"&gt;&lt;/a&gt;  &lt;br /&gt;  &lt;h3 align="center"&gt;&lt;a href="http://www.coding4fun.net/media/slpinball/slpinball.html" target="_blank"&gt;Open the video player in a new window/tab&lt;/a&gt;&lt;/h3&gt;  &lt;h3&gt;&amp;#160;&lt;/h3&gt;  &lt;h3&gt;&lt;a name="_Toc237838538"&gt;Setup and Prerequisites&lt;/a&gt;&lt;/h3&gt;  &lt;p&gt;First you have to setup Physics Helper controls into Expression Blend. Follow these steps:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Download the Physics Helper ZIP file from &lt;a href="http://physicshelper.codeplex.com/Release/ProjectReleases.aspx"&gt;http://physicshelper.codeplex.com/Release/ProjectReleases.aspx&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Extract the ZIP file contents and dump them into a folder. &lt;/li&gt;    &lt;li&gt;Run &lt;b&gt;install.bat&lt;/b&gt;. (Note: this copies a few required assemblies to a folder in Blend 3.&lt;b&gt;)&lt;/b&gt;       &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image001.jpg" /&gt; &lt;/li&gt; &lt;/ol&gt;  &lt;h3&gt;&lt;a name="_Toc237838539"&gt;Getting Started &amp;amp; Making the Ball&lt;/a&gt;&lt;/h3&gt;  &lt;p&gt;Next, you create the controller and the pinball. Here's how.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Open Expression Blend 3 and create a new “Silverlight 3 Application + Website” named PinballGame.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image002.jpg" width="400" height="324" /&gt; &lt;/li&gt;    &lt;li&gt;Change your default layout container to a Canvas. You can do this in the Objects and Timeline panel by right-clicking LayoutRoot and selecting Change Layout Type/Canvas. Canvas layout containers are better for games because they allow for positioning of elements at absolute (x,y) coordinates.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image003.jpg" width="290" height="266" /&gt; &lt;/li&gt;    &lt;li&gt;Give the application a bigger default size. Select the UserControl element in the Objects and Timeline panel. Then, in the Properties panel, change the Width to 800 and the Height to 600.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image004.png" width="269" height="190" /&gt; &lt;/li&gt;    &lt;li&gt;Add a &lt;b&gt;Physics Controller Behavior&lt;/b&gt; to our main game Canvas. From the Asset Panel, find the PhysicsController Behavior and drag it to the Objects and Timeline Panel. Then, drop it on the LayoutRoot Canvas.       &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image005.jpg" width="233" height="253" /&gt;       &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image006.jpg" width="233" height="122" /&gt; &lt;/li&gt;    &lt;li&gt;Change a few of the PhysicsController's properties, to make it easier to use. In the Objects and Timeline Panel, select the PhysicsControllerBehavior assigned to LayoutRoot. Then, set the highlighted properties as follows in the Properties Window:      &lt;br /&gt;1.)&amp;#160; Decrease the vertical gravity to 250       &lt;br /&gt;2.)&amp;#160; Increase the iterations to 200. This will help with collision detection in a fast-action game.       &lt;br /&gt;3.)&amp;#160; Set MousePickEnabled to true. This will allow us to manipulate objects with the mouse during development and testing.       &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image007.png" width="261" height="218" /&gt; &lt;/li&gt;    &lt;li&gt;Create a &amp;quot;playing field&amp;quot; for the pinball. To do this, draw out a Rectangle onto the artboard, just off the bottom of the user control bounds. Set the properties of the Rectangle as follows:      &lt;br /&gt;      &lt;br /&gt;&lt;b&gt;Name&lt;/b&gt;: rectPlatform       &lt;br /&gt;&lt;b&gt;Fill&lt;/b&gt;: select a color of your choosing (light blue is selected below).       &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image008.jpg" width="364" height="317" /&gt; &lt;/li&gt;    &lt;li&gt;To make “rectPlatform” a physics element, we need to add a PhysicsObjectBehavior to it. Find the PhysicsObjectBehavior in the Assets panel and drag/drop it onto rectPlatform on the Artboard.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image009.png" width="343" height="369" /&gt; &lt;/li&gt;    &lt;li&gt;In the Objects and Timeline Panel, select the PhysicsObjectBehavior belonging to rectPlatform. Then in the Properties Panel, set the IsStatic property to True so that the platform will stay in place.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image010.png" width="264" height="173" /&gt; &lt;/li&gt;    &lt;li&gt;Next we’ll create the ball. Draw out an Ellipse near the very top of the UserControl and set the following properties:      &lt;br /&gt;&lt;b&gt;       &lt;br /&gt;Name: &lt;/b&gt;ellBall&lt;b&gt;        &lt;br /&gt;Height&lt;/b&gt;: 50       &lt;br /&gt;&lt;b&gt;Width&lt;/b&gt;: 50       &lt;br /&gt;&lt;b&gt;Fill&lt;/b&gt;: A color of your choosing.       &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image011.jpg" width="378" height="326" /&gt; &lt;/li&gt;    &lt;li&gt;To make the ball a Physics Object, we need to add a PhysicsObjectBehavior to it. From the Asset Library, drag and drop a PhysicsObjectBehavior onto ellBall. &lt;/li&gt;    &lt;li&gt;Run the project by clicking F5. The ball should fall and hit the platform. Use the mouse to manipulate the ball. &lt;/li&gt; &lt;/ol&gt;  &lt;h3&gt;&lt;a name="_Toc237838540"&gt;Making the Flippers&lt;/a&gt;&lt;/h3&gt;  &lt;p&gt;In a pinball game, the Flippers are controlled by the player and are used to move the ball up the playfield. Standard pinball games have one right and one left flipper, but many games have three or more flippers located in various locations on the playfield. We’ll create two user controls to represent the flippers – a right flipper and a left flipper – that way we can easily add as many flippers as we want to our main playfield.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;In the Projects Panel, right-click the PinballGame (Silverlight) project and select Add New Item. Select UserControl and name the control LeftFlipper.xaml.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image012.jpg" width="222" height="241" /&gt; &lt;/li&gt;    &lt;li&gt;In the Objects and Timeline Panel, right-click LayoutRoot and select Change Layout Type/Canvas. This is required for the Physics Helper to work with nested User Controls.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image013.jpg" width="256" height="281" /&gt; &lt;/li&gt;    &lt;li&gt;In the Objects and Timeline Panel, select the UserControl element and set its Width and Height to 200 x 200 pixels in the Properties Panel. &lt;/li&gt;    &lt;li&gt;Draw out a Rectangle that is about 200 x 50 pixels big, centered in the user control. Round the corners of the Rectangle.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image014.png" width="235" height="185" /&gt; &lt;/li&gt;    &lt;li&gt;From the main Blend menu, select Object/Path/Convert to Path. This will create a new Path element from the Rectangle primitive. Then select the Direct Selection tool from the Toolbox, which will allow you to manipulate the individual points on the Path.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image015.png" width="37" height="84" /&gt; &lt;/li&gt;    &lt;li&gt;Select the top, left two points on the Path and move them down so that you end up with a more “flipper like” shape:      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image016.png" width="224" height="171" /&gt; &lt;/li&gt;    &lt;li&gt;In the Toolbox, select the Selection arrow and rotate the flipper 45 degrees so that it looks similar to this (be sure the flipper is still within the bounds of the user control):      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image017.jpg" width="196" height="186" /&gt; &lt;/li&gt;    &lt;li&gt;Right-click the flipper Path and select Group Into/Canvas. Name the Canvas &lt;b&gt;cnvLeftFlipper&lt;/b&gt; and resize it so that it surrounds the entire flipper Path. Note that you could “embellish” the cnvLeftFlipper with additional elements if you wish. &lt;/li&gt;    &lt;li&gt;Next we’ll create a Joint so that the Flipper can rotate on an axis. We’ll need something to anchor the joint to, so first let’s add a small Static object to hold the Joint in place. Draw out a Rectangle at the upper left of the flipper and name the Rectangle &lt;b&gt;rectHolder&lt;/b&gt;.       &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image018.jpg" width="196" height="197" /&gt; &lt;/li&gt;    &lt;li&gt;To add a Joint, we will need a UI element to visually represent where we want the joint created. You can use any element you want for this, but an Ellipse works pretty well. Draw out an Ellipse on top of the rectHolder.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image019.jpg" width="196" height="197" /&gt; &lt;/li&gt;    &lt;li&gt;Now we can add all of the required Physics Behaviors to the User Control.      &lt;ol&gt;       &lt;li&gt;From the Asset Library, drag a PhysicsObjectBehavior to the rectHolder Rectangle. Set its IsStatic property to True. Since we don’t really want to see the rectHolder in our UI, just send it to the back of the elements (Right-click, then select Order/Send to Back). &lt;/li&gt;        &lt;li&gt;Drag a PhysicsObjectBehavior to the cnvLeftFlipper Canvas. Set the RestitutionCoefficent property to “0.1”. (This makes the flipper a bit more “bouncy” on collisions) &lt;/li&gt;        &lt;li&gt;Drag a PhysicsJointBehavior to the Ellipse. Set the Body1 property to “cnvLeftFlipper” and the Body2 property to “rectHolder”. Set the CollisionGroup to “1” and the AngleLimitLower and AngleLimitUpper to 0 and 50 respectively. Also enable the AngleSpring:          &lt;br /&gt;&lt;/li&gt;        &lt;li&gt;Drag a PhysicsApplyTorqueBehavior to cnvLeftFlipper. We want to apply Torque when the user presses the Left arrow key, so we’ll create a Trigger for this. Select the Behavior and in the Properties Panel, click the New Trigger button.          &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image021.png" width="267" height="191" /&gt;&amp;#160; &lt;br /&gt;          &lt;br /&gt;Select the PhysicsKeyTrigger type from the popup.           &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image022.jpg" width="197" height="296" /&gt;           &lt;br /&gt;Set the Key property for the Trigger to “Left” and the TorqueValue to -100000           &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image023.png" width="264" height="187" /&gt; &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;Build the Project so that the LeftFlipper user control is available. &lt;/li&gt;    &lt;li&gt;Open MainPage.xaml and drag an instance of LeftFlipper from the Asset Library onto the Artboard. Position it near the bottom of the user control and slightly to the Left.      &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image024.jpg" width="373" height="321" /&gt; &lt;/li&gt;    &lt;li&gt;Run the project and try the flipper with the Left arrow key. &lt;/li&gt;    &lt;li&gt;In this step, we’ll create a copy of “LeftFlipper” to make&amp;#160; a “RightFlipper”. In the Projects Panel, right-click LeftFlipper.xaml and select Copy. Then right-click the PinBallGame Project and select Paste.      &lt;br /&gt;You now have a new User Control named “Copy of LeftFlipper.xaml”. Rename this to RightFlipper.xaml. Then in the code-behind file, make sure the Class and Constructor are also renamed to “RightFlipper”.       &lt;br /&gt;Change to XAML view for RightFlipper.xaml and change the x:Class attribute so that it inherits from RightFlipper:&amp;#160; &lt;br /&gt;      &lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;       &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;UserControl&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #ff0000"&gt;xmlns&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #ff0000"&gt;xmlns:x&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #ff0000"&gt;xmlns:d&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;http://schemas.microsoft.com/expression/blend/2008&amp;quot;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #ff0000"&gt;xmlns:mc&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;http://schemas.openxmlformats.org/markup-compatibility/2006&amp;quot;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #ff0000"&gt;mc:Ignorable&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;d&amp;quot;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #ff0000"&gt;xmlns:i&lt;/span&gt;=&amp;amp;&lt;span style="color: #ff0000"&gt;amp&lt;/span&gt;;&lt;span style="color: #ff0000"&gt;quotclr-namespace:System&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;Windows&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;Interactivity&lt;/span&gt;;&lt;span style="color: #ff0000"&gt;assembly&lt;/span&gt;=&lt;span style="color: #ff0000"&gt;System&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;Windows&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;Interactivity&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;quot; &lt;br /&gt;    xmlns:pb=&amp;quot;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;clr-namespace:Spritehand&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;PhysicsBehaviors&lt;/span&gt;;&lt;span style="color: #ff0000"&gt;assembly&lt;/span&gt;=&lt;span style="color: #ff0000"&gt;Spritehand&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;PhysicsBehaviors&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;quot; &lt;br /&gt;x:Class=&amp;quot;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;PinballGame&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;RightFlipper&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;quot; &lt;br /&gt;d:DesignWidth=&amp;quot;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;640&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;quot; d:DesignHeight=&amp;quot;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;480&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;quot; Height=&amp;quot;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;200&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;quot; Width=&amp;quot;&lt;/span&gt;&lt;span style="color: #ff0000"&gt;200&lt;/span&gt;&amp;quot;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

      &lt;br /&gt;&lt;/div&gt;
Open RightFlipper.xaml and find the pathLeftFlipper element. Rename this to pathRightFlipper. Then, rename “cnvLeftFlipper” to “cnvRightFlipper”. Rename “rectHolder” to “rectRightHolder” and position the rectHolder and joint ellipse so that they are in the correct location for the flipper: 

    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image026.png" width="163" height="159" /&gt; 

    &lt;br /&gt;Modify the PhysicsJointBehavior for the joint in the Objects and Timeline Panel. Change BodyOne to “cnvRightFlipper”, BodyTwo to “rectRightHolder” and change the Angle limits so they are appropriate for the opposite flipper: 

    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image027.png" width="264" height="219" /&gt; 

    &lt;br /&gt;Select the PhysicsApplyTorqueBehavior for cnvRightFlipper in the Objects and Timeline Panel. Change the Key property for the trigger to “Right” so that it will initiate on the Right Arrow key down. Also change the TorqueValue so that it will apply torque clockwise: 

    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image028.png" width="265" height="190" /&gt; 

    &lt;br /&gt;&lt;/li&gt;

  &lt;li&gt;Add an instance of RightFlipper onto the MainPage.xaml and position it to the right of LeftFlipper. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image029.jpg" width="372" height="325" /&gt; &lt;/li&gt;

  &lt;li&gt;Test the project by clicking F5. You can now click left and right arrows keys to manipulate the paddles. &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;&lt;a name="_Toc237838541"&gt;Creating the Playfield + Scrolling&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Now let’s create more of a playfield for our Pinball game and add a Camera control so that the game will scroll and always follow the pinball. Feel free to tweak the design as you wish.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Open MainPage.xaml in Expression Blend and zoom out by using Ctrl plus minus (Ctrl -). &lt;/li&gt;

  &lt;li&gt;Draw out a Path using the Pen tool for the right border. Note that you can extend above the upper bounds of the User Control. Group this into a Canvas named “cnvLeftBorder” (right click the Path and select Group Into/Canvas). &lt;/li&gt;

  &lt;li&gt;Drag a PhysicsObjectBehavior from the Asset Library onto cnvLeftBorder and set the IsStatic property to True. Also set the CollisionGroup property to “1” so that collisions will not occur between the border and the Flippers. &lt;/li&gt;

  &lt;li&gt;Repeat Steps 2 and 3 above for the right border, naming that cnvRightBorder. You should have something similar to the following: 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image030.jpg" width="281" height="267" /&gt; &lt;/li&gt;

  &lt;li&gt;Continue adding border controls and applying physics behaviors until you are happy with your playfield. Make the playfield completely closed in so that the ball cannot escape (except by the bottom trap of course!) &lt;/li&gt;

  &lt;li&gt;You can also add more LeftFlipper and RightFlipper controls so that the player can more easily move the ball up the playfield. Here is one example playfield design: 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image031.jpg" width="238" height="336" /&gt; &lt;/li&gt;

  &lt;li&gt;To enable scrolling, we can add a Camera Controller to the ball, causing the camera to follow the ball around the screen. Drag a PhysicsCameraBehavior from the Asset Library onto the ellBall element. &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;&lt;a name="_Toc237838542"&gt;Creating a Kicking Target&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Pinball games have lots of different types of targets – some are simple sensors which give points on contact, others are little toys that gobble up the ball for a few seconds. In this step, we’ll create a Kicking Target, which gives points when hit by the ball but also kicks the ball back in the opposite direction.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Create a new User Control named KickingTarget. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image032.jpg" width="203" height="220" /&gt; &lt;/li&gt;

  &lt;li&gt;Change the LayoutRoot container to a Canvas by right-clicking and selecting Change Layout Type/Canvas. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image033.jpg" width="255" height="234" /&gt; &lt;/li&gt;

  &lt;li&gt;Select the [UserControl] in the Objects and Timeline Panel and set its Width and Height to 100 x 100. &lt;/li&gt;

  &lt;li&gt;Draw out an Ellipse that is 90x90 pixels, and positioned at Left, Top 5,5 (so that it is centered in the control). 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image034.png" width="484" height="141" /&gt; &lt;/li&gt;

  &lt;li&gt;Draw out a second Ellipse that is 70x70 pixels and positioned and Left, Top 15, 15. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image035.png" width="131" height="130" /&gt; &lt;/li&gt;

  &lt;li&gt;Group the two Ellipses into a Canvas by selecting them both in the Objects and Timeline Panel and selecting Group Into/Canvas. Name the Canvas “cnvKicker”. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image036.png" width="281" height="204" /&gt; &lt;/li&gt;

  &lt;li&gt;Drag a PhysicsObjectBehavior from the Asset Library onto cnvKicker. Set the IsStatic and RestitutionCoefficient properties as shown below. Setting the RestitutionCoefficient above a value of 1 will make the object “kick” things back. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image037.png" width="265" height="174" /&gt; &lt;/li&gt;

  &lt;li&gt;Build the project so that the new user control is available. Then open MainPage.xaml and add a few instance of “KickingTarget” to the page. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image038.jpg" width="208" height="296" /&gt; &lt;/li&gt;

  &lt;li&gt;Run the project and try out the Kicking Targets. &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;&lt;a name="_Toc237838543"&gt;Optimizing Performance&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;Our pinball game is looking up, but the performance could be a lot better. The startup time for the game is taking quite awhile because the Physics Helper Library is determining the outline of all of the shapes. Also, the frame rate is too low.&lt;/p&gt;

&lt;p&gt;By default, Silverlight has a target frame rate of 60 frames per second. This is great for a lot of casual games, but Pinball requires a bit more speed. Additionally, Silverlight 3 introduces GPU Acceleration which can greatly increase the performance of our game by offloading graphics operations to the Video Card.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Open the Default.html page in the website project. This page hosts the Silverlight control and exposes the plugin parameters. Add the following parameters to the plug in to increase the default frame rate and enable GPU Acceleration for our game. 
    &lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
      &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;object&lt;/span&gt; &lt;span style="color: #ff0000"&gt;data&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;data:application/x-silverlight,&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;type&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;application/x-silverlight-2&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;width&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;800&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;height&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;600&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;param&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;source&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;ClientBin/PinballGame.xap&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;param&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;onerror&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;onSilverlightError&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;param&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;background&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;#010141&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;param&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;minRuntimeVersion&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;3.0.40624.0&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;param&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;MaxFrameRate&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;160&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;param&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;EnableGPUAcceleration&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;param&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;EnableCacheVisualization&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;false&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;param&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;autoUpgrade&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;value&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;a&lt;/span&gt; &lt;span style="color: #ff0000"&gt;href&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;http://go.microsoft.com/fwlink/?LinkID=149156&amp;amp;ampv=3.0.40624.0&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;style&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;text-decoration: none;&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;img&lt;/span&gt; &lt;span style="color: #ff0000"&gt;src&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;http://go.microsoft.com/fwlink/?LinkId=108181&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;alt&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;Get Microsoft Silverlight&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;style&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;border-style: none&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;a&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;object&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

      &lt;br /&gt;&lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;Apply the CacheMode attribute to &lt;b&gt;&lt;i&gt;all elements&lt;/i&gt;&lt;/b&gt; in the game that are static. Since our pinball game’s elements are not animated, we can apply the CacheMode to each of them. (If the objects contained animations within them, then the Cache would be invalidated during animation). 

    &lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
      &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;Rectangle&lt;/span&gt; &lt;span style="color: #ff0000"&gt;x:Name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;rectPlatform&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;CacheMode&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;BitmapCache&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Fill&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;#FF8AD0C9&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Height&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;70&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Width&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;800&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Canvas&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;Left&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;-14&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Canvas&lt;/span&gt;.&lt;span style="color: #ff0000"&gt;Top&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;2172&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;Opacity&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;0&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;i:Interaction.Behaviors&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;pb:PhysicsObjectBehavior&lt;/span&gt; &lt;span style="color: #ff0000"&gt;IsStatic&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;True&amp;quot;&lt;/span&gt;&lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;i:Interaction.Behaviors&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;Rectangle&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

      &lt;br /&gt;&lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;Now we’ll speed up the startup time by adding in a pre-calculated Point Cache. The Physics Helper Library echoes out the calculated points at runtime. Open the project in Visual Studio, and run with debugging on. In the Output Window, Find the ReadBoundaryCache method that was echoed out and Copy the entire method. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image041.jpg" width="523" height="218" /&gt; &lt;/li&gt;

  &lt;li&gt;Note that the copied code can contain multiple rows of the same points. You can trim out any rows that are duplicated, for example cnvRightFlipper_1 is a copy of cnvRightFlipper, so you can remove that from the list of points. &lt;/li&gt;

  &lt;li&gt;In order to load the list of boundary outlines into the Physics Controller, we need to get a reference to the controller through code. Open up MainPage.xaml.cs and add the highlighted code: 
    &lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
      &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;partial&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; MainPage : UserControl &lt;br /&gt;{ &lt;br /&gt;    PhysicsControllerMain _physicsController; &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; MainPage() &lt;br /&gt;    { &lt;br /&gt;        &lt;span style="color: #008000"&gt;// Required to initialize variables &lt;/span&gt;&lt;br /&gt;        InitializeComponent(); &lt;br /&gt;        &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Loaded += &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; RoutedEventHandler(MainPage_Loaded); &lt;br /&gt;    } &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; MainPage_Loaded(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, RoutedEventArgs e) &lt;br /&gt;    { &lt;br /&gt;        _physicsController = LayoutRoot.GetValue( PhysicsControllerMain.PhysicsControllerProperty) &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; PhysicsControllerMain; &lt;br /&gt;        BoundaryCache.ReadBoundaryCache(_physicsController); &lt;br /&gt;    } &lt;br /&gt;}&lt;/pre&gt;

      &lt;br /&gt;&lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;Run the project and test the performance. Note that items that have a tint applied to them (Red, Blue, etc.) are NOT being cached and GPU accelerated. &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;&lt;a name="_Toc237838544"&gt;Scoring&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;In this section, we’ll embellish our game with a Score.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;We’ll create a Canvas that overlays our main Canvas to contain a High Score TextBlock. This is so that the Score TextBlock will not scroll with the rest of the Playfield. Open MainPage.xaml and the select LayoutRoot in the Objects and Timeline Panel. Right-click LayoutRoot and select Group Into/Canvas. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image043.jpg" width="227" height="200" /&gt; &lt;/li&gt;

  &lt;li&gt;Add a new StackPanel into this outside Canvas that contains two TextBlock aligned Horizontally. The first TextBlock should contain the Text “Score:” and the second TextBlock should contain the Text “0”. Set the Font Size to a larger value, around 12 pt. Position the TextBlock at the Top, Left portion of the Canvas. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image044.jpg" width="367" height="273" /&gt; &lt;/li&gt;

  &lt;li&gt;Next we’ll handle the Collision event for the Physics Controller so we can add to the score. Open MainPage.xaml.cs and add in a Property wrapper for the current&amp;#160; score: 
    &lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
      &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; Score &lt;br /&gt;{ &lt;br /&gt;    get &lt;br /&gt;    { &lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; Convert.ToInt32(txtScore.Text); &lt;br /&gt;    } &lt;br /&gt;    set &lt;br /&gt;    { &lt;br /&gt;        txtScore.Text = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;.ToString(); &lt;br /&gt;    } &lt;br /&gt;}&lt;/pre&gt;

      &lt;br /&gt;&lt;/div&gt;
  &lt;/li&gt;

  &lt;li&gt;Handle the Collision event for the Physics Controller and increment the Score. 
    &lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
      &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; MainPage_Loaded(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, RoutedEventArgs e) &lt;br /&gt;{ &lt;br /&gt;    _physicsController = LayoutRoot.GetValue(PhysicsControllerMain.PhysicsControllerProperty) &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; PhysicsControllerMain; &lt;br /&gt;    BoundaryCache.ReadBoundaryCache(_physicsController); &lt;br /&gt;        _physicsController.Collision += &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; PhysicsControllerMain.CollisionHandler(_physicsController_Collision); &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; _physicsController_Collision(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; sprite1, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; sprite2) &lt;br /&gt;{ &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (sprite1 == &amp;quot;ellBall&lt;span style="color: #006080"&gt;&amp;quot; &amp;amp;&amp;amp; sprite2.StartsWith(&amp;quot;ellKicker&amp;quot;&lt;/span&gt;)) &lt;br /&gt;        Score += 10; &lt;br /&gt;}&lt;/pre&gt;

      &lt;br /&gt;&lt;/div&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;&lt;a name="_Toc237838545"&gt;Tracking Lives&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;When the ball collides with the “rectPlatform” obstacle at the bottom, the ball has been lost and we should launch a new ball. &lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Add a new ChildWindow to the project named “LostTheBall.xaml”. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image047.jpg" width="269" height="292" /&gt; &lt;/li&gt;

  &lt;li&gt;Set the “Cancel” button’s Visibility to Collapsed. &lt;/li&gt;

  &lt;li&gt;Add a TextBlock and set its Text to a message for the user stating the ball was lost. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image048.jpg" width="320" height="239" /&gt; &lt;/li&gt;

  &lt;li&gt;Add code in MainPage.xaml.cs to display the dialog and reset the ball. 
    &lt;br /&gt;LostTheBall _lostTheBall; 

    &lt;div style="border-bottom: silver 1px solid; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
      &lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; _physicsController_Collision(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; sprite1, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; sprite2) &lt;br /&gt;{ &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (sprite1 == &lt;span style="color: #006080"&gt;&amp;quot;ellBall&amp;quot;&lt;/span&gt; &amp;amp;&amp;amp; sprite2.StartsWith(&lt;span style="color: #006080"&gt;&amp;quot;cnvKicker&amp;quot;&lt;/span&gt;)) &lt;br /&gt;    { &lt;br /&gt;        Score += 10; &lt;br /&gt;    } &lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (_lostTheBall == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt; &amp;amp;&amp;amp;ampsprite1 == &lt;span style="color: #006080"&gt;&amp;quot;ellBall&amp;quot;&lt;/span&gt; &amp;amp;&amp;amp; sprite2 == &lt;span style="color: #006080"&gt;&amp;quot;rectPlatform&amp;quot;&lt;/span&gt;) &lt;br /&gt;    { &lt;br /&gt;        _lostTheBall = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; LostTheBall(); &lt;br /&gt;        _lostTheBall.Closed += &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; EventHandler(dialog_Closed); &lt;br /&gt;        _lostTheBall.Show(); &lt;br /&gt;    } &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; dialog_Closed(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; sender, EventArgs e) &lt;br /&gt;{ &lt;br /&gt;    _lostTheBall = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;; &lt;br /&gt;    PhysicsSprite ball = _physicsController.PhysicsObjects[&lt;span style="color: #006080"&gt;&amp;quot;ellBall&amp;quot;&lt;/span&gt;]; &lt;br /&gt;    ball.BodyObject.Position = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Vector2(460, 430); &lt;br /&gt;    }&lt;/pre&gt;

      &lt;br /&gt;&lt;/div&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;&lt;a name="_Toc237838546"&gt;Adding Sound Effects&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;We can easily add buffered sound effects using the PhysicsSoundBehavior.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;In Visual Studio, Import two sound files into the project, score.wma and click.wma. Set their Build Action to Content. 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image050.jpg" width="220" height="315" /&gt; &lt;/li&gt;

  &lt;li&gt;Open MainPage.xaml in Expression Blend. Drag a PhysicsSoundBehavior to the LayoutRoot Canvas. Set the TriggerType to PhysicsCollisionTrigger and set the following properties: 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image051.png" width="268" height="288" /&gt; &lt;/li&gt;

  &lt;li&gt;Drag another PhysicsSoundBehavior to the LayoutRoot Canvas. Set the TriggerType to PhysicsCollisionTrigger and set the following properties: 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image052.png" width="265" height="263" /&gt; &lt;/li&gt;

  &lt;li&gt;Drag another PhysicsSoundBehavior to the LayoutRoot Canvas. Set the TriggerType to PhysicsCollisionTrigger and set the following properties: 
    &lt;br /&gt;&lt;img border="0" src="http://www.coding4fun.net/media/slpinball/images/image053.png" width="267" height="263" /&gt; &lt;/li&gt;

  &lt;li&gt;Run the project. &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;About the Author&lt;/h3&gt;

&lt;p&gt;Andy Beaulieu is a software developer and trainer who is well versed in many Microsoft technologies including Silverlight, ASP.NET, ADO.NET and WindowsForms. &lt;a href="http://www.andybeaulieu.com"&gt;Visit Andy's Blog&lt;/a&gt; for more fun and games with Silverlight.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9896181" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Silverlight/default.aspx">Silverlight</category></item><item><title>Popfly game engine – open source</title><link>http://blogs.msdn.com/coding4fun/archive/2009/08/27/9887173.aspx</link><pubDate>Thu, 27 Aug 2009 21:01:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9887173</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9887173.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9887173</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9887173</wfw:comment><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/Popflygameengineopensource_B726/codeplex_2%5B1%5D_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 0px 0px 5px; display: inline; border-top: 0px; border-right: 0px" title="codeplex_2[1]" border="0" alt="codeplex_2[1]" align="right" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/Popflygameengineopensource_B726/codeplex_2%5B1%5D_thumb.jpg" width="240" height="181" /&gt;&lt;/a&gt; As popfly is getting shut down August 31st, they released the game engine under the Ms-Pl.&amp;#160; If you used the &lt;a href="http://blogs.msdn.com/coding4fun/archive/2009/07/22/9845135.aspx"&gt;Popfly Game Downloader&lt;/a&gt; we released a few weeks back to grab your games, there’s also instructions on how to crack them open and grab the data files.&amp;#160; Which means you may be able to recreate the actual GUI for creating games!&lt;/p&gt;  &lt;p&gt;Check it out over at &lt;a title="Codeplex" href="http://popflygameengine.codeplex.com/"&gt;Codeplex&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;[via &lt;a href="http://blogs.msdn.com/ben_anderson/archive/2009/08/26/popfly-parting-present.aspx"&gt;Ben Anderson&lt;/a&gt;]&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9887173" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/c4fnews/default.aspx">c4fnews</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/mash+up/default.aspx">mash up</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category></item><item><title>AI with c# and LINQ</title><link>http://blogs.msdn.com/coding4fun/archive/2009/07/28/9842146.aspx</link><pubDate>Tue, 28 Jul 2009 22:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9842146</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9842146.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9842146</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9842146</wfw:comment><description>&lt;P&gt;&lt;IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; DISPLAY: inline; MARGIN-LEFT: 0px; BORDER-TOP: 0px; MARGIN-RIGHT: 0px; BORDER-RIGHT: 0px" title=artificialintelligence[1] border=0 alt=artificialintelligence[1] align=right src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/AIwithcandLINQ_C888/artificialintelligence%5B1%5D_3.gif" width=255 height=236 mce_src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/AIwithcandLINQ_C888/artificialintelligence%5B1%5D_3.gif"&gt; I came across this from &lt;A href="http://twitter.com/migueldeicaza" mce_href="http://twitter.com/migueldeicaza"&gt;Miguel de Icaza’s twitter feed&lt;/A&gt;, Christopher Park designing an AI with c# and LINQ.&amp;nbsp; Currently it is a 4 part series, each is an interesting read into what exactly goes into an artificial intelligence system.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;A href="http://christophermpark.blogspot.com/2009/06/designing-emergent-ai-part-1.html" mce_href="http://christophermpark.blogspot.com/2009/06/designing-emergent-ai-part-1.html"&gt;Introduction&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://christophermpark.blogspot.com/2009/06/designing-emergent-ai-part-2-queries.html" mce_href="http://christophermpark.blogspot.com/2009/06/designing-emergent-ai-part-2-queries.html"&gt;Queries and Code&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://christophermpark.blogspot.com/2009/06/designing-emergent-ai-part-2.html" mce_href="http://christophermpark.blogspot.com/2009/06/designing-emergent-ai-part-2.html"&gt;Limitations&lt;/A&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://christophermpark.blogspot.com/2009/07/designing-emergent-ai-part-4.html" mce_href="http://christophermpark.blogspot.com/2009/07/designing-emergent-ai-part-4.html"&gt;Asymmetrical Goals&lt;/A&gt; &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;I was a bit surprised at the work that is needed to get a decent decision making AI system up and working.&amp;nbsp; Christopher also has another amazing read on &lt;A href="http://christophermpark.blogspot.com/2009/06/optimizing-30000-ships-in-realtime-in-c.html" mce_href="http://christophermpark.blogspot.com/2009/06/optimizing-30000-ships-in-realtime-in-c.html"&gt;optimizing his game to do 30,000+ ships&lt;/A&gt;!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9842146" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/c4fnews/default.aspx">c4fnews</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/artificial+intelligence/default.aspx">artificial intelligence</category></item><item><title>Download and Play Popfly Games Offline</title><link>http://blogs.msdn.com/coding4fun/archive/2009/07/22/9845135.aspx</link><pubDate>Wed, 22 Jul 2009 21:59:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9845135</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9845135.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9845135</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9845135</wfw:comment><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_63.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_thumb_26.png" width="223" height="176" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;With the announcement that the &lt;a href="http://popflyteam.spaces.live.com/blog/cns!51018025071FD37F!336.entry"&gt;Popfly Game Creator&lt;/a&gt; will be shut down starting August 24th, we’ve published the Popfly Game Downloader, a utility to download Popfly games while the &lt;a href="http://www.popfly.com"&gt;Popfly&lt;/a&gt; service is still running so you can play them even after the Popfly service is shut down!&lt;/p&gt;  &lt;li&gt;&lt;strong&gt;Authors: &lt;/strong&gt;&lt;a href="http://blogs.msdn.com/Ben_Anderson/"&gt;Ben Anderson&lt;/a&gt; and &lt;a href="http://blogs.msdn.com/adam_nathan/"&gt;Adam Nathan&lt;/a&gt; &lt;/li&gt;  &lt;li&gt;&lt;strong&gt;Download: &lt;/strong&gt;&lt;a href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=PopflyGameDownloader&amp;amp;DownloadId=6631"&gt;Popfly Game Downloader&lt;/a&gt; (&lt;a href="https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=PopflyGameDownloader&amp;amp;ReleaseId=3008"&gt;Project Page&lt;/a&gt;) &lt;/li&gt;  &lt;li&gt;&lt;b&gt;Difficulty:&lt;/b&gt; Beginner &lt;/li&gt;  &lt;li&gt;&lt;b&gt;Time Required:&lt;/b&gt; 15 minutes &lt;/li&gt;  &lt;li&gt;&lt;b&gt;Cost:&lt;/b&gt; $0 &lt;/li&gt;  &lt;li&gt;&lt;strong&gt;Software Needed: &lt;/strong&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=AB99342F-5D1A-413D-8319-81DA479AB0D7&amp;amp;displaylang=en"&gt;.NET Framework 2.0 or higher&lt;/a&gt;, &lt;a href="http://www.microsoft.com/silverlight/get-started/install/default.aspx"&gt;Silverlight 3.0&lt;/a&gt; or higher     &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_43.png" width="106" height="103" /&gt; &lt;/p&gt;    &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: This tool hasn’t been fully tested on multiple operating systems. Some games may not work properly offline.&lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;Step 1: Download Popfly Game Downloader&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;Download the zip file for the Popfly Game Downloader to a directory, say on your desktop and unzip the file as shown in the figure below.&lt;/p&gt;    &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_42.png" width="400" height="217" /&gt; &lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;Step 2: Run Popfly Game Downloader&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;Next, double click “PopflyGameDownloader.exe” and click Run or “Accept” if you get a security warning to run the file. &lt;/p&gt;    &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_41.png" width="422" height="317" /&gt; &lt;/p&gt;    &lt;p&gt;The Popfly Game Downloader should then open as shown below. &lt;/p&gt;    &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_22.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_thumb.png" width="640" height="423" /&gt;&lt;/a&gt; &lt;/p&gt;    &lt;p&gt;From inside the Popfly Game Downloader, you can download any Popfly Game that has been &lt;u&gt;shared&lt;/u&gt;, including:&lt;/p&gt;    &lt;ul&gt;     &lt;li&gt;       &lt;p&gt;&lt;/p&gt;        &lt;p&gt;&lt;/p&gt;        &lt;p&gt;&lt;/p&gt;        &lt;p&gt;&lt;/p&gt;        &lt;p&gt;&lt;/p&gt;        &lt;p&gt;Popfly games built by the community&lt;/p&gt;     &lt;/li&gt;      &lt;li&gt;       &lt;p&gt;Your personal Popfly game projects&lt;/p&gt;     &lt;/li&gt;   &lt;/ul&gt;    &lt;p&gt;Downloading mashups for offline use is not supported given that the mashups require connections to live services. Similarly, private games that have not been shared publicly cannot be downloaded either. &lt;/p&gt; &lt;/li&gt;  &lt;li&gt;   &lt;p&gt;&lt;strong&gt;Step 4: Downloading a community game&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;For example purposes, let’s download the Chicken Long Jump game. To do this, in the &lt;strong&gt;Search &lt;/strong&gt;text box, enter “Chicken Long Jump” and click the Go button to start the search. Click the link for the first option in the search results as shown below.&lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_28.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_thumb_1.png" width="550" height="480" /&gt;&lt;/a&gt; &lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;Step 4a: The Download prompt&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;This will take you to the game home page and, after a few seconds, you’ll notice a new yellow colored bar at the top of the page that gives you an option to download the game.&lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_32.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_thumb_2.png" width="513" height="409" /&gt;&lt;/a&gt; &lt;/p&gt;    &lt;p&gt;&lt;/p&gt;    &lt;p&gt;&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;Step 4b: Downloading a Game&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;Click on the link and Popfly Game Downloader will begin downloading the game. You’ll be able to see detailed progress information while Popfly Game downloader builds the offline version of the game. &lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_45.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_thumb_3.png" width="338" height="93" /&gt;&lt;/a&gt; &lt;/p&gt;    &lt;p&gt;When it’s finished downloading, you’ll see a notice that the status message changes to &lt;strong&gt;Done&lt;/strong&gt;. You can then click the link to launch a local copy of the game!&lt;/p&gt;    &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_75.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_thumb_13.png" width="435" height="64" /&gt;&lt;/a&gt; &lt;/p&gt;    &lt;p&gt;&lt;strong&gt;Step 5: Run your newly downloaded game&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;Clicking on the link will open a new browser window to a html file that includes the offline version of the Popfly game. &lt;/p&gt;    &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_24.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_thumb_11.png" width="560" height="526" /&gt;&lt;/a&gt; &lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;Step 6: Bulk downloading a user’s projects&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;Another handy feature that you can use to download either all of your shared projects (the project must be set to shared) or all projects built by a specific Popfly user is the &lt;strong&gt;Online Bulk Download &lt;/strong&gt;feature. Switch back to the Popfly Game Downloader and click on the &lt;strong&gt;Online Bulk Download &lt;/strong&gt;tab. &lt;/p&gt;    &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_55.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_thumb_6.png" width="285" height="73" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;From here, enter the user name of Popfly user who’s games you want to download. For example, to download all of the games Adam Nathan built (&lt;a href="http://popfly.com/users/adam/"&gt;http://popfly.com/users/adam/&lt;/a&gt;), enter “adam” in the download text box and hit enter.&lt;/p&gt;    &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_61.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_thumb_7.png" width="401" height="135" /&gt;&lt;/a&gt; &lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;This will start downloading all of the games that Adam has created. You can remove a specific game by clicking the &lt;strong&gt;X&lt;/strong&gt; next to the name of the game. &lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_67.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_thumb_8.png" width="528" height="285" /&gt;&lt;/a&gt; &lt;/p&gt;    &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;Viewing the Games You’ve Downloaded&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;Once you’ve finished downloading all of the projects you’re interested in, you can see the list of all of the games you’ve downloaded by switching to the &lt;strong&gt;Offline &lt;/strong&gt;tab Popfly Game Downloader.&lt;/p&gt;    &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_69.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_thumb_9.png" width="356" height="323" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;Now that you’ve downloaded the games, if you switch back to the directory that you put the &lt;strong&gt;PopflyGameDownloader.exe &lt;/strong&gt;in, you’ll see a folder for each of the games that you downloaded (using the format “PopflyUserName.ProjectName”).&amp;#160; &lt;/p&gt;    &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_71.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_thumb_10.png" width="389" height="382" /&gt;&lt;/a&gt; &lt;/p&gt;    &lt;p&gt;Inside each directory is the Silverlight application for the game (the .xap file) and an index.html file that hosts the .xap file. &lt;/p&gt;    &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_73.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_thumb_12.png" width="391" height="192" /&gt;&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;You can now play games like &lt;a href="http://www.popfly.com/users/Adam/Crayon%20Cannon"&gt;Crayon Cannon&lt;/a&gt; to your heart’s content :)&lt;/p&gt;    &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_77.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/DownloadandPlayPopflyGamesOffline_10E0D/image_thumb_14.png" width="465" height="353" /&gt;&lt;/a&gt; &lt;/p&gt;    &lt;h3&gt;&amp;#160;&lt;/h3&gt;    &lt;h3&gt;How does it work?&lt;/h3&gt;    &lt;p&gt;When you click the “download” link, Popfly Game Downloader goes to the game you selected and does a number of things, including:&lt;/p&gt;    &lt;ul&gt;     &lt;li&gt;It downloads all of the game assets (audio, images, XAML) &lt;/li&gt;      &lt;li&gt;It dynamically builds a .xap file including all of the game assets &lt;/li&gt;      &lt;li&gt;It also removes server-side features like the high-score and achievement services &lt;/li&gt;      &lt;li&gt;Finally, it builds an index.html file to host the Silverlight game &lt;/li&gt;   &lt;/ul&gt;    &lt;p&gt;&amp;#160; &lt;/p&gt;    &lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;While it’s sad that one of our favorite Microsoft services is shutting down, we can at least take solace in the fact that Popfly users can download and play with their creations long after the Popfly service is shut down.&lt;/p&gt;    &lt;p&gt;&lt;/p&gt; &lt;/li&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9845135" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/utility/default.aspx">utility</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/web/default.aspx">web</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category></item><item><title>Creating Games with Silverlight: A Simple Shooter</title><link>http://blogs.msdn.com/coding4fun/archive/2009/07/21/9658490.aspx</link><pubDate>Tue, 21 Jul 2009 20:19:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9658490</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9658490.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9658490</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9658490</wfw:comment><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="image" border="0" alt="image" align="right" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_thumb.png" width="300" height="240" /&gt;&lt;/a&gt;This article will take you through some of the steps to creating the basics for writing games with Silverlight. This is a simple shooter style game that contains some of the building blocks for games that require abilities such as vectors, collision detection, a game loop, movement, and keyboard input. Please refer to the full source code for this project for any areas of the code that are not covered in detail in this article.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;b&gt;Source Code:&lt;/b&gt; &lt;a href="http://simpleshooter.codeplex.com/"&gt;http://simpleshooter.codeplex.com/&lt;/a&gt;       &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;&lt;b&gt;Difficulty:&lt;/b&gt; Beginner/Intermediate &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Time Required:&lt;/b&gt; 3 hours &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Cost:&lt;/b&gt; $0 &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Software Needed:&lt;/b&gt;       &lt;ul&gt;       &lt;li&gt;&lt;a href="http://www.microsoft.com/express/download/"&gt;Visual Basic or Visual C# Express&lt;/a&gt; &lt;/li&gt;        &lt;li&gt;&lt;a href="http://www.microsoft.com/expression/"&gt;Expression Blend&lt;/a&gt; (optional) &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Inspiration&lt;/h3&gt;  &lt;p&gt;There are a number of examples and tutorials out there to help get started creating games. You will find the basics of this game to be very similar to a Visual C# Coding4Fun article &lt;a href="http://blogs.msdn.com/coding4fun/archive/2006/10/31/916430.aspx"&gt;here&lt;/a&gt;. This article will show you how to create a shooter style game in Silverlight. My first Silverlight game experience was playing with a Asteroids style game by Bill Reiss. My usage of vectors and how to do a game loop came from great examples like his and others. There are a number of sites and blogs where you can get good info on game development with Silverlight. Here are just a few.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.bluerosegames.com/brg/"&gt;BlueRoseGames.com&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.farseergames.com/"&gt;Farseer Games&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.cameronalbert.com/"&gt;Cameron Albert&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://silverlight.net/blogs/msnow/default.aspx"&gt;Mike Snow&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Basic Layout&lt;/h3&gt;  &lt;p&gt;To begin, Open Visual Studio and create a new Silverlight Application in either C# or VB.net. First, we will start with the layout of the screen. First, I added a canvas to the grid inside the Page.xaml that is created for you. I set it's background to black, and named it 'gameRoot'. &lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;UserControl&lt;/span&gt; &lt;span class="attr"&gt;x:Class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;SimpleShooter.Page&amp;quot;&lt;/span&gt;
    &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;&lt;/span&gt; 
    &lt;span class="attr"&gt;xmlns:x&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;&lt;/span&gt; 
    &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;400&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;300&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LayoutRoot&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Canvas&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;gameRoot&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;500&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;400&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Background&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Black&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Canvas&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;UserControl&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;Next we will start adding controls to our project for things like game entities, and information displays. In this example. To complete layout of the game, add user controls to the progect called: Info, LivesRemaining, Score, and WaveInfo. In those controls, To begin I added basic TextBlocks to display information on the state of the game. Please note there are better ways to arrange things in a user control, beyond what I show here. I am keeping to a simple canvas and direct positioning to introduce the concept of Canvas.Top and Canvas.Left. I have now placed these controls on Page.xaml and assigned an x:Name to each. At this point, the controls have only a TextBlock in them.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;UserControl&lt;/span&gt; &lt;span class="attr"&gt;x:Class&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;SimpleShooter.Page&amp;quot;&lt;/span&gt;
    &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;&lt;/span&gt; 
    &lt;span class="attr"&gt;xmlns:x&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;&lt;/span&gt; 
    &lt;span class="attr"&gt;xmlns:SimpleShooter&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;clr-namespace:SimpleShooter&amp;quot;&lt;/span&gt;
    &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;500&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;400&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LayoutRoot&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Canvas&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;gameRoot&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;500&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;400&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Background&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Black&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;SimpleShooter:RemainingLives&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ctlLives&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Canvas&lt;/span&gt;.&lt;span class="attr"&gt;Top&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;380&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Canvas&lt;/span&gt;.&lt;span class="attr"&gt;Left&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;SimpleShooter:Score&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ctlScore&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Canvas&lt;/span&gt;.&lt;span class="attr"&gt;Top&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Canvas&lt;/span&gt;.&lt;span class="attr"&gt;Left&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;SimpleShooter:WaveInfo&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ctlWaveInfo&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Canvas&lt;/span&gt;.&lt;span class="attr"&gt;Left&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;440&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Canvas&lt;/span&gt;.&lt;span class="attr"&gt;Top&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;SimpleShooter:Info&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;ctlInfo&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Canvas&lt;/span&gt;.&lt;span class="attr"&gt;Top&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Canvas&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Grid&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;UserControl&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;To finish our layout lets add a star field. To do this, we will write function to generate random numbers, and another that will randomly distribute ellipses on our base canvas. We will need to inherit from System.Security.Cryptography for this:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Page : UserControl
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; Page()
    {
        InitializeComponent();

        GenerateStarField(350);
    }

    &lt;span class="kwrd"&gt;void&lt;/span&gt; GenerateStarField(&lt;span class="kwrd"&gt;int&lt;/span&gt; numberOfStars)
    {

        &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; numberOfStars; i++)
        {

            Ellipse star = &lt;span class="kwrd"&gt;new&lt;/span&gt; Ellipse();
            &lt;span class="kwrd"&gt;double&lt;/span&gt; size = GetRandInt(10, 800) * .01;
            star.Width = size;
            star.Height = size;
            star.Opacity = GetRandInt(1, 5) * .1;
            star.Fill = &lt;span class="kwrd"&gt;new&lt;/span&gt; SolidColorBrush(Colors.White);
            &lt;span class="kwrd"&gt;int&lt;/span&gt; x = GetRandInt(0, (&lt;span class="kwrd"&gt;int&lt;/span&gt;)Math.Round(gameRoot.Height, 0));
            &lt;span class="kwrd"&gt;int&lt;/span&gt; y = GetRandInt(0, (&lt;span class="kwrd"&gt;int&lt;/span&gt;)Math.Round(gameRoot.Width, 0));
            star.SetValue(Canvas.TopProperty, (&lt;span class="kwrd"&gt;double&lt;/span&gt;)x);
            star.SetValue(Canvas.LeftProperty, (&lt;span class="kwrd"&gt;double&lt;/span&gt;)y);
            gameRoot.Children.Add(star);
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; GetRandInt(&lt;span class="kwrd"&gt;int&lt;/span&gt; min, &lt;span class="kwrd"&gt;int&lt;/span&gt; max)
    {
        Byte[] rndBytes = &lt;span class="kwrd"&gt;new&lt;/span&gt; Byte[10];
        RNGCryptoServiceProvider rndC = &lt;span class="kwrd"&gt;new&lt;/span&gt; RNGCryptoServiceProvider();
        rndC.GetBytes(rndBytes);
        &lt;span class="kwrd"&gt;int&lt;/span&gt; seed = BitConverter.ToInt32(rndBytes, 0);
        Random rand = &lt;span class="kwrd"&gt;new&lt;/span&gt; Random(seed);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; rand.Next(min, max);
    }
}&lt;/pre&gt;
&lt;b&gt;&lt;/b&gt;

&lt;p&gt;&lt;b&gt;VB &lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;Partial &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; Page
    &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; UserControl

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;()
        InitializeComponent()
        GenerateStarField(350)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; GenerateStarField(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; numberOfStars &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;)

        &lt;span class="kwrd"&gt;For&lt;/span&gt; i &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt; = 0 &lt;span class="kwrd"&gt;To&lt;/span&gt; numberOfStars - 1

            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; star &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Ellipse()
            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; size &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt; = GetRandInt(10, 800) * 0.01
            star.Width = size
            star.Height = size
            star.Opacity = GetRandInt(1, 5) * 0.1
            star.Fill = &lt;span class="kwrd"&gt;New&lt;/span&gt; SolidColorBrush(Colors.White)
            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; x &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt; = GetRandInt(0, &lt;span class="kwrd"&gt;CInt&lt;/span&gt;(Math.Round(gameRoot.Height, 0)))
            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; y &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt; = GetRandInt(0, &lt;span class="kwrd"&gt;CInt&lt;/span&gt;(Math.Round(gameRoot.Width, 0)))
            star.SetValue(Canvas.TopProperty, &lt;span class="kwrd"&gt;CDbl&lt;/span&gt;(x))
            star.SetValue(Canvas.LeftProperty, &lt;span class="kwrd"&gt;CDbl&lt;/span&gt;(y))
            gameRoot.Children.Add(star)
        &lt;span class="kwrd"&gt;Next&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; GetRandInt(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; min &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; max &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;) &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; rndBytes &lt;span class="kwrd"&gt;As&lt;/span&gt; [&lt;span class="kwrd"&gt;Byte&lt;/span&gt;]() = &lt;span class="kwrd"&gt;New&lt;/span&gt; [&lt;span class="kwrd"&gt;Byte&lt;/span&gt;](9) {}
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; rndC &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; RNGCryptoServiceProvider()
        rndC.GetBytes(rndBytes)
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; seed &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt; = BitConverter.ToInt32(rndBytes, 0)
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; rand &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Random(seed)
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; rand.[&lt;span class="kwrd"&gt;Next&lt;/span&gt;](min, max)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;
&lt;b&gt;&lt;/b&gt;

&lt;p&gt;We now have a function called GenerateStarField. Note that it is adding each ellipse to the 'Children' of our base gameRoot canvas, and how the Top and Left properties determine the positions of those ellipses. Now we have a basic layout of our game and a background.&lt;/p&gt;

&lt;h4&gt;&lt;/h4&gt;

&lt;h4&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_thumb_1.png" width="500" height="400" /&gt;&lt;/a&gt; &lt;/h4&gt;

&lt;h4&gt;Sprites and Vectors&lt;/h4&gt;

&lt;p&gt;I am not going to go in to great detail on &lt;a href="http://en.wikipedia.org/wiki/Sprite_(computer_graphics)"&gt;sprites&lt;/a&gt; or &lt;a href="http://en.wikipedia.org/wiki/Euclidean_vector"&gt;vectors&lt;/a&gt;. There are a number of great resources (see other articles linked to in this post), as well as &lt;a href="http://blogs.msdn.com/coding4fun/archive/2007/02/20/1727608.aspx"&gt;another good Coding4Fun article&lt;/a&gt; on this topic. We are however, going to add a class to represent a sprite and a vector to our code:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C# 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Sprite
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; Width { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; Height { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; Vector Velocity { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; Canvas SpriteCanvas { get; set; }
    &lt;span class="kwrd"&gt;private&lt;/span&gt; Point _position;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; Point Position
    {
        get
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; _position;
        }
        set
        {
            _position = &lt;span class="kwrd"&gt;value&lt;/span&gt;;
            SpriteCanvas.SetValue(Canvas.TopProperty, _position.Y - (Height / 2));
            SpriteCanvas.SetValue(Canvas.LeftProperty, _position.X - (Width / 2));
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Sprite(Double width, Double height, Point position)
    {
        Width = width;
        Height = height;

        SpriteCanvas = RenderSpriteCanvas();

        SpriteCanvas.Width = width;
        SpriteCanvas.Height = height;
        &lt;span class="rem"&gt;//NOTE: because the setter for Position uses both Height and Width, it is important this comes after they are set.&lt;/span&gt;
        Position = position;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; Canvas RenderSpriteCanvas();

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Canvas LoadSpriteCanvas(&lt;span class="kwrd"&gt;string&lt;/span&gt; xamlPath)
    {
        System.IO.Stream s = &lt;span class="kwrd"&gt;this&lt;/span&gt;.GetType().Assembly.GetManifestResourceStream(xamlPath);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; (Canvas)XamlReader.Load(&lt;span class="kwrd"&gt;new&lt;/span&gt; System.IO.StreamReader(s).ReadToEnd());
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Update(TimeSpan elapsedTime)
    {
        Position = (Position + Velocity * elapsedTime.TotalSeconds);
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB &lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;MustInherit&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; Sprite
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _Width &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; Width() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _Width
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _Width = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _Height &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; Height() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _Height
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _Height = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _Velocity &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; Velocity() &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _Velocity
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector)
            _Velocity = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _SpriteCanvas &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; SpriteCanvas() &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _SpriteCanvas
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas)
            _SpriteCanvas = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _position &lt;span class="kwrd"&gt;As&lt;/span&gt; Point
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; Position() &lt;span class="kwrd"&gt;As&lt;/span&gt; Point
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _position
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; Point)
            _position = value
            SpriteCanvas.SetValue(Canvas.TopProperty, _position.Y - (Height / 2))
            SpriteCanvas.SetValue(Canvas.LeftProperty, _position.X - (Width / 2))
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; initialWidth &lt;span class="kwrd"&gt;As&lt;/span&gt; [&lt;span class="kwrd"&gt;Double&lt;/span&gt;], &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; initialHeight &lt;span class="kwrd"&gt;As&lt;/span&gt; [&lt;span class="kwrd"&gt;Double&lt;/span&gt;], &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; initialPosition &lt;span class="kwrd"&gt;As&lt;/span&gt; Point)
        Width = initialWidth
        Height = initialHeight

        SpriteCanvas = RenderSpriteCanvas()

        SpriteCanvas.Width = Width
        SpriteCanvas.Height = Height
        &lt;span class="rem"&gt;'NOTE: because the setter for Position uses both Height and Width, it is important this comes after they are set. &lt;/span&gt;
        Position = initialPosition
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;MustOverride&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; RenderSpriteCanvas() &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; LoadSpriteCanvas(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; xamlPath &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt;) &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; s &lt;span class="kwrd"&gt;As&lt;/span&gt; System.IO.Stream = &lt;span class="kwrd"&gt;Me&lt;/span&gt;.[&lt;span class="kwrd"&gt;GetType&lt;/span&gt;]().&lt;span class="kwrd"&gt;Assembly&lt;/span&gt;.GetManifestResourceStream(xamlPath)
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;DirectCast&lt;/span&gt;(XamlReader.Load(&lt;span class="kwrd"&gt;New&lt;/span&gt; System.IO.StreamReader(s).ReadToEnd()), Canvas)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overridable&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Update(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; elapsedTime &lt;span class="kwrd"&gt;As&lt;/span&gt; TimeSpan)
        Position = (Position + Velocity * elapsedTime.TotalSeconds)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

&lt;p&gt;This Sprite class will give us the basis for entities in the game such as a ship, aliens, and projectiles. For all these items, we need to know the location of the item, the site, and what it looks like. We will take advantage of Point to track our position, and a property of type Canvas to display the XAML for each of these items. The constructor sets these initial parameters, and calls a RenderSpriteCanvas method to be implemented by each class that inherits from it. This method allows the inheriting class to set the contents of the canvas and therefore as control of the way the sprite looks.&lt;/p&gt;

&lt;p&gt;Next we have our vector class that will help us control movement of our sprite. Again, I will not go into detail on vectors as this code is borrowed and adapted from many other readily available sources:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C# 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;struct&lt;/span&gt; Vector
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; X;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; Y;

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Vector(&lt;span class="kwrd"&gt;double&lt;/span&gt; x, &lt;span class="kwrd"&gt;double&lt;/span&gt; y)
    {
        X = x;
        Y = y;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; Length
    {
        get
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; Math.Sqrt(LengthSquared);
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; LengthSquared
    {
        get
        {
            &lt;span class="kwrd"&gt;return&lt;/span&gt; X * X + Y * Y;
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Normalize()
    {
        &lt;span class="kwrd"&gt;double&lt;/span&gt; length = Length;
        X /= length;
        Y /= length;
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Vector &lt;span class="kwrd"&gt;operator&lt;/span&gt; -(Vector vector)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector(-vector.X, -vector.Y);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Vector &lt;span class="kwrd"&gt;operator&lt;/span&gt; *(Vector vector, &lt;span class="kwrd"&gt;double&lt;/span&gt; scalar)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector(scalar * vector.X, scalar * vector.Y);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Point &lt;span class="kwrd"&gt;operator&lt;/span&gt; +(Point point, Vector vector)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(point.X + vector.X, point.Y + vector.Y);
    }

    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; Vector CreateVectorFromAngle(&lt;span class="kwrd"&gt;double&lt;/span&gt; angleInDegrees, &lt;span class="kwrd"&gt;double&lt;/span&gt; length)
    {
        &lt;span class="kwrd"&gt;double&lt;/span&gt; x = Math.Sin(DegreesToRadians(180 - angleInDegrees)) * length;
        &lt;span class="kwrd"&gt;double&lt;/span&gt; y = Math.Cos(DegreesToRadians(180 - angleInDegrees)) * length;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector(x, y);
    }

    &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; DegreesToRadians(&lt;span class="kwrd"&gt;double&lt;/span&gt; degrees)
    {
        &lt;span class="kwrd"&gt;double&lt;/span&gt; radians = ((degrees / 360) * 2 * Math.PI);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; radians;
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB &lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Structure&lt;/span&gt; Vector
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; X &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; Y &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; x__1 &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; y__2 &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
        X = x__1
        Y = y__2
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;ReadOnly&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; Length() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; Math.Sqrt(LengthSquared)
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;ReadOnly&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; LengthSquared() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; X * X + Y * Y
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Normalize()
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; length__1 &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt; = Length
        X /= length__1
        Y /= length__1
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Shared&lt;/span&gt; &lt;span class="kwrd"&gt;Operator&lt;/span&gt; -(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; vector &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector) &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Vector(-vector.X, -vector.Y)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Operator&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Shared&lt;/span&gt; &lt;span class="kwrd"&gt;Operator&lt;/span&gt; *(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; vector &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; scalar &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;) &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Vector(scalar * vector.X, scalar * vector.Y)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Operator&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Shared&lt;/span&gt; &lt;span class="kwrd"&gt;Operator&lt;/span&gt; +(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; point &lt;span class="kwrd"&gt;As&lt;/span&gt; Point, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; vector &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector) &lt;span class="kwrd"&gt;As&lt;/span&gt; Point
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Point(point.X + vector.X, point.Y + vector.Y)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Operator&lt;/span&gt;


    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Shared&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; CreateVectorFromAngle(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; angleInDegrees &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; length &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;) &lt;span class="kwrd"&gt;As&lt;/span&gt; Vector
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; x &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt; = Math.Sin(DegreesToRadians(180 - angleInDegrees)) * length
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; y &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt; = Math.Cos(DegreesToRadians(180 - angleInDegrees)) * length
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Vector(x, y)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Shared&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; DegreesToRadians(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; degrees &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;) &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; radians &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt; = ((degrees / 360) * 2 * Math.PI)
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; radians
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Structure&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;
    &lt;br /&gt;&lt;/b&gt;Now we can implement Sprite with a new Ship class. Add a class called Ship, and a file called Ship.xaml to your project. Be sure to set the properties of Ship.xaml to 'Embedded Resource'. We now need to inherit from our Sprite class:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Ship : Sprite
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; Ship(&lt;span class="kwrd"&gt;double&lt;/span&gt; width, &lt;span class="kwrd"&gt;double&lt;/span&gt; height, Point firstPosition)
        : &lt;span class="kwrd"&gt;base&lt;/span&gt;(width, height, firstPosition)
    {

    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; Canvas RenderSpriteCanvas()
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; LoadSpriteCanvas(&lt;span class="str"&gt;&amp;quot;SimpleShooter.Sprites.Ship.xaml&amp;quot;&lt;/span&gt;);
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB &lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; Ship
    &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; Sprite
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; width &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; height &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; firstPosition &lt;span class="kwrd"&gt;As&lt;/span&gt; Point)
        &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.&lt;span class="kwrd"&gt;New&lt;/span&gt;(width, height, firstPosition)

    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overloads&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; RenderSpriteCanvas() &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; LoadSpriteCanvas(&lt;span class="str"&gt;&amp;quot;SimpleShooter.Ship.xaml&amp;quot;&lt;/span&gt;)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

&lt;p&gt;When Ship is instantiated, it calls the constructor of it's base, Sprite. It also implements the RenderSpriteCanvas method and specifies the XAML (a simple white square) to load into the sprite's canvas. Now we are ready to add a sprite to our main page. In this simple game, we will have only one ship (the others will be aliens), so lets add a property to our page, and a function that will instantiate our ship:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Canvas&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;LayoutRoot&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;30&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;30&amp;quot;&lt;/span&gt; 
    &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://schemas.microsoft.com/client/2007&amp;quot;&lt;/span&gt;
    &lt;span class="attr"&gt;xmlns:x&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Rectangle&lt;/span&gt; &lt;span class="attr"&gt;Height&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;30&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Width&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;30&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Fill&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;White&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Canvas&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;void&lt;/span&gt; InitializeGame()
{
    PlayerShip = &lt;span class="kwrd"&gt;new&lt;/span&gt; Ship(10, 10, &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(100, 300));
    gameRoot.Children.Add(PlayerShip.SpriteCanvas);
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB &lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; InitializeGame()
    PlayerShip = &lt;span class="kwrd"&gt;New&lt;/span&gt; Ship(10, 10, &lt;span class="kwrd"&gt;New&lt;/span&gt; Point(100, 300))
    gameRoot.Children.Add(PlayerShip.SpriteCanvas)
&lt;span class="kwrd"&gt;End&lt;/span&gt; Sub&lt;/pre&gt;

&lt;p&gt;We can now call this method from our page constructor and when we run the project, we get a white square (our ship) at the bottom left of our Page. Taking a closer look, we set our page size to 500 x 400, and the InitializeGame method places our Ship at a point 100 pixels from the left of the gameRoot canvas, and 300pixels from the top.&lt;/p&gt;

&lt;h4&gt;Keyboard Input and the Game Loop&lt;/h4&gt;

&lt;p&gt;Now we are ready to make some things move. To start, we need choose keys on the keyboard to allow movement, and act on those events. We then need to capture those key presses and act on them if they are relevant to our game loop. Once again, may examples of key handlers and game loops are readily available, so I won't go into detail. The keyboard handler captures all key up and down events. We can therefore ask our instance of the handler if a key is pressed at any given time. The game loop is just that, a constant loop. It consists of a storyboard that has the start called on it and it immediately ends. The class raises an event, and starts the storyboard again. Subscribers to Update event are provided a value that reports the number of milliseconds since the last update. That value can be applied to vectors to apply smooth movement to sprites. To take advantage of these classes, we need to add an instance of both a KeyHandler and GameLoop to our Page. To do this, we update the InitializeGame and Page constructor, and add a handler for the GameLoop:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C# 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; Page()
{
    InitializeComponent();
    keyHandler = &lt;span class="kwrd"&gt;new&lt;/span&gt; KeyHandler(&lt;span class="kwrd"&gt;this&lt;/span&gt;);
    GenerateStarField(350);
    InitializeGame();
}

&lt;span class="kwrd"&gt;void&lt;/span&gt; InitializeGame()
{
    gameLoop = &lt;span class="kwrd"&gt;new&lt;/span&gt; GameLoop(&lt;span class="kwrd"&gt;this&lt;/span&gt;);
    gameLoop.Update += &lt;span class="kwrd"&gt;new&lt;/span&gt; GameLoop.UpdateHandler(gameLoop_Update);

    PlayerShip = &lt;span class="kwrd"&gt;new&lt;/span&gt; Ship(10, 10, &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(100, 360));
    gameRoot.Children.Add(PlayerShip.SpriteCanvas);

    gameLoop.Start();
}

&lt;span class="kwrd"&gt;void&lt;/span&gt; gameLoop_Update(TimeSpan elapsed)
{
    &lt;span class="rem"&gt;//clear the current vector so the sprite is not moving unless a keys is pressed&lt;/span&gt;
    PlayerShip.Velocity = &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector(0, 0);
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (keyHandler.IsKeyPressed(Key.Left))
    {
        PlayerShip.Velocity = Vector.CreateVectorFromAngle(270, 125);
    }
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (keyHandler.IsKeyPressed(Key.Right))
    {
        PlayerShip.Velocity = Vector.CreateVectorFromAngle(90, 125);
    }
    PlayerShip.Update(elapsed);
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB &lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;()
    InitializeComponent()
    keyHandler = &lt;span class="kwrd"&gt;New&lt;/span&gt; KeyHandler(&lt;span class="kwrd"&gt;Me&lt;/span&gt;)
    GenerateStarField(350)
    InitializeGame()
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

&lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; InitializeGame()
    gameLoop = &lt;span class="kwrd"&gt;New&lt;/span&gt; GameLoop(&lt;span class="kwrd"&gt;Me&lt;/span&gt;)
    &lt;span class="kwrd"&gt;AddHandler&lt;/span&gt; gameLoop.Update, &lt;span class="kwrd"&gt;AddressOf&lt;/span&gt; gameLoop_Update

    PlayerShip = &lt;span class="kwrd"&gt;New&lt;/span&gt; Ship(10, 10, &lt;span class="kwrd"&gt;New&lt;/span&gt; Point(100, 360))
    gameRoot.Children.Add(PlayerShip.SpriteCanvas)

    gameLoop.Start()
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

&lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; gameLoop_Update(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; elapsed &lt;span class="kwrd"&gt;As&lt;/span&gt; TimeSpan)
    &lt;span class="rem"&gt;'clear the current vector so the sprite is not moving unless a keys is pressed &lt;/span&gt;
    PlayerShip.Velocity = &lt;span class="kwrd"&gt;New&lt;/span&gt; Vector(0, 0)
    &lt;span class="kwrd"&gt;If&lt;/span&gt; keyHandler.IsKeyPressed(Key.Left) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
        PlayerShip.Velocity = Vector.CreateVectorFromAngle(270, 125)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
    &lt;span class="kwrd"&gt;If&lt;/span&gt; keyHandler.IsKeyPressed(Key.Right) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
        PlayerShip.Velocity = Vector.CreateVectorFromAngle(90, 125)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;

    PlayerShip.Update(elapsed)
&lt;span class="kwrd"&gt;End&lt;/span&gt; Sub&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_6.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_thumb_2.png" width="500" height="400" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Now we have a functioning game loop. Launch the app, click on the Silverlight control to give it focus, and you can now use the arrow keys to move our ship right and left. One problem however is that you are able to take the ship completely off the screen. To prevent this, we can add MinX and MaxX properties to our ship class and override the Update method it inherits from Sprite. Be sure to also add those min and max values after the Ship is instantiated in InitializeGame of our Page: &lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Ship : Sprite
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; MaxX { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; MinX { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Ship(&lt;span class="kwrd"&gt;double&lt;/span&gt; width, &lt;span class="kwrd"&gt;double&lt;/span&gt; height, Point firstPosition)
        : &lt;span class="kwrd"&gt;base&lt;/span&gt;(width, height, firstPosition)
    {

    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; Canvas RenderSpriteCanvas()
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; LoadSpriteCanvas(&lt;span class="str"&gt;&amp;quot;SimpleShooter.Sprites.Ship.xaml&amp;quot;&lt;/span&gt;);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Update(System.TimeSpan elapsedTime)
    {
        &lt;span class="rem"&gt;//verify that this is a position we can move to&lt;/span&gt;
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (Position.X &amp;gt; MaxX)
        {
            Position = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(MaxX, Position.Y);
            Velocity = &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector(0, 0);
        }
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (Position.X &amp;lt; MinX)
        {
            Position = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(MinX, Position.Y);
            Velocity = &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector(0, 0);
        }
        &lt;span class="kwrd"&gt;base&lt;/span&gt;.Update(elapsedTime);
    }
}&lt;/pre&gt;
&lt;b&gt;&lt;/b&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; Ship
    &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; Sprite
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _MaxX &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; MaxX() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _MaxX
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _MaxX = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _MinX &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; MinX() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _MinX
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _MinX = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; width &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; height &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; firstPosition &lt;span class="kwrd"&gt;As&lt;/span&gt; Point)
        &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.&lt;span class="kwrd"&gt;New&lt;/span&gt;(width, height, firstPosition)

    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overloads&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; RenderSpriteCanvas() &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; LoadSpriteCanvas(&lt;span class="str"&gt;&amp;quot;SimpleShooter.Ship.xaml&amp;quot;&lt;/span&gt;)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overloads&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Update(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; elapsedTime &lt;span class="kwrd"&gt;As&lt;/span&gt; System.TimeSpan)
        &lt;span class="rem"&gt;'verify that this is a position we can move to &lt;/span&gt;
        &lt;span class="kwrd"&gt;If&lt;/span&gt; Position.X &amp;gt; MaxX &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            Position = &lt;span class="kwrd"&gt;New&lt;/span&gt; Point(MaxX, Position.Y)
            Velocity = &lt;span class="kwrd"&gt;New&lt;/span&gt; Vector(0, 0)
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
        &lt;span class="kwrd"&gt;If&lt;/span&gt; Position.X &amp;lt; MinX &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            Position = &lt;span class="kwrd"&gt;New&lt;/span&gt; Point(MinX, Position.Y)
            Velocity = &lt;span class="kwrd"&gt;New&lt;/span&gt; Vector(0, 0)
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
        &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.Update(elapsedTime)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

&lt;h4&gt;Prepare to Fire!&lt;/h4&gt;

&lt;p&gt;We now have all the basic plumbing to add additional sprites such as aliens or projectiles. To begin, we will add additional classes inheriting from Sprite: Alien, Missle, and Bomb, along with Alien.xaml, Missle.xaml, and Bomb.xaml (these new .xaml files need to be set as Embedded Resources). These xaml files are just like our Ship.xaml with different sizes and colors. Make the Aliens and their bombs red, and shrink the bomb and missile height and width to 5. The xaml is very similar, but the classes themselves are going to have a few differing capabilities. Bomb and Missile are very similar. They only need to load their corresponding xaml. Here is our Bomb class for example:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Bomb : Sprite
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; MaxX { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; MinX { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Bomb(&lt;span class="kwrd"&gt;double&lt;/span&gt; width, &lt;span class="kwrd"&gt;double&lt;/span&gt; height, Point firstPosition)
        : &lt;span class="kwrd"&gt;base&lt;/span&gt;(width, height, firstPosition)
    {

    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; Canvas RenderSpriteCanvas()
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; LoadSpriteCanvas(&lt;span class="str"&gt;&amp;quot;SimpleShooter.Sprites.Bomb.xaml&amp;quot;&lt;/span&gt;);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Update(System.TimeSpan elapsedTime)
    {
        &lt;span class="kwrd"&gt;base&lt;/span&gt;.Update(elapsedTime);
    }
}&lt;/pre&gt;
&lt;b&gt;&lt;/b&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; Bomb
    &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; Sprite
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _MaxX &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; MaxX() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _MaxX
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _MaxX = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _MinX &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; MinX() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _MinX
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _MinX = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; width &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; height &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; firstPosition &lt;span class="kwrd"&gt;As&lt;/span&gt; Point)
        &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.&lt;span class="kwrd"&gt;New&lt;/span&gt;(width, height, firstPosition)

    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overloads&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; RenderSpriteCanvas() &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; LoadSpriteCanvas(&lt;span class="str"&gt;&amp;quot;SimpleShooter.Sprites.Bomb.xaml&amp;quot;&lt;/span&gt;)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overloads&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Update(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; elapsedTime &lt;span class="kwrd"&gt;As&lt;/span&gt; System.TimeSpan)
        &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.Update(elapsedTime)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

&lt;p&gt;We now need to add an Alien class similar to our Ship class. These will both need a bit more capability. For one, we are going to have them both fire at each other. In the case of the Alien, it will be firing down, and it's monition of choice will be a bomb:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Alien : Sprite
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; fireRateMilliseconds = 2000;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; fireVelocity = 250;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; wayPointMin;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; wayPointMax;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; speed = 100;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; spawnWait;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; DateTime spawnComplete;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; MaxX { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; MinX { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Alien(&lt;span class="kwrd"&gt;double&lt;/span&gt; width, &lt;span class="kwrd"&gt;double&lt;/span&gt; height, Point firstPosition)
        : &lt;span class="kwrd"&gt;base&lt;/span&gt;(width, height, firstPosition)
    {

    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; CheckDirection()
    {
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (Position.X &amp;gt; wayPointMax)
        {
            Velocity = Vector.CreateVectorFromAngle(270, speed);
        }
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (Position.X &amp;lt; wayPointMin)
        {
            Velocity = Vector.CreateVectorFromAngle(90, speed);
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; Canvas RenderSpriteCanvas()
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; LoadSpriteCanvas(&lt;span class="str"&gt;&amp;quot;SimpleShooter.Sprites.Alien.xaml&amp;quot;&lt;/span&gt;);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Update(TimeSpan elapsedTime)
    {
        CheckDirection();
        &lt;span class="kwrd"&gt;base&lt;/span&gt;.Update(elapsedTime);
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; Bomb Fire()
    {
        Bomb bomb = &lt;span class="kwrd"&gt;new&lt;/span&gt; Bomb(5, 5, Position);
        bomb.Velocity = Vector.CreateVectorFromAngle(180, fireVelocity);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; bomb;
    }
}&lt;/pre&gt;
&lt;b&gt;&lt;/b&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; Alien
    &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; Sprite
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; fireRateMilliseconds &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt; = 2000
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; fireVelocity &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt; = 250
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; wayPointMin &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; wayPointMax &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; speed &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt; = 100
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; spawnWait &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; spawnComplete &lt;span class="kwrd"&gt;As&lt;/span&gt; DateTime
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _MaxX &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; MaxX() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _MaxX
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _MaxX = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _MinX &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; MinX() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _MinX
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _MinX = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; width &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; height &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; firstPosition &lt;span class="kwrd"&gt;As&lt;/span&gt; Point)
        &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.&lt;span class="kwrd"&gt;New&lt;/span&gt;(width, height, firstPosition)

    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; CheckDirection()
        &lt;span class="kwrd"&gt;If&lt;/span&gt; Position.X &amp;gt; wayPointMax &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            Velocity = Vector.CreateVectorFromAngle(270, speed)
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
        &lt;span class="kwrd"&gt;If&lt;/span&gt; Position.X &amp;lt; wayPointMin &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            Velocity = Vector.CreateVectorFromAngle(90, speed)
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overloads&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; RenderSpriteCanvas() &lt;span class="kwrd"&gt;As&lt;/span&gt; Canvas
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; LoadSpriteCanvas(&lt;span class="str"&gt;&amp;quot;SimpleShooter.Alien.xaml&amp;quot;&lt;/span&gt;)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Overloads&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Update(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; elapsedTime &lt;span class="kwrd"&gt;As&lt;/span&gt; TimeSpan)
        CheckDirection()
        &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.Update(elapsedTime)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Fire() &lt;span class="kwrd"&gt;As&lt;/span&gt; Bomb
        &lt;span class="kwrd"&gt;Dim&lt;/span&gt; bomb &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Bomb(5, 5, Position)
        bomb.Velocity = Vector.CreateVectorFromAngle(180, fireVelocity)
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; bomb
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

&lt;p&gt;Now that we are going to add munitions to the game, we need to be able to know when these munitions collide with other sprites. To do this, we are going to add a collision detection method to our sprite class. As a form of simple collision detection, we will give each sprite a CollisionRadius from its center point. To keep it simple we will make the radius one half the width of our sprites. A vector created from these two points can then detect if the sum of those two radius is greater than the length of our vector. If it is, they have collided:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; Collides(Sprite s1, Sprite s2)
{
    Vector v = &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector((s1.Position.X) - (s2.Position.X), (s1.Position.Y) - (s2.Position.Y));
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (s1.CollisionRadius + s2.CollisionRadius &amp;gt; v.Length)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;true&lt;/span&gt;;
    }
    &lt;span class="kwrd"&gt;else&lt;/span&gt;
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;;
    }
}&lt;/pre&gt;
&lt;b&gt;&lt;/b&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Shared&lt;/span&gt; &lt;span class="kwrd"&gt;Function&lt;/span&gt; Collides(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; s1 &lt;span class="kwrd"&gt;As&lt;/span&gt; Sprite, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; s2 &lt;span class="kwrd"&gt;As&lt;/span&gt; Sprite) &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;
    &lt;span class="kwrd"&gt;Dim&lt;/span&gt; v &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt; Vector((s1.Position.X) - (s2.Position.X), (s1.Position.Y) - (s2.Position.Y))
    &lt;span class="kwrd"&gt;If&lt;/span&gt; s1.CollisionRadius + s2.CollisionRadius &amp;gt; v.Length &lt;span class="kwrd"&gt;Then&lt;/span&gt;
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;True&lt;/span&gt;
    &lt;span class="kwrd"&gt;Else&lt;/span&gt;
        &lt;span class="kwrd"&gt;Return&lt;/span&gt; &lt;span class="kwrd"&gt;False&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Function&lt;/pre&gt;

&lt;p&gt;We are now ready to extend our game loop of our Page to do more than track the movement of our Ship. We begin by taking action on more than just the arrow keys. We will wire up the space bar for shooting. Lets add an enumeration to track the state of our game. We will also add a supporting class that will handle the firing of munitions and adding those to our game. We will keep this simple for now, but it is an obvious place to being extending this code to take advantage of things like partial classes that could contain functionality like our 'Fire' method:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;enum&lt;/span&gt; GameState
{
    Ready = 0,
    Running = 1,
    Paused = 2,
    BetweenWaves = 3,
    GameOver = 4
}

&lt;span class="kwrd"&gt;if&lt;/span&gt; (keyHandler.IsKeyPressed(Key.Space))
{
    &lt;span class="kwrd"&gt;switch&lt;/span&gt; (status)
    {
        &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.Ready:
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.Running:
            EntityFired(PlayerShip);
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.Paused:
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.BetweenWaves:
            status = GameState.Running;
            ctlInfo.GameInfo = &lt;span class="str"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;
            StartWave();
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.GameOver:
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;default&lt;/span&gt;:
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
    }
}

&lt;span class="kwrd"&gt;void&lt;/span&gt; EntityFired(Sprite shooter)
{
    Debug.WriteLine(shooter);
    &lt;span class="kwrd"&gt;switch&lt;/span&gt; (shooter.ToString())
    {
        &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="str"&gt;&amp;quot;SimpleShooter.Ship&amp;quot;&lt;/span&gt;:
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (missles.Count == 0)
            {
                Missle missle = ((Ship)shooter).Fire();
                missles.Add(missle);
                gameRoot.Children.Add(missle.SpriteCanvas);

            }
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="str"&gt;&amp;quot;SimpleShooter.Alien&amp;quot;&lt;/span&gt;:
            Bomb bomb = ((Alien)shooter).Fire();
            bombs.Add(bomb);
            gameRoot.Children.Add(bomb.SpriteCanvas);
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;default&lt;/span&gt;:
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
    }
}&lt;/pre&gt;
&lt;b&gt;&lt;/b&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Enum&lt;/span&gt; GameState
    Ready = 0
    Running = 1
    Paused = 2
    BetweenWaves = 3
    GameOver = 4
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Enum&lt;/span&gt;

&lt;span class="kwrd"&gt;If&lt;/span&gt; keyHandler.IsKeyPressed(Key.Space) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
    &lt;span class="kwrd"&gt;Select&lt;/span&gt; &lt;span class="kwrd"&gt;Case&lt;/span&gt; status
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.Ready
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.Running
            EntityFired(PlayerShip)
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.Paused
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.BetweenWaves
            status = GameState.Running
            ctlInfo.GameInfo = &lt;span class="str"&gt;&amp;quot;&amp;quot;&lt;/span&gt;
            StartWave()
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.GameOver
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; &lt;span class="kwrd"&gt;Else&lt;/span&gt;
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;

&lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; EntityFired(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; shooter &lt;span class="kwrd"&gt;As&lt;/span&gt; Sprite)
    Debug.WriteLine(shooter)
    &lt;span class="kwrd"&gt;Select&lt;/span&gt; &lt;span class="kwrd"&gt;Case&lt;/span&gt; shooter.ToString()
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; &lt;span class="str"&gt;&amp;quot;SimpleShooter.Ship&amp;quot;&lt;/span&gt;
            &lt;span class="kwrd"&gt;If&lt;/span&gt; missles.Count = 0 &lt;span class="kwrd"&gt;Then&lt;/span&gt;
                &lt;span class="kwrd"&gt;Dim&lt;/span&gt; missle &lt;span class="kwrd"&gt;As&lt;/span&gt; Missle = &lt;span class="kwrd"&gt;DirectCast&lt;/span&gt;(shooter, Ship).Fire()
                missles.Add(missle)

                gameRoot.Children.Add(missle.SpriteCanvas)
            &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; &lt;span class="str"&gt;&amp;quot;SimpleShooter.Alien&amp;quot;&lt;/span&gt;
            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; bomb &lt;span class="kwrd"&gt;As&lt;/span&gt; Bomb = &lt;span class="kwrd"&gt;DirectCast&lt;/span&gt;(shooter, Alien).Fire()
            bombs.Add(bomb)
            gameRoot.Children.Add(bomb.SpriteCanvas)
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
        &lt;span class="kwrd"&gt;Case&lt;/span&gt; &lt;span class="kwrd"&gt;Else&lt;/span&gt;
            &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Sub&lt;/pre&gt;

&lt;p&gt;To make it easier to track the state of our game, let's add public properties to our controls and let them be the keepers of those game states. For instance, give the ReamainingLives control a Lives property, and have the setter of that property also update the TextBlock on the control to show the user how many lives are left. Perform a similar task for the other three controls:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; RemainingLives : UserControl
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; _lives;
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; Lives
    {
        get { &lt;span class="kwrd"&gt;return&lt;/span&gt; _lives; }
        set
        {
            _lives = &lt;span class="kwrd"&gt;value&lt;/span&gt;;
            &lt;span class="kwrd"&gt;string&lt;/span&gt; livesString = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Empty;
            &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 0; i &amp;lt; _lives - 1; i++)
            {
                livesString = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Format(&lt;span class="str"&gt;&amp;quot;{0}{1}&amp;quot;&lt;/span&gt;, livesString, &lt;span class="str"&gt;&amp;quot;A&amp;quot;&lt;/span&gt;);
            }
            txtRemainingLives.Text = livesString;
        }
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; RemainingLives()
    {
        InitializeComponent();
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;Partial &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; RemainingLives
    &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; UserControl
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _lives &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; Lives() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _lives
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;)
            _lives = value
            &lt;span class="kwrd"&gt;Dim&lt;/span&gt; livesString &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;String&lt;/span&gt; = &lt;span class="kwrd"&gt;String&lt;/span&gt;.Empty
            &lt;span class="kwrd"&gt;For&lt;/span&gt; i &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt; = 0 &lt;span class="kwrd"&gt;To&lt;/span&gt; _lives - 2
                livesString = &lt;span class="kwrd"&gt;String&lt;/span&gt;.Format(&lt;span class="str"&gt;&amp;quot;{0}{1}&amp;quot;&lt;/span&gt;, livesString, &lt;span class="str"&gt;&amp;quot;A&amp;quot;&lt;/span&gt;)
            &lt;span class="kwrd"&gt;Next&lt;/span&gt;
            txtRemainingLives.Text = livesString
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;

    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;()
        InitializeComponent()
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

&lt;p&gt;Now we have a number of other things to add to our page class. We are going to have lots of entities to deal with in our game loop. We will have a ship, x number of Aliens, x number of Bombs, and at some points we will have our Missiles, though we restricted that to one at a time in our EntityFired method. We need to, in each game loop check if any missile hits any alien or leaves the game map, if any bomb hits our ship or leaves the game map, and if it is time for a Alien to fire at the ship. Our Page already has a property for our Ship, but the Aliens, Bombs, Missiles need to be collections. When we begin to loop through our Bombs for instance, we will want to remove a bomb if it strikes a Ship or leaves the game map. Since we are going to be iterating these collections, we cannot subtract from those collections as we are looping through them. There are a number of approaches for this, but to keep it simple, we will add a corresponding collection for each of our Bomb, Alien, and Missile collections to track the ones that need to be removed and removed from our game canvas. With this approach, our game loop can add to the &amp;quot;remove these&amp;quot; collection, and then take action on them when we leave the main loop for the parent collection:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;List&amp;lt;Alien&amp;gt; aliens;
List&amp;lt;Alien&amp;gt; aliensRemove;
List&amp;lt;Alien&amp;gt; alienShooters;
List&amp;lt;Bomb&amp;gt; bombs;
List&amp;lt;Bomb&amp;gt; bombsRemove;
List&amp;lt;Missle&amp;gt; missles;
List&amp;lt;Missle&amp;gt; misslesRemove;&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Private&lt;/span&gt; aliens &lt;span class="kwrd"&gt;As&lt;/span&gt; List(Of Alien)
&lt;span class="kwrd"&gt;Private&lt;/span&gt; aliensRemove &lt;span class="kwrd"&gt;As&lt;/span&gt; List(Of Alien)
&lt;span class="kwrd"&gt;Private&lt;/span&gt; alienShooters &lt;span class="kwrd"&gt;As&lt;/span&gt; List(Of Alien)
&lt;span class="kwrd"&gt;Private&lt;/span&gt; bombs &lt;span class="kwrd"&gt;As&lt;/span&gt; List(Of Bomb)
&lt;span class="kwrd"&gt;Private&lt;/span&gt; bombsRemove &lt;span class="kwrd"&gt;As&lt;/span&gt; List(Of Bomb)
&lt;span class="kwrd"&gt;Private&lt;/span&gt; missles &lt;span class="kwrd"&gt;As&lt;/span&gt; List(Of Missle)
&lt;span class="kwrd"&gt;Private&lt;/span&gt; misslesRemove &lt;span class="kwrd"&gt;As&lt;/span&gt; List(Of Missle)&lt;/pre&gt;

&lt;p&gt;We also want to add a class to our page that can track the waves of aliens we are going to send one at a time. In addition, we can add a collection to hold these waves. Each wave will track the number of aliens that spawn at once, the total number of aliens faced in the wave, how many aliens get to drop bombs, and how frequently they can fire.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; WaveData
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; WaveData(&lt;span class="kwrd"&gt;int&lt;/span&gt; count, &lt;span class="kwrd"&gt;double&lt;/span&gt; fireRate, &lt;span class="kwrd"&gt;int&lt;/span&gt; atOnce, &lt;span class="kwrd"&gt;int&lt;/span&gt; fireatonce)
    {
        EnemyCount = count;
        fireRateMilliseconds = fireRate;
        enemiesAtOnce = atOnce;
        fireAtOnce = fireatonce;
        waveEmpty = &lt;span class="kwrd"&gt;false&lt;/span&gt;;
    }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; EnemyCount { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; fireRateMilliseconds { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; enemiesAtOnce { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; fireAtOnce { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; waveEmpty { get; set; }
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; WaveData
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; count &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; fireRate &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; atOnce &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; fireatonce__1 &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;)
        EnemyCount = count
        fireRateMilliseconds = fireRate
        enemiesAtOnce = atOnce
        fireAtOnce = fireatonce__1
        waveEmpty = &lt;span class="kwrd"&gt;False&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _EnemyCount &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; EnemyCount() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _EnemyCount
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;)
            _EnemyCount = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _fireRateMilliseconds &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; fireRateMilliseconds() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _fireRateMilliseconds
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Double&lt;/span&gt;)
            _fireRateMilliseconds = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _enemiesAtOnce &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; enemiesAtOnce() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _enemiesAtOnce
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;)
            _enemiesAtOnce = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _fireAtOnce &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; fireAtOnce() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _fireAtOnce
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Integer&lt;/span&gt;)
            _fireAtOnce = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
    &lt;span class="kwrd"&gt;Private&lt;/span&gt; _waveEmpty &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt; waveEmpty() &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;
        &lt;span class="kwrd"&gt;Get&lt;/span&gt;
            &lt;span class="kwrd"&gt;Return&lt;/span&gt; _waveEmpty
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Get&lt;/span&gt;
        &lt;span class="kwrd"&gt;Set&lt;/span&gt;(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; value &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Boolean&lt;/span&gt;)
            _waveEmpty = value
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Set&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Property&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Class&lt;/pre&gt;

&lt;p&gt;We will need to setup an initialization method for our game, to setup all our collections for sprites, and add progressively difficult waves to our game. The meat of it all now lies in our game loop. For each collection of sprites we call methods that will iterate over its contents and make decisions based on collisions, locations of the sprites. After a loop on each main collection, we can use the 'remove us' collection to clear items from the main collection, remove their canvas from our game canvas, and delete them from the main collection. Finally, we check to see if enough time has elapsed to let the aliens drop another bomb:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;void&lt;/span&gt; gameLoop_Update(TimeSpan elapsed)
{
    &lt;span class="rem"&gt;//clear the current Vector so the sprite is not moving unless a keys is pressed&lt;/span&gt;
    PlayerShip.Velocity = &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector(0, 0);
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (keyHandler.IsKeyPressed(Key.Left))
    {
        PlayerShip.Velocity = Vector.CreateVectorFromAngle(270, 125);
    }
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (keyHandler.IsKeyPressed(Key.Right))
    {
        PlayerShip.Velocity = Vector.CreateVectorFromAngle(90, 125);
    }
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (keyHandler.IsKeyPressed(Key.Space))
    {
        &lt;span class="kwrd"&gt;switch&lt;/span&gt; (status)
        {
            &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.Ready:
                &lt;span class="kwrd"&gt;break&lt;/span&gt;;
            &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.Running:
                EntityFired(PlayerShip);
                &lt;span class="kwrd"&gt;break&lt;/span&gt;;
            &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.Paused:
                &lt;span class="kwrd"&gt;break&lt;/span&gt;;
            &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.BetweenWaves:
                status = GameState.Running;
                ctlInfo.GameInfo = &lt;span class="str"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;
                StartWave();
                &lt;span class="kwrd"&gt;break&lt;/span&gt;;
            &lt;span class="kwrd"&gt;case&lt;/span&gt; GameState.GameOver:
                &lt;span class="kwrd"&gt;break&lt;/span&gt;;
            &lt;span class="kwrd"&gt;default&lt;/span&gt;:
                &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        }
    }
    PlayerShip.Update(elapsed);

    BombLoop(elapsed);
    MissleLoop(elapsed);
    AlienLoop(elapsed);

    &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Alien alien &lt;span class="kwrd"&gt;in&lt;/span&gt; aliensRemove)
    {
        aliens.Remove(alien);
        gameRoot.Children.Remove(alien.SpriteCanvas);
        AlienShot(alien);
    }
    aliensRemove.Clear();

    &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Missle missle &lt;span class="kwrd"&gt;in&lt;/span&gt; misslesRemove)
    {
        missles.Remove(missle);
        gameRoot.Children.Remove(missle.SpriteCanvas);
    }
    misslesRemove.Clear();

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (nextShot &amp;lt;= DateTime.Now)
    {
        nextShot = DateTime.Now.AddMilliseconds(enemyShootMilliseonds).AddMilliseconds(elapsed.Milliseconds * -1);

        shotsThisPass = shotsAtOnce;
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (shotsThisPass &amp;gt; aliens.Count)
        {
            shotsThisPass = aliens.Count;
        }

        &lt;span class="kwrd"&gt;if&lt;/span&gt; (aliens.Count &amp;gt; 0)
        {
            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Alien alien &lt;span class="kwrd"&gt;in&lt;/span&gt; aliens)
            {
                alienShooters.Add(alien);
            }
        }

        &lt;span class="kwrd"&gt;while&lt;/span&gt; (alienShooters.Count &amp;gt; shotsThisPass)
        {
            alienShooters.RemoveAt(GetRandInt(0, alienShooters.Count - 1));
        }

        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Alien alien &lt;span class="kwrd"&gt;in&lt;/span&gt; alienShooters)
        {
            EntityFired(alien);
        }

        alienShooters.Clear();
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;VB 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; gameLoop_Update(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; elapsed &lt;span class="kwrd"&gt;As&lt;/span&gt; TimeSpan)
    &lt;span class="rem"&gt;'clear the current Vector so the sprite is not moving unless a keys is pressed &lt;/span&gt;
    PlayerShip.Velocity = &lt;span class="kwrd"&gt;New&lt;/span&gt; Vector(0, 0)
    &lt;span class="kwrd"&gt;If&lt;/span&gt; keyHandler.IsKeyPressed(Key.Left) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
        PlayerShip.Velocity = Vector.CreateVectorFromAngle(270, 125)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
    &lt;span class="kwrd"&gt;If&lt;/span&gt; keyHandler.IsKeyPressed(Key.Right) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
        PlayerShip.Velocity = Vector.CreateVectorFromAngle(90, 125)
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
    &lt;span class="kwrd"&gt;If&lt;/span&gt; keyHandler.IsKeyPressed(Key.Space) &lt;span class="kwrd"&gt;Then&lt;/span&gt;
        &lt;span class="kwrd"&gt;Select&lt;/span&gt; &lt;span class="kwrd"&gt;Case&lt;/span&gt; status
            &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.Ready
                &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
            &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.Running
                EntityFired(PlayerShip)
                &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
            &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.Paused
                &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
            &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.BetweenWaves
                status = GameState.Running
                ctlInfo.GameInfo = &lt;span class="str"&gt;&amp;quot;&amp;quot;&lt;/span&gt;
                StartWave()
                &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
            &lt;span class="kwrd"&gt;Case&lt;/span&gt; GameState.GameOver
                &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
            &lt;span class="kwrd"&gt;Case&lt;/span&gt; &lt;span class="kwrd"&gt;Else&lt;/span&gt;
                &lt;span class="kwrd"&gt;Exit&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Select&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
    PlayerShip.Update(elapsed)

    BombLoop(elapsed)
    MissleLoop(elapsed)
    AlienLoop(elapsed)

    &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; alien &lt;span class="kwrd"&gt;As&lt;/span&gt; Alien &lt;span class="kwrd"&gt;In&lt;/span&gt; aliensRemove
        aliens.Remove(alien)
        gameRoot.Children.Remove(alien.SpriteCanvas)
        AlienShot(alien)
    &lt;span class="kwrd"&gt;Next&lt;/span&gt;
    aliensRemove.Clear()

    &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; missle &lt;span class="kwrd"&gt;As&lt;/span&gt; Missle &lt;span class="kwrd"&gt;In&lt;/span&gt; misslesRemove
        missles.Remove(missle)
        gameRoot.Children.Remove(missle.SpriteCanvas)
    &lt;span class="kwrd"&gt;Next&lt;/span&gt;
    misslesRemove.Clear()

    &lt;span class="kwrd"&gt;If&lt;/span&gt; nextShot &amp;lt;= DateTime.Now &lt;span class="kwrd"&gt;Then&lt;/span&gt;
        nextShot = DateTime.Now.AddMilliseconds(enemyShootMilliseonds).AddMilliseconds(elapsed.Milliseconds * -1)

        shotsThisPass = shotsAtOnce
        &lt;span class="kwrd"&gt;If&lt;/span&gt; shotsThisPass &amp;gt; aliens.Count &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            shotsThisPass = aliens.Count
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;

        &lt;span class="kwrd"&gt;If&lt;/span&gt; aliens.Count &amp;gt; 0 &lt;span class="kwrd"&gt;Then&lt;/span&gt;
            &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; alien &lt;span class="kwrd"&gt;As&lt;/span&gt; Alien &lt;span class="kwrd"&gt;In&lt;/span&gt; aliens
                alienShooters.Add(alien)
            &lt;span class="kwrd"&gt;Next&lt;/span&gt;
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;

        &lt;span class="kwrd"&gt;While&lt;/span&gt; alienShooters.Count &amp;gt; shotsThisPass
            alienShooters.RemoveAt(GetRandInt(0, alienShooters.Count - 1))
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;While&lt;/span&gt;

        &lt;span class="kwrd"&gt;For&lt;/span&gt; &lt;span class="kwrd"&gt;Each&lt;/span&gt; alien &lt;span class="kwrd"&gt;As&lt;/span&gt; Alien &lt;span class="kwrd"&gt;In&lt;/span&gt; alienShooters
            EntityFired(alien)
        &lt;span class="kwrd"&gt;Next&lt;/span&gt;

        alienShooters.Clear()
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;If&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Now we can almost call it a game:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_8.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_thumb_3.png" width="500" height="402" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Taking it one step further, I jumped into Expression Blend to create a bit more interesting XAML that our sprites are loading into their SpriteCanvas. This XAML can be found in the full download of the source code. The results make our game just a tad bit more interesting:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_10.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/CreatingGameswithSilverlightASimpleShoot_14662/image_thumb_4.png" width="500" height="400" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Silverlight provides a number of capabilities to enable robust game development. It should be noted that in this game, we are only scratching the surface. The core of this example used a game loop to position sprites on a canvas. Additional capabilities within Silverlight such as &lt;a href="http://blogs.msdn.com/silverlight_sdk/archive/2008/03/21/silverlight-animations-a-walkthrough.aspx"&gt;animations&lt;/a&gt;, &lt;a href="http://silverlight.net/blogs/msnow/archive/2008/10/02/silverlight-tip-of-the-day-55-how-to-apply-styles-in-silverlight-part-i.aspx"&gt;styling&lt;/a&gt;, &lt;a href="http://timheuer.com/blog/archive/2008/10/06/silverlight-control-template-gallery-styles.aspx"&gt;templating&lt;/a&gt;, and &lt;a href="http://timheuer.com/blog/archive/2008/06/04/silverlight-introduces-visual-state-manager-vsm.aspx"&gt;visual states&lt;/a&gt; provide even more ways to push the limits of browser based games. If you are at all like me, dabbling with game development is a great way to get your feet wet with Silverlight and learn some basics. Happy coding!&lt;/p&gt;

&lt;p&gt;If you want to try this out, the download link for the source code is at the top of the article!&lt;/p&gt;

&lt;h3&gt;About The Author&lt;/h3&gt;

&lt;p&gt;Roger Guess is the Director of IT for The Wedge Group where he works with technologies such as Silverlight and WPF. He writes games in his spare time, and blogs at &lt;a href="http://silverlightaddict.com/blogs/"&gt;SilverlightAddict.com&lt;/a&gt;. He can be reached via email at &lt;a href="mailto:email@rogerguess.net"&gt;email@rogerguess.net&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9658490" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/web/default.aspx">web</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Audio/default.aspx">Audio</category></item><item><title>WiiMoteLib with MotionPlus</title><link>http://blogs.msdn.com/coding4fun/archive/2009/07/20/9842053.aspx</link><pubDate>Mon, 20 Jul 2009 21:35:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9842053</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9842053.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9842053</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9842053</wfw:comment><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/WiiMoteLibwithMotionPlus_BF3A/wiimote_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="wiimote" border="0" alt="wiimote" align="right" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/WiiMoteLibwithMotionPlus_BF3A/wiimote_thumb.jpg" width="240" height="240" /&gt;&lt;/a&gt; Brian Peek has updated his &lt;a href="http://www.brianpeek.com/blog/archive/2009/07/20/wiimotelib-1-8-beta-1-posted.aspx"&gt;WiiMote library to 1.8b1&lt;/a&gt; with freshly added support for the MotionPlus add-on.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Taiko Drum Master's TaTaCon drum controller supported (Dean Herbert) &lt;/li&gt;    &lt;li&gt;Bare-bones Wii MotionPlus support (&lt;a href="http://wiibrew.org"&gt;wiibrew.org&lt;/a&gt;, testing by Tyler Tolley) &lt;/li&gt;    &lt;li&gt;WiimoteTest UI changed to add MotionPlus data      &lt;ul&gt;       &lt;li&gt;Please note that the current version is VERY flakey and may not work at all for you. &lt;/li&gt;        &lt;li&gt;Pair your Wiimote, plug in your MotionPlus, call InitializeMotionPlus() &lt;/li&gt;        &lt;li&gt;Extensions plugged into the MotionPlus will give wacky results...not supported...yet! &lt;/li&gt;        &lt;li&gt;Internal changes to perhaps fix some threading issues &lt;/li&gt;     &lt;/ul&gt;   &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Be advised, this is beta and the data from MotionPlus is raw data.&amp;#160; You won’t get real orientation values&lt;/p&gt;  &lt;p&gt;If you feel like testing it out, pop over to &lt;a href="http://wiimotelib.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=30401"&gt;Codeplex&lt;/a&gt; and start playing with it!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9842053" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/c4fnews/default.aspx">c4fnews</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/wiimote/default.aspx">wiimote</category></item><item><title>Aliens invading with XNA</title><link>http://blogs.msdn.com/coding4fun/archive/2009/07/14/9831949.aspx</link><pubDate>Tue, 14 Jul 2009 21:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9831949</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9831949.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9831949</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9831949</wfw:comment><description>&lt;p&gt;&lt;a href="http://www.brianpeek.com/blog/"&gt;Brian Peek&lt;/a&gt;, of wiimote fame, gave a talk to the Facebook .NET Programmers Group and had it recorded.&amp;#160; The talk is about creating a clone of Space Invaders called Alien Attack in XNA Game Studio 3.0.&amp;#160; If you want even greater detail, this is the &lt;a href="http://www.amazon.com/dp/0596520743?tag=brianpcom-20&amp;amp;camp=213381&amp;amp;creative=390973&amp;amp;linkCode=as4&amp;amp;creativeASIN=0596520743&amp;amp;adid=19TVZXZSKP8VYN1181MD&amp;amp;"&gt;first chapter in the Coding4Fun book&lt;/a&gt;.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:164f176b-68e0-4dcf-a8f0-a5986c2964f8" class="wlWriterEditableSmartContent"&gt;&lt;div&gt;&lt;object width="400" height="302"&gt;&lt;param name="allowfullscreen" value="true" /&gt;&lt;param name="allowscriptaccess" value="always" /&gt;&lt;param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=5044730&amp;amp;server=vimeo.com&amp;amp;show_title=1&amp;amp;show_byline=1&amp;amp;show_portrait=0&amp;amp;color=&amp;amp;fullscreen=1" /&gt;&lt;embed src="http://vimeo.com/moogaloop.swf?clip_id=5044730&amp;amp;server=vimeo.com&amp;amp;show_title=1&amp;amp;show_byline=1&amp;amp;show_portrait=0&amp;amp;color=&amp;amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="302"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;p&gt;&lt;a href="http://vimeo.com/5044730"&gt;Meeting #13 - Coding4Fun: Designing a Video Game using XNA Game Studio 3.0&lt;/a&gt; from &lt;a href="http://vimeo.com/user1696553"&gt;Muneeb&lt;/a&gt; on &lt;a href="http://vimeo.com"&gt;Vimeo&lt;/a&gt;.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;  &lt;p&gt;You can find a quick, low-res, streaming version below, or you can download and &lt;a href="http://www.netprogrammers.org/dnlinks/recordings/13/ReplayMeeting.htm"&gt;play the recording at a higher resolution&lt;/a&gt; in &lt;a href="http://office.microsoft.com/en-us/livemeeting/default.aspx"&gt;Microsoft Office Live Meeting&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9831949" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/c4fnews/default.aspx">c4fnews</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/XNA/default.aspx">XNA</category></item><item><title>TweetCraft - An in-game Twitter client for World of Warcraft</title><link>http://blogs.msdn.com/coding4fun/archive/2009/07/02/9813390.aspx</link><pubDate>Thu, 02 Jul 2009 11:18:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9813390</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>17</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9813390.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9813390</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9813390</wfw:comment><description>&lt;p&gt;&lt;/p&gt; &lt;object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="320" height="240"&gt; &lt;param name="source" value="http://channel9.msdn.com/App_Themes/default/vp09_06_22.xap" /&gt; &lt;param name="initParams" value="m=mms://mschnlnine.wmod.llnwd.net/a1809/d1/ch9/8/0/6/6/7/4/TweetCraftTrailer_s_ch9.wmv,autostart=false,autohide=true,showembed=true, thumbnail=http://mschnlnine.vo.llnwd.net/d1/ch9/8/0/6/6/7/4/TweetCraftTrailer_large_ch9.png, postid=476608" /&gt; &lt;param name="background" value="#00FFFFFF" /&gt; &lt;a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;"&gt; &lt;img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none" /&gt; &lt;/a&gt; &lt;/object&gt; &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://ch9.ms/tweetcraft"&gt;TweetCraft&lt;/a&gt; is a World of Warcraft add-on that enables you to send and receive tweets using &lt;a href="http://twitter.com/"&gt;Twitter&lt;/a&gt; without leaving the game, automatically upload and post screenshots using &lt;a href="http://twitpic.com/"&gt;TwitPic&lt;/a&gt; and automatically tweet certain in-game events such as achievements and moving around in the world of Azeroth. TweetCraft customizes your World of Warcraft user interface by adding its own frame similar to the built-in ones (such as the Quest Log) and provides easy access to it through a button near the minimap, slash commands or your favorite launcher (e.g. ChocolateBar).&lt;/p&gt;  &lt;li&gt;&lt;b&gt;Compiled version: &lt;/b&gt;&lt;a href="http://tweetcraft.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29622#DownloadId=73735"&gt;Download&lt;/a&gt; &lt;/li&gt;  &lt;li&gt;&lt;b&gt;Source Code:&lt;/b&gt; &lt;a href="http://tweetcraft.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=29622"&gt;Download&lt;/a&gt; &lt;/li&gt;  &lt;li&gt;&lt;b&gt;Difficulty:&lt;/b&gt; Intermediate &lt;/li&gt;  &lt;li&gt;&lt;b&gt;Time Required:&lt;/b&gt; 3-5 hours &lt;/li&gt;  &lt;li&gt;&lt;b&gt;Cost:&lt;/b&gt; $19.99 for World of Warcraft; $14.99 for monthly fee &lt;/li&gt;  &lt;li&gt;&lt;b&gt;Software Needed:&lt;/b&gt; &lt;a href="http://addonstudio.codeplex.com/"&gt;AddOn Studio for World of Warcraft&lt;/a&gt;, Visual Studio Standard (or higher), although possible to use Visual C# Express as well, &lt;/li&gt;  &lt;li&gt;&lt;b&gt;Hardware:&lt;/b&gt; none     &lt;h3&gt;How TweetCraft works&lt;/h3&gt;    &lt;p&gt;Let&amp;#8217;s quickly walk through the different things that TweetCraft does to bring Twitter into World of Warcraft.&lt;/p&gt;    &lt;h4&gt;Overview&lt;/h4&gt;    &lt;p&gt;World of Warcraft&amp;#8217;s user interface is highly customizable and enables add-on developers to do almost anything in the game they can dream of. Add-ons can interact with the world, listen to events that happen in the game, alter the look of the default user interface or create brand new ones, but add-ons are seriously limited when it comes to talking to the world outside World of Warcraft. They cannot read or write files, call web services or do anything that could be potentially harmful. This is why TweetCraft comes with a Windows application that sits in the Notification Area (also known as the tray) and does the bulk of the work, giving the TweetCraft add-on only the data that it needs to display, such as tweets, replies and user pictures. All the Twitter web service calls, uploading of screenshots and housekeeping is done by this application. Let&amp;#8217;s see how!&lt;/p&gt;    &lt;h4&gt;The TweetCraft Tray application&lt;/h4&gt;    &lt;p&gt;The TweetCraft Tray application is a WPF application that periodically checks for new tweets, dowloads and converts the user pictures of the tweets&amp;#8217; authors and writes it out to a file that World of Warcraft picks up and the add-on can use. It also picks up all outgoing tweets queued up for sending and sends them using Twitter one-by-one. It watches for screenshots taken by World of Warcraft and uploads them using TwitPic immediately.&lt;/p&gt;    &lt;p&gt;To do all this, it has a couple of settings that need to be configured before it can work. For starters, it needs your Twitter credentials to be able to send tweets and post pictures on your behalf. It also needs to know which World of Warcraft account you are going to use to play and send and receive tweets with. Finally, you can opt out of automatically uploading screenshots you take during game. Figure 1 shows the TweetCraft Settings window that shows up when you first start TweetCraft after installation and is also available by right clicking the tray icon (Figure 2).&lt;/p&gt;    &lt;p&gt;&lt;img border="0" alt="Figure1_TweetCraftSettingsWindow" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/TweetCraftAningameTwitterclientforWorldo_1236/Figure1_TweetCraftSettingsWindow_96f280c9-cef7-47db-a5a8-1b3ae3da5d13.png" width="415" height="544" /&gt;       &lt;br /&gt;&lt;strong&gt;Figure 1. TweetCraft Settings Window&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;&lt;img border="0" alt="Figure2_TweetCraftTrayIcon" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/TweetCraftAningameTwitterclientforWorldo_1236/Figure2_TweetCraftTrayIcon_647e915d-5ae9-41ab-9f6a-85ffbf94c0cc.png" width="418" height="172" /&gt;       &lt;br /&gt;&lt;strong&gt;Figure 2. TweetCraft tray icon in the notification area&lt;/strong&gt;&lt;/p&gt;    &lt;h4&gt;Getting data from Twitter&lt;/h4&gt;    &lt;p&gt;TweetCraft uses an open-source library called &lt;strong&gt;TwitterLib&lt;/strong&gt; to send and receive tweets using Twitter&amp;#8217;s API. TwitterLib is part of the open-source WPF Twitter Client &lt;a href="http://code.google.com/p/wittytwitter/"&gt;WittyTwitter&lt;/a&gt; and wraps the web services provided by Twitter into an easy to use .NET class library. TweetCraft wraps this library into a simple class called &lt;strong&gt;TwitterClient&lt;/strong&gt; that all the services in TweetCraft rely on. One of those services is the &lt;strong&gt;TwitterBackgroundService&lt;/strong&gt; which polls Twitter periodically to check for new tweets. By default, this is set to five minutes. If new tweets or replies are available, an event is raised that other components of TweetCraft can handle and work with the new tweets, download user pictures, etc. Figure 3 shows the three classes that take part in downloading new tweets and replies and notifying other classes.&lt;/p&gt;    &lt;p&gt;&lt;img border="0" alt="Figure3_TwitterBackgroundService" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/TweetCraftAningameTwitterclientforWorldo_1236/Figure3_TwitterBackgroundService_2c37e43e-7c00-4a0b-8357-f821fc443b79.png" width="715" height="333" /&gt;       &lt;br /&gt;&lt;strong&gt;Figure 3. TwitterBackgroundService, TwitterClient and TwitterNet (TwitterLib)&lt;/strong&gt;&lt;/p&gt;    &lt;h4&gt;&amp;#160;&lt;/h4&gt;    &lt;h4&gt;Dynamically building TGA images&lt;/h4&gt;    &lt;p&gt;When it comes to visualizing images (or textures, in WoW parlance), World of Warcraft poses quite a few restrictions on what files can be used and where does files should be. First of all, World of Warcraft only supports the TGA and BLP file formats. TGA stands for the Truevision Targa format, whereas BLP is Blizzard&amp;#8217;s own image format. The images must also have dimensions of two (32&amp;#215;32, 64&amp;#215;64 pixels and so on&amp;#8230;), otherwise they won&amp;#8217;t show up. On the other hand, Twitter user pictures are available as 48&amp;#215;48 or 75&amp;#215;75 pixels and in various well-known formats, such as JPG, PNG or even GIF. TweetCraft needs to convert these pictures to 64&amp;#215;64 TGA files if it wants to play nice with World of Warcraft. This is done for each tweet&amp;#8217;s authors user picture that will show up in World of Warcraft. The converted pictures must be placed in the &lt;em&gt;Interface&lt;/em&gt; folder of World of Warcraft where the add-ons themselves also reside. The &lt;strong&gt;TwitterUserPictureService&lt;/strong&gt; can retrieve the user picture and convert it to a 64&amp;#215;64 TGA file that contains the 48&amp;#215;48 user picture in the middle and puts that file into the same directory where the TweetCraft add-on is installed. The files are named after each user&amp;#8217;s screen name and if &lt;strong&gt;TwitterUserPictureService&lt;/strong&gt; sees that a file is already there, it won&amp;#8217;t convert it again, saving some time and computing power.&lt;/p&gt;    &lt;p&gt;Figure 4 shows &lt;strong&gt;TwitterUserPictureService&lt;/strong&gt; and two helper classes that take care of downloading and the actual conversion. The &lt;code&gt;GetUserPicturePath&lt;/code&gt; method returns the path of the resulting World of Warcraft compatible TGA file for a single user.&lt;/p&gt;    &lt;p&gt;&lt;img border="0" alt="Figure4_TwitterUserPictureService" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/TweetCraftAningameTwitterclientforWorldo_1236/Figure4_TwitterUserPictureService_abf4a52c-8e97-4458-b620-dc62d0be314d.png" width="475" height="313" /&gt;       &lt;br /&gt;&lt;strong&gt;Figure 4. TwitterUserPictureService and its helper classes&lt;/strong&gt;&lt;/p&gt;    &lt;h4&gt;Serializing tweets into Lua&lt;/h4&gt;    &lt;p&gt;Until now, we were quite secretive about how we can avoid World of Warcraft&amp;#8217;s restrictions and let TweetCraft be able to read the data and show it. We&amp;#8217;ve said that add-ons cannot read or write files and that still hasn&amp;#8217;t changed during the last few minutes, but add-ons must have a way somehow to save their settings and other information when I log out of the game and load that when I log back in, you sincerely ask and you&amp;#8217;re right! Although add-ons cannot read or write to files, they can ask World of Warcraft to save and load some of their global variables whenever the user reloads the UI (by entering an instance or hearthing), logs in or logs out. These files are called the SavedVariables files. When TweetCraft finishes downloading tweets, converting user pictures and everything else, it saves them using the very same format and to the very same file World of Warcraft uses on behalf of the TweetCraft add-on. Neat, huh? Just wait a little bit more&amp;#8230; :)&lt;/p&gt;    &lt;p&gt;Those variables that an add-on asks to be saved between sessions are saved into a Lua file, which is the same programming language add-on developers can use to write their add-ons. So basically, World of Warcraft creates a file that contains code full of assignment statements (think &lt;code&gt;a = 5&lt;/code&gt;) that it will just run whenever those values need to be set again. More complex data structures can also be expressed this way using Lua tables, the swiss army-knife of Lua.&lt;/p&gt;    &lt;p&gt;It sounds like a great idea to put all the data we would like for TweetCraft to use, but all we have is .NET objects, arrays and dictionaries. Fortunately, all these data structures can be expressed with simple Lua types and tables and TweetCraft comes with a class called &lt;strong&gt;LuaSerializer&lt;/strong&gt; that produces Lua code from any .NET object (with a few restrictions, of course), very similar to how &lt;strong&gt;XmlSerializer&lt;/strong&gt; works.&lt;/p&gt;    &lt;p&gt;To make this even more simple, TweetCraft contains the concept of a &lt;strong&gt;SavedVariablesChannel&lt;/strong&gt; that you can just drop objects in, flush it and those object show up on the other side. In our case, as Lua variables. What&amp;#8217;s even better, it works the other way around as well. The TweetCraft add-on stores all the tweets queued up and ready to be sent in a variable and triggers World of Warcraft to save them to the SavedVariables file. The &lt;strong&gt;SavedVariablesChannel&lt;/strong&gt; detects this by watching for changes made to the SavedVariables file using &lt;strong&gt;FileSystemWatcher&lt;/strong&gt; and notifies other components of the TweetCraft application that can pull out the information and send the tweets.&lt;/p&gt;    &lt;p&gt;Figure 5 shows &lt;strong&gt;SavedVariablesChannel&lt;/strong&gt; and its base class, &lt;strong&gt;ValueChannelBase&lt;/strong&gt; which keeps track of all objects thrown into it until it is flushed.&lt;/p&gt;    &lt;p&gt;&lt;img border="0" alt="Figure5_SavedVariablesChannel" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/TweetCraftAningameTwitterclientforWorldo_1236/Figure5_SavedVariablesChannel_d3bf3853-d03e-43a6-b8e4-899708f431dd.png" width="475" height="325" /&gt;       &lt;br /&gt;&lt;strong&gt;Figure 5. SavedVariablesChannel and ValueChannelBase&lt;/strong&gt;&lt;/p&gt;    &lt;h4&gt;Refreshing the data in World of Warcraft&lt;/h4&gt;    &lt;p&gt;There&amp;#8217;s one last problem we face when trying to achieve this two-way communication between the TweetCraft add-on and the application. If World of Warcraft is running, we cannot just overwrite the SavedVariables file and expect World of Warcraft to pick it up as it does not watch for changes made to the file. If we were to overwrite the file and subsequently the user logs out, World of Warcraft will happily overwrite the information we placed there. There&amp;#8217;s another trick we have to resort to: the TweetCraft add-on can trigger the reloading of the UI which will unload all add-ons, save their variables and then load them again. There&amp;#8217;s a a little bit of time between the saving and loading of a particular add-ons SavedVariables file and that&amp;#8217;s when TweetCraft can safely read the file for queued up tweets and overwrite it with the new data. This is what happens when the user clicks the Refresh button in TweetCraft&amp;#8217;s frame.&lt;/p&gt;    &lt;h4&gt;Sending tweets and uploading screenshots&lt;/h4&gt;    &lt;p&gt;Whenever the user queues up tweets for sending, they are not immediately sent but kept in a data structure that will be saved out to the SavedVariables file by World of Warcraft. We&amp;#8217;ve seen that his happens when either the user logs out or presses the Refresh button. The updated SavedVariables file gets detected by &lt;strong&gt;SavedVariablesChannel&lt;/strong&gt; and the &lt;code&gt;ChannelUpdated&lt;/code&gt; event is raised. When the TweetCraft application handles this event, it cannot immediately send the tweets as that would take a couple seconds and if you remember from the last section, we only have a tiny little window of time to process the values and save back all the data we would like to send back. For this reason, the tweets that are picked up from the SavedVariables file using the &lt;strong&gt;SavedVariablesChannel&lt;/strong&gt; get quickly queued up in the &lt;strong&gt;TwitterDispatcherService&lt;/strong&gt; that then goes ahead and starts sending them one by one in the background. To make sure that all the values we prepared to save into the file get flushed, the &lt;strong&gt;SavedVariablesChannel&lt;/strong&gt; has a property called &lt;code&gt;AutoFlush&lt;/code&gt; that is enabled by default.&lt;/p&gt;    &lt;p&gt;One thing we have not mentioned is that &lt;strong&gt;TwitterDispatcherService&lt;/strong&gt; can also upload and post pictures using TwitPic. It watches the &lt;em&gt;Screenshots&lt;/em&gt; folder of World of Warcraft for new files and if AutoTweeting of screenshots is enabled, immediately queues up the picture for uploading and posting it. This also means that you don&amp;#8217;t need to press the Refresh button or wait until you log off for screenshots to be posted. They will show up immediately in your Twitter feed.&lt;/p&gt;    &lt;p&gt;The &lt;strong&gt;TwitterDispatcherService&lt;/strong&gt; uses &lt;strong&gt;TwitterClient&lt;/strong&gt; (and &lt;strong&gt;TwitterLib&lt;/strong&gt;) to send the tweets but it does not interfere with the quick reading and writing of the SavedVariables file we discussed previously. It also uses &lt;strong&gt;TwitPicClient&lt;/strong&gt; to upload and post pictures. Figure 6 shows &lt;strong&gt;TwitterDispatcherService&lt;/strong&gt;, &lt;strong&gt;TwitterClient&lt;/strong&gt; and &lt;strong&gt;TwitPicClient&lt;/strong&gt;.&lt;/p&gt;    &lt;p&gt;&lt;img border="0" alt="Figure6_TwitterDispatcherService" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/TweetCraftAningameTwitterclientforWorldo_1236/Figure6_TwitterDispatcherService_575f8576-4afc-4191-b41e-e45084b19ae6.png" width="691" height="348" /&gt;       &lt;br /&gt;&lt;strong&gt;Figure 6. TwitterDispatcherService, TwitterClient and TwitPicClient&lt;/strong&gt;&lt;/p&gt;    &lt;h4&gt;Bringing it all together&lt;/h4&gt;    &lt;p&gt;So far, we&amp;#8217;ve looked at components of TweetCraft that implemented a particular part of the application. Retrieving and sending tweets, converting user pictures and making all this information available for the TweetCraft add-on, but there&amp;#8217;s an important class that brings all this together and contains all the logic of how these components interact with each other. We also need to be able to detect World of Warcraft installations, figure out what accounts are available and where to look for files like the SavedVariables files and put the converted user pictures. The &lt;strong&gt;TweetCraft&lt;/strong&gt; class implements the logic required and relies on the &lt;strong&gt;WorldOfWarcraft&lt;/strong&gt; class for everything World of Warcraft related.&lt;/p&gt;    &lt;p&gt;Figure 7 shows the &lt;strong&gt;TweetCraft&lt;/strong&gt; and &lt;strong&gt;WorldOfWarcraft&lt;/strong&gt; side by side.&lt;/p&gt;    &lt;p&gt;&lt;img border="0" alt="Figure7_TweetCraft" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/TweetCraftAningameTwitterclientforWorldo_1236/Figure7_TweetCraft_6bf95ca8-d5c2-4d54-8169-c02de469ecb3.png" width="523" height="463" /&gt;       &lt;br /&gt;&lt;strong&gt;Figure 7. TweetCraft and WorldOfWarcraft&lt;/strong&gt;&lt;/p&gt;    &lt;h3&gt;Summary&lt;/h3&gt;    &lt;p&gt;While this is a high-level overview of TweetCraft only, it gives you a good idea of how it enables World of Warcraft players to use Twitter to send and receive tweets, upload the screenshots and share their achievements right inside the game. Make sure you try out TweetCraft if you have World of Warcraft installed and if you&amp;#8217;re curious, &lt;a href="http://tweetcraft.codeplex.com/SourceControl/ListDownloadableCommits.aspx"&gt;download the source code&lt;/a&gt; and take a deeper look.&lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;&lt;img src="http://blogs.msdn.com/blogfiles/danielfe/WindowsLiveWriter/Coding4FunBookThepeopleandtheprojects_99D7/gaborratky_762a3823-615d-45f0-84f7-bb5c1d0a2707.png" /&gt;       &lt;br /&gt;Gabor Ratky (&lt;a href="http://rgabostyle.com/"&gt;Blog&lt;/a&gt;, &lt;a href="http://twitter.com/rgabostyle"&gt;Twitter&lt;/a&gt;), Senior Software Engineer and Coding4Fun Ninja at &lt;a href="http://www.epam.com"&gt;EPAM Systems&lt;/a&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;Gabor is the developer behind TweetCraft and has been working on Coding4Fun projects for almost two years. He also leads the development of &lt;a href="http://addonstudio.codeplex.com/"&gt;AddOn Studio for World of Warcraft&lt;/a&gt;, a Visual Studio-based IDE for building World of Warcraft add-ons and he co-authored the chapter in the &lt;a href="http://www.amazon.com/Coding4Fun-Programming-Projects-Wiimote-Warcraft/dp/0596520743/ref=sr_1_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1246522615&amp;amp;sr=8-1"&gt;Coding4Fun&lt;/a&gt; book about it. He is a VSX Insider and was invited to speak at various events about Visual Studio Extensibility. When not working on Coding4Fun projects, Gabor works on large scale, enterprise solutions using exciting Microsoft technologies. He lives in Budapest, Hungary and loves traveling, good wine and Channel9 videos.&lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;&lt;/p&gt; &lt;/li&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9813390" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/mash+up/default.aspx">mash up</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/web+services/default.aspx">web services</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Visual+Studio/default.aspx">Visual Studio</category></item><item><title>XNA Game Studio 3.1</title><link>http://blogs.msdn.com/coding4fun/archive/2009/06/11/9727453.aspx</link><pubDate>Thu, 11 Jun 2009 22:14:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9727453</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9727453.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9727453</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9727453</wfw:comment><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/XNAGameStudio3.1_C84A/cco_shell_default_header_xna_logo_print%5B1%5D_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="cco_shell_default_header_xna_logo_print[1]" border="0" alt="cco_shell_default_header_xna_logo_print[1]" align="right" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/XNAGameStudio3.1_C84A/cco_shell_default_header_xna_logo_print%5B1%5D_thumb.jpg" width="316" height="83" /&gt;&lt;/a&gt; XNA Game Studio 3.1 has just been released!&amp;#160; &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=80782277-d584-42d2-8024-893fcd9d3e82"&gt;Download XNA GS 3.1&lt;/a&gt; and check out the &lt;a href="http://creators.xna.com/en-us/article/convert30to31"&gt;walkthrough for upgrading your old projects to v3.1&lt;/a&gt;!&amp;#160; For a more in-depth overview of what is new in 3.1, head to &lt;a title="http://creators.xna.com/en-us/article/xnags3.1whatsnew" href="http://creators.xna.com/en-us/article/xnags3.1whatsnew"&gt;http://creators.xna.com/en-us/article/xnags3.1whatsnew&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;A quick overview of what is new:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;Avatar Support&lt;/strong&gt;: Render and animate Avatars to use in your game to represent gamers and other characters within your game. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Xbox LIVE Party Support&lt;/strong&gt;: Enabling gamers to communicate, even when each gamer is not playing the same game in the same multiplayer session. LIVE Party supports up to an eight-way group voice chat for gamers and keeps gamers connected before, during, and after a gameplay session, persisting across title switches. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Video Playback&lt;/strong&gt;: XNA Game Studio now supports the ability to play back video that can be used for such purposes as opening splash and logo scenes, cut scenes, or in-game video displays. This set of XNA Framework APIs supports the following features: &lt;/li&gt;    &lt;li&gt;Full screen video playback &lt;/li&gt;    &lt;li&gt;Video playback to simple textures in game &lt;/li&gt;    &lt;li&gt;Control of playback such as pause/resume and stop &lt;/li&gt;    &lt;li&gt;Retrieve properties of the video, such as playback time, size, and frame rate &lt;/li&gt;    &lt;li&gt;Determine the type and usage of the audio track, such as if it has music, dialog, or music and dialog &lt;/li&gt;    &lt;li&gt;Play back multiple video streams at the same time &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Audio API&lt;/strong&gt;: 3.1 has a new usage pattern of SoundEffect.Play. Sound instances created by Play calls are disposed automatically when playback ends, and SoundEffect.Play returns a Boolean to indicate success or failure. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Content Pipeline Enhancements&lt;/strong&gt;: improvements making it much easier to add customer types (custom attributes for run-time of an object and run-time type version of an object, and the ability to determine if deserialization into an existing object is possible). &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;XACT3 Support&lt;/strong&gt;: includes support for XACT3 with new features including the ability to enable a filter on every track, and support for the xWMA compression format. &lt;/li&gt;    &lt;li&gt;Visual Studio Changes: XNA Game Studio 3.1 supports both 3.0 and 3.1 projects, and it includes support for upgrading projects from 3.0 to 3.1.&lt;/li&gt; &lt;/ul&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9727453" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/c4fnews/default.aspx">c4fnews</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/XNA/default.aspx">XNA</category></item><item><title>Robozzle</title><link>http://blogs.msdn.com/coding4fun/archive/2009/06/02/9688468.aspx</link><pubDate>Wed, 03 Jun 2009 04:02:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9688468</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9688468.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9688468</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9688468</wfw:comment><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/Robozzle_119B7/image_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 0px 0px 5px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" align="right" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/Robozzle_119B7/image_thumb.png" width="240" height="172" /&gt;&lt;/a&gt; &lt;a href="http://www.robozzle.com/"&gt;Robozzle&lt;/a&gt; is a Silverlight game that helps teach players the art of programming logic through a series of fun and challenging community-created puzzles.&amp;#160; What's more, the game is &amp;quot;community sourced&amp;quot; meaning not only can you contribute new levels - but actually help expand the game and shape it's future. (&lt;a href="http://robozzle.codeplex.com/"&gt;http://robozzle.codeplex.com/&lt;/a&gt;).&lt;/p&gt;  &lt;p&gt;[via &lt;a href="http://www.imagitronics.org/"&gt;Tim Farley&lt;/a&gt;]&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9688468" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/c4fnews/default.aspx">c4fnews</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/Silverlight/default.aspx">Silverlight</category></item><item><title>Building Multiplayer Texas Holdem Poker For The Zune</title><link>http://blogs.msdn.com/coding4fun/archive/2009/05/05/9582637.aspx</link><pubDate>Tue, 05 May 2009 22:06:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9582637</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9582637.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9582637</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9582637</wfw:comment><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/BuildingMultiplayerTexasHoldemPokerForTh_10423/clip_image002_2.gif"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="clip_image002" border="0" alt="clip_image002" align="right" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/BuildingMultiplayerTexasHoldemPokerForTh_10423/clip_image002_thumb.gif" width="240" height="320" /&gt;&lt;/a&gt;Have you ever been interested in creating a game that harnesses XNA’s powerful network library to create multiplayer experiences for the Zune device?&lt;/p&gt;  &lt;p&gt;The Zune firmware version 3.1 brought us a professionally built incarnation of Texas Holdem that supports network play. Understanding how to send and receive data with the Zune can be a little daunting at first, but once you understand the pattern, it’s easy. &lt;/p&gt;  &lt;p&gt;To build out the entire game, you probably need about a week, but you can build some simpler examples in far less time. &lt;/p&gt;  &lt;p&gt;This is an earlier project from before the release of my &lt;a href="http://www.tinyurl.com/zunebook"&gt;Zune game development book&lt;/a&gt;. Accordingly, some of the code samples you see in this article may be inconsistent with what you find in the download. The code in the article is the “correct” way to do things. The code in the download is still a work in progress.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;b&gt;Source Code:&lt;/b&gt; &lt;a href="https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=zuneholdem&amp;amp;ReleaseId=2382"&gt;Download&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Difficulty &lt;/b&gt;Intermediate|Advanced &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Time Required:&lt;/b&gt; 40 hours &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Hardware:&lt;/b&gt; 2x &lt;a href="http://www.amazon.com/Zune-Video-MP3-Player-Black/dp/B001EWDFCI/ref=pd_bbs_sr_3?ie=UTF8&amp;amp;s=electronics&amp;amp;qid=1237183912&amp;amp;sr=8-3"&gt;Zune&lt;/a&gt; devices ($170 ea) &lt;/li&gt;    &lt;li&gt;&lt;b&gt;Software Needed:&lt;/b&gt; &lt;a href="http://www.microsoft.com/express/download/"&gt;Visual C# Express&lt;/a&gt;, &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7d70d6ed-1edd-4852-9883-9a33c0ad8fee&amp;amp;displaylang=en"&gt;XNA Game Studio 3.0&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;The Workflow&lt;/h3&gt;  &lt;p&gt;Developing multiplayer games for the Zune is interesting because you have to deploy to each device individually. Once you have a build that works for you, it’s helpful to run-deploy (Control+F5) to one device, leave it running there, and then plug in the other Zune and debug-deploy (F5) to it. This way you have one debuggable instance of the game running. Make sure to set the appropriate Zune device as the default in the XNA Game Studio Device Center (accessible from the Tools menu).&lt;/p&gt;  &lt;h3&gt;Starting A Network Session&lt;/h3&gt;  &lt;p&gt;Because the Zunes connect over an ad-hoc, peer-to-peer connection rather than through an access point, you will have to designate one Zune as the host device. The host is usually determined to be the one that creates a new game. Therefore, all Zunes that join the host’s network session are simply peers. The difference between the host and the peers is that the host usually maintains the game state on top of executing the game as well, because the game data has to be centralized somewhere. Keep that in mind, because if one Zune is doing substantially more processing, it can lag behind and mess up your network session. Also, remember that all Zunes are running the exact same copy of the game, so the game must support both host and peer scenarios.&lt;/p&gt;  &lt;h4&gt;Create / Join / Lobby Model&lt;/h4&gt;  &lt;p&gt;Most peer-to-peer connected games allow a user to create or join a game. After doing so, the player is funneled into an area called the Lobby where they can specify their readiness. When all players are ready, the host can start the game. Some of this functionality is provided directly by the XNA Framework’s Net and GamerServices libraries. &lt;/p&gt;  &lt;p&gt;I normally create three separate screens based on the &lt;a href="http://creators.xna.com/en-US/sample/network_game_state_mgt_sample"&gt;Network Game State Management sample&lt;/a&gt; from the Creators Club website. The first is the Create screen, which looks exactly like a lobby. It starts up a network session and waits for players to join and become ready. The code to create a network session looks like this (note that I have employed some abstraction to make my code a little more cohesive):&lt;/p&gt;  &lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; CreateZuneSession(&lt;span class="kwrd"&gt;int&lt;/span&gt; maxNetworkPlayers)
{
    KillSession();

    &lt;span class="kwrd"&gt;try&lt;/span&gt;
    {
        Session = NetworkSession.Create(NetworkSessionType.SystemLink, 1,      
            maxNetworkPlayers); 
        Me = Session.LocalGamers[0];
    }
    &lt;span class="kwrd"&gt;catch&lt;/span&gt; (NetworkNotAvailableException)
    {
        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NetworkNotAvailableException(&lt;span class="str"&gt;&amp;quot;Zune wireless is not 
            enabled.&amp;quot;&lt;/span&gt;);
    }
    &lt;span class="kwrd"&gt;catch&lt;/span&gt; (NetworkException ne)
    {
        &lt;span class="kwrd"&gt;throw&lt;/span&gt; ne;
    }

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (Session == &lt;span class="kwrd"&gt;null&lt;/span&gt;)
        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NetworkException(&lt;span class="str"&gt;&amp;quot;The network session could not be 
            created.&amp;quot;&lt;/span&gt;);
}&lt;/pre&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;All Zune network sessions are of the type SystemLink, much like LAN-networked Xbox consoles. The ‘1’ parameter specifies the number of local players – of course, on a Zune, there can only be one.&lt;/p&gt;

&lt;p&gt;This line of code sets the Session property to a newly created network session. The Join screen, running on another Zune, will find this session asynchronously as an available network session and attempt to join it. The first step is to enumerate available network sessions:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C# 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// Asynchronous: Begins to discover network sessions.&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; BeginGetAvailableSessions()
{
    &lt;span class="rem"&gt;// Destroy any existing connections&lt;/span&gt;
    KillSession();

    NetworkSession.BeginFind(NetworkSessionType.SystemLink, 1, &lt;span class="kwrd"&gt;null&lt;/span&gt;, &lt;span class="kwrd"&gt;new&lt;/span&gt; 
        AsyncCallback(SessionsFoundCallback), &lt;span class="kwrd"&gt;null&lt;/span&gt;);
}&lt;/pre&gt;

&lt;p&gt;This code looks for SystemLink sessions and calls the callback method SessionsFoundCallback when the operation completes (successfully or unsuccessfully). If sessions are found, an event is fired. Other screens can subscribe to this event and transition to other screens or do other processing with the network session.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// Called when network sessions are found.&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;param name=&amp;quot;result&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SessionsFoundCallback(IAsyncResult result)
{
    AvailableNetworkSessionCollection availableSessions = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
    availableSessions = NetworkSession.EndFind(result);

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (NetworkSessionsFound != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
        NetworkSessionsFound(availableSessions);
}        &lt;/pre&gt;

&lt;p&gt;When all players are ready and the host presses the middle button, the network session’s StartGame() method is called, which will cause all connected peers to receive a GameStarted event. This loads up the playing screen.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; (allPlayersReady &amp;amp;&amp;amp; atLeastTwoPlayers)
{
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (ScreenManager.Network.Session != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
        ScreenManager.Network.Session.StartGame();
}&lt;/pre&gt;

&lt;p&gt;That’s basically how to connect up two Zunes in a network session. See my &lt;a href="http://www.tinyurl.com/zunebook"&gt;book&lt;/a&gt; for a much more detailed explanation.&lt;/p&gt;

&lt;h3&gt;Dealing With Cards&lt;/h3&gt;

&lt;p&gt;As Poker is a card game, you might want to develop a testable, standalone library that you can use not only to house your objects, but also to write out all the intense logic required for a game like poker. The task of determining what a player’s best hand is out of seven cards (and whether it beats another player’s hand) is more in-depth than you might expect. &lt;/p&gt;

&lt;p&gt;An easy way to go about this is to create a Zune game library. I called mine CardLib. CardLib has objects such as these:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Card which has a suit, a value, some comparers and some utility methods) &lt;/li&gt;

  &lt;li&gt;Deck, which has a collection of cards and methods like Shuffle, ResetDeck, etc. &lt;/li&gt;

  &lt;li&gt;Dealer, which has a deck, a list of the five community cards, and methods like Shuffle, DealCard, Burn, DealFlop, DealTurn, etc. &lt;/li&gt;

  &lt;li&gt;HoldemHand, which contains all of the logic for determining what a hand is (always 7 cards) &lt;/li&gt;

  &lt;li&gt;BestHand, which takes a HoldemHand and determines what the best possible hand is within that hand &lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;Shuffling Cards&lt;/h4&gt;

&lt;p&gt;Shuffling cards is surprisingly easy. I use the &lt;a href="http://www.codinghorror.com/blog/archives/001015.html"&gt;Knuth-Fisher-Yates shuffling algorithm&lt;/a&gt;, which takes every card in the deck and randomly swaps it with another.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C# 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
&lt;span class="rem"&gt;/// Knuth-Fisher-Yates shuffling algorithm:&lt;/span&gt;
&lt;span class="rem"&gt;/// http://www.codinghorror.com/blog/archives/001015.html&lt;/span&gt;
&lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Shuffle()
{
    Random rand = &lt;span class="kwrd"&gt;new&lt;/span&gt; Random();
    &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; cardIndex = _cards.Count - 1; cardIndex &amp;gt; 0; cardIndex--)
    {
        &lt;span class="kwrd"&gt;int&lt;/span&gt; randomIndex = rand.Next(cardIndex + 1);
        SwapCardsByIndex(cardIndex, randomIndex);
    }
    _dealIndex = 0;
}&lt;/pre&gt;

&lt;h4&gt;Poker Logic&lt;/h4&gt;

&lt;p&gt;An example method you’ll see in this library is this snippet to check if the hand is a royal flush:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C# 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsRoyalFlush(&lt;span class="kwrd"&gt;out&lt;/span&gt; List&amp;lt;Card&amp;gt; winningCards)
{
    List&amp;lt;Card&amp;gt; straightFlushCards;

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (IsStraightFlush(&lt;span class="kwrd"&gt;out&lt;/span&gt; straightFlushCards))
    {                
        &lt;span class="rem"&gt;// Check to make sure the straight flush cards are: 10 J Q K A&lt;/span&gt;
        straightFlushCards.Sort();

        &lt;span class="kwrd"&gt;if&lt;/span&gt; (straightFlushCards[0].Value == 10 &amp;amp;&amp;amp;
            straightFlushCards[1].Value == Card.Jack &amp;amp;&amp;amp;
            straightFlushCards[2].Value == Card.Queen &amp;amp;&amp;amp;
            straightFlushCards[3].Value == Card.King &amp;amp;&amp;amp;
            straightFlushCards[4].Value == Card.Ace)
        {
            winningCards = straightFlushCards;
            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;true&lt;/span&gt;;
        }

        &lt;span class="kwrd"&gt;else&lt;/span&gt;
        {
            winningCards = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;;
        }
    }
    &lt;span class="kwrd"&gt;else&lt;/span&gt;
    {
        winningCards = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
        &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;;
    }
}&lt;/pre&gt;

&lt;p&gt;There is a ton of similar and even more disgusting logic to peruse in the HoldemHand.cs file in the region entitled “Poker Logic Fun Times.” This logic covers every possible poker hand and could probably be refactored to be way more elegant.&lt;/p&gt;

&lt;p&gt;When it comes time to evaluate the winner, each player’s best hand is determined and they are evaluated against each other to determine the winner. This code returns a list of WinnerInfo objects, which returns the player info and a list of the cards they won with.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C#&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; List&amp;lt;WinnerInfo&amp;gt; DetermineWinners()
{
    BestHand overallBestHand = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
    HoldemPlayer winner = &lt;span class="kwrd"&gt;null&lt;/span&gt;;

    List&amp;lt;WinnerInfo&amp;gt; winners = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;WinnerInfo&amp;gt;();

    &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (HoldemPlayer player &lt;span class="kwrd"&gt;in&lt;/span&gt; _players)
    {
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (player.Status != PlayerStatus.Folded)
        {
            HoldemHand tempHand = &lt;span class="kwrd"&gt;new&lt;/span&gt; HoldemHand(player.Pocket, _communityCards);
            BestHand bestHand = tempHand.GetBestHand();

            &lt;span class="kwrd"&gt;if&lt;/span&gt; (overallBestHand == &lt;span class="kwrd"&gt;null&lt;/span&gt;) &lt;span class="rem"&gt;// if there is no best hand yet&lt;/span&gt;
            {
                overallBestHand = bestHand;
                winner = player;
                winners.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; WinnerInfo(overallBestHand, winner, Me));
            }
            &lt;span class="kwrd"&gt;else&lt;/span&gt;
            {
                &lt;span class="rem"&gt;// if this hand beats the current best hand&lt;/span&gt;
                &lt;span class="kwrd"&gt;if&lt;/span&gt; (BestHand.Beats(bestHand, overallBestHand))
                {
                    winners.Clear();
                    overallBestHand = bestHand;
                    winner = player;
                    winners.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; WinnerInfo(overallBestHand, winner, Me));
                }
                &lt;span class="kwrd"&gt;else&lt;/span&gt;
                {
                    &lt;span class="kwrd"&gt;if&lt;/span&gt; (BestHand.IsEquivalentTo(bestHand, overallBestHand))
                    {
                        winners.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; WinnerInfo(bestHand, player, Me));
                    }
                }
            }
        }
    }

    _winners = winners;
    &lt;span class="kwrd"&gt;return&lt;/span&gt; winners;
}&lt;/pre&gt;

&lt;h4&gt;Drawing Cards On The Screen&lt;/h4&gt;

&lt;p&gt;Rather than have 52 different sprites that represent each possible card, I just made my own sheet of Zune-sized playing cards. Horizontally, they are ordered by value (1-13) and vertically they are ordered by suit (both alphabetically and corresponding to the numeric value of the Suit enumeration in the project, 1-4).&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/BuildingMultiplayerTexasHoldemPokerForTh_10423/clip_image004_2.gif"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://blogs.msdn.com/blogfiles/coding4fun/WindowsLiveWriter/BuildingMultiplayerTexasHoldemPokerForTh_10423/clip_image004_thumb.gif" width="572" height="240" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When a specific card is requested to be drawn, a formula is used to calculate the source rectangle from this larger image. This is very similar to how fonts worked before spritefonts were introduced into XNA. This allows us to strip out the graphic of a specific card and draw it onscreen at some other location.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C# 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DrawCard(Card card, Vector2 position)
{
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (card != Card.Undefined &amp;amp;&amp;amp; card.Suit != Suit.Unassigned)
        ScreenManager.SpriteBatch.Draw(_texDeck, position, GetCardSourceRect(card), Color.White);
}

&lt;span class="kwrd"&gt;private&lt;/span&gt; Rectangle GetCardSourceRect(Card card)
{
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (card.Suit == Suit.Unassigned)
        &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentException(&lt;span class="str"&gt;&amp;quot;Unassigned cards cannot be drawn.&amp;quot;&lt;/span&gt;);

    &lt;span class="kwrd"&gt;int&lt;/span&gt; cardColumn = card.Value - 1;
    &lt;span class="kwrd"&gt;int&lt;/span&gt; cardRow = (&lt;span class="kwrd"&gt;int&lt;/span&gt;)card.Suit - 1;

    &lt;span class="kwrd"&gt;int&lt;/span&gt; x = cardColumn * MainScreenElements.CARD_WIDTH;
    &lt;span class="kwrd"&gt;int&lt;/span&gt; y = cardRow * MainScreenElements.CARD_HEIGHT;

    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Rectangle(x, y, MainScreenElements.CARD_WIDTH, MainScreenElements.CARD_HEIGHT);
}&lt;/pre&gt;

&lt;h3&gt;Managing Network Data&lt;/h3&gt;

&lt;p&gt;One thing that increases the complexity of this game (in no small way) is the management of network data. First of all, the host’s actions are a superset of the peer’s actions (a host is also a peer, but it also has to manage the game and network state). For example, when a peer decides to bet, it has to send a message to the host saying “I would like to bet,” and then the host will process that message and relay it to the other peers. The host is also responsible for managing whose turn it is and determining who the winner is. Theoretically, you could do a lot of this with every peer acting equally but it feels safer to me to have the host responsible for important activities like dealing cards. In fact, the host has to be the only one who can deal cards, because if each client maintained its own deck, it would be randomized for every peer when the deck is shuffled. &lt;/p&gt;

&lt;h4&gt;How Data Is Sent And Received&lt;/h4&gt;

&lt;p&gt;I will usually create a static class called NetworkMessageSender that is responsible for sending various messages. I keep an enumeration of type byte that holds the possible network messages. &lt;/p&gt;

&lt;p&gt;I always send the byte indicating the message type first, so that when the peer receives a byte, it knows how to respond. For example, if the peer receives a CardDealt message, it can pop off a string and a card from the incoming packets. If the card is not intended for the peer, it can simply discard the message. &lt;/p&gt;

&lt;p&gt;Data you want to send is written to a packet writer object. When the data is ready to be sent, you call the SendData method of the LocalGamer object. Depending on how important packet receipt is, you can specify the type of transmission. I use ReliableInOrder during poker because network data is exchanged relatively infrequently. Although there is only one local network gamer, you can use a foreach loop to ensure this code will work on other platforms.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C# 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SendData()
{
    &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (LocalNetworkGamer gamer &lt;span class="kwrd"&gt;in&lt;/span&gt; NetworkSessionManager.Session.LocalGamers)
        gamer.SendData(NetworkSessionManager.PacketWriter, SendDataOptions.ReliableInOrder);
}&lt;/pre&gt;

&lt;p&gt;Specific chunks of data are then sent using various static methods. Each piece of information is written sequentially. The card is serialized into a string format before being sent. The following chunk of code is used to send a card to a player.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C# 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DealCards(HoldemPlayer player)
{
    NetworkSessionManager.PacketWriter.Write((&lt;span class="kwrd"&gt;byte&lt;/span&gt;)NetworkMessageType.Deal);
    NetworkSessionManager.PacketWriter.Write(player.Name);
    NetworkSessionManager.PacketWriter.Write(player.Pocket.Card1.Serialize());
    NetworkSessionManager.PacketWriter.Write(player.Pocket.Card2.Serialize());
    SendData();
}&lt;/pre&gt;

&lt;p&gt;On the other side of that, whenever the screen updates, the game is constantly checking for new network data. The following code is part of a method that is called from the game screen’s Update loop.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;C# 
    &lt;br /&gt;&lt;/b&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; UpdateNetworkSession()
{
    NetworkSessionManager.Update();

    &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (LocalNetworkGamer localGamer &lt;span class="kwrd"&gt;in&lt;/span&gt; 
        NetworkSessionManager.Session.LocalGamers)
    {
        &lt;span class="kwrd"&gt;while&lt;/span&gt; (localGamer.IsDataAvailable)
        {
            NetworkGamer sender;
            localGamer.ReceiveData(NetworkSessionManager.PacketReader, 
                &lt;span class="kwrd"&gt;out&lt;/span&gt; sender);

            &lt;span class="rem"&gt;// Interpret the first piece of information, the message.&lt;/span&gt;
            NetworkMessageType message = (NetworkMessageType)NetworkSessionManager.PacketReader.ReadByte();

            &lt;span class="rem"&gt;// Determine what to read and what to do based on this message type&lt;/span&gt;
            &lt;span class="kwrd"&gt;switch&lt;/span&gt; (message)
            {
                &lt;span class="rem"&gt;// major snippage&lt;/span&gt;
                &lt;span class="kwrd"&gt;case&lt;/span&gt; NetworkMessageType.Deal: &lt;span class="rem"&gt;// Happens when a card is dealt&lt;/span&gt;
                {
                    &lt;span class="kwrd"&gt;string&lt;/span&gt; playerName = NetworkSessionManager.PacketReader.ReadString();
                    &lt;span class="kwrd"&gt;string&lt;/span&gt; card1 = NetworkSessionManager.PacketReader.ReadString();
                    &lt;span class="kwrd"&gt;string&lt;/span&gt; card2 = NetworkSessionManager.PacketReader.ReadString();

                 _gameplayManager.Players[playerName].Pocket.Set(PlayerPocket.FIRST_CARD_INDEX, &lt;span class="kwrd"&gt;new&lt;/span&gt; 
                    Card(card1));
                 _gameplayManager.Players[playerName].Pocket.Set(PlayerPocket.SECOND_CARD_INDEX, &lt;span class="kwrd"&gt;new&lt;/span&gt; 
                    Card(card2));
                }
                &lt;span class="kwrd"&gt;break&lt;/span&gt;;
            }
        }
    }
}&lt;/pre&gt;

&lt;p&gt;There is a whole bunch of similar code that underlies how the game executes. When a message needs to be sent, the NetworkMessageSender class is used. To determine what happens when a message is received, we look at that ginormous switch statement that gets called in the screen update loop. &lt;/p&gt;

&lt;p&gt;Remember that the host is also a peer and it will receive the very same messages it sends (unless you specify otherwise). Be careful not to double-process messages if they are sent by the host. In some cases you will need to check that the current device is or is not the host. In the sample download, this is achieved just by checking a Boolean value that is set early on in the game.&lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Building Poker for the Zune, from the ground up, is no small feat! Networked Zune games are far simpler when you have a much more limited set of possible data and messages that could be sent. For example, networked Pong, Battleship or Tetris would be pretty easy compared to poker. Turn-based games on the Zune also provide an interesting challenge in terms of what happens when a Zune drops from the network session.&lt;/p&gt;

&lt;p&gt;This is just a small dip into networked Zune game development. For a deep dive, check out my book, &lt;a href="http://www.tinyurl.com/zunebook"&gt;Zune Game Development using XNA 3.0&lt;/a&gt;, also available on &lt;a href="http://www.apress.com/book/view/9781430218616"&gt;Apress.com as an eBook&lt;/a&gt;. The final chapter is a sprawling 120 pages covering how to build Crazy Eights for the Zune from the ground up.&lt;/p&gt;

&lt;h3&gt;About The Author&lt;/h3&gt;

&lt;p&gt;Dan Waters is an Academic Developer Evangelist at Microsoft, based in Tampa, FL. When he’s not out showing the latest and greatest MS technology to students and faculty, he’s spending time with his wife and young daughter or rocking out on one of his (far too numerous) guitars. Follow Dan on &lt;a href="http://www.twitter.com/danwaters"&gt;Twitter&lt;/a&gt; or check out his &lt;a href="http://www.danwaters.com"&gt;blog&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9582637" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/puzzle/default.aspx">puzzle</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/hardware/default.aspx">hardware</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/card+and+board+games/default.aspx">card and board games</category></item><item><title>Coding Unreal Tournament3 Bots</title><link>http://blogs.msdn.com/coding4fun/archive/2008/12/16/9228221.aspx</link><pubDate>Tue, 16 Dec 2008 23:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9228221</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/9228221.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=9228221</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=9228221</wfw:comment><description>&lt;p&gt;Have you ever played a first person shooter? Then you've probably played against computer controlled players known as &amp;quot;bots&amp;quot; which are computer programs designed with artificial intelligence. Up until now the only way to do create your own bot for a game like Unreal Tournament 3 was to learn a bunch of C++ and complicated AI routines. Of course, what you really want to do is code up your bot using C# or VB and Visual Studio Express. So here is a way to do just that.&lt;/p&gt;  &lt;h2&gt;Your First Bot&lt;/h2&gt;  &lt;p&gt;Once you've created a new bot you'll have a very basic class with just a constructor and an overridden &lt;b&gt;ProcessActions&lt;/b&gt;() method.&lt;/p&gt;  &lt;h5&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/h5&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Linq;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Text;

&lt;span class="kwrd"&gt;using&lt;/span&gt; UT3Bots;
&lt;span class="kwrd"&gt;using&lt;/span&gt; UT3Bots.UTItems;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Threading;
&lt;span class="kwrd"&gt;using&lt;/span&gt; UT3Bots.Communications;


&lt;span class="kwrd"&gt;namespace&lt;/span&gt; UT3UserBot
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyBot : UTBot
    {

        &lt;span class="rem"&gt;//Constructor for your bot&lt;/span&gt;
        &lt;span class="kwrd"&gt;public&lt;/span&gt; MyBot() : &lt;span class="kwrd"&gt;base&lt;/span&gt; (&lt;span class="str"&gt;&amp;quot;174.133.254.34&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;Sitting Duck&amp;quot;&lt;/span&gt;, BotMesh.Harbinger, BotColor.Red)
        {
        }


        &lt;span class="rem"&gt;//Method to control the bot once it has connected to the server&lt;/span&gt;
        &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ProcessActions()
        {
            &lt;span class="rem"&gt;//Add code here to perform some actions at specific intevals&lt;/span&gt;
        }
    }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;h5&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/h5&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Imports&lt;/span&gt; System
&lt;span class="kwrd"&gt;Imports&lt;/span&gt; System.Collections.Generic
&lt;span class="kwrd"&gt;Imports&lt;/span&gt; System.Linq
&lt;span class="kwrd"&gt;Imports&lt;/span&gt; System.Text

&lt;span class="kwrd"&gt;Imports&lt;/span&gt; UT3Bots
&lt;span class="kwrd"&gt;Imports&lt;/span&gt; UT3Bots.UTItems
&lt;span class="kwrd"&gt;Imports&lt;/span&gt; System.Threading
&lt;span class="kwrd"&gt;Imports&lt;/span&gt; UT3Bots.Communications


&lt;span class="kwrd"&gt;Namespace&lt;/span&gt; UT3UserBot
    &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt; MyBot
        &lt;span class="kwrd"&gt;Inherits&lt;/span&gt; UTBot

        &lt;span class="rem"&gt;'Constructor for your bot&lt;/span&gt;
        &lt;span class="kwrd"&gt;Public&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; &lt;span class="kwrd"&gt;New&lt;/span&gt;()
            &lt;span class="kwrd"&gt;MyBase&lt;/span&gt;.&lt;span class="kwrd"&gt;New&lt;/span&gt;(&lt;span class="str"&gt;&amp;quot;174.133.254.34&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;Sitting Duck&amp;quot;&lt;/span&gt;, BotMesh.Harbinger, BotColor.Red)
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;


        &lt;span class="rem"&gt;'Method to control the bot once it has connected to the server&lt;/span&gt;
        &lt;span class="kwrd"&gt;Protected&lt;/span&gt; &lt;span class="kwrd"&gt;Overloads&lt;/span&gt; &lt;span class="kwrd"&gt;Overrides&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; ProcessActions()
            &lt;span class="rem"&gt;'Add code here to perform some actions at specific intevals&lt;/span&gt;
        &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt;
    &lt;span class="kwrd"&gt;End&lt;/span&gt; &lt;span class="kwrd"&gt;Class&lt;/span&gt;
&lt;span class="kwrd"&gt;End&lt;/span&gt; Namespace&lt;/pre&gt;

&lt;h2&gt;Setting Up Your Bot&lt;/h2&gt;

&lt;p&gt;The first thing you need to do for your bot is configure it with a look and feel, and then actually connect to the server and get in the game. This is pretty straightforward, all you need to do is alter the parameters that are getting sent up to the base bot class in the constructor.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Server IPAddress&lt;/strong&gt; - The first parameter is the IPAddress of the server to connect to. By default this points to the UT3Bots server we have running on the internet (ut.utbots.com), you'll need to alter this to point to the server you need to connect to.&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Name&lt;/strong&gt; – The name of your bot will be seen on the Visualizer and in game.&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Bot Mesh&lt;/strong&gt; – This is the appearance of your bot on the server.&lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Bot Color&lt;/strong&gt; – This is the color of your bot.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now you are ready to join a UT3 game.&lt;/p&gt;

&lt;p&gt;Pressing F5 will build and run your bot. The bot project will load up a little console application which provides info about what your bot is doing.&lt;/p&gt;

&lt;h2&gt;Bot's First Sight&lt;/h2&gt;

&lt;p&gt;Now that we have a bot in the game we need to make it actually do something. The first thing we are going to make it do is move to a UTNavPoint. This is a location in the game which might hold something interesting. A series of UTNavPoints provide a path through the level to interesting items. A bot can travel around a game level by moving from one UTNavPoint to another. &lt;/p&gt;

&lt;p&gt;Within the game framework (UT3RemoteBot.dll), an instance of the UTVector class can be used to express the actual location of something. The location of a UTNavPoint is given by its Location property, which is given as a UTVector value. &lt;/p&gt;

&lt;p&gt;Our bot needs to have a way of keeping track of where it is going so in the UT3UserBot class create a new private UTVector called destinationLocation&lt;/p&gt;

&lt;h5&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/h5&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; UTVector destinationLocation;&lt;/pre&gt;

&lt;h5&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/h5&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Private&lt;/span&gt; destinationLocation &lt;span class="kwrd"&gt;As&lt;/span&gt; UTVector&lt;/pre&gt;

&lt;p&gt;We want our bot to head towards the first navpoint that it “sees”. To do this we place the following code into the &lt;b&gt;ProcessActions&lt;/b&gt;() method. &lt;/p&gt;

&lt;h5&gt;&lt;strong&gt;C#&lt;/strong&gt; &lt;/h5&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;this&lt;/span&gt;.destinationLocation = &lt;span class="kwrd"&gt;this&lt;/span&gt;.GameState.NavPointsVisible[0].Location;&lt;/pre&gt;

&lt;h5&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/h5&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Me&lt;/span&gt;.destinationLocation = &lt;span class="kwrd"&gt;Me&lt;/span&gt;.GameState.NavPointsVisible(0).Location&lt;/pre&gt;

&lt;p&gt;The&lt;b&gt; GameState &lt;/b&gt;property of our bot contains information on the current state of the game. &lt;b&gt;NavPointsVisible&lt;/b&gt; is a list of references to UTNavPoint instances which refer to all of the UTNavPoints a bot can currently see. The above code simply takes the UTVector at position 0 (the first navigation point your bot can see) and sets destinationLocation to refer to it. &lt;/p&gt;

&lt;p&gt;So at this point the bot has identified the location it wants to move towards, now we have to make it move in that direction. &lt;/p&gt;

&lt;h2&gt;Bot's First Steps&lt;b&gt; &lt;/b&gt;&lt;/h2&gt;

&lt;p&gt;Now we are going to make our bot move to the destination it has selected. Place the following code into the ProcessActions() method just below the code written in the previous example.&lt;/p&gt;

&lt;h5&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/h5&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;this&lt;/span&gt;.Commands.RunTo(&lt;span class="kwrd"&gt;this&lt;/span&gt;.destinationLocation);&lt;/pre&gt;

&lt;h5&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/h5&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Me&lt;/span&gt;.Commands.RunTo(&lt;span class="kwrd"&gt;Me&lt;/span&gt;.destinationLocation)&lt;/pre&gt;

&lt;p&gt;The&lt;b&gt; Commands&lt;/b&gt; property provides a set of methods which are the commands that your program can give to the bot. There are a wide range of commands, some of which are listed below. For full details you should investigate the API reference. &lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;b&gt;Jump()&lt;/b&gt; - Jumps in the air &lt;/li&gt;

  &lt;li&gt;&lt;b&gt;RotateBy()&lt;/b&gt; - Rotates by a specific number of degrees&lt;/li&gt;

  &lt;li&gt;&lt;b&gt;RunTo()&lt;/b&gt; - Runs to a specific location &lt;/li&gt;

  &lt;li&gt;&lt;b&gt;StartFiring()&lt;/b&gt; - Attacks a target or location &lt;/li&gt;

  &lt;li&gt;&lt;b&gt;StopFiring()&lt;/b&gt; - Stops the bot from shooting &lt;/li&gt;

  &lt;li&gt;&lt;b&gt;StrafeTo()&lt;/b&gt; - Strafes to a specific location while looking at another&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;b&gt;RunTo()&lt;/b&gt; command is provided with a destination location. When this method is called your bot will start running towards the destination. &lt;/p&gt;

&lt;p&gt;If you execute this program you will see your bot start running towards a navigation point.&lt;/p&gt;

&lt;h2&gt;Bot's First Shot&lt;/h2&gt;

&lt;p&gt;For the purpose of this example we will next make our bot shoot at the location it is running towards, it is recommended that you change this when creating your own implementation as shooting at an empty UTNavPoint is unlikely to get you many frags. &lt;/p&gt;

&lt;p&gt;Place the following code into the &lt;b&gt;ProcessActions&lt;/b&gt;()&lt;b&gt; &lt;/b&gt;method just below the code written in the previous example. &lt;/p&gt;

&lt;h5&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/h5&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;this&lt;/span&gt;.Commands.StartFiring(&lt;span class="kwrd"&gt;this&lt;/span&gt;.destinationLocation, &lt;span class="kwrd"&gt;false&lt;/span&gt;);&lt;/pre&gt;

&lt;h5&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/h5&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Me&lt;/span&gt;.Commands.StartFiring(&lt;span class="kwrd"&gt;Me&lt;/span&gt;.destinationLocation, &lt;span class="kwrd"&gt;false&lt;/span&gt;)&lt;/pre&gt;

&lt;p&gt;If you execute the program you will see that your bot now shoots at the location as it runs towards it. It will continue running and shooting until you call a method to instruct it to stop, or it runs out of ammo! &lt;/p&gt;

&lt;p&gt;Place the following code into the &lt;b&gt;ProcessActions&lt;/b&gt;()&lt;b&gt; &lt;/b&gt;method just below the code written in the previous step. &lt;/p&gt;

&lt;h5&gt;&lt;strong&gt;C#&lt;/strong&gt; &lt;/h5&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;this&lt;/span&gt;.Commands.StopFiring();&lt;/pre&gt;

&lt;h5&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/h5&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Me&lt;/span&gt;.Commands.StopFiring()&lt;/pre&gt;

&lt;p&gt;Your bot will now stop shooting.&lt;/p&gt;

&lt;p&gt;So you now know how to get your bot to do something in the game. Running around and shooting things is as easy as using the &lt;b&gt;GameState&lt;/b&gt; property to access what your bot can see, and the &lt;b&gt;Commands&lt;/b&gt; property to call methods that perform actions.&lt;/p&gt;

&lt;h3&gt;Bot's First Reaction&lt;/h3&gt;

&lt;p&gt;Now we are going to make the bot react to things that happen in the game. This is done by subscribing to any of the events that the bot exposes through its &lt;b&gt;Events&lt;/b&gt; property. We normally want to subscribe to the game events as soon as the bot starts, so that we get all the juicy information that we can use to make a clever bot. You can do this by attaching an event handler to an event in the constructor for your bot.&lt;/p&gt;

&lt;p&gt;In this example we will subscribe to the OnSpawned event which is trigged whenever the bot spawns in the game, whether this is the first time, or after it has died.&lt;/p&gt;

&lt;p&gt;Place the following code into the &lt;b&gt;constructor&lt;/b&gt; of the &lt;b&gt;MyBot&lt;/b&gt; class.&lt;/p&gt;

&lt;h5&gt;&lt;strong&gt;C# &lt;/strong&gt;&lt;/h5&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;this&lt;/span&gt;.Events.OnSpawned += &lt;span class="kwrd"&gt;new&lt;/span&gt; EventHandler&amp;lt;BotSpawnedEventArgs&amp;gt;(Events_OnSpawned);&lt;/pre&gt;

&lt;h5&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/h5&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;AddHandler&lt;/span&gt; &lt;span class="kwrd"&gt;Me&lt;/span&gt;.Events.OnSpawned, &lt;span class="kwrd"&gt;AddressOf&lt;/span&gt; Events_OnSpawned&lt;/pre&gt;

&lt;p&gt;You also need to add a new method to the class that will be called when the bot spawns.&lt;/p&gt;

&lt;p&gt;Place the following method code into the &lt;b&gt;MyBot&lt;/b&gt; class.&lt;/p&gt;

&lt;h5&gt;&lt;strong&gt;C#&lt;/strong&gt; &lt;/h5&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;void&lt;/span&gt; Events_OnSpawned(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, BotSpawnedEventArgs e)
{
    &lt;span class="kwrd"&gt;this&lt;/span&gt;.Commands.PerformEmote(Emote.PelvicThrust);
} &lt;/pre&gt;

&lt;h5&gt;&lt;strong&gt;VB&lt;/strong&gt;&lt;/h5&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;Private&lt;/span&gt; &lt;span class="kwrd"&gt;Sub&lt;/span&gt; Events_OnSpawned(&lt;span class="kwrd"&gt;ByVal&lt;/span&gt; sender &lt;span class="kwrd"&gt;As&lt;/span&gt; &lt;span class="kwrd"&gt;Object&lt;/span&gt;, &lt;span class="kwrd"&gt;ByVal&lt;/span&gt; e &lt;span class="kwrd"&gt;As&lt;/span&gt; BotSpawnedEventArgs)
    &lt;span class="kwrd"&gt;Me&lt;/span&gt;.Commands.PerformEmote(Emote.PelvicThrust)
&lt;span class="kwrd"&gt;End&lt;/span&gt; Sub&lt;/pre&gt;

&lt;p&gt;Your bot will now start performing some fetching pelvic thrusts whenever it spawns in the game. This won't actually help you get any extra frags, but maybe it'll start a new craze!&lt;/p&gt;

&lt;p&gt;There are many different events you can attach handlers to which will allow you to change your bot behavior to react to different circumstances. For full details you should investigate the &lt;a href="http://www.net.dcs.hull.ac.uk/utbot/APIDoc/index.html"&gt;API reference&lt;/a&gt; or check out Visual Studio's intellisense.&lt;/p&gt;

&lt;h2&gt;Viewing Your Bot In Action&lt;/h2&gt;

&lt;p&gt;You can easily find out what your bot is up to by using the special Silverlight 2 visualizer we have created. When you open the visualizer web page in your browser, you can automatically see the status of the server running the Bot game type and get a bird's eye view of the game in progress. You can see the waypoints which are marked by green dots and the active players which have their name next to moving circles with a line indicating which way they are facing. From here you can view the game and see what your bot is doing as shown in the visualizer screenshot.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.coding4fun.net/images/CodingUnrealTournament3Bots_CC49/clip_image002.jpg"&gt;&lt;img title="clip_image002" style="display: inline" height="174" alt="clip_image002" src="http://www.coding4fun.net/images/CodingUnrealTournament3Bots_CC49/clip_image002_thumb.jpg" width="269" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;What To Do Next&lt;/h2&gt;

&lt;p&gt;Now that you have got your bot into the game and made it move around a bit you can start thinking about making it into a decent player of the game. Take a look at the other members of the &lt;b&gt;GameMap&lt;/b&gt; class so that your bot can look for weapons and health packs and move towards them. Then take a look at the members of the &lt;b&gt;GameState&lt;/b&gt; class for the information that your bot is given about the game around it. &lt;/p&gt;

&lt;p&gt;One thing you will need to add is some way that your bot can be in a particular state at any given time. Sensible states might be &amp;quot;Roaming&amp;quot;, &amp;quot;Hunting&amp;quot;, &amp;quot;Idle&amp;quot;, &amp;quot;Recovering&amp;quot;, etc. Depending on the state your bot is in it will do different things each time &lt;b&gt;ProcessActions&lt;/b&gt; is called. &lt;/p&gt;

&lt;p&gt;If it is in the roaming state, for example, it will be looking for a navigation point. If it is in the hunting state it will be chasing other bots. If it is in the recovering state it will be avoiding other bots and looking for a health pack, and so on. You can keep track of the state of your bot by creating an enumerated type with the different values. Then your &lt;b&gt;ProcessActions&lt;/b&gt; method can contain a switch statement which makes it behave differently, depending on what it is doing. &lt;/p&gt;

&lt;p&gt;A state machine is just one way you could program your AI, you could make a sophisticated bot that had a way of determining if a result had a positive or negative consequence and then have the bot perform more actions that had a positive consequence. Essentially a learning bot, be careful though if your bot takes too long executing &lt;b&gt;ProcessActions&lt;/b&gt; it will get out of touch with the game as messages will sit on the queue unprocessed waiting for your code to finish. &lt;/p&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;With this quick getting started guide you should be able to go about creating your own UT3 Deathmatch player. There are also some very handy hints in the FAQ below about getting your bot on the road to stardom.&lt;/p&gt;

&lt;p&gt;If you’re feeling particularly brave join our project on CodePlex at &lt;a href="http://www.codeplex.com/UT3Bots"&gt;http://www.codeplex.com/UT3Bots&lt;/a&gt; and help pitch in with improving the bot client, the visualizer, or even the server mutator.&lt;/p&gt;

&lt;p&gt;Happy Fraggin'!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9228221" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/arcade/default.aspx">arcade</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category></item><item><title>Sammy The Snake: An XNA game for the Zune</title><link>http://blogs.msdn.com/coding4fun/archive/2008/10/06/8976852.aspx</link><pubDate>Mon, 06 Oct 2008 21:06:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8976852</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>10</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/8976852.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=8976852</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=8976852</wfw:comment><description>&lt;p&gt;&lt;strong&gt;Nick Gravelyn&lt;/strong&gt; of &lt;a href="http://www.nickontech.com"&gt;NickOnTech.com&lt;/a&gt;     &lt;br /&gt;&lt;b&gt;     &lt;br /&gt;&lt;b&gt;Download: &lt;/b&gt;&lt;a href="http://codeplex.com/sammythesnake"&gt;http://codeplex.com/sammythesnake&lt;/a&gt;       &lt;br /&gt;&lt;/b&gt;&lt;b&gt;Software: &lt;/b&gt;&lt;a href="http://msdn.com/express/"&gt;Visual C# Express Editions&lt;/a&gt;, &lt;a href="http://creators.xna.com/en-US/downloads"&gt;XNA Game Studios 3.0&lt;/a&gt;&lt;b&gt;      &lt;br /&gt;Difficulty: &lt;/b&gt;Intermediate     &lt;br /&gt;&lt;b&gt;Time Required:&lt;/b&gt; 8 hours     &lt;br /&gt;&lt;b&gt;Cost: &lt;/b&gt;Free&lt;/p&gt;  &lt;p&gt;Artwork courtesy of George Clingerman of &lt;a href="http://www.xnadevelopment.com"&gt;XNADevelopment.com&lt;/a&gt;     &lt;br /&gt;&lt;strong&gt;Side note: &lt;/strong&gt;XNA is c# only&lt;/p&gt;  &lt;p&gt;In the exciting world of XNA Game Studio, the new big thing is the XNA Game Studio 3.0 Beta. This beta allows developers to glimpse what will be coming with the full release of XNA Game Studio 3.0. One of the largest new features coming with XNA Game Studio 3.0 is the new ability to create games for Microsoft’s Zune media device. What we’re going to cover today is how to create a full game for the Zune, specifically a clone of the classic “Snake” game.&lt;/p&gt;  &lt;p&gt;To begin you need to make sure you are set up to start working on Zune games. For all the details and instructions you will need for this step, please visit &lt;a href="http://creators.xna.com/en-us/3.0beta_mainpage"&gt;http://creators.xna.com/en-us/3.0beta_mainpage&lt;/a&gt;. Once you have read and completed steps one and two, continue on with this tutorial.&lt;/p&gt;  &lt;p&gt;Welcome back! Now that you’re all set up with XNA Game Studio 3.0 Beta, we can start working on our game. Since we want to test our game as we go forward, we’re actually going to create the game as a Windows game first and then simply convert it to be a Zune project when we are finished. First open up Visual Studio 2008 or Visual C# 2008 Express and create a new Windows project. Let’s call it “SammyTheSnake”. Choose the location to save the project and hit Ok.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.coding4fun.net/images/43268cd15815_1369/image8.png"&gt;&lt;img style="display: inline; margin-left: 0px; margin-right: 0px" height="236" alt="image" src="http://www.coding4fun.net/images/43268cd15815_1369/image8_thumb.png" width="269" align="right" border="0" /&gt;&lt;/a&gt;Now that we have the project created let’s start out first by adding in the content for our game. George Clingerman of &lt;a href="http://www.xnadevelopment.com"&gt;XNADevelopment.com&lt;/a&gt; was nice enough to create some pretty graphics for this tutorial so those are the ones we are going to add to the project. In Visual Studio you will have a panel called the &lt;i&gt;Solution Explorer&lt;/i&gt; which will show you all of the files in your project. In that window you want to find the &lt;i&gt;Content&lt;/i&gt; node of your project. Right click on the &lt;i&gt;Content&lt;/i&gt; node and choose Add-&amp;gt;New Folder. Name this new folder “Fonts”. Then add a second folder called “Sprites”. At this point your &lt;i&gt;Solution Explorer&lt;/i&gt; should look like what is to your right:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.coding4fun.net/images/43268cd15815_1369/image11.png"&gt;&lt;img style="display: inline; margin-left: 0px; margin-right: 0px" height="276" alt="image" src="http://www.coding4fun.net/images/43268cd15815_1369/image11_thumb.png" width="212" align="right" border="0" /&gt;&lt;/a&gt;Let’s continue by adding our content files. The easiest way to add the sprites to our project is to simply drag and drop them in from the folder. Another method is to right click on the “Sprites” folder, select Add-&amp;gt;Existing Item. Then you can navigate to the directory where the graphics are, select them, and click Ok. When completed you will see all five images are copied into the Sprites directory:&lt;/p&gt;  &lt;p&gt;Next we’ll create the three fonts that we will be using for the game. To create a font, right click on the “Fonts” folder and select Add-&amp;gt;New Item. In the pop up window you will see an item called a “Sprite Font”. Select that item, name your file “MiniFont.spritefont”, and then press Ok. When created it will open up in Visual Studio. The .spritefont file is simply an XML document describing which font you want to use. For our purposes there are only two nodes we need to change. First find the node that looks like this:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;FontName&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;MiniFont&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;FontName&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;




.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;The &lt;i&gt;FontName&lt;/i&gt; node specifies which font we wish to use. Let’s change our font to Arial:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;FontName&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;Arial&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;FontName&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Next we need to change the size of the font. Let’s find the Size node which, by default, is set to 14:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Size&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;14&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Size&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;And let’s change our font to size 10:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Size&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;10&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Size&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Now that you know how to create fonts, create two more fonts: “MediumFont.spritefont” and “TitleFont.spritefont”. Both fonts should use the Arial font. MediumFont should be size 14 (the default value) and TitleFont should be size 18.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.coding4fun.net/images/43268cd15815_1369/image.png"&gt;&lt;img style="display: inline; margin-left: 0px; margin-right: 0px" height="406" alt="image" src="http://www.coding4fun.net/images/43268cd15815_1369/image_thumb.png" width="271" align="right" border="0" /&gt;&lt;/a&gt;Now you have all the content our game needs. Let’s take a look at the &lt;i&gt;Solution Explorer&lt;/i&gt; one last time to make sure we have all the files in the correct locations:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.coding4fun.net/images/43268cd15815_1369/image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that all of the content is in place, we can begin working on the game. Let’s start writing that code!&lt;/p&gt;

&lt;p&gt;To begin we want to make sure our Windows game looks just like the Zune version will. Since the Zune screen has a resolution of 240 pixels by 320 pixels, we want to make our Windows game run in a window set at 240x320. To do this we just need to add two lines of code to our Game1 class’s constructor:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; Game1()
{
    graphics = &lt;span class="kwrd"&gt;new&lt;/span&gt; GraphicsDeviceManager(&lt;span class="kwrd"&gt;this&lt;/span&gt;);
    Content.RootDirectory = &lt;span class="str"&gt;&amp;quot;Content&amp;quot;&lt;/span&gt;;

    graphics.PreferredBackBufferWidth = 240;
    graphics.PreferredBackBufferHeight = 320;
}&lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.coding4fun.net/images/43268cd15815_1369/image_3.png"&gt;&lt;img title="image" style="display: inline; margin-left: 0px; margin-right: 0px" height="234" alt="image" src="http://www.coding4fun.net/images/43268cd15815_1369/image_thumb_3.png" width="347" align="right" border="0" /&gt;&lt;/a&gt;The next thing to consider is our game’s flow. Since we are making a simple game, we are only ever going to have three states in which the game can be in as illustrated by this diagram:&lt;/p&gt;

&lt;p&gt;From the diagram we see that our game will have a title screen, some game play, and a game over screen. The arrows represent the changes in game state. From the title screen we can only progress to the in-game screen. From the in-game screen we can either quit, going back to the title screen, or we can finish the game and move to the game over screen. From the game over screen we then return to the title screen and the process repeats until the user quits.&lt;/p&gt;

&lt;p&gt;Now that we have an idea of how our game will work we will start by creating the framework that will manage our game’s state and make sure that we are executing the appropriate code based on the state of the game. First we’ll create a new enumeration type to represent the three states of our game. In the Game1.cs file right above the declaration for the Game class, add this new enumeration:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;enum&lt;/span&gt; GameState
{
    Title,
    InGame,
    GameOver
}&lt;/pre&gt;

&lt;p&gt;Now in the actual Game1 class, we need to add a variable to hold the game’s state:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; GameState state = GameState.Title;&lt;/pre&gt;

&lt;p&gt;Now we need to create some new methods which will be used to handle updating and drawing our various states. First let’s add three new methods to our Game1 class:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; UpdateTitleScreen()
{ }

&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; UpdateInGame(GameTime gameTime)
{ }

&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; UpdateGameOver()
{ }&lt;/pre&gt;

&lt;p&gt;From the names you can probably see that we have one for each of our three game states. Our UpdateInGame method takes in the GameTime as a parameter because it is used for moving our snake around. The other two game states don’t need that value, so they have no parameters. Now let’s make some changes to the Update method to handle calling each of these methods based on the state of our game:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Update(GameTime gameTime)
{
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
        &lt;span class="kwrd"&gt;this&lt;/span&gt;.Exit();

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (state == GameState.Title)
        UpdateTitleScreen();
    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (state == GameState.InGame)
        UpdateInGame(gameTime);
    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (state == GameState.GameOver)
        UpdateGameOver();

    &lt;span class="kwrd"&gt;base&lt;/span&gt;.Update(gameTime);
}&lt;/pre&gt;

&lt;p&gt;Now we’ll repeat those steps to make three methods for rendering each state and updating the Draw method to call each of them as appropriate. While we’re at it, we’re also going to change the background from the default of CornflowerBlue to Gray to make it a little easier on the eyes with the white text we’re going to add later:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DrawTitleScreen()
{ }

&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DrawInGame()
{ }

&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DrawGameOver()
{ }

&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Draw(GameTime gameTime)
{
    graphics.GraphicsDevice.Clear(Color.Gray);

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (state == GameState.Title)
        DrawTitleScreen();
    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (state == GameState.InGame)
        DrawInGame();
    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (state == GameState.GameOver)
        DrawGameOver();

    &lt;span class="kwrd"&gt;base&lt;/span&gt;.Draw(gameTime);
}&lt;/pre&gt;

&lt;p&gt;With that our game now has a fully functioning state system. Of course our game only ever has one state since we don’t currently change it, and none of the game states do anything, but the functionality is there. Let’s move on to create the code that will power the title screen.&lt;/p&gt;

&lt;p&gt;First we need to add a few strings to our game which will be used on the title screen. Towards the top of your Game1 class, add these string constants:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; gameTitle = &lt;span class="str"&gt;&amp;quot;Sammy the Snake!&amp;quot;&lt;/span&gt;;
&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; playInstructions = &lt;span class="str"&gt;&amp;quot;Press Play to Begin&amp;quot;&lt;/span&gt;;
&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; quitInsructions = &lt;span class="str"&gt;&amp;quot;Press Back to Quit&amp;quot;&lt;/span&gt;;&lt;/pre&gt;

&lt;p&gt;Now we need to add a few variables to the Game1 class to hold our fonts which we’ll be using to render these strings:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; SpriteFont titleFont;
&lt;span class="kwrd"&gt;private&lt;/span&gt; SpriteFont mediumFont;
&lt;span class="kwrd"&gt;private&lt;/span&gt; SpriteFont miniFont;&lt;/pre&gt;

&lt;p&gt;Next we’ll go to our &lt;i&gt;LoadContent&lt;/i&gt; method and add in the code that will load those .spritefont files we created earlier and store them in these three variables:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LoadContent()
{
    spriteBatch = &lt;span class="kwrd"&gt;new&lt;/span&gt; SpriteBatch(GraphicsDevice);
    titleFont = Content.Load&amp;lt;SpriteFont&amp;gt;(&lt;span class="str"&gt;&amp;quot;Fonts/TitleFont&amp;quot;&lt;/span&gt;);
    mediumFont = Content.Load&amp;lt;SpriteFont&amp;gt;(&lt;span class="str"&gt;&amp;quot;Fonts/MediumFont&amp;quot;&lt;/span&gt;);
    miniFont = Content.Load&amp;lt;SpriteFont&amp;gt;(&lt;span class="str"&gt;&amp;quot;Fonts/MiniFont&amp;quot;&lt;/span&gt;);
}&lt;/pre&gt;

&lt;p&gt;After that we also need to add two more variables. The first will store the current frame’s input state. The second will store the previous frame’s input state. By storing these two values, we will be able to test for when a button gets pressed by verifying that the button was down the frame before and is pressed in the current frame.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; GamePadState gamePadState;
&lt;span class="kwrd"&gt;private&lt;/span&gt; GamePadState lastGamePadState;&lt;/pre&gt;

&lt;p&gt;Now let’s go ahead and make a method which we’ll use to test for new button presses for us. We’ll take a parameter for which button to test and then check for that button being down in &lt;i&gt;gamePadState&lt;/i&gt; but up in &lt;i&gt;lastGamePadState&lt;/i&gt;.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsNewButtonPress(Buttons button)
{
    &lt;span class="kwrd"&gt;return&lt;/span&gt; (gamePadState.IsButtonDown(button) &amp;amp;&amp;amp; lastGamePadState.IsButtonUp(button));
}&lt;/pre&gt;

&lt;p&gt;We’ll go ahead, now, and modify the game’s Update method to handle updating those GamePadState variables we created above. We get the current state right away and then, at the end of the Update method, we store the current state in our last state variable for next frame. We also want to remove the default code in there for exiting when the Back button is pressed because we will handle this in our UpdateTitleScreen method:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Update(GameTime gameTime)
{
    gamePadState = GamePad.GetState(PlayerIndex.One);

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (state == GameState.Title)
        UpdateTitleScreen();
    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (state == GameState.InGame)
        UpdateInGame(gameTime);
    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (state == GameState.GameOver)
        UpdateGameOver();

    lastGamePadState = gamePadState;

    &lt;span class="kwrd"&gt;base&lt;/span&gt;.Update(gameTime);
}&lt;/pre&gt;

&lt;p&gt;With that in place we can move on to filling in our UpdateTitleScreen method. For our simple game, we’re going to simply detect the Play/Pause button to start the game and the Back button to exit. These buttons map to the GamePadState’s B and Back buttons, respectively.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; UpdateTitleScreen()
{
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (IsNewButtonPress(Buttons.Back))
        Exit();

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (IsNewButtonPress(Buttons.B))
    { }
}&lt;/pre&gt;

&lt;p&gt;For now we’ll leave the inside of our second if statement empty because we have not implemented the in-game game state at this point. &lt;/p&gt;

&lt;p&gt;Let’s now move on to rendering our title screen. We know we are going to have to render three different strings to the screen, so what we’re going to do first is write a helper method that will help us write text centered on a point. This will make it easier for us so that we don’t have to try and calculate where to draw each string individually; we can simply use this one method everywhere we need to draw text.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DrawText(SpriteFont font, &lt;span class="kwrd"&gt;string&lt;/span&gt; text, Vector2 position)
{
    Vector2 halfSize = font.MeasureString(text) / 2f;
    position = position - halfSize;

    position.X = (&lt;span class="kwrd"&gt;int&lt;/span&gt;)position.X;
    position.Y = (&lt;span class="kwrd"&gt;int&lt;/span&gt;)position.Y;

    spriteBatch.Begin();
    spriteBatch.DrawString(
        font,
        text,
        position, 
        Color.White);
    spriteBatch.End();
}&lt;/pre&gt;

&lt;p&gt;This is our first complex code so we’re going to take a look at it line by line. The method itself takes in three parameters: the SpriteFont to use for rendering, the text we want to render, and the point at which we want the text centered.&lt;/p&gt;

&lt;p&gt;First we use the font’s MeasureString method to calculate how big the text will be (in pixels) if rendered with that font. We then divide this size by two and subtract it from the position. Why do we do all of this? In the XNA framework (and most 2D graphics APIs) positions of objects are specified using the top-left corner of the object. So by getting half of the size of the object and offsetting the position by it, we are able to set the center point and know that the text will be positioned properly. &lt;/p&gt;

&lt;p&gt;Once we have offset the position, we then cast each of the components to integers. We do this to avoid unnecessary blurring of our text. Since all text is rendered as whole pixels (because there’s no such thing as a fractional pixel), having a position that is not an integer value will cause the device to try and filter the text to get it to look right, or simply render it incorrectly. So when drawing in 2D, it’s always best to cast your positions to integers before using them to render.&lt;/p&gt;

&lt;p&gt;Lastly our method uses a simple SpriteBatch routine to draw the text at the desired position using the desired font using the White color.&lt;/p&gt;

&lt;p&gt;With that helper method out of the way, we can add three simple lines to the DrawTitleScreen method to draw our title screen:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DrawTitleScreen()
{
    DrawText(titleFont, gameTitle, &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector2(120f, 25f));
    DrawText(mediumFont, playInstructions, &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector2(120f, 200f));
    DrawText(mediumFont, quitInsructions, &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector2(120f, 225f));
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://www.coding4fun.net/images/43268cd15815_1369/image_4.png"&gt;&lt;img title="image" style="display: inline; margin-left: 0px; margin-right: 0px" height="349" alt="image" src="http://www.coding4fun.net/images/43268cd15815_1369/image_thumb_4.png" width="250" align="right" border="0" /&gt;&lt;/a&gt;Now if you run your game, you should see a window like this:&lt;/p&gt;

&lt;p&gt;If you have an Xbox 360 controller hooked to your computer, you could press the Back button to quit, but otherwise you can simply use the close button of the window to exit the game.&lt;/p&gt;

&lt;p&gt;From this point we’re going to start working on the gameplay. I am only going to be showing GamePad controls for this (since we are targeting the Zune), so if you don't have an Xbox 360 controller connected to your PC, you are either going to have to deploy the game to the Zune each time you want to test or implement keyboard controls. You can skip down to the end of this tutorial where we convert the project to be a Zune game or use other references for that process.&lt;/p&gt;

&lt;p&gt;Next we need to add in the game play code itself. Our game is rather simple. At any given time our game will have a single orange on screen, the snake, and a display of the number of oranges consumed. We’re going to start with the orange, then handle the snake, and finally the scoring system.&lt;/p&gt;

&lt;p&gt;To begin any of our rendering we need to get setup with a system for easily rendering our game. Since the whole game is grid based, we are going to make a class whose only purpose is to handle rendering a sprite at a given point using grid coordinates. We’ll start by creating a new static class called Grid:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Grid
{ }&lt;/pre&gt;

&lt;p&gt;Next we will define a few constants. The first sets the size of each grid cell. The second is one half of the grid cell size. We will be using a 16x16 pixel area for our grids. This number has been chosen because both 240 and 320 are even divisible by it, thus giving us a perfect grid for the resolution. We then have values to represent the largest row and column values in our grid:&lt;/p&gt;

&lt;p&gt;Next we’ll define a method called &lt;i&gt;PointToVector2&lt;/i&gt;. The whole purpose of this method will be to take a &lt;i&gt;Point&lt;/i&gt; value representing cell coordinates and converting them into the pixel coordinates needed to render a sprite. To do this we simply multiply the point’s coordinates by the scale and add in the half scale value. We add the half scale so that the resulting Vector2 is at the center of the cell. As I said before you always want to convert to integer based coordinates, but since the Point only contains integer values, and all our constants are integers, we don’t need to explicitly cast the values to ints: &lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; Vector2 PointToVector2(Point p)
{
    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector2(
        p.X * Scale + HalfScale,
        p.Y * Scale + HalfScale);
}&lt;/pre&gt;

&lt;p&gt;The only other method we’re going to add to our Grid class is the &lt;i&gt;DrawSprite&lt;/i&gt; method. &lt;i&gt;DrawSprite&lt;/i&gt; will handle rendering a given texture at a specific point with a given angle of rotation.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DrawSprite(
    SpriteBatch spriteBatch, 
    Texture2D texture, 
    Point point, 
    &lt;span class="kwrd"&gt;float&lt;/span&gt; rotation)
{
    &lt;span class="kwrd"&gt;float&lt;/span&gt; spriteSize = (&lt;span class="kwrd"&gt;float&lt;/span&gt;)Math.Max(texture.Width, texture.Height);
    Vector2 origin = &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector2(texture.Width / 2f, texture.Height / 2f);
    spriteBatch.Draw(
        texture,
        PointToVector2(point),
        &lt;span class="kwrd"&gt;null&lt;/span&gt;,
        Color.White,
        rotation,
        origin,
        Scale / spriteSize,
        SpriteEffects.None,
        0);
} &lt;/pre&gt;

&lt;p&gt;Let’s walk through what this method is doing so that we’re all clear as to how it works. We first calculate the largest dimension of the texture given to us. We do this so we know how much to scale the sprite to make it fit into a single cell of the grid. Then we compute the origin we’ll use for rendering. I’ll explain the origin below as we go through the parameters of our Draw call.&lt;/p&gt;

&lt;p&gt;Next we make a large call to &lt;i&gt;spriteBatch.Draw&lt;/i&gt;, where we have lots of variables going in. Let’s just explain each one briefly:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;b&gt;texture &lt;/b&gt;– This represents the texture object we want to draw. &lt;/li&gt;

  &lt;li&gt;&lt;b&gt;PointToVector2(point)&lt;/b&gt; – Here we leverage the &lt;i&gt;Grid.PointToVector2 &lt;/i&gt;method we wrote before to convert our grid coordinate to pixel coordinates. &lt;/li&gt;

  &lt;li&gt;&lt;b&gt;null&lt;/b&gt; – This parameter represents the source rectangle used to render. The source rectangle is used to specify a subsection of the area of the texture to use for rendering. Since we want to draw the entire texture, we use null here. &lt;/li&gt;

  &lt;li&gt;&lt;b&gt;Color&lt;/b&gt;.&lt;b&gt;White&lt;/b&gt; – Using white here simply causes the sprite batch to render the texture with no color modifications. &lt;/li&gt;

  &lt;li&gt;&lt;b&gt;rotation – &lt;/b&gt;The rotation (specified in radians) instructs the sprite batch to rotate the texture as desired. &lt;/li&gt;

  &lt;li&gt;&lt;b&gt;origin&lt;/b&gt; – Here we use the origin value we calculated earlier. This value specifies how to place, rotate, and scale the texture. What we’ve done is specified the pixel value of the center of our sprites. The sprites created for us are 32x32 pixels making their center point 16x16. Rather than hard coding in that value, we simply divide the width and height by two. By using the center of the texture, our sprite will be positioned such that the second parameter tells the sprite batch where we want the center of our texture. It also means that the sprite will be rotated and scaled about its center. &lt;/li&gt;

  &lt;li&gt;&lt;b&gt;Scale / spriteSize&lt;/b&gt; – Here we use our grid’s Scale value and divide it by the size of the sprite. This will cause sprite batch to scale the texture so that it fits completely within a single grid cell. &lt;/li&gt;

  &lt;li&gt;&lt;b&gt;SpriteEffects&lt;/b&gt;.&lt;b&gt;None&lt;/b&gt; – SpriteEffects are used for flipping the texture over the horizontal axis, vertical axis, or both. We don’t want to flip the texture, so we specify none. &lt;/li&gt;

  &lt;li&gt;&lt;b&gt;0 &lt;/b&gt;– The last parameter is the layer depth. This can be used to specify the order in which sprites drawn in the same batch are layered when rendered with zero being the topmost drawn sprites and one being the bottom most drawn sprites. We just use zero here because we will be handling the rendering order by hand so that sprites we want lower will be drawn first. In addition we will rarely ever have overlapping sprites, so ordering is hardly an issue for us. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With the Grid class complete, we can move on to create the Orange class:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Orange
{
    Position = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(Grid.MaxColumn / 2, Grid.MaxRow / 2);
}&lt;/pre&gt;

&lt;p&gt;Now let’s think about what data our orange will need. The two obvious ones are a position and the texture. But we also need a third value: a random number generator which will be used to place the orange in a random place on the screen:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; Point Position;
&lt;span class="kwrd"&gt;private&lt;/span&gt; Texture2D texture;
&lt;span class="kwrd"&gt;private&lt;/span&gt; Random rand = &lt;span class="kwrd"&gt;new&lt;/span&gt; Random();&lt;/pre&gt;

&lt;p&gt;Now we’re going to add three short methods to our Orange class that will handle just about all the functionality we will need for this game:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Reposition()
{
    Position = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(rand.Next(Grid.MaxColumn), rand.Next(Grid.MaxRow));
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Load(ContentManager content)
{
    texture = content.Load&amp;lt;Texture2D&amp;gt;(&lt;span class="str"&gt;&amp;quot;Sprites/Orange&amp;quot;&lt;/span&gt;);
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Begin();
    Grid.DrawSprite(spriteBatch, texture, Position, 0f);
    spriteBatch.End();
}&lt;/pre&gt;

&lt;p&gt;Our &lt;i&gt;Reposition&lt;/i&gt; method is what will generate a random location in our grid at which the orange will be placed. The &lt;i&gt;Load&lt;/i&gt; method requires a &lt;i&gt;ContentManager&lt;/i&gt; instance and handles loading our orange texture from file. Lastly the &lt;i&gt;Draw&lt;/i&gt; method takes in a &lt;i&gt;SpriteBatch&lt;/i&gt; instance and handles rendering the orange using our Grid class’s &lt;i&gt;DrawSprite&lt;/i&gt; method.&lt;/p&gt;

&lt;p&gt;Before continuing on, let’s give this class a try to make sure it works like we think it should. First let’s head to the Game1 class and add a new variable for the orange. We’ll also set our starting game state to the InGame playing state:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; Orange orange = &lt;span class="kwrd"&gt;new&lt;/span&gt; Orange();
&lt;span class="kwrd"&gt;private&lt;/span&gt; GameState state = GameState.InGame;&lt;/pre&gt;

&lt;p&gt;Then we need to make sure call the orange’s &lt;i&gt;Load&lt;/i&gt; method in our &lt;i&gt;LoadContent&lt;/i&gt; method:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; LoadContent()
{
    spriteBatch = &lt;span class="kwrd"&gt;new&lt;/span&gt; SpriteBatch(GraphicsDevice);
    titleFont = Content.Load&amp;lt;SpriteFont&amp;gt;(&lt;span class="str"&gt;&amp;quot;Fonts/TitleFont&amp;quot;&lt;/span&gt;);
    mediumFont = Content.Load&amp;lt;SpriteFont&amp;gt;(&lt;span class="str"&gt;&amp;quot;Fonts/MediumFont&amp;quot;&lt;/span&gt;);
    miniFont = Content.Load&amp;lt;SpriteFont&amp;gt;(&lt;span class="str"&gt;&amp;quot;Fonts/MiniFont&amp;quot;&lt;/span&gt;);

    orange.Load(Content);
}&lt;/pre&gt;

&lt;p&gt;Next we’ll go to our &lt;i&gt;UpdateInGame&lt;/i&gt; method and add in some code that will call the orange’s &lt;i&gt;Reposition&lt;/i&gt; method every second:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; UpdateInGame(GameTime gameTime)
{
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (gameTime.TotalGameTime.Milliseconds % 1000 == 0)
        orange.Reposition();
}&lt;/pre&gt;

&lt;p&gt;Lastly we’ll add to the &lt;i&gt;DrawInGame&lt;/i&gt; method so that it call’s the orange’s &lt;i&gt;Draw &lt;/i&gt;method:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DrawInGame()
{
    orange.Draw(spriteBatch);
}&lt;/pre&gt;

&lt;p&gt;Build and run the game. You should see the same gray window with a single orange drawn. Every second the orange will move to a new grid location on the screen. &lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.coding4fun.net/images/43268cd15815_1369/image_5.png"&gt;&lt;img title="image" style="display: inline" height="349" alt="image" src="http://www.coding4fun.net/images/43268cd15815_1369/image_thumb_5.png" width="250" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Now we will be creating our Snake class:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Snake
{ }&lt;/pre&gt;

&lt;p&gt;The Snake class is a bit more complex. Logically let’s lay out what data the class will need:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A list of points for each segment of the body. &lt;/li&gt;

  &lt;li&gt;The four textures (head, straight body piece, angled body piece, and tail) with which we will render the snake &lt;/li&gt;

  &lt;li&gt;A value indicating how fast the snake should be moving. &lt;/li&gt;

  &lt;li&gt;A value indicating the snake’s current direction and the direction he will be moving next. &lt;/li&gt;

  &lt;li&gt;A value indicating whether or not to extend the snake’s length the next time it moves. &lt;/li&gt;

  &lt;li&gt;A timer value so that we can control how often the snake moves from grid cell to grid cell. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A good amount of data for something that seems rather simple. We’ll start by creating an enumeration for those direction values we are going to need:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;enum&lt;/span&gt; Direction
{
    Up,
    Down,
    Left,
    Right
}&lt;/pre&gt;

&lt;p&gt;Next we’ll go ahead and add all the variables to our Snake class itself:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;float&lt;/span&gt; MoveSpeed = .2f; 
&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;float&lt;/span&gt; moveTimer;
&lt;span class="kwrd"&gt;private&lt;/span&gt; Texture2D head, straight, angle, tail;
&lt;span class="kwrd"&gt;private&lt;/span&gt; List&amp;lt;Point&amp;gt; bodyPoints = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Point&amp;gt;();
&lt;span class="kwrd"&gt;private&lt;/span&gt; Direction currentDirection = Direction.Right;
&lt;span class="kwrd"&gt;private&lt;/span&gt; Direction nextDirection = Direction.Right;
&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; extending;&lt;/pre&gt;

&lt;p&gt;Everything here should be rather self explanatory. The &lt;i&gt;MoveSpeed&lt;/i&gt; represents the number of seconds before the snake proceeds to the next grid cell and the &lt;i&gt;moveTimer&lt;/i&gt; will be used to count up to this value. We then have our four textures, a list for the body points, and two values for the current and next direction. Lastly our bool value to tell us whether the snake should extend the next time it moves.&lt;/p&gt;

&lt;p&gt;Now we’re going to add a new method called &lt;i&gt;Reset &lt;/i&gt;which will be called from the Snake’s constructor. &lt;i&gt;Reset&lt;/i&gt; is responsible for placing our Snake it its starting position in the game.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; Snake()
{
    Reset();
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Reset()
{
    bodyPoints.Clear();

    bodyPoints.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Point(2, 0));
    bodyPoints.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Point(1, 0));
    bodyPoints.Add(&lt;span class="kwrd"&gt;new&lt;/span&gt; Point(0, 0));

    currentDirection = Direction.Right;
    nextDirection = Direction.Right;
}&lt;/pre&gt;

&lt;p&gt;In the &lt;i&gt;Reset&lt;/i&gt; method, first we make sure to clear out the list of points for the snake’s body. Next we add three points to the body. The first point sets the position of the head, followed by a single body piece, and lastly the tail. To finish we also make sure to reset both the current and next direction values to Right so that the snake begins by heading to the right.&lt;/p&gt;

&lt;p&gt;Next we’ll make a &lt;i&gt;Load&lt;/i&gt; method which, like the Orange class, handles loading in the textures needed to render the snake:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Load(ContentManager content)
{
    head = content.Load&amp;lt;Texture2D&amp;gt;(&lt;span class="str"&gt;&amp;quot;Sprites/Head&amp;quot;&lt;/span&gt;);
    straight = content.Load&amp;lt;Texture2D&amp;gt;(&lt;span class="str"&gt;&amp;quot;Sprites/Straight&amp;quot;&lt;/span&gt;);
    angle = content.Load&amp;lt;Texture2D&amp;gt;(&lt;span class="str"&gt;&amp;quot;Sprites/Angle&amp;quot;&lt;/span&gt;);
    tail = content.Load&amp;lt;Texture2D&amp;gt;(&lt;span class="str"&gt;&amp;quot;Sprites/Tail&amp;quot;&lt;/span&gt;);
}&lt;/pre&gt;

&lt;p&gt;Continuing on we’re going to implement the rendering for the snake. This is the most complex part of this entire game, but we’ll take it one step at a time to make sure each line of code is explained. First let’s add the five methods we’ll be using:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Begin();

    &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 1; i &amp;lt; bodyPoints.Count - 1; i++)
    {
        DrawBody(
            spriteBatch, 
            bodyPoints[i], 
            bodyPoints[i - 1], 
            bodyPoints[i + 1]);
    }

    DrawTail(spriteBatch);
    DrawHead(spriteBatch);
    spriteBatch.End();
}

&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DrawHead(SpriteBatch spriteBatch) { }
&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DrawTail(SpriteBatch spriteBatch) { }
&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsAnglePiece(Point current, Point last, Point next) { }
&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DrawBody(SpriteBatch spriteBatch, Point current, Point last, Point next) { }
&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;float&lt;/span&gt; GetAngleRotation(Point current, Point last, Point next) { }&lt;/pre&gt;

&lt;p&gt;We can see that we have broken the rendering up into multiple methods to make the code a bit more organized. We have the main &lt;i&gt;Draw &lt;/i&gt;method which will be used by our Game1 class to handle drawing the snake. The method has a single &lt;i&gt;SpriteBatch&lt;/i&gt; instance for a parameter. &lt;/p&gt;

&lt;p&gt;Then we loop through the &lt;i&gt;bodyPoints&lt;/i&gt; list starting on the second item and going up to the second to last item. We intentionally do not use the first or last points because those represent the head and tail, respectively.&lt;/p&gt;

&lt;p&gt;After we draw the body, we then draw the tail and head. The order in which we call these methods will affect the graphics to a small degree. The important part is drawing the head last. By doing this we ensure that even when the head and a body piece overlap (for instance, when the snake runs into itself) the head will be drawn on top of the body part.&lt;/p&gt;

&lt;p&gt;Next we’ll go ahead and fill in the &lt;i&gt;DrawHead&lt;/i&gt; method:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DrawHead(SpriteBatch spriteBatch)
{
    Point headPoint = bodyPoints[0];
    Point nextBody = bodyPoints[1];

    &lt;span class="kwrd"&gt;float&lt;/span&gt; rotation;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (headPoint.Y == nextBody.Y - 1)
    {
        rotation = -MathHelper.PiOver2;
    }
    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (headPoint.Y == nextBody.Y + 1)
    {
        rotation = MathHelper.PiOver2;
    }
    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (headPoint.X == nextBody.X - 1)
    {
        rotation = MathHelper.Pi;
    }
    &lt;span class="kwrd"&gt;else&lt;/span&gt;
    {
        rotation = 0f;
    }

    Grid.DrawSprite(spriteBatch, head, headPoint, rotation);
}&lt;/pre&gt;

&lt;p&gt;While this looks really complex, it’s rather simply once you look at it. First we get out the position of the head and then the first body point after that. What we then do is compare those points to figure out what angle to use when rendering the head. Finally we just use the &lt;i&gt;Grid.DrawSprite&lt;/i&gt; method to render the head to the screen.&lt;/p&gt;

&lt;p&gt;Now we’ll fill in the DrawTail method. This method is almost identical to the DrawHead method so there really is no explanation needed:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DrawTail(SpriteBatch spriteBatch)
{
    Point tailPoint = bodyPoints[bodyPoints.Count - 1];
    Point lastBody = bodyPoints[bodyPoints.Count - 2];

    &lt;span class="kwrd"&gt;float&lt;/span&gt; rotation;
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (tailPoint.Y == lastBody.Y - 1)
    {
        rotation = MathHelper.PiOver2;
    }
    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (tailPoint.Y == lastBody.Y + 1)
    {
        rotation = -MathHelper.PiOver2;
    }
    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (tailPoint.X == lastBody.X + 1)
    {
        rotation = MathHelper.Pi;
    }
    &lt;span class="kwrd"&gt;else&lt;/span&gt;
    {
        rotation = 0f;
    }

    Grid.DrawSprite(spriteBatch, tail, tailPoint, rotation);
}&lt;/pre&gt;

&lt;p&gt;We’ll tackle the &lt;i&gt;IsAnglePiece&lt;/i&gt; method next. This method is used by the &lt;i&gt;DrawBody&lt;/i&gt; method to determine if a given point should be drawn with the angled body piece or the straight body piece. To do this we compare the positions of the current body piece, the previous body piece, and the next body piece to determine if they make up a right angle or not:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsAnglePiece(Point current, Point last, Point next)
{
    &lt;span class="kwrd"&gt;return&lt;/span&gt; (current.X == last.X &amp;amp;&amp;amp; current.X != next.X &amp;amp;&amp;amp; current.Y != last.Y) ||
           (current.X == next.X &amp;amp;&amp;amp; current.X != last.X &amp;amp;&amp;amp; current.Y != next.Y);
}&lt;/pre&gt;

&lt;p&gt;With that completed we can fill in the &lt;i&gt;DrawBody&lt;/i&gt; method. This method is also pretty simple:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DrawBody(SpriteBatch spriteBatch, Point current, Point last, Point next)
{
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (IsAnglePiece(current, last, next))
    {
        Grid.DrawSprite(
            spriteBatch, 
            angle, 
            current, 
            GetAngleRotation(current, last, next));
    }
    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (current.X != last.X)
    {
        Grid.DrawSprite(
            spriteBatch, 
            straight, 
            current, 
            0f);
    }
    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (current.Y != last.Y)
    {
        Grid.DrawSprite(
            spriteBatch, 
            straight, 
            current, 
            MathHelper.PiOver2);
    }
}&lt;/pre&gt;

&lt;p&gt;First we check to see if the three points comprise and angle. If they do we use the &lt;i&gt;Grid.DrawSprite&lt;/i&gt; method to render the angle texture at the current position. For the angle we utilize the &lt;i&gt;GetAngleRotation &lt;/i&gt;method which we will be filling in later.&lt;/p&gt;

&lt;p&gt;If we don’t have an angle piece, we then figure out whether the body piece is horizontal or vertical and use that to render the straight body piece with either zero or PiOver2 as the angle.&lt;/p&gt;

&lt;p&gt;Finally we come to the &lt;i&gt;GetAngleRotation&lt;/i&gt; method. This method takes three Points and determines the angle at which the sprite must be drawn in order to connect the three body pieces. Here’s the code for this method:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;float&lt;/span&gt; GetAngleRotation(Point current, Point last, Point next)
{
    Point negPiOver2 = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(next.X + 1, last.Y - 1);
    Point negPiOver22 = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(last.X + 1, next.Y - 1);

    Point pi = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(next.X - 1, last.Y - 1);
    Point pi2 = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(last.X - 1, next.Y - 1);

    Point piOver2 = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(next.X - 1, last.Y + 1);
    Point piOver22 = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(last.X - 1, next.Y + 1);

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (current == negPiOver2 || current == negPiOver22)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; -MathHelper.PiOver2;
    }
    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (current == pi || current == pi2)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; MathHelper.Pi;
    }
    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (current == piOver2 || current == piOver22)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; MathHelper.PiOver2;
    }
    &lt;span class="kwrd"&gt;else&lt;/span&gt;
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; 0f;
    }
}&lt;/pre&gt;

&lt;p&gt;The method begins by calculating a set of Points. Each pair of points represents one of the combinations that will result in a particular angle. This can take a little bit to wrap your head around, so if you have trouble visualizing it, I’d recommend grabbing (or making) some grid paper and drawing the points out so you can see how they all fall into place.&lt;/p&gt;

&lt;p&gt;We then go through and compare the current point to each set of our values. If any match up, we return that value. If none match, we don’t have any rotation to apply to the sprite.&lt;/p&gt;

&lt;p&gt;That completes the snake’s rendering system. It was a pretty big hill to take, but with it behind us we can feel good knowing that the rest of the code will be quite a bit simpler. Before we move on, let’s head back to the Game1 class to test out our new class and make sure it works.&lt;/p&gt;

&lt;p&gt;First we’ll add a new variable for the snake:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; Snake snake = &lt;span class="kwrd"&gt;new&lt;/span&gt; Snake();&lt;/pre&gt;

&lt;p&gt;Next we head to the &lt;i&gt;LoadContent&lt;/i&gt; method and make sure we call the snake’s &lt;i&gt;Load&lt;/i&gt; method:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;snake.Load(Content);&lt;/pre&gt;

&lt;p&gt;Lastly for this test we’ll just add a call to the snake’s &lt;i&gt;Draw&lt;/i&gt; method in our &lt;i&gt;DrawInGame&lt;/i&gt; method:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DrawInGame()
{
    orange.Draw(spriteBatch);
    snake.Draw(spriteBatch);
}&lt;/pre&gt;

&lt;p&gt;Go ahead and run the game now and you should see the little snake in the top-left corner of the screen along with the orange that we added earlier:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.coding4fun.net/images/43268cd15815_1369/image_6.png"&gt;&lt;img title="image" style="display: inline" height="349" alt="image" src="http://www.coding4fun.net/images/43268cd15815_1369/image_thumb_6.png" width="250" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;There is already one issue with our game. The orange, as it keeps repositioning, occasionally will position itself underneath the snake. We want to avoid this at all costs. To accomplish this we need to add a new method to the Snake class called &lt;i&gt;IsBodyOnPoint&lt;/i&gt; which will tell us whether a given grid cell is occupied by one of the snake’s body parts. To do this we simply leverage the list’s &lt;i&gt;Contains&lt;/i&gt; method:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsBodyOnPoint(Point p)
{
    &lt;span class="kwrd"&gt;return&lt;/span&gt; bodyPoints.Contains(p);
}&lt;/pre&gt;

&lt;p&gt;Next we will modify the Orange class’s &lt;i&gt;Reposition &lt;/i&gt;method to take in a snake instance and use this to make sure we generate a random point not occupied by the snake:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Reposition(Snake snake)
{
    &lt;span class="kwrd"&gt;do&lt;/span&gt;
    {
        Position = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(rand.Next(Grid.MaxColumn), rand.Next(Grid.MaxRow));
    } &lt;span class="kwrd"&gt;while&lt;/span&gt; (snake.IsBodyOnPoint(Position));
}&lt;/pre&gt;

&lt;p&gt;By using the do-while loop, we ensure that we will run that code at least once. Then if the position is found to be occupied by the snake, the code will continue looping until an unoccupied space has been found. &lt;/p&gt;

&lt;p&gt;Let’s go ahead and update our code found in the Game1 &lt;i&gt;UpdateInGame&lt;/i&gt; method to match this new method signature:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; UpdateInGame(GameTime gameTime)
{
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (gameTime.TotalGameTime.Milliseconds % 1000 == 0)
        orange.Reposition(snake);
}&lt;/pre&gt;

&lt;p&gt;Now if you run the game, you’ll notice the orange will never, ever position itself underneath of the snake. We now have enough of the core to begin working on making the snake move around. Let’s head to the Snake class and add a few new methods:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Update(GameTime gameTime)
{ }

&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; HandleInput()
{ }

&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; MoveSnake()
{ }&lt;/pre&gt;

&lt;p&gt;These three methods will handle all of our update code for the snake. First we’ll start by filling in the main &lt;i&gt;Update&lt;/i&gt; method:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Update(GameTime gameTime)
{
    HandleInput();

    moveTimer += (&lt;span class="kwrd"&gt;float&lt;/span&gt;)gameTime.ElapsedGameTime.TotalSeconds;

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (moveTimer &amp;lt; MoveSpeed)
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt;;
    }

    moveTimer = 0f;
    currentDirection = nextDirection;
    MoveSnake();
}&lt;/pre&gt;

&lt;p&gt;The first thing our method does is call the &lt;i&gt;HandleInput. &lt;/i&gt;Next we add the elapsed time in seconds to our &lt;i&gt;moveTimer&lt;/i&gt; variable. Then we see if the &lt;i&gt;moveTimer&lt;/i&gt; is less than the &lt;i&gt;MoveSpeed&lt;/i&gt; value. If &lt;i&gt;moveTimer&lt;/i&gt; is less than the &lt;i&gt;MoveSpeed&lt;/i&gt; we know that enough time has not elapsed so we call ‘return’ to exit out of our update method. If &lt;i&gt;moveTimer&lt;/i&gt; has exceeded the &lt;i&gt;MoveSpeed&lt;/i&gt; value, we proceed on with updating. The next thing we do is reset the &lt;i&gt;moveTimer&lt;/i&gt;. Then we assign the &lt;i&gt;nextDirection &lt;/i&gt;as our &lt;i&gt;currentDirection&lt;/i&gt; and call the &lt;i&gt;MoveSnake&lt;/i&gt; method.&lt;/p&gt;

&lt;p&gt;We’ll continue by filling in the &lt;i&gt;HandleInput&lt;/i&gt; method:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; HandleInput()
{
    GamePadState gps = GamePad.GetState(PlayerIndex.One);

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (gps.IsButtonDown(Buttons.DPadDown) &amp;amp;&amp;amp; currentDirection != Direction.Up)
    {
        nextDirection = Direction.Down;
    }
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (gps.IsButtonDown(Buttons.DPadUp) &amp;amp;&amp;amp; currentDirection != Direction.Down)
    {
        nextDirection = Direction.Up;
    }
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (gps.IsButtonDown(Buttons.DPadLeft) &amp;amp;&amp;amp; currentDirection != Direction.Right)
    {
        nextDirection = Direction.Left;
    }
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (gps.IsButtonDown(Buttons.DPadRight) &amp;amp;&amp;amp; currentDirection != Direction.Left)
    {
        nextDirection = Direction.Right;
    }
}&lt;/pre&gt;

&lt;p&gt;All we are doing here is finding out which DPad direction is down and using that for the next direction. We also check to make sure that the next direction is not the direct opposite of the current direction. Pretty simple.&lt;/p&gt;

&lt;p&gt;Next is the &lt;i&gt;MoveSnake&lt;/i&gt; method. This method takes care of two things for us. First it handles updating all the body points in the snake to move him around the grid. The second thing it handles is inserting new body pieces as requested. Let’s take a look at the code:&lt;/p&gt;

&lt;pre class="csharpcode"&gt; &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; MoveSnake()
{
    Point p1 = bodyPoints[0];
    &lt;span class="kwrd"&gt;switch&lt;/span&gt; (currentDirection)
    {
        &lt;span class="kwrd"&gt;case&lt;/span&gt; Direction.Up:
            bodyPoints[0] = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(p1.X, p1.Y - 1);
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; Direction.Down:
            bodyPoints[0] = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(p1.X, p1.Y + 1);
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; Direction.Left:
            bodyPoints[0] = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(p1.X - 1, p1.Y);
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
        &lt;span class="kwrd"&gt;case&lt;/span&gt; Direction.Right:
            bodyPoints[0] = &lt;span class="kwrd"&gt;new&lt;/span&gt; Point(p1.X + 1, p1.Y);
            &lt;span class="kwrd"&gt;break&lt;/span&gt;;
    }
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (extending)
    {
        bodyPoints.Insert(1, p1);
        extending = &lt;span class="kwrd"&gt;false&lt;/span&gt;;
        &lt;span class="kwrd"&gt;return&lt;/span&gt;;
    }
    &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 1; i &amp;lt; bodyPoints.Count; i++)
    {
        Point p2 = bodyPoints[i];
        bodyPoints[i] = p1;
        p1 = p2;
    }
}&lt;/pre&gt;

&lt;p&gt;Going through this little by little, what we first do is get the position of the head to store that for the next body part. We then use the &lt;i&gt;currentDirection&lt;/i&gt; value to move the head to the next position it should be at.&lt;/p&gt;

&lt;p&gt;Following that we see if we need to add in a new body piece. If we need to, we simply insert the point the head used to be at into the list behind the head and exit the method since we have filled the gap left by moving our head.&lt;/p&gt;

&lt;p&gt;If we did not extend, we then loop through all remaining body parts (including the tail) and update them by moving them to position of the body part in front of them.&lt;/p&gt;

&lt;p&gt;We’ll follow this up by again testing our code to make sure it all works. Head back to the Game1 class and update the &lt;i&gt;UpdateInGame&lt;/i&gt; method to call the snake’s &lt;i&gt;Update&lt;/i&gt; method:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; UpdateInGame(GameTime gameTime)
{
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (gameTime.TotalGameTime.Milliseconds % 1000 == 0)
        orange.Reposition(snake);
    snake.Update(gameTime);
}&lt;/pre&gt;

&lt;p&gt;Now when you run the game your snake will take off towards the right. You can now use the DPad of your Xbox 360 controller (or Zune if you have deployed it there) to direct the snake around.&lt;/p&gt;

&lt;p&gt;At this point we have all the ground work in place. Let’s go ahead and make our gameplay actually goal oriented. First let’s increase functionality that lets our snake eat the oranges. For us to do this, we need to be able to see if the snake’s head is in the same grid cell as the orange. Our orange already has a position value, so we just need to add a method to see if the snake’s head is at that position. We’ll add the following method now to our Snake class:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsHeadAtPosition(Point position)
{
    &lt;span class="kwrd"&gt;return&lt;/span&gt; (bodyPoints[0] == position);
}&lt;/pre&gt;

&lt;p&gt;All this method does is compare the first point in our array to the position given and return if the two values are the same. With this in place, let’s fix up the &lt;i&gt;UpdateInGame&lt;/i&gt; method to only reposition the orange when the snake eats it:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; UpdateInGame(GameTime gameTime)
{
    snake.Update(gameTime);

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (snake.IsHeadAtPosition(orange.Position))
        orange.Reposition(snake);
}&lt;/pre&gt;

&lt;p&gt;Our orange is now updated appropriately so that it only repositions itself if eaten by the snake. However our snake still doesn’t actually grow like he should. So we’ll add another method to the Snake class that tells our snake to extend itself:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Extend()
{
    extending = &lt;span class="kwrd"&gt;true&lt;/span&gt;;
}&lt;/pre&gt;

&lt;p&gt;We can now add this to our &lt;i&gt;UpdateInGame &lt;/i&gt;method so that when the snake eats the orange, not only does the orange reposition itself, but the snake will also grow the next time it moves:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; UpdateInGame(GameTime gameTime)
{
    snake.Update(gameTime);

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (snake.IsHeadAtPosition(orange.Position))
    {
        orange.Reposition(snake);
        snake.Extend();
    }
}&lt;/pre&gt;

&lt;p&gt;Now we’re making some progress. We still have a big problem: there’s no way to lose. In the original game of Snake you would lose by either crashing into the wall of the window or by making the snake crash into itself. We can address both of these cases by adding two methods to the Snake class:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsLooped()
{
    &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; i = 1; i &amp;lt; bodyPoints.Count; i++)
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (IsHeadAtPosition(bodyPoints[i]))
            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;true&lt;/span&gt;;

    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;;
}

&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsHeadOffScreen()
{
    Point h = bodyPoints[0];
    &lt;span class="kwrd"&gt;return&lt;/span&gt; (h.X &amp;lt; 0 || h.Y &amp;lt; 0 || h.X &amp;gt;= Grid.MaxColumn || h.Y &amp;gt;= Grid.MaxRow);
}&lt;/pre&gt;

&lt;p&gt;The first method, &lt;i&gt;IsLooped&lt;/i&gt;, tests all body pieces against the head to see if the head is on the same space as them. The second method, &lt;i&gt;IsHeadOffScreen&lt;/i&gt;, checks to see if the head is out of the valid grid locations. Let’s head back to the &lt;i&gt;UpdateInGame&lt;/i&gt; method and add in these tests:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; UpdateInGame(GameTime gameTime)
{
    snake.Update(gameTime);

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (snake.IsHeadAtPosition(orange.Position))
    {
        orange.Reposition(snake);
        snake.Extend();
    }

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (snake.IsLooped())
        state = GameState.GameOver;

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (snake.IsHeadOffScreen())
        state = GameState.GameOver;
}&lt;/pre&gt;

&lt;p&gt;We now have the ability to lose the game which brings us very close to having a completed game. Let’s now add some code to the start of the &lt;i&gt;UpdateInGame&lt;/i&gt; method to let the user exit back to the title screen by pressing the Back button.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; UpdateInGame(GameTime gameTime)
{
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (IsNewButtonPress(Buttons.Back))
        state = GameState.Title;

    snake.Update(gameTime);

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (snake.IsHeadAtPosition(orange.Position))
    {
        orange.Reposition(snake);
        snake.Extend();
    }

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (snake.IsLooped())
        state = GameState.GameOver;

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (snake.IsHeadOffScreen())
        state = GameState.GameOver;
}&lt;/pre&gt;

&lt;p&gt;We are just about done with all of the code for the &lt;i&gt;UpdateInGame&lt;/i&gt; method. Let’s finish up by adding the ability to count the oranges that have been eaten. First we need to add a new integer variable to the Game1 class to hold the score:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; score;&lt;/pre&gt;

&lt;p&gt;We now just increment the score every time the snake eats and orange. Inside of our &lt;i&gt;UpdateInGame&lt;/i&gt; method, update the code for the snake eating the orange to look like this:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; (snake.IsHeadAtPosition(orange.Position))
{
    orange.Reposition(snake);
    snake.Extend();
    score++;
}&lt;/pre&gt;

&lt;p&gt;We can now focus on finishing the drawing for the &lt;i&gt;DrawInGame&lt;/i&gt;. All we need to do is display the current score on the screen. We’ll start by adding a new string constant to the Game1 class:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; scoreFormat = &lt;span class="str"&gt;&amp;quot;Oranges Eaten: {0}&amp;quot;&lt;/span&gt;;&lt;/pre&gt;

&lt;p&gt;The “{0}” in there, if you haven’t worked with format strings before, is a placeholder that specifies where we want to insert our score.&lt;/p&gt;

&lt;p&gt;Now let’s head down to the &lt;i&gt;DrawInGame&lt;/i&gt; method and add in the code to draw the score to our screen:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DrawInGame()
{
    orange.Draw(spriteBatch);
    snake.Draw(spriteBatch);
    DrawText(miniFont, &lt;span class="kwrd"&gt;string&lt;/span&gt;.Format(scoreFormat, score), &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector2(120f, 5f));
}&lt;/pre&gt;

&lt;p&gt;We can run the game and now see how well we’re doing:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.coding4fun.net/images/43268cd15815_1369/image_7.png"&gt;&lt;img title="image" style="display: inline" height="349" alt="image" src="http://www.coding4fun.net/images/43268cd15815_1369/image_thumb_7.png" width="250" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;We can now quickly fill in both the &lt;i&gt;UpdateGameOver&lt;/i&gt; and &lt;i&gt;DrawGameOver&lt;/i&gt; screens to simply display the results of the game and await input to return to the title screen. First we’ll add two more string constants to the game:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; gameOver = &lt;span class="str"&gt;&amp;quot;Game Over!&amp;quot;&lt;/span&gt;;
&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;const&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; gameOverInstructions = &lt;span class="str"&gt;&amp;quot;Press Play to Continue&amp;quot;&lt;/span&gt;;&lt;/pre&gt;

&lt;p&gt;And then we can fill in the code for our two methods:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; UpdateGameOver()
{
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (IsNewButtonPress(Buttons.B))
        state = GameState.Title;
}

&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; DrawGameOver()
{
    orange.Draw(spriteBatch);
    snake.Draw(spriteBatch);

    DrawText(titleFont, gameOver, &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector2(120f, 25f));
    DrawText(mediumFont, &lt;span class="kwrd"&gt;string&lt;/span&gt;.Format(scoreFormat, score), &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector2(120f, 200f));
    DrawText(mediumFont, gameOverInstructions, &lt;span class="kwrd"&gt;new&lt;/span&gt; Vector2(120f, 225f));
}&lt;/pre&gt;

&lt;p&gt;You can see that we not only draw text in our state, but we also draw the orange and snake. This lets the user see where things were when the game ended and is a nice touch to add to the game.&lt;/p&gt;

&lt;p&gt;The last thing we add is the code to make sure that the &lt;i&gt;UpdateTitleScreen&lt;/i&gt; method makes sure to reset the snake, orange, and score when the user starts a new game:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; UpdateTitleScreen()
{
    &lt;span class="kwrd"&gt;if&lt;/span&gt; (IsNewButtonPress(Buttons.Back))
        Exit();

    &lt;span class="kwrd"&gt;if&lt;/span&gt; (IsNewButtonPress(Buttons.B))
    {
        snake.Reset();
        score = 0;
        orange.Reposition(snake);
        state = GameState.InGame;
    }
}&lt;/pre&gt;

&lt;p&gt;And lastly we make sure the initial state is set to the title screen:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; GameState state = GameState.Title;&lt;/pre&gt;

&lt;p&gt;With that your game is complete! You now have a fully functional Snake clone. “But what about the Zune?”, you ask. Let’s take care of that right now.&lt;/p&gt;

&lt;p&gt;XNA Game Studio has always aimed to be a cross-platform tool and the new Zune support is no exception. Getting our project onto a Zune is literally just a matter of clicks. First right click on your game project in the &lt;i&gt;Solution Explorer&lt;/i&gt; and choose the &lt;i&gt;Create Copy of Project for Zune&lt;/i&gt; menu item. When completed you will have a complete duplicate project that targets the Zune platform. The &lt;i&gt;Solution Explorer&lt;/i&gt; should now look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.coding4fun.net/images/43268cd15815_1369/image_8.png"&gt;&lt;img title="image" style="display: inline" height="428" alt="image" src="http://www.coding4fun.net/images/43268cd15815_1369/image_thumb_8.png" width="271" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Next we need to build and deploy the game to the Zune. This process is also rather simple. If you went through the tutorial on the download instructions page, this will already be familiar to you. If not, we’ll cover the whole process here. &lt;/p&gt;

&lt;p&gt;Head up to the &lt;i&gt;Tools&lt;/i&gt; menu and choose the &lt;i&gt;Launch XNA Game Studio Device Center&lt;/i&gt; option. You will be presented with a window that looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.coding4fun.net/images/43268cd15815_1369/image_9.png"&gt;&lt;img title="image" style="display: inline" height="329" alt="image" src="http://www.coding4fun.net/images/43268cd15815_1369/image_thumb_9.png" width="500" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Click on the Add Device button and you will see the following dialog:&lt;/p&gt;
&lt;a href="http://www.coding4fun.net/images/43268cd15815_1369/image_10.png"&gt;&lt;img title="image" style="display: inline" height="303" alt="image" src="http://www.coding4fun.net/images/43268cd15815_1369/image_thumb_10.png" width="500" border="0" /&gt;&lt;/a&gt; 

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;We want to add a Zune, so click on the Zune button. You should see your Zune appear in the window now:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.coding4fun.net/images/43268cd15815_1369/image_11.png"&gt;&lt;img title="image" style="display: inline" height="303" alt="image" src="http://www.coding4fun.net/images/43268cd15815_1369/image_thumb_11.png" width="500" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;i&gt;Note: If your Zune did not appear in the window,make sure you have the latest Zune firmware on your device and that the Zune Player application is closed. &lt;/i&gt;&lt;/p&gt;

&lt;p&gt;From here select your Zune and press the Next button. After a brief moment you will see the success screen:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.coding4fun.net/images/43268cd15815_1369/image_12.png"&gt;&lt;img title="image" style="display: inline" height="303" alt="image" src="http://www.coding4fun.net/images/43268cd15815_1369/image_thumb_12.png" width="500" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Press the Finish button and then close the XNA Game Studio Device Center.&lt;/p&gt;

&lt;p&gt;Now click on the Zune project you created earlier and go up to the &lt;i&gt;Build&lt;/i&gt; menu and click on &lt;i&gt;Deploy Zune Copy of SammyTheSnake&lt;/i&gt; to begin deploying the game to your Zune.&lt;/p&gt;
&lt;a href="http://www.coding4fun.net/images/43268cd15815_1369/image_13.png"&gt;&lt;img title="image" style="display: inline" height="262" alt="image" src="http://www.coding4fun.net/images/43268cd15815_1369/image_thumb_13.png" width="267" border="0" /&gt;&lt;/a&gt; 

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;You should see the Zune screen update to show that the game is deploying. Once the text on the screen changes to ”connected” or “waiting for computer”, your game has been completely deployed.&lt;/p&gt;

&lt;p&gt;To get to the game on your Zune, simply press the center button to exit XNA Game Studio Connect. Next navigate in your main menu to find the Games option. Inside of there will you will see your newly deployed game. Simply click Play and away you go. Enjoy the new snake game and good luck on your adventures of Zune game development with XNA Game Studio.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8976852" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/arcade/default.aspx">arcade</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category></item><item><title>Your Code Sunk My Battleship!</title><link>http://blogs.msdn.com/coding4fun/archive/2008/08/24/8892343.aspx</link><pubDate>Sun, 24 Aug 2008 21:00:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8892343</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/8892343.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=8892343</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=8892343</wfw:comment><description>&lt;p&gt;&lt;/p&gt; &lt;span id="c4fmetadata"&gt;   &lt;table cellspacing="0" cellpadding="1" width="100%" border="0"&gt;&lt;tbody&gt;       &lt;tr class="entry_overview"&gt;         &lt;td&gt;&lt;span class="entry_description"&gt;Clarity Battleship is a multi-player game in which players code the &amp;quot;artificial intelligence&amp;quot; needed to command their fleet to victory on the high seas. The game is built using WPF and WCF.&lt;/span&gt;&lt;/td&gt;       &lt;/tr&gt;        &lt;tr&gt;         &lt;td colspan="2"&gt;           &lt;div class="entry_author"&gt;Bryan Dougherty&lt;/div&gt;            &lt;div class="entry_company"&gt;&lt;a href="http://blogs.claritycon.com/blogs/bryan_dougherty"&gt;Bryan's Blog&lt;/a&gt;&lt;/div&gt;            &lt;br /&gt;            &lt;div class="entry_details"&gt;&lt;b&gt;Difficulty: &lt;/b&gt;&lt;span class="entry_details_input"&gt;Intermediate&lt;/span&gt;&lt;/div&gt;            &lt;div class="entry_details"&gt;&lt;b&gt;Time Required:&lt;/b&gt; &lt;span class="entry_details_input"&gt;1-3 hours to create your own fleet&lt;/span&gt;&lt;/div&gt;            &lt;div class="entry_details"&gt;&lt;b&gt;Cost: &lt;/b&gt;&lt;span class="entry_details_input"&gt;Free&lt;/span&gt;&lt;/div&gt;            &lt;div class="entry_details"&gt;&lt;b&gt;Software: &lt;/b&gt;&lt;span class="entry_details_input"&gt;&lt;a href="http://msdn.com/express/"&gt;Visual Basic or Visual C# Express Editions&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;            &lt;div class="entry_details"&gt;&lt;b&gt;Hardware: &lt;/b&gt;&lt;span class="entry_details_input"&gt;&lt;/span&gt;&lt;/div&gt;            &lt;div class="entry_details"&gt;&lt;b&gt;Download: &lt;/b&gt;&lt;a href="http://employees.claritycon.com/bdougherty/coding4fun/claritybattleship.zip"&gt;Download&lt;/a&gt;               &lt;ul&gt;&lt;/ul&gt;           &lt;/div&gt;         &lt;/td&gt;       &lt;/tr&gt;     &lt;/tbody&gt;&lt;/table&gt; &lt;/span&gt;  &lt;h2&gt;Clarity Battleship&lt;/h2&gt;  &lt;p&gt;At Clarity Consulting we&amp;#8217;re always looking for a little friendly competition. That&amp;#8217;s why a few times a year we hold coding competitions that we call Tech Challenges. I recently organized one based on the classic board game Battleship.&lt;/p&gt;  &lt;p&gt;The flow of the game is based loosely on the board game. The simple goal is to sink all of your opponents&amp;#8217; ships. Since we&amp;#8217;re writing code though, I decided to step up the game play and make it a more interesting, requiring more strategy than luck.&amp;#160; Just like the board game, the battlefield is a grid. Unlike the classic, however, multiple teams can battle at the same time. Each team&amp;#8217;s fleet is assigned to a region of the Battlefield. &lt;/p&gt;  &lt;p&gt;In our tournament we had six teams battle at a time and held one &amp;#8220;Battle Royale&amp;#8221; with eleven teams.&amp;#160; Below you can see a sample screen shot of a game with three teams. Blocks in red indicate damage. The bottom of the screen displays messages broadcast or information about fleets that have been sunk.&amp;#160; &lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.coding4fun.net/images/3a42a85d3afe_9B5D/screenshotLarge.jpg"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="154" alt="Screen Shot" src="http://www.coding4fun.net/images/3a42a85d3afe_9B5D/screenshotLarge_thumb.jpg" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Teams position ships at the beginning of the game and give ships orders to execute including:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Attacking locations on the battlefield &lt;/li&gt;    &lt;li&gt;Performing reconnaissance on an area of the battlefield &lt;/li&gt;    &lt;li&gt;Broadcasting messages to opponents or conspirators via an &amp;#8220;open radio frequency&amp;#8221; (collusion and deception are allowed and encouraged!) &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Game play is pseudo turn-based. Teams are notified by the game when their ships are ready to execute an order. Teams are also notified as ships are damaged or sunk. The game ends when only one team remains.&lt;/p&gt;  &lt;h2&gt;Application Architecture&lt;/h2&gt;  &lt;p&gt;Now that you have a feel for the game play, let&amp;#8217;s talk about the code. One reason for building this game was to give myself a fun way to learn more about Windows Communication Foundation (WCF) and Windows Presentation Foundation (WPF), new features of the .NET Framework 3.0.&lt;/p&gt;  &lt;p&gt;WCF provides a new unified mechanism for client-server communication that greatly simplifies that amount of work necessary to by standardizing the way you build your communication pieces. I recommend reading &lt;a href="http://www.code-magazine.com/Article.aspx?QuickID=0705041"&gt;this article&lt;/a&gt; as a good place to start learning about WCF.&lt;/p&gt;  &lt;p&gt;WPF enables richer user interfaces and better integration between developer and designer through the use of XAML. As you can see by my simple (though I think pretty cool) UI, I focused my interest on WCF, but did take advantage of a bit of what WPF has to offer.&lt;/p&gt;  &lt;h3&gt;Overview&lt;/h3&gt;  &lt;p&gt;The Battleship application is divided into three parts. The Battleship.Components project is a shared assembly that contains the classes which manage the communication and the logic of the game as well as class definitions for all of the ships (i.e. AircraftCarrier, Battleship, etc.). The Battleship.Gameboard uses WPF to display the action on the battlefield. Finally, console applications are built by each team wishing to compete.&lt;/p&gt;  &lt;p&gt;The diagram below shows how the pieces fit together.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.coding4fun.net/images/3a42a85d3afe_9B5D/ArchitectureDiagramOverview.jpg"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="200" alt="Architecture Diagram - Overview" src="http://www.coding4fun.net/images/3a42a85d3afe_9B5D/ArchitectureDiagramOverview_thumb.jpg" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;h3&gt;WCF Communication&lt;/h3&gt;  &lt;p&gt;As I mentioned, my focus in development was using WCF to manage the communication between the fleets and the game. I was very interested in exploring the bidirectional communication that can be achieved with WCF because it seemed like a perfect fit for the scenario. A traditional web service can receive messages from clients and send back responses. However, it can&amp;#8217;t originate a message on its own to send to the client. For example, with a web service I could easily have a fleet tell the game about an order to execute (i.e. &amp;#8220;I want my Battleship to attack 7,2&amp;#8221;). However, I can&amp;#8217;t have that web service inform another team that its ship was just sunk.&lt;/p&gt;  &lt;p&gt;With WCF I was able to achieve this without writing a lot of custom code. There is some complexity in the code, but most of that is because my goal was to hide the communication layer from the building of a fleet so that players could simply focus on writing the code to implement their strategy. The following sections will walk through the flow of the communication.&lt;/p&gt;  &lt;h4&gt;Server Side&lt;/h4&gt;  &lt;p&gt;While the Battleship.GameBoard is Windows Forms App, it actually should be considered the server in this application. That&amp;#8217;s because it hosts the WCF service that the Fleets call.&lt;/p&gt;  &lt;p&gt;To define a WCF service, you start by defining the interface for the service. Similar to the WSDL of a web service, this is going to tell consumers what the contract, or inputs and outputs, of the service are. Callers will call the different operations exposed by the service.&lt;/p&gt;  &lt;p&gt;In this application, the IBattleshipGameService interface seen below defines the contract for the service.&lt;/p&gt;  &lt;pre class="csharpcode"&gt;[ServiceContract(CallbackContract=&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(IBattleshipGameServiceCallback))]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IBattleshipGameService
{
    [OperationContract(IsOneWay = &lt;span class="kwrd"&gt;true&lt;/span&gt;)]
    &lt;span class="kwrd"&gt;void&lt;/span&gt; RegisterFleet(Fleet fleet);

    [OperationContract(IsOneWay = &lt;span class="kwrd"&gt;true&lt;/span&gt;)]
    &lt;span class="kwrd"&gt;void&lt;/span&gt; ExecuteOrder(&lt;span class="kwrd"&gt;string&lt;/span&gt; shipID, Order order);
}&lt;/pre&gt;
&lt;style type="text/css"&gt;












.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;The interface shows that there are two methods for consumers to call, RegisterFleet and ExecuteOrder. The OperationContract attribute indicates that these are methods. The IsOneWay parameter of the attributes indicates that no response is sent back to callers. That probably seems straightforward for the RegsterFleet call, but you might expect that ExecuteOrder might return a result.&lt;/p&gt;

&lt;p&gt;Actually what happens is that when a fleet is registered, the service holds on to a channel for calling back to the fleets. And the ExecuteOrder method only queues up an order for processing, but doesn&amp;#8217;t immediately execute it. Before queuing it up, the game logic ensures that there aren&amp;#8217;t already too many pending orders in the queue for the ship. This prevents a team from cheating by blasting the server with orders. We&amp;#8217;ll delve more into how the communication back to the client is achieved later.&lt;/p&gt;

&lt;p&gt;With the interface to the service defined, let&amp;#8217;s turn our attention to a portion of the application configuration file.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;configuration&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;system.serviceModel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;services&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;service&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Clarity.Battleship.Components.GameSide.BattleshipGameService&amp;quot;&lt;/span&gt; 
                    &lt;span class="attr"&gt;behaviorConfiguration&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;BattleshipGameServiceBehavior&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;endpoint&lt;/span&gt; &lt;span class="attr"&gt;address&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://localhost:8088/BattleshipGameService&amp;quot;&lt;/span&gt;
                  &lt;span class="attr"&gt;binding&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;wsDualHttpBinding&amp;quot;&lt;/span&gt;
                  &lt;span class="attr"&gt;contract&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Clarity.Battleship.Components.GameSide.IBattleshipGameService&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;service&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;This shows the definition of the service. The contract attribute indicates the service will adhere to the IBattleshipGameService interface. The binding attribute indicates that the WSDualHttpBinding will be used. This is the plumbing that allows both services and clients to send and receive messages.&lt;/p&gt;

&lt;p&gt;With the interface and configuration in place, a concrete implementation has to be built. The BattleshipGameService class provides this. This class implements the IBattleshipGameService interface described above. The actual code for registering a fleet and executing an order are therefore defined here.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,UseSynchronizationContext = &lt;span class="kwrd"&gt;false&lt;/span&gt;)]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; BattleshipGameService : IBattleshipGameService&lt;/pre&gt;

&lt;p&gt;Note the ServiceBehavior attribute and its InstanceContextMode property that decorate the BattleshipGameService class. WCF uses this attribute to decide how to execute the operation when a call comes into the service. In this case, the attributes indicate that the service should run as a singleton. In other words, all calls will be executed by a single instance of the BattleshipGameService. You can further see this in the following code taken from the constructor of the Game class that shows how the service is actually brought to life.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;Uri u = &lt;span class="kwrd"&gt;new&lt;/span&gt; Uri(&lt;span class="str"&gt;&amp;quot;http://localhost:8088/BattleshipService&amp;quot;&lt;/span&gt;);
_gameHost = &lt;span class="kwrd"&gt;new&lt;/span&gt; ServiceHost(&lt;span class="kwrd"&gt;new&lt;/span&gt; BattleshipGameService(), &lt;span class="kwrd"&gt;new&lt;/span&gt; Uri[] { u });
_gameHost.Open();&lt;/pre&gt;

&lt;p&gt;The ServiceHost (which does what it sounds like and hosts the service) is created by passing in an instance of our service object and the uri for the address of the service (I know the uri seems redundant since it&amp;#8217;s in the config, but as far as I saw I had to use this constructor). Since we know the service host will just use the single instance of the BattleShipGameService, we can track that instance through a private property for convenience and hook its events as seen in the code snippets below.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; BattleshipGameService BattleshipGameService
{
    get
    {
        &lt;span class="kwrd"&gt;return&lt;/span&gt; (BattleshipGameService)_gameHost.SingletonInstance;
    }
}

&lt;span class="kwrd"&gt;this&lt;/span&gt;.BattleshipGameService.FleetRegistered +=
   &lt;span class="kwrd"&gt;new&lt;/span&gt; FleetRegisteredEventHandler(BattleshipGameService_FleetRegistered);
&lt;span class="kwrd"&gt;this&lt;/span&gt;.BattleshipGameService.OrderExecuted += 
   &lt;span class="kwrd"&gt;new&lt;/span&gt; OrderExecutedEventHandler(BattleshipGameService_OrderExecuted);&lt;/pre&gt;
&lt;style type="text/css"&gt;












.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;style type="text/css"&gt;












.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;style type="text/css"&gt;












.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;This allows us to elegantly pass off the calls to the Game class. The service then acts as a simple pass through and leaves the Game class to manage the game.&lt;/p&gt;

&lt;h4&gt;Client Side&lt;/h4&gt;

&lt;p&gt;Now that we&amp;#8217;ve covered the basics of the server side, let&amp;#8217;s look at what happens on the client side. In each console app, a class is created that derives from the FleetCommander class (there are complete directions on how to do this with the downloadable source). The FleetCommander class exposes all of the ships of the Fleet for the team to program against.&lt;/p&gt;

&lt;p&gt;To avoid cheating, (some would call it creative programming) each ship gets a unique identifier set by the Game at startup. That way the Game knows which orders are legitimate. To hide that id and details from the FleetCommander and make a very simple and intuitive interface, the method for executing an order is exposed off of the ship classes. The code snippet below shows how FleetCommander could react to an event indicating that a ship is ready for orders. In this case it is saying that the ship should attack the coordinate at location _targetX, _targetY.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; OnShipAwaitingOrders(Ship ship, Order lastOrder)
{
      Order orderToExecute = &lt;span class="kwrd"&gt;new&lt;/span&gt; Order();
      orderToExecute.Coordinate = &lt;span class="kwrd"&gt;new&lt;/span&gt; Coordinate(_targetX, _targetY);
      orderToExecute.Type = OrderType.Attack;
      ship.ExecuteOrder(orderToExecute);&lt;/pre&gt;

&lt;p&gt;Under the covers, the Ship.ExecuteOrder method raises an event which is handled by the base FleetCommander class. The FleetCommander calls the appropriate method on a WCF client class which makes the call to the BattleshipGameService.&lt;/p&gt;

&lt;p&gt;A portion of the definition of the WCF client can be seen below. You&amp;#8217;ll see that it is a class that derives from the DuplexClientBase. This class manages the channels that pass messages back and forth. You can think of it as similar to a web service proxy class.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;partial&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; BattleshipGameServiceClient : 
    System.ServiceModel.DuplexClientBase&amp;lt;IBattleshipGameService&amp;gt;, IBattleshipGameService
{&lt;/pre&gt;

&lt;p&gt;So that covers how messages are sent to the server, but you may be asking yourself &amp;#8220;How did a FleetCommander find out it that a ship was awaiting orders?&amp;#8221; or for that matter &amp;#8220;How does it get notified about anything?&amp;#8221;&lt;/p&gt;

&lt;p&gt;Well, each FleetCommander has an instance of a listener class which receives callbacks from the game service. The way this is set up for the listener is very similarly to how we set up the service. Once again we start with an interface, in this case the IBattleshipGameServiceCallback interface.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IBattleshipGameServiceCallback
{
    [OperationContract(IsOneWay = &lt;span class="kwrd"&gt;true&lt;/span&gt;)]
    &lt;span class="kwrd"&gt;void&lt;/span&gt; OnFleetUpdated(Fleet fleet, FleetUpdateReason reason);

    [OperationContract(IsOneWay = &lt;span class="kwrd"&gt;true&lt;/span&gt;)]
    &lt;span class="kwrd"&gt;void&lt;/span&gt; OnGameStateChanged(GameInfo gameInfo);

    [OperationContract(IsOneWay=&lt;span class="kwrd"&gt;true&lt;/span&gt;)]
    &lt;span class="kwrd"&gt;void&lt;/span&gt; OnOrderExecuted(&lt;span class="kwrd"&gt;string&lt;/span&gt; shipID, Order lastOrder);

    [OperationContract(IsOneWay = &lt;span class="kwrd"&gt;true&lt;/span&gt;)]
    &lt;span class="kwrd"&gt;void&lt;/span&gt; OnMessageReceived(&lt;span class="kwrd"&gt;string&lt;/span&gt; data, Coordinate c);

    [OperationContract(IsOneWay = &lt;span class="kwrd"&gt;true&lt;/span&gt;)]
    &lt;span class="kwrd"&gt;void&lt;/span&gt; OnFleetSunk(&lt;span class="kwrd"&gt;string&lt;/span&gt; fleetName);
}&lt;/pre&gt;

&lt;p&gt;Just like before the OperationContract attributes are used to indicate the methods available. And just like before we need a concrete implementation of the interface. Enter the FleetCommanderListener.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; FleetCommanderListener : IBattleshipGameServiceCallback&lt;/pre&gt;

&lt;p&gt;The FleetCommanderListener is used when creating the BattleshipGameServiceClient. As you can see in the constructor definition below, a callbackInstance parameter must be passed in. The BattleshipGameServiceClient can then handle communication to the server via the IBattleshipGameService interface and also listen for communication back that adheres to the IBattleshipGameServiceCallback interface! &lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; BattleshipGameServiceClient(System.ServiceModel.InstanceContext callbackInstance) :
     &lt;span class="kwrd"&gt;base&lt;/span&gt;(callbackInstance)&lt;/pre&gt;

&lt;p&gt;The last little piece of the puzzle is the client configuration file.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;  &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;system.serviceModel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;client&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;endpoint&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Clarity.Battleship.Components.IBattleshipGameService&amp;quot;&lt;/span&gt;
                &lt;span class="attr"&gt;address&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://localhost:8088/BattleshipGameService&amp;quot;&lt;/span&gt;
                &lt;span class="attr"&gt;binding&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;wsDualHttpBinding&amp;quot;&lt;/span&gt;
                &lt;span class="attr"&gt;bindingConfiguration&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;BattleshipBinding&amp;quot;&lt;/span&gt;
                &lt;span class="attr"&gt;contract&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Clarity.Battleship.Components.GameSide.IBattleshipGameService&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;client&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;bindings&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="rem"&gt;&amp;lt;!-- configure a binding that support duplex communication --&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;wsDualHttpBinding&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;binding&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;BattleshipBinding&amp;quot;&lt;/span&gt;
                 &lt;span class="attr"&gt;clientBaseAddress&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;http://localhost:6088/BattleshipGameClient/&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;binding&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;wsDualHttpBinding&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;bindings&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;system.serviceModel&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;As you can see from the example above, the configuration is fairly straightforward. Just like in the server config, we say we&amp;#8217;re using the wsDualHttpBinding. The one additional piece of info is the clientBaseAddress which tell the server how to call back into the client.&lt;/p&gt;

&lt;h4&gt;The Complete Picture&lt;/h4&gt;

&lt;p&gt;So after reading all of this, you might be thinking &amp;#8220;I thought he said there wasn&amp;#8217;t much custom code needed.&amp;#8221; I think that is still true in terms of the WCF. While certainly not trivial, it really boils down to building a couple interfaces, their concrete implementations, and filling out some config files. Most of the complexity came in was in building out the eventing model to simplify the interface for teams.&lt;/p&gt;

&lt;p&gt;The following diagram helps pull everything together by showing an example of the flow of the communication for execution of an order.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.coding4fun.net/images/3a42a85d3afe_9B5D/ArchitectureDiagramFlow.jpg"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="135" alt="Architecture Diagram - Flow" src="http://www.coding4fun.net/images/3a42a85d3afe_9B5D/ArchitectureDiagramFlow_thumb.jpg" width="244" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;1. The derived FleetCommander makes calls the ExecuteOrder method on a Ship (i.e. &amp;#8220;I want my Battleship to attack 7,2&amp;#8221;)&lt;/p&gt;

&lt;p&gt;2. The Ship raises an event which is handled by the FleetCommander base class which in turn calls to the BattleshipGameServiceClient&lt;/p&gt;

&lt;p&gt;3. This WCF client class makes the WCF call to execute the order&lt;/p&gt;

&lt;p&gt;4. The single instance of the service raises an event to the Game class with the order information &lt;/p&gt;

&lt;p&gt;5. The Game class which controls all the game flow enqueues the order&lt;/p&gt;

&lt;p&gt;6. After processing an order from the queue, the Game calls the OnOrderExecuted WCF callback which gets invoked on the FleetCommanderListener&lt;/p&gt;

&lt;p&gt;7. Finally the FleetCommanderListener raises an OrderExecuted event to the derived FleetCommander class (i.e. &amp;#8220;Your Battleship&amp;#8217;s attack at 7,2 resulted in a Hit&amp;#8221;) which is turned calls the OnShipAwaitingOrders method and the cycle starts over&lt;/p&gt;

&lt;h3&gt;WPF User Interface&lt;/h3&gt;

&lt;p&gt;The last thing to cover with the architecture is the UI. It is pretty simple, but I wanted to experiment a little with WPF. The Gameboard assembly contains a class called Battlefield that is a WPF Window (comparable to a Winform Form). A Grid control makes up the main component of the gameboard. The grid lent itself to building the UI since everything stretches nicely to fit the screen, regardless of the number of teams competing.&lt;/p&gt;

&lt;p&gt;Once the game starts, the game board is setup by adding rectangles and images to the grid control. Throughout the game, the colors of the rectangles are changed to indicate shots, recon, or hit targets.&lt;/p&gt;

&lt;p&gt;Below is an example of some of the Battlefield code behind in which the rectangle for the location of the order is painted a different color depending on the result of an order.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;Rectangle r = _rectangles[_lastOrderStarted.Order.Coordinate];
&lt;span class="kwrd"&gt;if&lt;/span&gt; (e.Order.Result == OrderResult.ShipHit)
{
    r.Fill = Brushes.Red;
    _rectangleFills[e.Order.Coordinate] = r.Fill;
}
&lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (e.Order.Result == OrderResult.ShipSunk)
{
    r.Fill = Brushes.Red;
    _rectangleFills[e.Order.Coordinate] = r.Fill;
}
&lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (e.Order.Result == OrderResult.Miss)
{
    r.Fill = _rectangleFills[e.Order.Coordinate];
}
&lt;span class="kwrd"&gt;else&lt;/span&gt;
{
    r.Fill = _rectangleFills[e.Order.Coordinate];
}&lt;/pre&gt;

&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Hopefully you enjoyed this game and learned a little in process. I encourage you to download the code and build your own fleet. The download includes a document called &amp;#8220;Clarity Battleship &amp;#8211; Building a FleetCommander&amp;#8221; which gives you step by step instructions. Feel free to post your own builds here for other people to challenge and good luck with your battles!&lt;/p&gt;

&lt;p&gt;Bryan Dougherty is a Principal with Clarity Consulting, a software development consulting firm based in Chicago, IL. He is an MCP with several years of professional software engineering experience. Bryan earned a B.S. in Computer Science from Northwestern University and writes about topics related to software development on his &lt;a href="http://blogs.claritycon.com/blogs/bryan_dougherty"&gt;blog&lt;/a&gt;.&lt;/p&gt;
&lt;style type="text/css"&gt;












.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;style type="text/css"&gt;












.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;style type="text/css"&gt;












.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;style type="text/css"&gt;












.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;style type="text/css"&gt;












.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;style type="text/css"&gt;












.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;style type="text/css"&gt;












.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8892343" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/card+and+board+games/default.aspx">card and board games</category></item><item><title>YeahTrivia: Creating a Trivia Server/Client with WPF and WCF</title><link>http://blogs.msdn.com/coding4fun/archive/2007/10/29/5773166.aspx</link><pubDate>Tue, 30 Oct 2007 03:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5773166</guid><dc:creator>Coding4Fun</dc:creator><slash:comments>13</slash:comments><comments>http://blogs.msdn.com/coding4fun/comments/5773166.aspx</comments><wfw:commentRss>http://blogs.msdn.com/coding4fun/commentrss.aspx?PostID=5773166</wfw:commentRss><wfw:comment>http://blogs.msdn.com/coding4fun/rsscomments.aspx?PostID=5773166</wfw:comment><description>&lt;SPAN id=c4fmetadata&gt;
&lt;TABLE class="" cellSpacing=0 cellPadding=1 width="100%" border=0&gt;
&lt;TBODY&gt;
&lt;TR class=entry_overview&gt;
&lt;TD class="" width=50&gt;&lt;IMG height=50 src="http://www.webholstad.com/clarity/coding4fun/yeahtriviathumb.gif" width=50 mce_src="http://www.webholstad.com/clarity/coding4fun/yeahtriviathumb.gif"&gt;&lt;/TD&gt;
&lt;TD class=""&gt;&lt;SPAN class=entry_description&gt;In this article I'll take you along for the ride as I attack the learning curve required to create a fun, interactive application using both the Microsoft Windows Presentation Foundation (WPF) and the Windows Communication Foundation (WCF). Afterwards, you'll have a flexible trivia client/server game ready to play by yourself or against friends, coworkers and your local know-it-all.&amp;nbsp; &lt;EM&gt;And in honor of Halloween, I've included three games of horror movie trivia.&lt;/EM&gt;&lt;/SPAN&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="" colSpan=2&gt;
&lt;DIV class=entry_author&gt;Steve Holstad&lt;/DIV&gt;
&lt;DIV class=entry_company&gt;&lt;A href="http://blogs.claritycon.com/blogs/steve_holstad/archive/2007/10/29/3403.aspx" target=_blank mce_href="http://blogs.claritycon.com/blogs/steve_holstad/archive/2007/10/29/3403.aspx"&gt;The Bright Lights&lt;/A&gt;&lt;/DIV&gt;&lt;BR&gt;
&lt;DIV class=entry_details&gt;&lt;B&gt;Difficulty: &lt;/B&gt;&lt;SPAN class=entry_details_input&gt;Advanced&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV class=entry_details&gt;&lt;B&gt;Time Required:&lt;/B&gt; &lt;SPAN class=entry_details_input&gt;6-10 hours&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV class=entry_details&gt;&lt;B&gt;Cost: &lt;/B&gt;&lt;SPAN class=entry_details_input&gt;Free&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV class=entry_details&gt;&lt;B&gt;Software: &lt;/B&gt;&lt;SPAN class=entry_details_input&gt;Visual Studio Express, 2005, 2008 Beta 2 &lt;A href="http://www.microsoft.com/downloads/details.aspx?FamilyID=10CC340B-F857-4A14-83F5-25634C3BF043&amp;amp;displaylang=en" mce_href="http://www.microsoft.com/downloads/details.aspx?FamilyID=10CC340B-F857-4A14-83F5-25634C3BF043&amp;amp;displaylang=en"&gt;Microsoft .NET Framework 3.0&lt;/A&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV class=entry_details&gt;&lt;B&gt;Hardware: &lt;/B&gt;&lt;SPAN class=entry_details_input&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV class=entry_details&gt;&lt;B&gt;Download: &lt;/B&gt;&lt;A class="" href="http://www.coding4fun.net/source/YeahTrivia.zip" target=_blank mce_href="http://www.coding4fun.net/source/YeahTrivia.zip"&gt;Download Source&lt;/A&gt; 
&lt;UL&gt;&lt;/UL&gt;&lt;/DIV&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/SPAN&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;H3&gt;What can YeahTrivia do for me?&lt;/H3&gt;
&lt;P&gt;Glad you asked. This project started when I decided I wanted to dive headfirst into learning WPF and WCF, without hacking through countless Hello World examples.&amp;nbsp; And I wanted something fun that I could improve over time as my skills developed in these new technologies.&amp;nbsp; In this article I'll take you along for the ride as I attack the learning curve required to create a fun, interactive application using both the Microsoft Windows Presentation Foundation (WPF) and the Windows Communication Foundation (WCF). Afterwards, you'll have a flexible trivia client/server game ready to play by yourself or against friends, coworkers and your local know-it-all.&amp;nbsp; And in honor of Halloween, I've included three games of horror movie trivia.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.coding4fun.net/images/YeahTriviaCreatingaTriviaServerandClient_F6C4/YeahTrivia_Client5.jpg" atomicselection="true" mce_href="http://www.coding4fun.net/images/YeahTriviaCreatingaTriviaServerandClient_F6C4/YeahTrivia_Client5.jpg"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=194 src="http://www.coding4fun.net/images/YeahTriviaCreatingaTriviaServerandClient_F6C4/YeahTrivia_Client_thumb1.jpg" width=280 border=0 mce_src="http://www.coding4fun.net/images/YeahTriviaCreatingaTriviaServerandClient_F6C4/YeahTrivia_Client_thumb1.jpg"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;In case you were wondering, the "YeahTrivia" name honors my new bride&amp;nbsp;&amp;amp; her&amp;nbsp;favorite prefix.&amp;nbsp; Think of it as&amp;nbsp;YeeeeaaaaahhTrivia!&lt;/P&gt;
&lt;H3&gt;Basic Setup&lt;/H3&gt;
&lt;P&gt;This demo project was built using Visual Studio 2008 Beta 2, and is based on the Microsoft .NET Framework version 3.0.&amp;nbsp; The game is fed by a small windows app that acts as a game server, providing game selection, user&amp;nbsp;registration, question loading, response times, chat messaging&amp;nbsp;and scorekeeping.&amp;nbsp; The server loads a game by reading a predefined XML format, which we will look at a bit later.&amp;nbsp; This dynamic load lets anyone create their own flavor of trivia game, with any number of questions.&lt;/P&gt;
&lt;P&gt;The server app exposes one WCF endpoint, a wsDualHttpBinding channel that supports duplex (two-way) communication between the server and any number of game clients.&amp;nbsp; Clients attach themselves to this endpoint and provide a callback address to the server.&amp;nbsp; The server saves an instance of this callback channel and uses each client's instance to&amp;nbsp;contact the clients en masse during general game flow.&amp;nbsp; Enough talk, here's how to build one yourself.&lt;/P&gt;
&lt;H3&gt;Trivia Common and WCF Service Interfaces&lt;/H3&gt;
&lt;P&gt;The TriviaCommon project provides both the server and client projects access to shared objects.&amp;nbsp; To keep things simple the Common project only contains two classes:&lt;/P&gt;
&lt;P&gt;Constants.cs defines the AnswerItem enumeration, which gives easy access to the five answer options (None, A, B, C, D), and the QuestionItem struct.&amp;nbsp; This structure is delivered to the client and contains question text, answer text, and the correct answer object of type AnswerItem.&amp;nbsp; Notice that objects being directly passed over the wire need to be marked with the [DataContract] attribute.&lt;/P&gt;
&lt;P&gt;The second class, Interfaces.cs is where our WCF fun begins.&amp;nbsp; Take a look:&lt;/P&gt;&lt;STRONG&gt;C#:&lt;/STRONG&gt;&lt;BR&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;using&lt;/SPAN&gt; System;
&lt;SPAN class=kwrd&gt;using&lt;/SPAN&gt; System.Collections.Generic;
&lt;SPAN class=kwrd&gt;using&lt;/SPAN&gt; System.ServiceModel;
&lt;SPAN class=kwrd&gt;using&lt;/SPAN&gt; System.Text;

&lt;SPAN class=kwrd&gt;namespace&lt;/SPAN&gt; Trivia.Common
{
    &lt;SPAN class=preproc&gt;#region&lt;/SPAN&gt; Interfaces

    &lt;SPAN class=preproc&gt;#region&lt;/SPAN&gt; Service Interface

    &lt;SPAN class=rem&gt;/// &amp;lt;summary&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=rem&gt;/// Interface that defines Trivia service contract.&lt;/SPAN&gt;
    &lt;SPAN class=rem&gt;/// &amp;lt;/summary&amp;gt;&lt;/SPAN&gt;
    [ServiceContract(CallbackContract = &lt;SPAN class=kwrd&gt;typeof&lt;/SPAN&gt;(ITriviaCallback))]
    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;interface&lt;/SPAN&gt; ITrivia
    {
        [OperationContract(IsOneWay = &lt;SPAN class=kwrd&gt;true&lt;/SPAN&gt;)]
        &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; RegisterUser(&lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; userName);

        [OperationContract(IsOneWay = &lt;SPAN class=kwrd&gt;true&lt;/SPAN&gt;)]
        &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; AcceptAnswer(&lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; userName, &lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; answerValue);

        [OperationContract(IsOneWay = &lt;SPAN class=kwrd&gt;true&lt;/SPAN&gt;)]
        &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; ChatPublish(&lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; userName, &lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; message);
    }

    &lt;SPAN class=preproc&gt;#endregion&lt;/SPAN&gt; Service Interface

    &lt;SPAN class=preproc&gt;#region&lt;/SPAN&gt; Client Callback Interface

    &lt;SPAN class=rem&gt;/// &amp;lt;summary&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=rem&gt;/// Interface that defines Trivia client callback contract.&lt;/SPAN&gt;
    &lt;SPAN class=rem&gt;/// &amp;lt;/summary&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;interface&lt;/SPAN&gt; ITriviaCallback
    {        
        [OperationContract(IsOneWay = &lt;SPAN class=kwrd&gt;true&lt;/SPAN&gt;)]
        &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; OnGameStart(&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; questionCount);

        [OperationContract(IsOneWay = &lt;SPAN class=kwrd&gt;true&lt;/SPAN&gt;)]
        &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; OnGameEnd();

        [OperationContract(IsOneWay = &lt;SPAN class=kwrd&gt;true&lt;/SPAN&gt;)]
        &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; OnUserRegistered(&lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; userName);

        [OperationContract(IsOneWay = &lt;SPAN class=kwrd&gt;true&lt;/SPAN&gt;)]
        &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; OnPublishChat(&lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; userName, &lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; chatMessage);

        [OperationContract(IsOneWay = &lt;SPAN class=kwrd&gt;true&lt;/SPAN&gt;)]
        &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; OnQuestionLoad(&lt;SPAN class=kwrd&gt;int&lt;/SPAN&gt; questionIndex, QuestionItem questionItem);

        [OperationContract(IsOneWay = &lt;SPAN class=kwrd&gt;true&lt;/SPAN&gt;)]
        &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; OnQuestionEnd();

        [OperationContract(IsOneWay = &lt;SPAN class=kwrd&gt;true&lt;/SPAN&gt;)]
        &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; OnQuestionStatusUpdate(Int64 questionTimeRemaining);

        [OperationContract(IsOneWay = &lt;SPAN class=kwrd&gt;true&lt;/SPAN&gt;)]
        &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; OnScoreboardUpdate(Dictionary&amp;lt;&lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt;, Int64&amp;gt; scoreboard);

        [OperationContract(IsOneWay = &lt;SPAN class=kwrd&gt;true&lt;/SPAN&gt;)]
        &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; OnGameServerStopped();
    }

    &lt;SPAN class=preproc&gt;#endregion&lt;/SPAN&gt; Client Callback Interface

    &lt;SPAN class=preproc&gt;#endregion&lt;/SPAN&gt; Interfaces
}
&lt;/PRE&gt;&lt;PRE class=csharpcode&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;&lt;STRONG&gt;VB:&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;Imports&lt;/SPAN&gt; System
&lt;SPAN class=kwrd&gt;Imports&lt;/SPAN&gt; System.Collections.Generic
&lt;SPAN class=kwrd&gt;Imports&lt;/SPAN&gt; System.ServiceModel
&lt;SPAN class=kwrd&gt;Imports&lt;/SPAN&gt; System.Text

&lt;SPAN class=kwrd&gt;Namespace&lt;/SPAN&gt; Trivia.Common

&lt;SPAN class=preproc&gt;#Region&lt;/SPAN&gt; &lt;SPAN class=str&gt;"Interfaces"&lt;/SPAN&gt;

&lt;SPAN class=preproc&gt;#Region&lt;/SPAN&gt; &lt;SPAN class=str&gt;"Service Interface"&lt;/SPAN&gt;
    
    &lt;SPAN class=rem&gt;''' &amp;lt;summary&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=rem&gt;''' Interface that defines Trivia service contract.&lt;/SPAN&gt;
    &lt;SPAN class=rem&gt;''' &amp;lt;/summary&amp;gt;&lt;/SPAN&gt;
    &amp;lt;ServiceContract(CallbackContract:=&lt;SPAN class=kwrd&gt;GetType&lt;/SPAN&gt;(ITriviaCallback))&amp;gt; _
    &lt;SPAN class=kwrd&gt;Public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Interface&lt;/SPAN&gt; ITrivia
        &amp;lt;OperationContract(IsOneWay:=&lt;SPAN class=kwrd&gt;True&lt;/SPAN&gt;)&amp;gt; _
        &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; RegisterUser(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; userName &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;String&lt;/SPAN&gt;)

        &amp;lt;OperationContract(IsOneWay:=&lt;SPAN class=kwrd&gt;True&lt;/SPAN&gt;)&amp;gt; _
        &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; AcceptAnswer(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; userName &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;String&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; answerValue &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;String&lt;/SPAN&gt;)

        &amp;lt;OperationContract(IsOneWay:=&lt;SPAN class=kwrd&gt;True&lt;/SPAN&gt;)&amp;gt; _
        &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; ChatPublish(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; userName &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;String&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; message &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;String&lt;/SPAN&gt;)
    &lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Interface&lt;/SPAN&gt;
        
&lt;SPAN class=preproc&gt;#End Region&lt;/SPAN&gt;

&lt;SPAN class=preproc&gt;#Region&lt;/SPAN&gt; &lt;SPAN class=str&gt;"Client Callback Interface"&lt;/SPAN&gt;

    &lt;SPAN class=rem&gt;''' &amp;lt;summary&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=rem&gt;''' Interface that defines Trivia client callback contract.&lt;/SPAN&gt;
    &lt;SPAN class=rem&gt;''' &amp;lt;/summary&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=rem&gt;''' &amp;lt;remarks&amp;gt;&amp;lt;/remarks&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;Public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Interface&lt;/SPAN&gt; ITriviaCallback
        &amp;lt;OperationContract(IsOneWay:=&lt;SPAN class=kwrd&gt;True&lt;/SPAN&gt;)&amp;gt; _
      &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; OnGameStart(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; questionCount &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Integer&lt;/SPAN&gt;)

        &amp;lt;OperationContract(IsOneWay:=&lt;SPAN class=kwrd&gt;True&lt;/SPAN&gt;)&amp;gt; _
        &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; OnGameEnd()

        &amp;lt;OperationContract(IsOneWay:=&lt;SPAN class=kwrd&gt;True&lt;/SPAN&gt;)&amp;gt; _
        &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; OnUserRegistered(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; userName &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;String&lt;/SPAN&gt;)

        &amp;lt;OperationContract(IsOneWay:=&lt;SPAN class=kwrd&gt;True&lt;/SPAN&gt;)&amp;gt; _
        &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; OnPublishChat(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; userName &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;String&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; chatMessage &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;String&lt;/SPAN&gt;)

        &amp;lt;OperationContract(IsOneWay:=&lt;SPAN class=kwrd&gt;True&lt;/SPAN&gt;)&amp;gt; _
        &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; OnQuestionLoad(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; questionIndex &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Integer&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; questionItem &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; QuestionItem)

        &amp;lt;OperationContract(IsOneWay:=&lt;SPAN class=kwrd&gt;True&lt;/SPAN&gt;)&amp;gt; _
        &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; OnQuestionEnd()

        &amp;lt;OperationContract(IsOneWay:=&lt;SPAN class=kwrd&gt;True&lt;/SPAN&gt;)&amp;gt; _
        &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; OnQuestionStatusUpdate(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; questionTimeRemaining &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Long&lt;/SPAN&gt;)

        &amp;lt;OperationContract(IsOneWay:=&lt;SPAN class=kwrd&gt;True&lt;/SPAN&gt;)&amp;gt; _
        &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; OnScoreboardUpdate(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; scoreboard &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; Dictionary(Of &lt;SPAN class=kwrd&gt;String&lt;/SPAN&gt;, &lt;SPAN class=kwrd&gt;Long&lt;/SPAN&gt;))

        &amp;lt;OperationContract(IsOneWay:=&lt;SPAN class=kwrd&gt;True&lt;/SPAN&gt;)&amp;gt; _
        &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; OnGameServerStopped()

    &lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Interface&lt;/SPAN&gt;

&lt;SPAN class=preproc&gt;#End Region&lt;/SPAN&gt;

&lt;SPAN class=preproc&gt;#End Region&lt;/SPAN&gt;

&lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Namespace&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;A few items of interest here.&amp;nbsp; Notice we're importing the System.ServiceModel namespace.&amp;nbsp; This is the key assembly when using WCF, so don't forget to add a reference to your project.&amp;nbsp; The first interface, ITrivia, defines the operations the server is required to implement, which handle basic user actions: Register a team, accept an answer and accept a chat message.&amp;nbsp; Easy.&lt;/P&gt;
&lt;P&gt;The second interface, cleverly named ITriviaCallback, lays out the callback contract a client must fulfill in order to play nicely with our server.&amp;nbsp; The operations listed here describe events the server will publish.&amp;nbsp; These aren't actual events, but the naming convention "OnX..." helps you remember that these are server published activities.&lt;/P&gt;
&lt;P&gt;Notice that the [OperationContract] attribute notifies WCF that each method is part of a service contract.&amp;nbsp; The IsOneWay flag gives us "fire-and-forget" behavior (the app shouldn't wait around for a response).&amp;nbsp; So now that the common objects are ready, let's move on to...&lt;/P&gt;
&lt;H3&gt;Building the Trivia Server&lt;/H3&gt;
&lt;P&gt;&lt;A href="http://www.coding4fun.net/images/YeahTriviaCreatingaTriviaServerandClient_F6C4/YeahTrivia_Server1.jpg" atomicselection="true" mce_href="http://www.coding4fun.net/images/YeahTriviaCreatingaTriviaServerandClient_F6C4/YeahTrivia_Server1.jpg"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=202 src="http://www.coding4fun.net/images/YeahTriviaCreatingaTriviaServerandClient_F6C4/YeahTrivia_Server.jpg" width=240 border=0 mce_src="http://www.coding4fun.net/images/YeahTriviaCreatingaTriviaServerandClient_F6C4/YeahTrivia_Server.jpg"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;The YeahTrivia Server is a simple utility app, but don't let&amp;nbsp;the plain looks fool you; I thought about calling this the BrainStumper Pumper, but this is a family site.&amp;nbsp; This app&amp;nbsp;lets a user (anyone acting as game admin)&amp;nbsp;start the service, select the desired game, await team registrations, and then start the game.&amp;nbsp; If this were to become a larger scale project, I would make this an IIS hosted service that manages multiple games and provides more autonomy.&amp;nbsp; Until those sports bar royalty checks start rolling in, however, a utility app will do just fine.&lt;/P&gt;
&lt;P&gt;The only form, GameServerDashboard, simply provides user interaction with the GameServer class.&amp;nbsp; On startup, the form&amp;nbsp;inits an instance of GameServer and tells it to start the service.&amp;nbsp; It also loads all trivia question sets from the Games directory, and displays them in the list.&lt;/P&gt;
&lt;P&gt;GameServer is the brain behind YeahTrivia.&amp;nbsp;The attributes below set up the GameServer for action:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;C#:&lt;/STRONG&gt;&lt;BR&gt;&lt;PRE class=csharpcode&gt;[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant
        ,InstanceContextMode = InstanceContextMode.Single)]
    &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;class&lt;/SPAN&gt; GameServer : ITrivia&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;VB:&lt;/STRONG&gt;&lt;BR&gt;&lt;PRE class=csharpcode&gt;&amp;lt;ServiceBehavior(ConcurrencyMode:=ConcurrencyMode.Reentrant, InstanceContextMode:=InstanceContextMode.&lt;SPAN class=kwrd&gt;Single&lt;/SPAN&gt;)&amp;gt; _
    &lt;SPAN class=kwrd&gt;Public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Class&lt;/SPAN&gt; GameServer
        &lt;SPAN class=kwrd&gt;Implements&lt;/SPAN&gt; ITrivia&lt;/PRE&gt;&lt;PRE class=csharpcode&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;ConcurrencyMode.Reentrant prevents deadlocks between the&amp;nbsp;apps&amp;nbsp;by stating that while this service is single-threaded, callbacks are allowed to invoke&amp;nbsp;new&amp;nbsp;operations.&lt;/P&gt;
&lt;P&gt;InstanceContextMode.Single sets the service as a singleton, so that all messages are delivered to the same object instance.&lt;/P&gt;
&lt;P&gt;These are advanced topics if you're new to WCF, so I'll include some links at the end that helped clarify some of this for me.&amp;nbsp; Let's keep moving forward by looking at a few activities of the game server:&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Start the Service:&lt;/EM&gt;&amp;nbsp; Passes in the configured base address as a Uri, inits and opens a new ServiceHost.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;C#:&lt;/STRONG&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; StartService()
{
&lt;SPAN class=rem&gt;   // Set base address and open service host&lt;/SPAN&gt;
   Uri baseAddress = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; Uri(ConfigurationManager.AppSettings[CONFIG_KEY_BASEURI]);
   _serviceHost = &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; ServiceHost(&lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; GameServer(), baseAddress);
   _serviceHost.Open();
}&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;&lt;STRONG&gt;VB:&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;  &lt;SPAN class=kwrd&gt;Public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; StartService()
     &lt;SPAN class=rem&gt;'Set base address and open service host&lt;/SPAN&gt;
     &lt;SPAN class=kwrd&gt;Dim&lt;/SPAN&gt; baseAddress &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;New&lt;/SPAN&gt; Uri(ConfigurationManager.AppSettings(CONFIG_KEY_BASEURI))
     _serviceHost = &lt;SPAN class=kwrd&gt;New&lt;/SPAN&gt; ServiceHost(&lt;SPAN class=kwrd&gt;New&lt;/SPAN&gt; GameServer(), baseAddress)
     _serviceHost.Open()
  &lt;SPAN class=kwrd&gt;End Sub&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;&lt;EM&gt;&lt;/EM&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Register a new team:&lt;/EM&gt;&amp;nbsp;Saves off the callback, adds it to the callback collection and scoreboard containers, notifies the user of a successful registration, and notifies all game clients of the new team.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;C#:&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt; &lt;SPAN class=kwrd&gt;public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;void&lt;/SPAN&gt; RegisterUser(&lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; userName)
 {
     _currentCallback = OperationContext.Current.GetCallbackChannel&amp;lt;ITriviaCallback&amp;gt;();

     &lt;SPAN class=rem&gt;// Store callback&lt;/SPAN&gt;
     &lt;SPAN class=kwrd&gt;if&lt;/SPAN&gt; (!_callbacks.ContainsKey(userName))
     {
         _callbacks.Add(userName, _currentCallback);
         _scoreboard.Add(userName, 0);
&lt;/PRE&gt;&lt;PRE class=csharpcode&gt;         &lt;SPAN class=rem&gt;// Notify user of registration&lt;/SPAN&gt;
         _currentCallback.OnUserRegistered(userName);
&lt;/PRE&gt;&lt;PRE class=csharpcode&gt;         &lt;SPAN class=rem&gt;// Notify clients of new user&lt;/SPAN&gt;
         &lt;SPAN class=kwrd&gt;foreach&lt;/SPAN&gt; (&lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt; userKey &lt;SPAN class=kwrd&gt;in&lt;/SPAN&gt; _callbacks.Keys)
         {
            _currentCallback = _callbacks[userKey];
            _currentCallback.OnPublishChat(GAME_USERKEY, &lt;SPAN class=kwrd&gt;string&lt;/SPAN&gt;.Format(FORMAT_USER_REGISTERED, userName));
         }                
     }
     &lt;SPAN class=kwrd&gt;else&lt;/SPAN&gt;
     {
         &lt;SPAN class=rem&gt;// User already registered, throw exception&lt;/SPAN&gt;
         &lt;SPAN class=kwrd&gt;throw&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;new&lt;/SPAN&gt; Exception(ERROR_USER_ALREADY_EXISTS);
     }
 }&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;&lt;STRONG&gt;VB:&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;Public&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Sub&lt;/SPAN&gt; RegisterUser(&lt;SPAN class=kwrd&gt;ByVal&lt;/SPAN&gt; userName &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;String&lt;/SPAN&gt;) &lt;SPAN class=kwrd&gt;Implements&lt;/SPAN&gt; ITrivia.RegisterUser
   _currentCallback = OperationContext.Current.GetCallbackChannel(Of ITriviaCallback)()

   &lt;SPAN class=rem&gt;'Store callback&lt;/SPAN&gt;
   &lt;SPAN class=kwrd&gt;If&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Not&lt;/SPAN&gt; _callbacks.ContainsKey(userName) &lt;SPAN class=kwrd&gt;Then&lt;/SPAN&gt;
      _callbacks.Add(userName, _currentCallback)
      _scoreboard.Add(userName, 0)

      &lt;SPAN class=rem&gt;'Notify user of registration&lt;/SPAN&gt;
      _currentCallback.OnUserRegistered(userName)

      &lt;SPAN class=rem&gt;'Notify clients of new user&lt;/SPAN&gt;
      &lt;SPAN class=kwrd&gt;For&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;Each&lt;/SPAN&gt; userKey &lt;SPAN class=kwrd&gt;As&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;String&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;In&lt;/SPAN&gt; _callbacks.Keys
         _currentCallback = _callbacks(userKey)
         _currentCallback.OnPublishChat(GAME_USERKEY, &lt;SPAN class=kwrd&gt;String&lt;/SPAN&gt;.Format(FORMAT_USER_REGISTERED, userName))
      &lt;SPAN class=kwrd&gt;Next&lt;/SPAN&gt;
   &lt;SPAN class=kwrd&gt;Else&lt;/SPAN&gt;
      &lt;SPAN class=rem&gt;'User already registered, throw exception&lt;/SPAN&gt;
      &lt;SPAN class=kwrd&gt;Throw&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;New&lt;/SPAN&gt; Exception(ERROR_USER_ALREADY_EXISTS)
   &lt;SPAN class=kwrd&gt;End&lt;/SPAN&gt; &lt;SPAN class=kwrd&gt;If&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;End Sub&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;STYLE type=text/css&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;You can start to see how the server is able to handle incoming calls and process them back to the clients using the duplex channel.&amp;nbsp; I'll leave the rest of the server actions for you to examine on your own.&lt;/P&gt;
&lt;H3&gt;Building the Game Client (WCF) &lt;/H3&gt;
&lt;P&gt;Address, Binding, Contract.&amp;nbsp; These are the ABC's of WCF.&amp;nbsp; The client's app.config file handles connecting to the service and specifying the channel and contract used to interact with it.&amp;nbsp; Examine the client portion of the client .config:&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;client&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
   &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;endpoint&lt;/SPAN&gt; &lt;SPAN class=attr&gt;address&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="http://localhost:8088/Trivia"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;binding&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="wsDualHttpBinding"&lt;/SPAN&gt;
      &lt;SPAN class=attr&gt;bindingConfiguration&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="WSDualHttpBinding_ITrivia"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;contract&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="Trivia.Common.ITrivia"&lt;/SPAN&gt;
      &lt;SPAN class=attr&gt;name&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="WSDualHttpBinding_ITrivia"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
   &lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;endpoint&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;client&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;The address points to the exposed service endpoint.&amp;nbsp; The binding sets the channel as "wsDualHttpBinding" (our http duplex channel).&amp;nbsp; The bindingConfiguration attribute is an optional&amp;nbsp;pointer to another group of configurations for the binding, and finally, the contract sets the interface with which our client will be interacting.&lt;/P&gt;
&lt;P&gt;Next it's time to generate a proxy class.&amp;nbsp;&amp;nbsp;Memorize this: Svcutil.exe is your friend.&amp;nbsp; The client needs a proxy class that it can use to interact with the service model.&amp;nbsp; The&amp;nbsp;utility is a bit tricky at first, because of the wealth of optional flags, but stick with it, it's better than coding the proxy class by hand!&amp;nbsp; You can generate your client code by starting the service and running a command line similar to this:&lt;/P&gt;&lt;PRE class=csharpcode&gt;svcutil /language:cs /out:ServerProxy.cs &lt;A href="http://localhost:8088/Trivia" mce_href="http://localhost:8088/Trivia"&gt;http://localhost:8088/Trivia&lt;/A&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;Add the generated class to the client project and you can begin to interact with the service.&amp;nbsp; The svcutil is available as part of the Windows SDK.&amp;nbsp; Check out the referenced site below&amp;nbsp;for more information on this handy utility.&lt;/P&gt;
&lt;H3&gt;Building the Game Client (WPF)&lt;/H3&gt;
&lt;P&gt;Building the WPF portion of the client app was really a learning experience, and it will&amp;nbsp;be the&amp;nbsp;priority for me to improve in a later version.&amp;nbsp; Learning to program in XAML markup can seem daunting, and it requires a large paradigm shift on your part in order to grasp the new concepts.&amp;nbsp;&amp;nbsp;Microsoft Expression is powerful tool for XAML work, but this time I coded all of the XAML by hand in order to fully see what was taking place inside.&lt;/P&gt;
&lt;P&gt;A deep discussion of WPF is beyond the scope of this article, but take a look at the example code to get an idea of the layout.&amp;nbsp; An excellent book to start with is &lt;U&gt;WPF Unleashed&lt;/U&gt; by Adam Nathan.&amp;nbsp; An easy read (for a programming book), and solid examples to get you rolling.&lt;/P&gt;
&lt;P&gt;I've set up the client using&amp;nbsp;a instance of the Window class, which is shown below:&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;Window&lt;/SPAN&gt; &lt;SPAN class=attr&gt;x:Class&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="Trivia.Client.GameClient"&lt;/SPAN&gt;
    &lt;SPAN class=attr&gt;xmlns&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&lt;/SPAN&gt;
    &lt;SPAN class=attr&gt;xmlns:x&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="http://schemas.microsoft.com/winfx/2006/xaml"&lt;/SPAN&gt;
    &lt;SPAN class=attr&gt;Title&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="YeahTrivia"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;MinHeight&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="620"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;MinWidth&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="620"&lt;/SPAN&gt; 
    &lt;SPAN class=attr&gt;WindowStartupLocation&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="CenterScreen"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;WindowState&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="Normal"&lt;/SPAN&gt;  &lt;SPAN class=attr&gt;Style&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="{DynamicResource MainWindowStyle}"&lt;/SPAN&gt;
    &lt;SPAN class=attr&gt;Background&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="{DynamicResource MainWindowBackgroundBrush}"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;The window class sets up the outermost container of the app, and includes two default namespaces.&amp;nbsp; I've also set a few other properties here: a min height and width, window startup location &amp;amp; state, and two styling attributes set by dynamic resources.&lt;/P&gt;
&lt;P&gt;Resources are a great way to further separate form &amp;amp; function, allowing a programmer to handle the behavior of an app, while a designer can work simultaneously on the look, all without stepping on too many toes.&amp;nbsp; As a 2.0 task for this project, I would remove all inline styling and move them into resource dictionaries.&amp;nbsp; A dictionary contains styles and control templates (a skin) that can be loaded &amp;amp; switched&amp;nbsp;at runtime.&lt;/P&gt;
&lt;P&gt;ResourceDictionaries are linked to your XAML in the App.Xaml file.&amp;nbsp; Once you've set up the reference, the&amp;nbsp;example above show&amp;nbsp;how to&amp;nbsp;reference the current dictionary:&amp;nbsp; The Background and Style&amp;nbsp;properties are set as dynamic resources by naming&amp;nbsp;the Keys specified in the&amp;nbsp;ResourceDictionary (DefaultResources.xaml):&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;LinearGradientBrush&lt;/SPAN&gt; &lt;SPAN class=attr&gt;x:Key&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="MainWindowBackgroundBrush"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;StartPoint&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;=".45,.45"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;EndPoint&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;=".55,.55"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;SpreadMethod&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="Reflect"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
   &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;GradientStop&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Offset&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="0"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Color&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="DodgerBlue"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;
   &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;GradientStop&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Offset&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;=".8"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Color&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="DarkSlateBlue"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;LinearGradientBrush&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;

&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;Style&lt;/SPAN&gt; &lt;SPAN class=attr&gt;TargetType&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="{x:Type Window}"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;x:Key&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="MainWindowStyle"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
   &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;Setter&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Property&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="FontFamily"&lt;/SPAN&gt; &lt;SPAN class=attr&gt;Value&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="Trebuchet MS"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;/&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;Style&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;P&gt;Not quite as easy as CSS, but for a Forms project, it's a nice feature.&amp;nbsp; The sample ResourceDictionary includes a button control template for all of my buttons, based on Rob Eisenberg's excellent article (link below).&lt;/P&gt;
&lt;P&gt;One other interesting feature to point out: Take a look at the code-behind class MarshalToUIThread method, which accepts a SendOrPostCallback parameter.&amp;nbsp; This is where&amp;nbsp;the client is transferring actions from the sync thread back to the UI thread.&amp;nbsp; In each interface method implementation (OnX..), you can see how I wrap any UI-directed code and assign&amp;nbsp;it to the postCallback field.&amp;nbsp; Passing this to the MarshalToUIThread method simply issues a SynchronizationContext.Post call, and the UI thread executes as expected.&lt;/P&gt;
&lt;H3&gt;Creating a New Trivia Game (XML)&lt;/H3&gt;
&lt;P&gt;New trivia games can be added at any time using&amp;nbsp;the simple XML format shown here.&amp;nbsp; After installing the server application, simply drop the new .XML file into the Games directory ([Server Install Location]\Yeah Trivia Server\Games).&amp;nbsp; This demo comes pre-loaded with Geek Trivia, and&amp;nbsp;a special Halloween Trivia version in case you're feeling festive.&lt;/P&gt;&lt;PRE class=csharpcode&gt;&lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;TriviaQuestionDataSet&lt;/SPAN&gt; &lt;SPAN class=attr&gt;xmlns&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;="http://tempuri.org/TriviaQuestionDataSet.xsd"&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
  &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;TriviaQuestion&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;QuestionText&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;Name the highest grossing Monty Python creation.&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;QuestionText&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;AnswerTextA&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;A) Holy Grail&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;AnswerTextA&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;AnswerTextB&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;B) The Life of Brian&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;AnswerTextB&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;AnswerTextC&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;C) Shrubbery&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;AnswerTextC&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;AnswerTextD&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;D) The Meaning of Life&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;AnswerTextD&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;CorrectAnswerKey&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;A&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;CorrectAnswerKey&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
  &lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;TriviaQuestion&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
  &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;TriviaQuestion&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;QuestionText&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;Ferris Bueller's best friend was _________.&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;QuestionText&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;AnswerTextA&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;A) Sloane Peterson&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;AnswerTextA&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;AnswerTextB&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;B) Edward R. Rooney&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;AnswerTextB&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;AnswerTextC&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;C) Charlie Sheen&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;AnswerTextC&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;AnswerTextD&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;D) Cameron Frye&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;AnswerTextD&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
    &lt;SPAN class=kwrd&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN class=html&gt;CorrectAnswerKey&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;D&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;CorrectAnswerKey&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
  &lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;TriviaQuestion&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;
&lt;SPAN class=kwrd&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN class=html&gt;TriviaQuestionDataSet&lt;/SPAN&gt;&lt;SPAN class=kwrd&gt;&amp;gt;&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;STYLE type=text/css&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;STYLE type=text/css&gt;.csharpcode {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	FONT-SIZE: small; COLOR: black; FONT-FAMILY: consolas, "Courier New", courier, monospace; BACKGROUND-COLOR: #ffffff
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	MARGIN: 0em; WIDTH: 100%; BACKGROUND-COLOR: #f4f4f4
}
.csharpcode .lnum {
	COLOR: #606060
}
&lt;/STYLE&gt;

&lt;H3&gt;It's Time to Play YeahTrivia&lt;/H3&gt;
&lt;P&gt;Install both the Server and Client applications.&amp;nbsp; First, fire up the server, and then start your game client(s).&amp;nbsp; Once everyone has registered a team, select your game and hit "Start Game".&amp;nbsp;&amp;nbsp;&amp;nbsp; You're off...&lt;/P&gt;
&lt;H3&gt;Game Over&lt;/H3&gt;
&lt;P&gt;So there you have it.&amp;nbsp; We've seen&amp;nbsp;how WCF manages communication between objects, extracting complicated service protocols away from your code.&amp;nbsp; And just a&amp;nbsp;small subset of the WPF functionality has shown us&amp;nbsp;how quickly a rich, XAML-based UI can come together.&amp;nbsp; And maybe, just maybe, you've learned why a CD is 74 minutes long... but I'm not telling.&lt;/P&gt;
&lt;P&gt;I challenge you to improve on this initial version of YeahTrivia:&amp;nbsp; Crack open the XAML and add animations, images, video, music &amp;amp; more.&amp;nbsp;&amp;nbsp;Create a self-maintaining server to run multiple games at once.&amp;nbsp; Add a High Score storage system.&amp;nbsp; Extend the WCF settings to enable other communication channels.&amp;nbsp; Add a new skin.&amp;nbsp;Create new question files to share with us.&amp;nbsp;&amp;nbsp;Whatever you do, have fun and&amp;nbsp;go nuts.&amp;nbsp; YeahCoding4Fun.&lt;/P&gt;
&lt;P&gt;Download &lt;A href="http://employees.claritycon.com/sholstad/blog/Coding4Fun/YeahTrivia/YeahTrivia.zip" mce_href="http://employees.claritycon.com/sholstad/blog/Coding4Fun/YeahTrivia/YeahTrivia.zip"&gt;source files&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Download &lt;A href="http://employees.claritycon.com/sholstad/blog/Coding4Fun/YeahTrivia/" mce_href="http://employees.claritycon.com/sholstad/blog/Coding4Fun/YeahTrivia/"&gt;compiled demo (C# and VB)&lt;/A&gt;&lt;/P&gt;
&lt;H3&gt;About The Author&lt;/H3&gt;
&lt;P&gt;Steve Holstad works as a software consultant for Clarity Consulting in Chicago.&amp;nbsp; He blogs at &lt;A href="http://blogs.claritycon.com/blogs/steve_holstad/default.aspx" target=_blank mce_href="http://blogs.claritycon.com/blogs/steve_holstad/default.aspx"&gt;The Bright Lights&lt;/A&gt;, and is available via email at &lt;A href="mailto:sholstad@claritycon.com" mce_href="mailto:sholstad@claritycon.com"&gt;sholstad@claritycon.com&lt;/A&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;H3&gt;References&lt;/H3&gt;
&lt;P&gt;&lt;A href="http://wcf.netfx3.com/" target=_blank mce_href="http://wcf.netfx3.com/"&gt;wcf.netfx3.com/&lt;/A&gt;&lt;BR&gt;&lt;A href="http://wpf.netfx3.com/" target=_blank mce_href="http://wpf.netfx3.com/"&gt;wpf.netfx3.com/&lt;/A&gt;&lt;BR&gt;&lt;A href="http://msdn2.microsoft.com/en-us/library/ms733133.aspx" target=_blank mce_href="http://msdn2.microsoft.com/en-us/library/ms733133.aspx"&gt;Generating a client proxy class&lt;/A&gt;&lt;BR&gt;&lt;A href="http://msdn2.microsoft.com/en-us/library/system.servicemodel.concurrencymode.aspx" target=_blank mce_href="http://msdn2.microsoft.com/en-us/library/system.servicemodel.concurrencymode.aspx"&gt;ConcurrencyMode&lt;/A&gt;&lt;BR&gt;&lt;A href="http://msdn2.microsoft.com/en-us/library/ms733133.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms733133.aspx"&gt;SvcUtil.exe Tutorial&lt;/A&gt; &lt;BR&gt;&lt;A href="http://devlicio.us/blogs/rob_eisenberg/archive/2006/12/03/net-3-0-crash-course-part-6-wpf-styles-and-control-templates.aspx" target=_blank mce_href="http://devlicio.us/blogs/rob_eisenberg/archive/2006/12/03/net-3-0-crash-course-part-6-wpf-styles-and-control-templates.aspx"&gt;Rob Eisenberg's WPF control template&lt;/A&gt;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Special thanks to &lt;A href="https://blogs.claritycon.com/blogs/bryan_dougherty/default.aspx" target=_blank mce_href="https://blogs.claritycon.com/blogs/bryan_dougherty/default.aspx"&gt;Bryan Dougherty&lt;/A&gt; for the WCF jumpstart, &lt;A href="https://blogs.claritycon.com/blogs/kevin_marshall/default.aspx" target=_blank mce_href="https://blogs.claritycon.com/blogs/kevin_marshall/default.aspx"&gt;Kevin Marshall&lt;/A&gt; for the Halloween Trivia and &lt;A href="http://blogs.techrepublic.com.com/geekend/index.php?cat=400&amp;amp;submit=view" target=_blank mce_href="http://blogs.techrepublic.com.com/geekend/index.php?cat=400&amp;amp;submit=view"&gt;Jay Garmon&lt;/A&gt;, creator of TechRepublic's Geek Trivia.&lt;/EM&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5773166" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/coding4fun/archive/tags/windows+miscellaneous/default.aspx">windows miscellaneous</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/arcade/default.aspx">arcade</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/windows/default.aspx">windows</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/gaming/default.aspx">gaming</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/card+and+board+games/default.aspx">card and board games</category><category domain="http://blogs.msdn.com/coding4fun/archive/tags/halloween/default.aspx">halloween</category></item></channel></rss>