<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>learning python &#187; Silverlight</title>
	<atom:link href="http://www.learningpython.com/category/python/silverlight/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learningpython.com</link>
	<description>one man's journey into python...</description>
	<lastBuildDate>Mon, 26 Apr 2010 01:21:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating a game using python and Silverlight 1.1</title>
		<link>http://www.learningpython.com/2007/07/29/creating-a-game-using-python-and-silverlight-11/</link>
		<comments>http://www.learningpython.com/2007/07/29/creating-a-game-using-python-and-silverlight-11/#comments</comments>
		<pubDate>Sun, 29 Jul 2007 16:52:32 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=64</guid>
		<description><![CDATA[
			
				
			
		
This tutorial assumes that you have a passing understanding of Silverlight and Microsoft&#8217;s .NET technologies.  If you do you should have no trouble understanding everything in this code, and chances are you will understand some of it more then I do!
But to start it off here is a little bit of information straight from [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.learningpython.com%2F2007%2F07%2F29%2Fcreating-a-game-using-python-and-silverlight-11%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2007%2F07%2F29%2Fcreating-a-game-using-python-and-silverlight-11%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>This tutorial assumes that you have a passing understanding of Silverlight and Microsoft&#8217;s .NET technologies.  If you do you should have no trouble understanding everything in this code, and chances are you will understand some of it more then I do!</p>
<p>But to start it off here is a little bit of information straight from <a href="http://msdn2.microsoft.com/en-us/library/bb404300.aspx#startingwithsilverlight_topic1">Microsoft&#8217;s website</a>:</p>
<blockquote><p>
Silverlight is a new Web presentation technology that is created to run on a variety of platforms. It enables the creation of rich, visually stunning and interactive experiences that can run everywhere: within browsers and on multiple devices and desktop operating systems (such as the Apple Macintosh). In consistency with WPF (Windows Presentation Foundation), the presentation technology in Microsoft .NET Framework 3.0 (the Windows programming infrastructure), XAML (eXtensible Application Markup Language) is the foundation of the Silverlight presentation capability.<br />
This white paper will step you through the basics of Silverlight and how you can use the Microsoft stack of tools, including Microsoft Expression Blend, Microsoft Visual Studio 2005, and XAML to build rich graphical sites. First, let&#8217;s take a primer on the background leading up to Silverlight and where it stands on the development landscape.
</p></blockquote>
<p>What we are going to do is create a simple game with falling targets that they user has to click on in order to “hit” them.  Each time they hit a target they will get a point and another target will be created.</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/silver/silver_03.png" alt="Python silverlight" border="0"/></p>
<p>You can try the “finished” product out here: <a href="http://www.learningpython.com/silverpy/index.htm">http://www.learningpython.com/silverpy/index.htm</a></p>
<p><span id="more-64"></span></p>
<p>This first step is to download and install Silverlight 1.1 from here: <a href="http://msdn2.microsoft.com/en-us/silverlight/bb419317.aspx">http://msdn2.microsoft.com/en-us/silverlight/bb419317.aspx</a></p>
<p>Sadly at this point there is no plugin available for Linux, however the people over at the <a href="http://www.mono-project.com">mono project</a> are working on <a href="http://www.mono-project.com/Moonlight">moonlight</a> so we should see some movement in that direction soon.</p>
<p>The next step is to download the SilverLight 1.1 SDK from: <a href="http://go.microsoft.com/fwlink/?LinkID=89145&#038;clcid=0x409">http://go.microsoft.com/fwlink/?LinkID=89145&#038;clcid=0&#215;409</a></p>
<p>Once you have done that you should create a folder somewhere on your hard drive where you want to store your files.  I created a folder called SilverPy.</p>
<p>Then browse to the DLR console sample located in this directory:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre>...Silverlight1.1SDK\Silverlight 1.1 Alpha Samples\DLRConsole\python</pre></div></div>
<p><strong>Note:</strong> The DLR sample appears to have gone missing in recent SDK releases, but it can be found on the <a href="http://silverlight.net/themes/silverlight/community/gallerydetail.aspx?cat=4">samples page</a>.</p>
<p>Then copy all of the files from that folder into your SilverPy folder.  Now rename the DLRConsole.xaml to SilverPy.xaml and you can get rid of the DLRConsole.py, in its place create a SilverPy.py file.  This will be the file that will contain our python code.</p>
<p>So once you are done you should be left with the following:</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/silver/silver_01.png" alt="Python Silverlight" border="0"/></p>
<p>Now we want to edit the index.html so that it doesn&#8217;t say that it&#8217;s the DLR sample.  Edit the html code to look something like:</p>
<div class="hl-surround" ><div class="hl-main"><pre>&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &gt;
&lt;head&gt;
    &lt;title&gt;SilverPy&lt;/title&gt;
   &lt;script src=&quot;silverlight.js&quot; type=&quot;text/javascript&quot; &gt;&lt;/script&gt;
   &lt;script src=&quot;CreateSilverlight.js&quot; type=&quot;text/javascript&quot; &gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div id=&quot;WpfeControl1Host&quot;&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
				var pe = document.getElementById(&quot;WpfeControl1Host&quot;);
				createSilverlight();
    &lt;/script&gt;
    &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>
<p>Now since we changed the name of our xaml file we need to edit the CreateSilverlight.js file:</p>
<p><code>function createSilverlight()<br />
{<br />
    Sys.Silverlight.createObject("SilverPy.xaml", pe, "wpf",<br />
                                 {width:"100%", height:"100%",<br />
                                  inplaceInstallPrompt:false, background:'white', isWindowless:'true',<br />
                                  framerate:'30', version:'0.95'},<br />
                                 {onError:null, onLoad:null},<br />
                                 null);<br />
}</code></p>
<p>We simply changed the first parameter to the createObject function to be “SilverPy.xaml” so that the correct file gets loaded.  CreateSilverlight.js calls  Silverlight.createObject to instantiate Silverlight and tell it to load our &#8220;Silver.xaml&#8221; file, it also controls other a few other option.  I don&#8217;t want to explain this in too much detail since Microsoft has already done this <a href="http://msdn2.microsoft.com/en-us/library/bb412401.aspx#Role_of_the_Helper_Files">for me</a>: </p>
<p>CreateSilverlight.js:</p>
<blockquote><p>
Defines the CreateSilverlight method, which invokes either the CreateObject or CreateObjectEx method, which are defined in the Silverlight.js file.
</p></blockquote>
<p>Silverlight.js:</p>
<blockquote><p>
Defines the CreateObject and CreateObjectEx methods, which provide version checking support and generate the <object> tag settings in the HTML file for hosting the Silverlight control.<br />
</object></p></blockquote>
<p>If you want more information on the nitty gritty of these these two files and the html file please read the <a href="http://msdn2.microsoft.com/en-us/library/bb412401.aspx">Microsoft website</a>.</p>
<p>The next step is to edit our XAML file, this is going to be pretty simple but I will explain it later:</p>
<p>This is our XAML file, it&#8217;s used to create our &#8220;GUI&#8221;.  The first step is to create the <a href="http://msdn2.microsoft.com/en-us/library/bb188312.aspx">Canvas element</a>: </p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-brackets">&lt;</span><span class="hl-reserved">canvas </span><span class="hl-var">x:Name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">root_canvas</span><span class="hl-quotes">&quot;  </span><span class="hl-var">Width</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">500</span><span class="hl-quotes">&quot; </span><span class="hl-var">Height</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">300</span><span class="hl-quotes">&quot;
 </span><span class="hl-var">xmlns</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">http://schemas.microsoft.com/client/2007</span><span class="hl-quotes">&quot;
 </span><span class="hl-var">xmlns:x</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">http://schemas.microsoft.com/winfx/2006/xaml</span><span class="hl-quotes">&quot;
 </span><span class="hl-var">Background</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Purple</span><span class="hl-quotes">&quot;
</span><span class="hl-brackets">&gt;
  &lt;</span><span class="hl-reserved">x </span><span class="hl-var">:Code Source</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Silver.py</span><span class="hl-quotes">&quot; </span><span class="hl-var">Type</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">text/python</span><span class="hl-quotes">&quot; </span><span class="hl-brackets">/&gt;
  &lt;</span><span class="hl-reserved">canvas </span><span class="hl-var">x:Name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">game_canvas</span><span class="hl-quotes">&quot;
         </span><span class="hl-var">Loaded</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">on_canvas_loaded</span><span class="hl-quotes">&quot;
          </span><span class="hl-var">Width</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">500</span><span class="hl-quotes">&quot; </span><span class="hl-var">Height</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">300</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">/&gt;
  &lt;</span><span class="hl-reserved">textblock </span><span class="hl-var">Name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Score</span><span class="hl-quotes">&quot; </span><span class="hl-var">Text</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Targets Hit: 0</span><span class="hl-quotes">&quot;  </span><span class="hl-var">FontSize</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">10</span><span class="hl-quotes">&quot; </span><span class="hl-var">Canvas</span><span class="hl-code">.</span><span class="hl-var">Left</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">400</span><span class="hl-quotes">&quot; </span><span class="hl-var">Canvas</span><span class="hl-code">.</span><span class="hl-var">Top</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">305</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">/&gt;
&lt;/</span><span class="hl-reserved">canvas</span><span class="hl-brackets">&gt;</span></pre></div></div>
<blockquote><p>
Defines an area within which you can explicitly position child elements by using coordinates relative to the Canvas area.
</p></blockquote>
<p>So this where we will create all of our visible objects, you can think of it like the canvas that a painter uses.  You can see in the XML that we give our canvas object a name: &#8220;root_canvas&#8221;, a height, a width, and a background colour: &#8220;purple&#8221;.</p>
<p>The next element that you see is an <a href="http://msdn2.microsoft.com/en-us/library/ms750494.aspx">x:Code element</a> which we use to load the &#8220;Silver.py&#8221; python file.  Here we could load as many code files as we wanted.</p>
<p>The next element is another Canvas element named &#8220;game_canvas&#8221;, which serves no purpose except to provide us with the <a href="http://msdn2.microsoft.com/en-us/library/bb188274.aspx"Loaded event</a>.  The value that we assign to the loaded attribute is the name of the function in our python file that will be called when this canvas has been loaded.</p>
<p>For some reason (perhaps it is explained somewhere or someone can tell me why) I could not get the &#8220;root_canvas&#8221; to send the Loaded event, so I needed to created the sub-canvas as a work-around.</p>
<p>The next element is a </a><a href="http://msdn2.microsoft.com/en-us/library/bb188394.aspx">TextBlock</a> element that is used to display (yes you guessed it) text.  We use this TextBlock to display the user&#8217;s score as they play our little game.</p>
<p>Now it&#8217;s time to work on our python code.</p>
<p>If you load the index.html file into Internet Explorer (you have to use Internet Explorer as there currently are some issues with loading external python modules when running local files, I&#8217;ve read that it will be fixed soon) you will get an obscure error.  This is because we have not created the on_canvas_loaded function that we described in our xaml.</p>
<p>So let&#8217;s add this to our Silver.py file:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">on_canvas_loaded</span><span class="hl-brackets">(</span><span class="hl-identifier">sender</span><span class="hl-code">, </span><span class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-reserved">pass</span></pre></div></div>
<p>The first thing that you will notice about out Loaded event handler is that it has two parameters, which is common will all event handlers.  Sender is the object that fired the event, and e is the event arguments associated with the event.  What is contained in the event arguments changes depending on what event is being caught.</p>
<p>Now if you load index.html into Internet Explorer you should see the following:</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/silver/silver_02.png" alt="Python Silverlight" border="0"/></p>
<p>So far so good! So now lets get into some serious python coding, now most of this code is simply Python/IronPython so if you are familiar with both you shouldn&#8217;t have any problem.</p>
<p>The fist step is to import all of the stuff that we are going to need:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">from </span><span class="hl-identifier">System </span><span class="hl-reserved">import</span><span class="hl-default"> *
</span><span class="hl-reserved">import </span><span class="hl-identifier">_random
</span><span class="hl-reserved">import </span><span class="hl-identifier">wpf
</span><span class="hl-reserved">from </span><span class="hl-identifier">wpf </span><span class="hl-reserved">import</span><span class="hl-default"> *
</span><span class="hl-reserved">import </span><span class="hl-identifier">clr
clr</span><span class="hl-default">.</span><span class="hl-identifier">AddReference</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">agclr</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">clr</span><span class="hl-default">.</span><span class="hl-identifier">AddReference</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">System.Silverlight</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span></pre></div></div>
<p>Now we are going to work with two classes in our game, one class that will be the target that we are going to aim at and the other that will serve as out main game.</p>
<div class="hl-surround" ><div class="hl-main"><pre>def __init__(self, game_canvas):
    &quot;&quot;&quot;Init the Target Game
    @param game_canvas - The canvas for the game.
    &quot;&quot;&quot;
    #Create a random object
    self.random = _random.Random()
    #Save the game canvas
    self.game_canvas = game_canvas
    #The score
    self.score = 0
    #The list of targets that we have
    self.targets = []
    #Make sure that we have the minimum number of targets
    self.ensure_targets()
    #hook into the keydown event
    self.game_canvas.KeyDown += EventHandler(self.on_key_down)
    #Not done yet
    self.done = False</pre></div></div>
<p>Now the first thing that we do is we create a <a href="http://www.astro.rug.nl/~gipsy/pydoc/_random.html">random object</a> that we will use in the future to position our Target sprites.  The next step is to save the game_canvas that was passed in to the __init__ function, game_canvas corresponds to a canvas object.  Then we initialize our score to zero, this will be the number of canvas sprites that our user hits.  Then we create an empty list, which will store references to the targets that are currently on the screen.</p>
<p>Finally we hook up our member function on_key_down to the game canvas objects <a href="http://msdn2.microsoft.com/en-us/library/bb188394.aspx">KeyDown event</a> and set our done flag to False, since we aren&#8217;t done.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">on_key_down</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">sender</span><span class="hl-code">, </span><span class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Check to see if they hit escape</span><span class="hl-quotes">&quot;&quot;&quot;

    </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">Key</span><span class="hl-code"> == </span><span class="hl-number">8</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_over</span><span class="hl-brackets">()</span></pre></div></div>
<p>This handler is pretty simple, it gets called when there is a key down event on the game_canvas, if the key is the Escape key (e.Key == <img src='http://www.learningpython.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> then we called self.game_over() to end the game.  I added this so that someone could quit the game at any time.</p>
<p>The game_over function is as follows:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">game_over</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Called when the game should end</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_over_text</span><span class="hl-default"> = </span><span class="hl-identifier">TextBlock</span><span class="hl-brackets">()
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_over_text</span><span class="hl-default">.</span><span class="hl-identifier">FontSize</span><span class="hl-default"> = </span><span class="hl-number">50
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_over_text</span><span class="hl-default">.</span><span class="hl-identifier">Text</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">GAME OVER</span><span class="hl-quotes">&quot;
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_canvas</span><span class="hl-default">.</span><span class="hl-identifier">Children</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">game_over_text</span><span class="hl-brackets">)
    </span><span class="hl-identifier">wpf</span><span class="hl-default">.</span><span class="hl-identifier">SetPosition</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">game_over_text</span><span class="hl-code">
        , </span><span class="hl-number">100</span><span class="hl-code">
        , </span><span class="hl-number">100</span><span class="hl-brackets">)
    </span><span class="hl-comment">#We are done
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">done</span><span class="hl-default"> = </span><span class="hl-reserved">True</span></pre></div></div>
<p>The first think that we do is create a <a href="http://msdn2.microsoft.com/en-us/library/bb188394.aspx">TextBlock object</a>, set some of its properties, and add it to the game_canvas&#8217;s children making the object visible. Then we use a helper function in the wpf module to set the position of the TextBlock object.  Finally we set done to True, done is simply a boolean flag that we use to know if the game is done or not.</p>
<p>The wpf modules SetPosition function is actually pretty simple and we could have done this ourselves:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">SetPosition</span><span class="hl-brackets">(</span><span class="hl-identifier">o</span><span class="hl-code">, </span><span class="hl-identifier">x</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-identifier">SetIntValue</span><span class="hl-brackets">(</span><span class="hl-identifier">o</span><span class="hl-code">, </span><span class="hl-identifier">Canvas</span><span class="hl-code">.</span><span class="hl-identifier">TopProperty</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-brackets">)
    </span><span class="hl-identifier">SetIntValue</span><span class="hl-brackets">(</span><span class="hl-identifier">o</span><span class="hl-code">, </span><span class="hl-identifier">Canvas</span><span class="hl-code">.</span><span class="hl-identifier">LeftProperty</span><span class="hl-code">, </span><span class="hl-identifier">x</span><span class="hl-brackets">)</span></pre></div></div>
<p>Another function that we called way back when in the __inti__ function is:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">ensure_targets</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-comment">#Make sure that we have enough targets
    </span><span class="hl-identifier">while </span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">targets</span><span class="hl-brackets">)</span><span class="hl-code"> &lt; </span><span class="hl-identifier">NUM_TARGETS</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">targets</span><span class="hl-default">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">create_target</span><span class="hl-brackets">())</span></pre></div></div>
<p>Pretty simply stuff, while the number of targets stored in our list is less then NUM_TARGETS we create and add targets to our list.  NUM_TARGETS is defined as follows at the beginning of the source code:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-identifier">NUM_TARGETS</span><span class="hl-default"> = </span><span class="hl-number">10</span></pre></div></div>
<p>The ensure_targets function uses the create_target function to create new targets:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">create_target</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-comment">#Create a new target and return it
    
    #Init to random size and position
    </span><span class="hl-identifier">x</span><span class="hl-default"> = </span><span class="hl-builtin">int</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-brackets">()</span><span class="hl-code">*</span><span class="hl-number">400</span><span class="hl-brackets">)
    </span><span class="hl-identifier">width</span><span class="hl-default"> = </span><span class="hl-builtin">int</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-brackets">()</span><span class="hl-code">*</span><span class="hl-number">30</span><span class="hl-brackets">)</span><span class="hl-default"> + </span><span class="hl-number">20
    </span><span class="hl-identifier">height</span><span class="hl-default"> = </span><span class="hl-builtin">int</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-brackets">()</span><span class="hl-code">*</span><span class="hl-number">30</span><span class="hl-brackets">)</span><span class="hl-default"> + </span><span class="hl-number">20
    </span><span class="hl-identifier">y</span><span class="hl-default"> = </span><span class="hl-number">0</span><span class="hl-default"> - </span><span class="hl-identifier">height
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Init the velocity to a random number based off of the 
    score so that more targets are hit, the game gets harder.</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">velocity</span><span class="hl-default"> = </span><span class="hl-builtin">int</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-brackets">()</span><span class="hl-code">*</span><span class="hl-brackets">(</span><span class="hl-builtin">int</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">score</span><span class="hl-code"> / </span><span class="hl-number">15</span><span class="hl-brackets">)</span><span class="hl-code">+</span><span class="hl-number">1</span><span class="hl-brackets">))
    </span><span class="hl-comment">#The minimum velocity is 1
    </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">velocity</span><span class="hl-code">&lt;=</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">velocity</span><span class="hl-default"> = </span><span class="hl-number">1

    </span><span class="hl-reserved">return </span><span class="hl-identifier">TargetSprite</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">on_target_event</span><span class="hl-code">
            , </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">game_canvas</span><span class="hl-code">, </span><span class="hl-identifier">x</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-code">,</span><span class="hl-identifier">width</span><span class="hl-code">, </span><span class="hl-identifier">height</span><span class="hl-code">
            , </span><span class="hl-identifier">velocity</span><span class="hl-brackets">)</span></pre></div></div>
<p>This might seem slightly confusing, but basically what we are doing is using our random object to randomly position and size our new target sprite. We set the x position to be a random number between 0 and 400, the width and height a random number between 20 and 50.  Then the Y position to be 0 – height, this is simply done to start the target off of the screen.</p>
<p>Next we set the velocity of the Target sprite to be between 1 and the current score divided by 15.  This makes it so that the game increases in difficulty as the more targets are hit.</p>
<p>Finally we return the TargetSprite that we create.</p>
<p>Now that we have looked at how we create TargetSprites in the TargetGame class lets look at our TargetSprite object, here is the __init__ function from the Target Sprite class, notice that in create_target we pass the function self.on_target_event as the first parameter:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">notify_function</span><span class="hl-code">, </span><span class="hl-identifier">game_canvas</span><span class="hl-code">, </span><span class="hl-identifier">x</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-code">, </span><span class="hl-identifier">width</span><span class="hl-code">, </span><span class="hl-identifier">height</span><span class="hl-code">, </span><span class="hl-identifier">velocity</span><span class="hl-code">=</span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Initialize the target sprite.
    @param notify_function - function - The function to call when an event happens.
    @param game_canvas - Canvas object - The canvas that the 
    TargetSprite will be added to.
    @param x - int - X position
    @param y - int - Y position
    @param width - int - The width
    @param height - int - The height
    @param velocity = 1 - int - How fast we are going to move down.
    </span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">notify_function</span><span class="hl-default"> = </span><span class="hl-identifier">notify_function
    self</span><span class="hl-default">.</span><span class="hl-identifier">game_canvas</span><span class="hl-default"> = </span><span class="hl-identifier">game_canvas
    self</span><span class="hl-default">.</span><span class="hl-identifier">x</span><span class="hl-default"> = </span><span class="hl-identifier">x
    self</span><span class="hl-default">.</span><span class="hl-identifier">y</span><span class="hl-default"> = </span><span class="hl-identifier">y
    self</span><span class="hl-default">.</span><span class="hl-identifier">width</span><span class="hl-default"> = </span><span class="hl-identifier">width
    self</span><span class="hl-default">.</span><span class="hl-identifier">height</span><span class="hl-default"> = </span><span class="hl-identifier">height
    self</span><span class="hl-default">.</span><span class="hl-identifier">velocity</span><span class="hl-default"> = </span><span class="hl-identifier">velocity
            
    self</span><span class="hl-default">.</span><span class="hl-identifier">add_to_canvas</span><span class="hl-brackets">()</span></pre></div></div>
<p>This function is pretty easy to understand, we save a reference to each parameter and then we add ourself to the canvas that was passed in.  add_to_canvas is where we actually create the ellipse that will be displayed on the screen as our target:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">add_to_canvas</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Add yourself to the canvas</span><span class="hl-quotes">&quot;&quot;&quot;
    
    </span><span class="hl-comment">#Create the Ellipse
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">ellipse</span><span class="hl-default"> = </span><span class="hl-identifier">System</span><span class="hl-default">.</span><span class="hl-identifier">Windows</span><span class="hl-default">.</span><span class="hl-identifier">XamlReader</span><span class="hl-default">.</span><span class="hl-identifier">Load</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">&lt;Ellipse Stroke=&quot;Black&quot; StrokeThickness=&quot;2&quot; Fill=&quot;SlateBlue&quot;/&gt;</span><span class="hl-quotes">'</span><span class="hl-brackets">)
    </span><span class="hl-comment">#Height, Width, and position
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">ellipse</span><span class="hl-default">.</span><span class="hl-identifier">Height</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">height
    self</span><span class="hl-default">.</span><span class="hl-identifier">ellipse</span><span class="hl-default">.</span><span class="hl-identifier">Width</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">width
    </span><span class="hl-comment">#Add to canvase
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_canvas</span><span class="hl-default">.</span><span class="hl-identifier">Children</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">ellipse</span><span class="hl-brackets">)
    </span><span class="hl-comment">#Set the position
    </span><span class="hl-identifier">wpf</span><span class="hl-default">.</span><span class="hl-identifier">SetPosition</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">ellipse</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">x</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">y</span><span class="hl-brackets">)
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Connect out click event handler with the Let button down
    event.</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">ellipse</span><span class="hl-default">.</span><span class="hl-identifier">MouseLeftButtonDown</span><span class="hl-default"> += </span><span class="hl-identifier">MouseEventHandler</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">on_ellipse_click</span><span class="hl-brackets">)</span></pre></div></div>
<p>Now here we do something interesting, we create an <a href="http://msdn2.microsoft.com/en-us/library/bb188394.aspx">ellipse object</a> using some in-line XAML code instead of creating the object directly like we did with the “game over” text.</p>
<p>After the ellipse has been created, we set the Height and Width, and then we add it to the canvas objects <a href="http://msdn2.microsoft.com/en-us/library/bb680308.aspx">children</a>.  Then we use the SetPosition helper function in the wpf module to set the position of our ellipse.</p>
<p>Then we add a handler to the ellipse&#8217;s <a href="http://msdn2.microsoft.com/en-us/library/bb188277.aspx">MouseLeftButtonDown event</a>.  We do this so that we know that the user has “hit” a target.</p>
<p>Our MouseLeftButtonDown function is pretty simple as well, we simply call the notify function that we were initialized with and tell them what happened:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">on_ellipse_click</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">sender</span><span class="hl-code">, </span><span class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">We have been clicked on, call the notify function
    and tell them.</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">notify_function</span><span class="hl-brackets">(</span><span class="hl-identifier">TARGET_HIT</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-brackets">)</span></pre></div></div>
<p>Target hit is defined at the start of the program along with GAME_OVER like so:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-identifier">TARGET_HIT</span><span class="hl-default"> = </span><span class="hl-number">0
</span><span class="hl-identifier">GAME_OVER</span><span class="hl-default"> = </span><span class="hl-number">1</span></pre></div></div>
<p>These are simply defines that we use to pass into the notify function in order to tell it what event has happened.</p>
<p>Now we switch back to the TargetGame clasas and the notify function that we passed to the TargetSprite():</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">on_target_event</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">event</span><span class="hl-code">, </span><span class="hl-identifier">target_sprite</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Called when an event is sent from the target
    sprite
    @param event - number - What has happened?
    @param target_sprite -TargetSprite - The sprite that
    has sent this event</span><span class="hl-quotes">&quot;&quot;&quot;
    
    </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">done</span><span class="hl-code">==</span><span class="hl-reserved">True</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-comment">#We are already done don't bother
        </span><span class="hl-reserved">return
    
    </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code"> == </span><span class="hl-identifier">GAME_OVER</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-comment">#The game is over!
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_over</span><span class="hl-brackets">()
    </span><span class="hl-identifier">elif </span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code"> == </span><span class="hl-identifier">TARGET_HIT</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">A target has been hit
        Remove the hit target from the list and from
        the canvass</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">targets</span><span class="hl-default">.</span><span class="hl-identifier">remove</span><span class="hl-brackets">(</span><span class="hl-identifier">target_sprite</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_canvas</span><span class="hl-default">.</span><span class="hl-identifier">Children</span><span class="hl-default">.</span><span class="hl-identifier">Remove</span><span class="hl-brackets">(</span><span class="hl-identifier">target_sprite</span><span class="hl-code">.</span><span class="hl-identifier">ellipse</span><span class="hl-brackets">)
        </span><span class="hl-comment">#Update the score and the score text
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">score</span><span class="hl-default"> += </span><span class="hl-number">1
        </span><span class="hl-identifier">Score</span><span class="hl-default">.</span><span class="hl-identifier">Text</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">Targets Hit: %d</span><span class="hl-quotes">&quot;</span><span class="hl-default"> % </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">score</span><span class="hl-brackets">)
        </span><span class="hl-comment">#Now make sure that we have enough targets
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">ensure_targets</span><span class="hl-brackets">()</span></pre></div></div>
<p>Now I hope you can see what is happening, we don&#8217;t have any game play just yet, but we have started working on the interaction between our sprites and the main game object.  This function is pretty straight forward, if we got the GAME_OVER event then we call the game_over function and everything finishes.</p>
<p>If the event is a TARGET_HIT event then we need to remove the target that was hit from the targets list and the game_canvas.  Then we need to update the score and the score text.  Now this is the first time that we have referenced our TextBlock object.  If you remember the XAML you&#8217;ll remember that we created a TextBlock object in order to display the score:</p>
<p>[code lang=”xml”]<br />
<textblock Name="Score" Text="Targets Hit: 0"  FontSize="10" Canvas.Left="400" Canvas.Top="305"/><br />
[/code]</p>
<p>So in order to set the text, we simply reference the object by it&#8217;s name and set its <a href=”http://msdn2.microsoft.com/en-us/library/bb188535.aspx”>text property</a>:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-identifier">Score</span><span class="hl-default">.</span><span class="hl-identifier">Text</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">Targets Hit: %d</span><span class="hl-quotes">&quot;</span><span class="hl-default"> % </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">score</span><span class="hl-brackets">)</span></pre></div></div>
<p>Which I think is some pretty cool and intuitive interaction between our xaml and our python code.</p>
<p>Now we have most of the game &#8220;architecture&#8221; in place, but we still need to create our game loop, and a way for outside callers to start the actual game, enter the run function:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">run</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Start the game.</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-comment">#initialize the elements
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">done</span><span class="hl-default"> = </span><span class="hl-reserved">False
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">score</span><span class="hl-default"> = </span><span class="hl-number">0
    </span><span class="hl-comment">#Our storyboard timer
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">gameLoop</span><span class="hl-default"> = </span><span class="hl-identifier">Storyboard</span><span class="hl-brackets">()
   </span><span class="hl-comment">#Set the name property for the 1.1 refresh
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">gameLoop</span><span class="hl-default">.</span><span class="hl-identifier">SetValue</span><span class="hl-brackets">[</span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">String</span><span class="hl-brackets">](</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">gameLoop</span><span class="hl-code">.</span><span class="hl-identifier">NameProperty</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">GameLoop</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
    </span><span class="hl-comment">#Add to the canvas
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_canvas</span><span class="hl-default">.</span><span class="hl-identifier">Resources</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">gameLoop</span><span class="hl-brackets">)
    </span><span class="hl-comment">#Add the completed event handler
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">gameLoop</span><span class="hl-default">.</span><span class="hl-identifier">Completed</span><span class="hl-default"> += </span><span class="hl-identifier">EventHandler</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">on_game_timer</span><span class="hl-brackets">)
    </span><span class="hl-comment">#Start the game loop
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">gameLoop</span><span class="hl-default">.</span><span class="hl-identifier">Begin</span><span class="hl-brackets">()</span></pre></div></div>
<p>Now because of the way that Silverlight works we cannot simply use a normal game loop, instead we use a <a href="http://msdn2.microsoft.com/en-us/library/bb188390.aspx">Storyboard object</a> which is generally used for animations.  What we do is tell the storyboard object to <a href="http://msdn2.microsoft.com/en-us/library/bb188390.aspx">Begin</a> its set of animations, but since there are no animations set up, it promptly quits and the <a href="http://msdn2.microsoft.com/en-us/library/bb190644.aspx">Completed event</a> fires, which we handle with the self.on_game_timer function.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">on_game_timer</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">sender</span><span class="hl-code">, </span><span class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This is the main game loop that we are going to be
        using.</span><span class="hl-quotes">&quot;&quot;&quot;

        </span><span class="hl-comment">#Update the sprites
        </span><span class="hl-reserved">for </span><span class="hl-identifier">sprite </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">targets</span><span class="hl-default">:
            </span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">update</span><span class="hl-brackets">()
        </span><span class="hl-comment">#If we are not done, start the timer again    
        </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-reserved">not </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">done</span><span class="hl-brackets">)</span><span class="hl-default">:
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">gameLoop</span><span class="hl-default">.</span><span class="hl-identifier">Begin</span><span class="hl-brackets">()</span></pre></div></div>
<p>What we do when our Storyboard ends is we update all of our targets, then if the game has not finished we restart the Storyboard, and the game loops.</p>
<p>The update function in the TargetSprite pretty simple, it updates the position of the Target and if the target has hit the bottom of the canvas then it fires the GAME_OVER event:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">update</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This is called when the sprite is supposed to update
    itself.</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">y</span><span class="hl-default"> += </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">velocity
    wpf</span><span class="hl-default">.</span><span class="hl-identifier">SetPosition</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">ellipse</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">x</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">y</span><span class="hl-brackets">)
    
    </span><span class="hl-identifier">if </span><span class="hl-brackets">((</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">y</span><span class="hl-code"> + </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">height</span><span class="hl-brackets">)</span><span class="hl-code"> &gt;= </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">game_canvas</span><span class="hl-code">.</span><span class="hl-identifier">Height</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-comment">#We have hit the bottom! Game over!
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">notify_function</span><span class="hl-brackets">(</span><span class="hl-identifier">GAME_OVER</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-brackets">)</span></pre></div></div>
<p>Now all we need is to do is start the game when the page loads, to do that we go back to our trusty on_canvas_loaded function:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">on_canvas_loaded</span><span class="hl-brackets">(</span><span class="hl-identifier">sender</span><span class="hl-code">, </span><span class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-identifier">target_game</span><span class="hl-default"> = </span><span class="hl-identifier">TargetGame</span><span class="hl-brackets">(</span><span class="hl-identifier">root_canvas</span><span class="hl-brackets">)
    </span><span class="hl-identifier">target_game</span><span class="hl-default">.</span><span class="hl-identifier">run</span><span class="hl-brackets">()</span></pre></div></div>
<p>Now if you&#8217;ve made it this far in the code there shouldn&#8217;t be anything surprising about this, we basically initialise our TargetGame using the root_canvas, and then we tell that game to run, that&#8217;s it.</p>
<p>So that&#8217;s it, you can go play the game here if you want: <a href="http://www.learningpython.com/silverpy/index.htm">http://www.learningpython.com/silverpy/index.htm</a> my highest score is 93.</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/silver/silver_03.png" alt="Python silverlight" border="0"/></p>
<p>Here is all of the code:</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-reserved">from </span><span class="hl-identifier">System </span><span class="hl-reserved">import</span><span class="hl-default"> *
</span><span class="hl-reserved">import </span><span class="hl-identifier">_random
</span><span class="hl-reserved">import </span><span class="hl-identifier">wpf
</span><span class="hl-reserved">from </span><span class="hl-identifier">wpf </span><span class="hl-reserved">import</span><span class="hl-default"> *
</span><span class="hl-reserved">import </span><span class="hl-identifier">clr
clr</span><span class="hl-default">.</span><span class="hl-identifier">AddReference</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">agclr</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">clr</span><span class="hl-default">.</span><span class="hl-identifier">AddReference</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">System.Silverlight</span><span class="hl-quotes">'</span><span class="hl-brackets">)

</span><span class="hl-identifier">TARGET_HIT</span><span class="hl-default"> = </span><span class="hl-number">0
</span><span class="hl-identifier">GAME_OVER</span><span class="hl-default"> = </span><span class="hl-number">1
</span><span class="hl-identifier">NUM_TARGETS</span><span class="hl-default"> = </span><span class="hl-number">10

</span><span class="hl-reserved">class </span><span class="hl-identifier">TargetSprite</span><span class="hl-brackets">(</span><span class="hl-identifier">object</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">The Target to hit</span><span class="hl-quotes">&quot;&quot;&quot;
    
    </span><span class="hl-reserved">def </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">notify_function</span><span class="hl-code">, </span><span class="hl-identifier">game_canvas</span><span class="hl-code">, </span><span class="hl-identifier">x</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-code">, </span><span class="hl-identifier">width</span><span class="hl-code">, </span><span class="hl-identifier">height</span><span class="hl-code">, </span><span class="hl-identifier">velocity</span><span class="hl-code">=</span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Initialize the target sprite.
        @param notify_function - function - The function to call when an event happens.
        @param game_canvas - Canvas object - The canvas that the 
        TargetSprite will be added to.
        @param x - int - X position
        @param y - int - Y position
        @param width - int - The width
        @param height - int - The height
        @param velocity = 1 - int - How fast we are going to move down.
        </span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">notify_function</span><span class="hl-default"> = </span><span class="hl-identifier">notify_function
        self</span><span class="hl-default">.</span><span class="hl-identifier">game_canvas</span><span class="hl-default"> = </span><span class="hl-identifier">game_canvas
        self</span><span class="hl-default">.</span><span class="hl-identifier">x</span><span class="hl-default"> = </span><span class="hl-identifier">x
        self</span><span class="hl-default">.</span><span class="hl-identifier">y</span><span class="hl-default"> = </span><span class="hl-identifier">y
        self</span><span class="hl-default">.</span><span class="hl-identifier">width</span><span class="hl-default"> = </span><span class="hl-identifier">width
        self</span><span class="hl-default">.</span><span class="hl-identifier">height</span><span class="hl-default"> = </span><span class="hl-identifier">height
        self</span><span class="hl-default">.</span><span class="hl-identifier">velocity</span><span class="hl-default"> = </span><span class="hl-identifier">velocity
                
        self</span><span class="hl-default">.</span><span class="hl-identifier">add_to_canvas</span><span class="hl-brackets">()
        
    </span><span class="hl-reserved">def </span><span class="hl-identifier">add_to_canvas</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Add yourself to the canvas</span><span class="hl-quotes">&quot;&quot;&quot;
        
        </span><span class="hl-comment">#Create the Ellipse
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">ellipse</span><span class="hl-default"> = </span><span class="hl-identifier">System</span><span class="hl-default">.</span><span class="hl-identifier">Windows</span><span class="hl-default">.</span><span class="hl-identifier">XamlReader</span><span class="hl-default">.</span><span class="hl-identifier">Load</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">&lt;ellipse Stroke=&quot;Black&quot; StrokeThickness=&quot;2&quot; Fill=&quot;SlateBlue&quot;/&gt;</span><span class="hl-quotes">'</span><span class="hl-brackets">)
        </span><span class="hl-comment">#Height, Width, and position
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">ellipse</span><span class="hl-default">.</span><span class="hl-identifier">Height</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">height
        self</span><span class="hl-default">.</span><span class="hl-identifier">ellipse</span><span class="hl-default">.</span><span class="hl-identifier">Width</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">width
        </span><span class="hl-comment">#Add to canvase
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_canvas</span><span class="hl-default">.</span><span class="hl-identifier">Children</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">ellipse</span><span class="hl-brackets">)
        </span><span class="hl-comment">#Set the position
        </span><span class="hl-identifier">wpf</span><span class="hl-default">.</span><span class="hl-identifier">SetPosition</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">ellipse</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">x</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">y</span><span class="hl-brackets">)
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Connect out click event handler with the Let button down
        event.</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">ellipse</span><span class="hl-default">.</span><span class="hl-identifier">MouseLeftButtonDown</span><span class="hl-default"> += </span><span class="hl-identifier">MouseEventHandler</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">on_ellipse_click</span><span class="hl-brackets">)
        
    </span><span class="hl-reserved">def </span><span class="hl-identifier">on_ellipse_click</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">sender</span><span class="hl-code">, </span><span class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">We have been clicked on, call the notify function
        and tell them.</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">notify_function</span><span class="hl-brackets">(</span><span class="hl-identifier">TARGET_HIT</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-brackets">)
    
    </span><span class="hl-reserved">def </span><span class="hl-identifier">update</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This is called when the sprite is supposed to update
        itself.</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">y</span><span class="hl-default"> += </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">velocity
        wpf</span><span class="hl-default">.</span><span class="hl-identifier">SetPosition</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">ellipse</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">x</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">y</span><span class="hl-brackets">)
        
        </span><span class="hl-identifier">if </span><span class="hl-brackets">((</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">y</span><span class="hl-code"> + </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">height</span><span class="hl-brackets">)</span><span class="hl-code"> &gt;= </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">game_canvas</span><span class="hl-code">.</span><span class="hl-identifier">Height</span><span class="hl-brackets">)</span><span class="hl-default">:
            </span><span class="hl-comment">#We have hit the bottom! Game over!
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">notify_function</span><span class="hl-brackets">(</span><span class="hl-identifier">GAME_OVER</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-brackets">)
    
</span><span class="hl-reserved">class </span><span class="hl-identifier">TargetGame</span><span class="hl-brackets">(</span><span class="hl-identifier">Object</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">A Quick Target game</span><span class="hl-quotes">&quot;&quot;&quot;
    
    </span><span class="hl-reserved">def </span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">game_canvas</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Init the Target Game
        @param game_canvas - The canvas for the game.
        </span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-comment">#Create a random object
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">random</span><span class="hl-default"> = </span><span class="hl-identifier">_random</span><span class="hl-default">.</span><span class="hl-identifier">Random</span><span class="hl-brackets">()
        </span><span class="hl-comment">#Save the game canvas
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_canvas</span><span class="hl-default"> = </span><span class="hl-identifier">game_canvas
        </span><span class="hl-comment">#The score
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">score</span><span class="hl-default"> = </span><span class="hl-number">0
        </span><span class="hl-comment">#The list of targets that we have
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">targets</span><span class="hl-default"> = </span><span class="hl-brackets">[]
        </span><span class="hl-comment">#Make sure that we have the minimum number of targets
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">ensure_targets</span><span class="hl-brackets">()
        </span><span class="hl-comment">#hook into the keydown event
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_canvas</span><span class="hl-default">.</span><span class="hl-identifier">KeyDown</span><span class="hl-default"> += </span><span class="hl-identifier">EventHandler</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">on_key_down</span><span class="hl-brackets">)
        </span><span class="hl-comment">#Not done yet
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">done</span><span class="hl-default"> = </span><span class="hl-reserved">False
    
    def </span><span class="hl-identifier">on_key_down</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">sender</span><span class="hl-code">, </span><span class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Check to see if they hit escape</span><span class="hl-quotes">&quot;&quot;&quot;

        </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">e</span><span class="hl-code">.</span><span class="hl-identifier">Key</span><span class="hl-code"> == </span><span class="hl-number">8</span><span class="hl-brackets">)</span><span class="hl-default">:
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_over</span><span class="hl-brackets">()
    
    </span><span class="hl-reserved">def </span><span class="hl-identifier">game_over</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Called when the game should end</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_over_text</span><span class="hl-default"> = </span><span class="hl-identifier">TextBlock</span><span class="hl-brackets">()
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_over_text</span><span class="hl-default">.</span><span class="hl-identifier">FontSize</span><span class="hl-default"> = </span><span class="hl-number">50
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_over_text</span><span class="hl-default">.</span><span class="hl-identifier">Text</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">GAME OVER</span><span class="hl-quotes">&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_canvas</span><span class="hl-default">.</span><span class="hl-identifier">Children</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">game_over_text</span><span class="hl-brackets">)
        </span><span class="hl-identifier">wpf</span><span class="hl-default">.</span><span class="hl-identifier">SetPosition</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">game_over_text</span><span class="hl-code">
            , </span><span class="hl-number">100</span><span class="hl-code">
            , </span><span class="hl-number">100</span><span class="hl-brackets">)
        </span><span class="hl-comment">#We are done
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">done</span><span class="hl-default"> = </span><span class="hl-reserved">True
            
    def </span><span class="hl-identifier">ensure_targets</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-comment">#Make sure that we have enough targets
        </span><span class="hl-identifier">while </span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">targets</span><span class="hl-brackets">)</span><span class="hl-code"> &lt; </span><span class="hl-identifier">NUM_TARGETS</span><span class="hl-brackets">)</span><span class="hl-default">:
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">targets</span><span class="hl-default">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">create_target</span><span class="hl-brackets">())
            
    </span><span class="hl-reserved">def </span><span class="hl-identifier">create_target</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-comment">#Create a new target and return it
        
        #Init to random size and position
        </span><span class="hl-identifier">x</span><span class="hl-default"> = </span><span class="hl-builtin">int</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-brackets">()</span><span class="hl-code">*</span><span class="hl-number">400</span><span class="hl-brackets">)
        </span><span class="hl-identifier">width</span><span class="hl-default"> = </span><span class="hl-builtin">int</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-brackets">()</span><span class="hl-code">*</span><span class="hl-number">30</span><span class="hl-brackets">)</span><span class="hl-default"> + </span><span class="hl-number">20
        </span><span class="hl-identifier">height</span><span class="hl-default"> = </span><span class="hl-builtin">int</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-brackets">()</span><span class="hl-code">*</span><span class="hl-number">30</span><span class="hl-brackets">)</span><span class="hl-default"> + </span><span class="hl-number">20
        </span><span class="hl-identifier">y</span><span class="hl-default"> = </span><span class="hl-number">0</span><span class="hl-default"> - </span><span class="hl-identifier">height
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Init the velocity to a random number based off of the 
        score so that more targets are hit, the game gets harder.</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">velocity</span><span class="hl-default"> = </span><span class="hl-builtin">int</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-code">.</span><span class="hl-identifier">random</span><span class="hl-brackets">()</span><span class="hl-code">*</span><span class="hl-brackets">(</span><span class="hl-builtin">int</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">score</span><span class="hl-code"> / </span><span class="hl-number">15</span><span class="hl-brackets">)</span><span class="hl-code">+</span><span class="hl-number">1</span><span class="hl-brackets">))
        </span><span class="hl-comment">#The minimum velocity is 1
        </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">velocity</span><span class="hl-code">&lt;=</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-default">:
            </span><span class="hl-identifier">velocity</span><span class="hl-default"> = </span><span class="hl-number">1
    
        </span><span class="hl-reserved">return </span><span class="hl-identifier">TargetSprite</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">on_target_event</span><span class="hl-code">
                , </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">game_canvas</span><span class="hl-code">, </span><span class="hl-identifier">x</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-code">,</span><span class="hl-identifier">width</span><span class="hl-code">, </span><span class="hl-identifier">height</span><span class="hl-code">
                , </span><span class="hl-identifier">velocity</span><span class="hl-brackets">)
                
    </span><span class="hl-reserved">def </span><span class="hl-identifier">on_target_event</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">event</span><span class="hl-code">, </span><span class="hl-identifier">target_sprite</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Called when an event is sent from the target
        sprite
        @param event - number - What has happened?
        @param target_sprite -TargetSprite - The sprite that
        has sent this event</span><span class="hl-quotes">&quot;&quot;&quot;
        
        </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">done</span><span class="hl-code">==</span><span class="hl-reserved">True</span><span class="hl-brackets">)</span><span class="hl-default">:
            </span><span class="hl-comment">#We are already done don't bother
            </span><span class="hl-reserved">return
        
        </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code"> == </span><span class="hl-identifier">GAME_OVER</span><span class="hl-brackets">)</span><span class="hl-default">:
            </span><span class="hl-comment">#The game is over!
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_over</span><span class="hl-brackets">()
        </span><span class="hl-identifier">elif </span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code"> == </span><span class="hl-identifier">TARGET_HIT</span><span class="hl-brackets">)</span><span class="hl-default">:
            </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">A target has been hit
            Remove the hit target from the list and from
            the canvass</span><span class="hl-quotes">&quot;&quot;&quot;
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">targets</span><span class="hl-default">.</span><span class="hl-identifier">remove</span><span class="hl-brackets">(</span><span class="hl-identifier">target_sprite</span><span class="hl-brackets">)
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_canvas</span><span class="hl-default">.</span><span class="hl-identifier">Children</span><span class="hl-default">.</span><span class="hl-identifier">Remove</span><span class="hl-brackets">(</span><span class="hl-identifier">target_sprite</span><span class="hl-code">.</span><span class="hl-identifier">ellipse</span><span class="hl-brackets">)
            </span><span class="hl-comment">#Update the score and the score text
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">score</span><span class="hl-default"> += </span><span class="hl-number">1
            </span><span class="hl-identifier">Score</span><span class="hl-default">.</span><span class="hl-identifier">Text</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">Targets Hit: %d</span><span class="hl-quotes">&quot;</span><span class="hl-default"> % </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">score</span><span class="hl-brackets">)
            </span><span class="hl-comment">#Now make sure that we have enough targets
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">ensure_targets</span><span class="hl-brackets">()
            
    </span><span class="hl-reserved">def </span><span class="hl-identifier">run</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Start the game.</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-comment">#initialize the elements
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">done</span><span class="hl-default"> = </span><span class="hl-reserved">False
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">score</span><span class="hl-default"> = </span><span class="hl-number">0
        </span><span class="hl-comment">#Our storyboard timer
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">gameLoop</span><span class="hl-default"> = </span><span class="hl-identifier">wpf</span><span class="hl-default">.</span><span class="hl-identifier">Storyboard</span><span class="hl-brackets">()
        </span><span class="hl-comment">#Set the name property for the 1.1 refresh
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">gameLoop</span><span class="hl-default">.</span><span class="hl-identifier">SetValue</span><span class="hl-brackets">[</span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">String</span><span class="hl-brackets">](</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">gameLoop</span><span class="hl-code">.</span><span class="hl-identifier">NameProperty</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">GameLoop</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
        </span><span class="hl-comment">#Add to the canvas
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">game_canvas</span><span class="hl-default">.</span><span class="hl-identifier">Resources</span><span class="hl-default">.</span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">gameLoop</span><span class="hl-brackets">)
        </span><span class="hl-comment">#Add the completed event handler
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">gameLoop</span><span class="hl-default">.</span><span class="hl-identifier">Completed</span><span class="hl-default"> += </span><span class="hl-identifier">EventHandler</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">on_game_timer</span><span class="hl-brackets">)
        </span><span class="hl-comment">#Start the game loop
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">gameLoop</span><span class="hl-default">.</span><span class="hl-identifier">Begin</span><span class="hl-brackets">()
        
    </span><span class="hl-reserved">def </span><span class="hl-identifier">on_game_timer</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">sender</span><span class="hl-code">, </span><span class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This is the main game loop that we are going to be
        using.</span><span class="hl-quotes">&quot;&quot;&quot;

        </span><span class="hl-comment">#Update the sprites
        </span><span class="hl-reserved">for </span><span class="hl-identifier">sprite </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">targets</span><span class="hl-default">:
            </span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">update</span><span class="hl-brackets">()
        </span><span class="hl-comment">#If we are not done, start the timer again    
        </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-reserved">not </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">done</span><span class="hl-brackets">)</span><span class="hl-default">:
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">gameLoop</span><span class="hl-default">.</span><span class="hl-identifier">Begin</span><span class="hl-brackets">()
            
</span><span class="hl-reserved">def </span><span class="hl-identifier">on_canvas_loaded</span><span class="hl-brackets">(</span><span class="hl-identifier">sender</span><span class="hl-code">, </span><span class="hl-identifier">e</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-identifier">target_game</span><span class="hl-default"> = </span><span class="hl-identifier">TargetGame</span><span class="hl-brackets">(</span><span class="hl-identifier">root_canvas</span><span class="hl-brackets">)
    </span><span class="hl-identifier">target_game</span><span class="hl-default">.</span><span class="hl-identifier">run</span><span class="hl-brackets">()</span></pre></div></div>
<p>All in all it&#8217;s pretty simple to create something like this using Silverlight and python.  Hopefully this tutorial provides enough information so that all of you can create your own game or application, if you do drop me a line I&#8217;d love to check it out.</p>
<p>As always if you have any questions, comments, or spot a problem with the tutorial, add a comment below.</p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2007/07/29/creating-a-game-using-python-and-silverlight-11/&title=Creating a game using python and Silverlight 1.1&srcTitle=learning python&srcURL=http://www.learningpython.com"target="_blank" rel=""><img border="0" src="http://www.learningpython.com/wp-content/plugins/wp-google-buzz/icon/12.png" style="opacity:1;filter:alpha(opacity=100)" onmouseover="this.style.opacity=0.8;this.filters.alpha.opacity=70" onmouseout="this.style.opacity=1;this.filters.alpha.opacity=100"/> </a></div>]]></content:encoded>
			<wfw:commentRss>http://www.learningpython.com/2007/07/29/creating-a-game-using-python-and-silverlight-11/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
	</channel>
</rss>
