<?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; pygame</title>
	<atom:link href="http://www.learningpython.com/category/python/pygame/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>TextWidget 0.1</title>
		<link>http://www.learningpython.com/2008/09/21/textwidget-01/</link>
		<comments>http://www.learningpython.com/2008/09/21/textwidget-01/#comments</comments>
		<pubDate>Sun, 21 Sep 2008 21:41:50 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[TextWidget]]></category>
		<category><![CDATA[pygame]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=99</guid>
		<description><![CDATA[
			
				
			
		
It&#8217;s been a long time since I worked on TextWidget at all, but since someone posted a question about it I decided to fix the issue and re-release the source. But since I didn&#8217;t want to simply update the blog post I decided to give the project a proper home on google code: http://code.google.com/p/textwidget/
The project [...]]]></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%2F2008%2F09%2F21%2Ftextwidget-01%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2008%2F09%2F21%2Ftextwidget-01%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>It&#8217;s been a long time since I worked on <a href="http://www.learningpython.com/2006/12/13/textwidget-a-simple-text-class-for-pygame/">TextWidget</a> at all, but since someone posted a question about it I decided to fix the issue and re-release the source. But since I didn&#8217;t want to simply update the blog post I decided to give the project a proper home on google code: <a href="http://code.google.com/p/textwidget/">http://code.google.com/p/textwidget/</a></p>
<p>The project is really simple and meant as an easy way for you to create &#8220;text buttons&#8221; for your PyGame projects.  It&#8217;s not meant to be the definitive way to do this, just a simple solution for people that just want to drop a class in and have working &#8220;text buttons&#8221;. It&#8217;s LGPL so you can use it in whatever way you want. If you do decide to use it please drop me an email and let me know.</p>
<p>For more information on how to use the project please take a look at the initial <a href="http://www.learningpython.com/2006/12/13/textwidget-a-simple-text-class-for-pygame/">blog post</a>.</p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2008/09/21/textwidget-01/&title=TextWidget 0.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/2008/09/21/textwidget-01/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Comment Moderation</title>
		<link>http://www.learningpython.com/2007/05/04/new-comment-moderation/</link>
		<comments>http://www.learningpython.com/2007/05/04/new-comment-moderation/#comments</comments>
		<pubDate>Fri, 04 May 2007 14:13:42 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[PyGTK]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[pygame]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/2007/05/04/new-comment-moderation/</guid>
		<description><![CDATA[
			
				
			
		
Hey everyone, I&#8217;ve been getting hit with a bunch of spam comments over the last few days so I&#8217;ve made it so that all comments must be moderated before they will appear.
Sorry about this, I don&#8217;t like this form of comment posting, but hopefully after a few days I will be able to switch it [...]]]></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%2F05%2F04%2Fnew-comment-moderation%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2007%2F05%2F04%2Fnew-comment-moderation%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Hey everyone, I&#8217;ve been getting hit with a bunch of spam comments over the last few days so I&#8217;ve made it so that all comments must be moderated before they will appear.</p>
<p>Sorry about this, I don&#8217;t like this form of comment posting, but hopefully after a few days I will be able to switch it back to normal.</p>
<p>And sorry about the lack of posts here, yes I am still alive, it&#8217;s just that real life has been working me pretty hard these last few weeks.</p>
<p>I&#8217;ve been playing around with my PyGTK app and a new PyGame engine that I am working on from the ground up.  Something that I can understand and that I can use.  I&#8217;ll post more information about it when there is more information&#8230;right now it does next to nothing!</p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2007/05/04/new-comment-moderation/&title=New Comment Moderation&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/05/04/new-comment-moderation/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Spanish Translations</title>
		<link>http://www.learningpython.com/2007/04/09/spanish-translations/</link>
		<comments>http://www.learningpython.com/2007/04/09/spanish-translations/#comments</comments>
		<pubDate>Mon, 09 Apr 2007 14:10:45 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[PyGTK]]></category>
		<category><![CDATA[pygame]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/2007/04/09/spanish-translations/</guid>
		<description><![CDATA[
			
				
			
		
So I&#8217;ve been working on PyGame for PyWeek lately and I&#8217;ve been having a lot of fun.  I also took a weekend off from my email so I did not notice that Lord Taran has been creating Spanish translations for a bunch of my PyGTK tutorials.
If you are interested in the Spanish tutorials check [...]]]></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%2F04%2F09%2Fspanish-translations%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2007%2F04%2F09%2Fspanish-translations%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>So I&#8217;ve been working on <a href="http://www.pygame.org/news.html">PyGame</a> for <a href="http://pyweek.org/">PyWeek</a> lately and I&#8217;ve been having a lot of fun.  I also took a weekend off from my email so I did not notice that <a href="http://lordtaran.eniac2000.com/">Lord Taran</a> has been creating Spanish translations for a bunch of my PyGTK tutorials.</p>
<p>If you are interested in the Spanish tutorials <a href="http://lordtaran.eniac2000.com/">check them out</a>, or if you are interested in seeing what happened at pyweek and the car crash that was my entry <a href="http://pyweek.org/">check it out</a> as well.</p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2007/04/09/spanish-translations/&title=Spanish Translations&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/04/09/spanish-translations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TextWidget &#8211; A simple text class for PyGame</title>
		<link>http://www.learningpython.com/2006/12/13/textwidget-a-simple-text-class-for-pygame/</link>
		<comments>http://www.learningpython.com/2006/12/13/textwidget-a-simple-text-class-for-pygame/#comments</comments>
		<pubDate>Thu, 14 Dec 2006 03:47:28 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[TextWidget]]></category>
		<category><![CDATA[pygame]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[reference]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=49</guid>
		<description><![CDATA[
			
				
			
		
Introduction
All right, this is just a little tutorial about working with text in pygame.  Now, this isn&#8217;t the only way to work with text, there are many other methods to do this, in fact much of the time you&#8217;ll probably end up using images for interactive text.  So this is mainly meant to [...]]]></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%2F2006%2F12%2F13%2Ftextwidget-a-simple-text-class-for-pygame%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F12%2F13%2Ftextwidget-a-simple-text-class-for-pygame%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<h2>Introduction</h2>
<p>All right, this is just a little tutorial about working with text in pygame.  Now, this isn&#8217;t the only way to work with text, there are many other methods to do this, in fact much of the time you&#8217;ll probably end up using images for interactive text.  So this is mainly meant to serve as a informative guide to using the text features in PyGame, and how you <em>might</em> want to implement them.</p>
<p>The TextWidget object discussed in this tutorial can be used to make something that looks like this (but you&#8217;d probably want to use better looking colours):</p>
<p><embed src="http://www.learningpython.com/flash/textwidget.swf" width="320" height="240" play="true" loop="True" quality="low" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"><br />
</embed></p>
<p>The full source and necessary files for this tutorial can be downloaded <a href="http://www.learningpython.com/sources/TextWidget.tar.gz">here</a>.</p>
<p>So, in order to make this easy to use and very reusable we&#8217;re going to create a class called TextWidget in a file called TextWidget.py.  The top of the file is full of the standard python initialization:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#! /usr/bin/env python

</span><span class="hl-reserved">import </span><span class="hl-identifier">pygame

TEXT_WIDGET_CLICK</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">locals</span><span class="hl-default">.</span><span class="hl-identifier">USEREVENT</span><span class="hl-default"> + </span><span class="hl-number">1</span></pre></div></div>
<p>We import pygame and then we set a define TEXT_WIDGET_CLICK , which will be used later on as the event type when the TextWidget is clicked on.</p>
<p>The next thing to do is define the actual class:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">TextWidget</span><span class="hl-brackets">(</span><span class="hl-identifier">object</span><span class="hl-brackets">)</span><span class="hl-default">:</span></pre></div></div>
<p>Now the astute among you will recognize that this is a &#8220;new-style&#8221; python class (i.e. it&#8217;s base class is the object class) rather then a classic class.  I did this for two reasons:</p>
<ol>
<li>I wanted to be able to control what happens when people set values in the class.  So, for example, if you set the size of the font to be something, I wanted the display to automatically adjust to reflect the new size.</li>
<li>I haven&#8217;t really used new-style classes before so I thought I&#8217;d try them out.</li>
</ol>
<p><strong>Note:</strong> If you are unfamiliar with properties or new-style classes you might want to give this a read: </p>
<ul>
<li><a href="http://www.python.org/download/releases/2.2.3/descrintro">Unifying types and classes in Python 2.2</a></li>
<li><a href="http://www.geocities.com/foetsch/python/new_style_classes.htm#property">Introduction To New-Style Classes In Python</a></li>
</ul>
<p><span id="more-49"></span></p>
<p>As a result of number one above I&#8217;m going to use <a href="http://www.python.org/download/releases/2.2.3/descrintro/#property">properties</a> for those values that I want to perform some &#8220;automatic processing&#8221; when their values changes.</p>
<p>The properties that I am going to define are as follows:</p>
<ul>
<li>text &#8211; The string of text that the TextWidget will display.</li>
<li>colour &#8211; The colour that the text will be rendered in.</li>
<li>size &#8211; The size of the font.</li>
<li>font_filename &#8211; The filename of the font to use for the text (probably something returned by <a href="http://www.pygame.org/docs/ref/font.html#pygame.font.get_fonts">pygame.font.get_fonts</a> or <a href="http://www.pygame.org/docs/ref/font.html#pygame.font.match_font">pygame.font.match_font</a>.</li>
<li>highlight &#8211; A boolean, whether the font is highlighted or not. (i.e. whether the mouse is over the TextWidget.</li>
<li>highlight_cursor &#8211; A boolean that controls whether or not to show a hand cursor when the TextWidget is in highlight mode.  If the TextWidget is not supposed to be interactive you should set this to False.</li>
</ul>
<p>There are also a few other important members of the class, but since they are not python properties I have listed them separately:</p>
<ul>
<li>__hand &#8211; A hand cursor, which can optionally by displayed when the cursor hovers over the text.</li>
<li>dirty &#8211; A boolean, that lets the TextWidget know whether or not it needs to draw.  If dirty = False it will not draw, if dirty = True it will draw.</li>
<li>bold_rect &#8211; pygame.Rect &#8211; A rectangle that is equal to the size of the highlighted text.  It is used so that when we move from highlighted to non-highlighted text all of the text is erased.  Otherwise if we just drew updated the normal rectangle, all of the highlighted text that was outside of it would not be redrawn.</li>
<li>rect – pygame.Rect – The Rectangle where the text will be drawn.  Use this to position your text, but its height and width will always be controlled by the text and the font.</li>
<li>highlight_increase &#8211; Boolean whether or not the text should increase in size when the cursor is over it.  If the text is &#8220;non-interactive&#8221; then this should be False.</li>
<li>tracking &#8211; Boolean whether or not the TextWidget is tracking a mouse click.  Used so that we can tell whether or not a true &#8220;click&#8221; has occurred.  A true click is one where both the mouse-down and mouse-up events occur on the TextWidget.</li>
</ul>
<p>To hopefully make the functionality of the TextWidget a bit clearer here is some non-highlighted text and then some highlighted text:</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/text_01.png" alt="Python pygame text" border="0" /></p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/text_02.png" alt="Python pygame text" border="0" /></p>
<h2>The Code</h2>
<p>Now we get to the heart of the actual python\PyGame code, where we actually create a working TextWidget object.</p>
<p>So we&#8217;ll start off at the top of the file and then work out way downwards in a linear manner.</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">TextWidget</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">This is a helper class for handling text in PyGame.  It performs 
    some basic highlighting and tells you when the text has been clicked.
    This is just one of the many ways to handle your text.
    This is a new-style class and I am somewhat new to them so hopefully it 
    all works.
    </span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-comment">#Hand Cursor
    </span><span class="hl-identifier">__hand_cursor_string</span><span class="hl-default"> = </span><span class="hl-brackets">(
    </span><span class="hl-quotes">&quot;</span><span class="hl-string">     XX         </span><span class="hl-quotes">&quot;</span><span class="hl-code">,
    </span><span class="hl-quotes">&quot;</span><span class="hl-string">    X..X        </span><span class="hl-quotes">&quot;</span><span class="hl-code">,
    </span><span class="hl-quotes">&quot;</span><span class="hl-string">    X..X        </span><span class="hl-quotes">&quot;</span><span class="hl-code">,
    </span><span class="hl-quotes">&quot;</span><span class="hl-string">    X..X        </span><span class="hl-quotes">&quot;</span><span class="hl-code">,
    </span><span class="hl-quotes">&quot;</span><span class="hl-string">    X..XXXXX    </span><span class="hl-quotes">&quot;</span><span class="hl-code">,
    </span><span class="hl-quotes">&quot;</span><span class="hl-string">    X..X..X.XX  </span><span class="hl-quotes">&quot;</span><span class="hl-code">,
    </span><span class="hl-quotes">&quot;</span><span class="hl-string"> XX X..X..X.X.X </span><span class="hl-quotes">&quot;</span><span class="hl-code">,
    </span><span class="hl-quotes">&quot;</span><span class="hl-string">X..XX.........X </span><span class="hl-quotes">&quot;</span><span class="hl-code">,
    </span><span class="hl-quotes">&quot;</span><span class="hl-string">X...X.........X </span><span class="hl-quotes">&quot;</span><span class="hl-code">,
    </span><span class="hl-quotes">&quot;</span><span class="hl-string"> X.....X.X.X..X </span><span class="hl-quotes">&quot;</span><span class="hl-code">,
    </span><span class="hl-quotes">&quot;</span><span class="hl-string">  X....X.X.X..X </span><span class="hl-quotes">&quot;</span><span class="hl-code">,
    </span><span class="hl-quotes">&quot;</span><span class="hl-string">  X....X.X.X.X  </span><span class="hl-quotes">&quot;</span><span class="hl-code">,
    </span><span class="hl-quotes">&quot;</span><span class="hl-string">   X...X.X.X.X  </span><span class="hl-quotes">&quot;</span><span class="hl-code">,
    </span><span class="hl-quotes">&quot;</span><span class="hl-string">    X.......X   </span><span class="hl-quotes">&quot;</span><span class="hl-code">,
    </span><span class="hl-quotes">&quot;</span><span class="hl-string">     X....X.X   </span><span class="hl-quotes">&quot;</span><span class="hl-code">,
    </span><span class="hl-quotes">&quot;</span><span class="hl-string">     XXXXX XX   </span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
    </span><span class="hl-identifier">__hcurs</span><span class="hl-default">, </span><span class="hl-identifier">__hmask</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">cursors</span><span class="hl-default">.</span><span class="hl-builtin">compile</span><span class="hl-brackets">(</span><span class="hl-identifier">__hand_cursor_string</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">.</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">X</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
    </span><span class="hl-identifier">__hand</span><span class="hl-default"> = </span><span class="hl-brackets">((</span><span class="hl-number">16</span><span class="hl-code">, </span><span class="hl-number">16</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-number">5</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-identifier">__hcurs</span><span class="hl-code">, </span><span class="hl-identifier">__hmask</span><span class="hl-brackets">)</span></pre></div></div>
<p>The above code is where we create the hand cursor that we will use if the person turns on the highlight_cursor.  We first define what the cursor will look like as explained in the <a href="http://www.pygame.org/docs/ref/cursors.html#pygame.cursors.compile">PyGame cursor documentation</a>.</p>
<p>The next step is to create all of our properties, now this code may seem a bit complicated, especially since it deals with functions that we have not created yet, but as I explain it, it could become clearer and clearer.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#Text
</span><span class="hl-reserved">def </span><span class="hl-identifier">__get_text</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-reserved">return </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">__m_text
</span><span class="hl-reserved">def </span><span class="hl-identifier">__set_text</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">text</span><span class="hl-brackets">)</span><span class="hl-default">:
    </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">__m_text</span><span class="hl-code"> != </span><span class="hl-identifier">text</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">__m_text</span><span class="hl-default"> = </span><span class="hl-identifier">text
        self</span><span class="hl-default">.</span><span class="hl-identifier">update_surface</span><span class="hl-brackets">()
</span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-builtin">property</span><span class="hl-brackets">(</span><span class="hl-identifier">__get_text</span><span class="hl-code">, </span><span class="hl-identifier">__set_text</span><span class="hl-brackets">)</span></pre></div></div>
<p>The above code creates the TextWidget.text property.  The __get_text() function is used to get the text property which is represented by the __m_text variable, and the __set_text() function is the function that is called when you want to set the value of the text property.</p>
<p>What you will notice about the __set_text() function is that it first tests to see if the text that we are setting (text) is any different then the current text (__m_text) if it isn&#8217;t any different we don&#8217;t do anything.  </p>
<p>However if it is different we set __m_text and we call update_surface().  update_surface() is a member function that I will explain in more detail later, but for now all you need to know is that it updates what the widget will display.  So since we changed the text we want the widget to update and display the new text.</p>
<p>You can probably see why I wanted to use new-style classes and properties now.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#Colour
</span><span class="hl-reserved">def </span><span class="hl-identifier">__get_colour</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-reserved">return </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">__m_colour
</span><span class="hl-reserved">def </span><span class="hl-identifier">__set_colour</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">colour</span><span class="hl-brackets">)</span><span class="hl-default">:
    </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">__m_colour</span><span class="hl-code"> != </span><span class="hl-identifier">colour</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">__m_colour</span><span class="hl-default"> = </span><span class="hl-identifier">colour
        self</span><span class="hl-default">.</span><span class="hl-identifier">update_surface</span><span class="hl-brackets">()
</span><span class="hl-identifier">colour</span><span class="hl-default"> = </span><span class="hl-builtin">property</span><span class="hl-brackets">(</span><span class="hl-identifier">__get_colour</span><span class="hl-code">, </span><span class="hl-identifier">__set_colour</span><span class="hl-brackets">)</span></pre></div></div>
<p>Next is the colour property, as you can see it&#8217;s basically identical to the text property, so I won&#8217;t bother explaining it.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#Size
</span><span class="hl-reserved">def </span><span class="hl-identifier">__get_size</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-reserved">return </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">__m_size
</span><span class="hl-reserved">def </span><span class="hl-identifier">__set_size</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">size</span><span class="hl-brackets">)</span><span class="hl-default">:
    </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">__m_size</span><span class="hl-code"> != </span><span class="hl-identifier">size</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">__m_size</span><span class="hl-default"> = </span><span class="hl-identifier">size
        self</span><span class="hl-default">.</span><span class="hl-identifier">create_font</span><span class="hl-brackets">()
</span><span class="hl-identifier">size</span><span class="hl-default"> = </span><span class="hl-builtin">property</span><span class="hl-brackets">(</span><span class="hl-identifier">__get_size</span><span class="hl-code">, </span><span class="hl-identifier">__set_size</span><span class="hl-brackets">)</span></pre></div></div>
<p>The next property is the size property, which controls the size of the font.  Now this is slightly different in that it calls the create_font() function rather then the update_surface() function that the text and the colour property did. </p>
<p>The reason for this is that the size property is used in the creation of the actual font object that is used to update the surface which is eventually drawn to the screen.  As with the update_surface() function I will explain the create_font() function later.  Properties that call create_font() do not have to call update_surface() because create_font() calls update_surface().</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#Font Filename
</span><span class="hl-reserved">def </span><span class="hl-identifier">__get_font_filename</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-reserved">return </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">__m_font_filename
</span><span class="hl-reserved">def </span><span class="hl-identifier">__set_font_filename</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">font_filename</span><span class="hl-brackets">)</span><span class="hl-default">:
    </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">__m_font_filename</span><span class="hl-code"> != </span><span class="hl-identifier">font_filename</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">__m_font_filename</span><span class="hl-default"> = </span><span class="hl-identifier">font_filename
        self</span><span class="hl-default">.</span><span class="hl-identifier">create_font</span><span class="hl-brackets">()
</span><span class="hl-identifier">font_filename</span><span class="hl-default"> = </span><span class="hl-builtin">property</span><span class="hl-brackets">(</span><span class="hl-identifier">__get_font_filename</span><span class="hl-code">, </span><span class="hl-identifier">__set_font_filename</span><span class="hl-brackets">)</span></pre></div></div>
<p>Then we have the font_filename property that represents the full path to the font file.  So if that changes we need to recreate the font in order to display the text using the correct font.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#Highlight
</span><span class="hl-reserved">def </span><span class="hl-identifier">__get_highlight</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-reserved">return </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">__m_highlight
</span><span class="hl-reserved">def </span><span class="hl-identifier">__set_highlight</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">highlight</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">not</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">__m_highlight</span><span class="hl-code"> == </span><span class="hl-identifier">highlight</span><span class="hl-brackets">))</span><span class="hl-default">:
        </span><span class="hl-comment">#Save the bold_rect
        </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">__m_highlight</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">bold_rect</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">rect
        self</span><span class="hl-default">.</span><span class="hl-identifier">__m_highlight</span><span class="hl-default"> = </span><span class="hl-identifier">highlight
        </span><span class="hl-comment">#update the cursor
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">update_cursor</span><span class="hl-brackets">()
        </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">highlight</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">size</span><span class="hl-default"> += </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">highlight_increase
        </span><span class="hl-reserved">else</span><span class="hl-default">:
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">size</span><span class="hl-default"> -= </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">highlight_increase
        if </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">highlight_increase</span><span class="hl-code"> == </span><span class="hl-number">0</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">create_font</span><span class="hl-brackets">()
</span><span class="hl-identifier">highlight</span><span class="hl-default"> = </span><span class="hl-builtin">property</span><span class="hl-brackets">(</span><span class="hl-identifier">__get_highlight</span><span class="hl-code">, </span><span class="hl-identifier">__set_highlight</span><span class="hl-brackets">)</span></pre></div></div>
<p>Next we have the highlight property, the highlight property is a boolean property that controls whether or not the TextWidget should display &#8220;highlighted&#8221; or not.  When a TextWidget is highlighted the font will be displayed in bold, have its size increased by a specific amount, and (if enabled) the hand cursor will be displayed.</p>
<p>So now since the __get_highlight() function is the same as all of the others, I&#8217;ll just explain the __set_highlight() function.</p>
<p>First we check to see that the new value is different then the old value.  We do this so that we don&#8217;t waste time with useless font creations or extra draws.  </p>
<p>Then we check to see if the current state of the TextWidget is highlighted, because if it is, we know that it is about to go back to normal (because of the first if.)  If that is the case we save the current rect as bold_rect so that the next time we draw we will want bold_rect to be drawn so that all of the previous highlighted text is overwritten.</p>
<p>Then we set the internal highlight value and call the update_cursor() function which will decide whether or not the cursor should be a hand or the default arrow.  </p>
<p>Then we set the size of the font depending on whether or not the highlight state has been turned on.  If it is we increase the size, and if it is not we decrease the size back down to normal.  </p>
<p>Since increasing or decreasing the size will call create_font() we only need to call create_font() if self.highlight_increase is equal to zero, since in that case the value of the size property will not change and the font will not be created. </p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#Show Highlight Cursor
</span><span class="hl-reserved">def </span><span class="hl-identifier">__get_highlight_cursor</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-reserved">return </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">__m_highlight_cursor
</span><span class="hl-reserved">def </span><span class="hl-identifier">__set_highlight_cursor</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">highlight_cursor</span><span class="hl-brackets">)</span><span class="hl-default">:
    </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">__m_highlight_cursor</span><span class="hl-code"> != </span><span class="hl-identifier">highlight_cursor</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">__m_highlight_cursor</span><span class="hl-default"> = </span><span class="hl-identifier">highlight_cursor
        self</span><span class="hl-default">.</span><span class="hl-identifier">update_cursor</span><span class="hl-brackets">()
</span><span class="hl-identifier">highlight_cursor</span><span class="hl-default"> = </span><span class="hl-builtin">property</span><span class="hl-brackets">(</span><span class="hl-identifier">__get_highlight_cursor</span><span class="hl-code">, </span><span class="hl-identifier">__set_highlight_cursor</span><span class="hl-brackets">)</span></pre></div></div>
<p>Lastly we have the highlight_cursor property, which is a boolean property that  controls whether or not the highlight (hand) cursor should be displayed.  If you have made it this far and have understood the rest of the properties then understanding this property shouldn&#8217;t be too difficult.  Basically it simply checks to see if we should update the property, and if so it updates it and then calls update_cursor() just in case we need the cursor to update right now.</p>
<div class="hl-surround" style="height:280px;"><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">text</span><span class="hl-code">=</span><span class="hl-quotes">&quot;&quot;</span><span class="hl-code">, </span><span class="hl-identifier">colour</span><span class="hl-code">=</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-identifier">size</span><span class="hl-code">=</span><span class="hl-number">32</span><span class="hl-code">
             , </span><span class="hl-identifier">highlight_increase</span><span class="hl-code"> = </span><span class="hl-number">20</span><span class="hl-code">, </span><span class="hl-identifier">font_filename</span><span class="hl-code">=</span><span class="hl-reserved">None</span><span class="hl-code">
             , </span><span class="hl-identifier">show_highlight_cursor</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-quotes">&quot;&quot;&quot;</span><span class="hl-string">Initialize the TextWidget
    @param text = &quot;&quot; - string - The text for the text widget
    @param colour = (0,0,0) - The colour of the text
    @param size = 32 - number - The size of the text
    @param highlight_increate - number - How large do we want the
    text to grow when it is highlighted?
    @param font_filename = None - string the patht to the font file
    to use, None to use the default pygame font.
    @param show_highlight_cursor = True - boolean - Whether or not to change
    the cursor when the text is highlighted.  The cursor will turn into
    a hand if this is true.
    </span><span class="hl-quotes">&quot;&quot;&quot;
    
    </span><span class="hl-comment">#inits
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">dirty</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">bold_rect</span><span class="hl-default"> = </span><span class="hl-reserved">None
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">highlight_increase</span><span class="hl-default"> = </span><span class="hl-identifier">highlight_increase
    self</span><span class="hl-default">.</span><span class="hl-identifier">tracking</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">rect</span><span class="hl-default"> = </span><span class="hl-reserved">None
    
    </span><span class="hl-comment">#property inits
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">__m_text</span><span class="hl-default"> = </span><span class="hl-reserved">None
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">__m_colour</span><span class="hl-default"> = </span><span class="hl-reserved">None
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">__m_size</span><span class="hl-default"> = </span><span class="hl-reserved">None
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">__m_font_filename</span><span class="hl-default"> = </span><span class="hl-reserved">None
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">__m_highlight</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">__m_font</span><span class="hl-default"> = </span><span class="hl-reserved">None
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">__m_highlight_cursor</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">__m_rect</span><span class="hl-default"> = </span><span class="hl-reserved">None
    
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-identifier">text
    self</span><span class="hl-default">.</span><span class="hl-identifier">colour</span><span class="hl-default"> = </span><span class="hl-identifier">colour
    self</span><span class="hl-default">.</span><span class="hl-identifier">size</span><span class="hl-default"> = </span><span class="hl-identifier">size
    self</span><span class="hl-default">.</span><span class="hl-identifier">font_filename</span><span class="hl-default"> = </span><span class="hl-identifier">font_filename
    self</span><span class="hl-default">.</span><span class="hl-identifier">highlight</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">highlight_cursor</span><span class="hl-default"> = </span><span class="hl-identifier">show_highlight_cursor
    
    self</span><span class="hl-default">.</span><span class="hl-identifier">create_font</span><span class="hl-brackets">()</span></pre></div></div>
<p>Now we actually start using the class the first thing we do is code the __init__() function in which we initialize all of our data members and properties, and then create the font at the end.  This may result in the font being create twice sometimes but it will only happen once when the Widget is created, and it makes sure that the font is always created.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">update_cursor</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-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">highlight_cursor</span><span class="hl-brackets">)</span><span class="hl-default">:
        </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">highlight</span><span class="hl-brackets">)</span><span class="hl-default">:
            </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">mouse</span><span class="hl-default">.</span><span class="hl-identifier">set_cursor</span><span class="hl-brackets">(</span><span class="hl-code">*</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">__hand</span><span class="hl-brackets">)
        </span><span class="hl-reserved">else</span><span class="hl-default">:
            </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">mouse</span><span class="hl-default">.</span><span class="hl-identifier">set_cursor</span><span class="hl-brackets">(</span><span class="hl-code">*</span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">cursors</span><span class="hl-code">.</span><span class="hl-identifier">arrow</span><span class="hl-brackets">)</span></pre></div></div>
<p>Here is the update_cursor() function, it&#8217;s pretty straight forward.  First we check whether or not we are supposed to change the cursor at all.  If we are then we check to see if we are highlighted or not.  If we are highlighted then we use the hand cursor that we created above, and if we are not highlighted we use the built-in pygame arrow cursor.</p>
<h2>Drawing and Fonts</h2>
<p>Now we finally get to drawing and the font creation that we talked about earlier.  The first thing to do is to create our font:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">create_font</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">Create the internal font, using the current settings
    </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">size</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">__m_font</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">font</span><span class="hl-default">.</span><span class="hl-identifier">Font</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">font_filename</span><span class="hl-code">
                                 , </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">size</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">update_surface</span><span class="hl-brackets">()</span></pre></div></div>
<p>As you can see this function really is pretty simple, all we need to make sure that we have is a size for the font.  Then we create the font using <a href="http://www.pygame.org/docs/ref/font.html#pygame.font.Font">pygame.font.Font()</a>  We don&#8217;t care if self.font_filename is None since if it is None, then the default font will be loaded. </p>
<p>After the font has been created we call update_surface():</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">update_surface</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">Update the current surface, basically render the
    text using the current settings.
    </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">__m_font</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">__m_font</span><span class="hl-default">.</span><span class="hl-identifier">set_bold</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">highlight</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">image</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">__m_font</span><span class="hl-default">.</span><span class="hl-identifier">render</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">text</span><span class="hl-code">
                                        , </span><span class="hl-reserved">True</span><span class="hl-code">
                                        , </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">colour</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">dirty</span><span class="hl-default"> = </span><span class="hl-reserved">True
        </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">rect</span><span class="hl-brackets">)</span><span class="hl-default">:
            </span><span class="hl-comment"># Used the current rects center point
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">image</span><span class="hl-default">.</span><span class="hl-identifier">get_rect</span><span class="hl-brackets">(</span><span class="hl-identifier">center</span><span class="hl-code">=</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">rect</span><span class="hl-code">.</span><span class="hl-identifier">center</span><span class="hl-brackets">)
        </span><span class="hl-reserved">else</span><span class="hl-default">:
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">image</span><span class="hl-default">.</span><span class="hl-identifier">get_rect</span><span class="hl-brackets">()</span></pre></div></div>
<p>In update_surfacte() we first make sure that our font (__m_font) exists, since if it does not then there is no point in creating any surface.  Then we set the font&#8217;s bold setting <a href="http://www.pygame.org/docs/ref/font.html#Font.set_bold">Font.set_bold()</a> function and controlled by the highlight property.</p>
<p>Then we render the font onto a <a href="http://www.pygame.org/docs/ref/surface.html#pygame.Surface">pygame.surface</a> using the <a href="http://www.pygame.org/docs/ref/font.html#Font.render">Font.render()</a> function and our current text and colour.  We also make sure that the font is anti-aliased by passing True as the second parameter.</p>
<p>Then we set self.Dirty to True to make sure that the next time through the draw loop the new surface gets displayed.  </p>
<p>The last thing we do in the function is move our text into the correct location.  If self.rect has been specified then we move the image into that position and set our rect, otherwise we initialize self.rect to be be surfaces rectangle.  </p>
<p>self.rect must eventually be move into some position because <a href="http://www.pygame.org/docs/ref/surface.html#Surface.get_rect">Surface.get_rect()</a> always returns a rectangle that starts at 0,0.</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">screen</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Draw yourself text widget
    @param screen - pygame.Surface - The surface that we will draw to
    @returns - pygame.rect - If drawing has occurred this is the 
    rect that we drew to.  None if no drawing has occurred.</span><span class="hl-quotes">&quot;&quot;&quot;
    
    </span><span class="hl-identifier">rect_return</span><span class="hl-default"> = </span><span class="hl-reserved">None
    </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">image</span><span class="hl-brackets">)  </span><span class="hl-identifier">and  </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">rect</span><span class="hl-brackets">) </span><span class="hl-identifier">and </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">dirty</span><span class="hl-brackets">))</span><span class="hl-default">:
        </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">bold_rect</span><span class="hl-brackets">)</span><span class="hl-default">:
            </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">We may need to overwrite the bold text size
            This gets rid of leftover text when moving from 
            bold text to non-bold text.
            </span><span class="hl-quotes">&quot;&quot;&quot;
            </span><span class="hl-identifier">rect_return</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">Rect</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">bold_rect</span><span class="hl-brackets">)
            </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Set to None, since we only need to do this
            once.</span><span class="hl-quotes">&quot;&quot;&quot;
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">bold_rect</span><span class="hl-default"> = </span><span class="hl-reserved">None
        else</span><span class="hl-default">:
            </span><span class="hl-identifier">rect_return</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">rect
        </span><span class="hl-comment">#Draw the text
        </span><span class="hl-identifier">screen</span><span class="hl-default">.</span><span class="hl-identifier">blit</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">image</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">rect</span><span class="hl-brackets">)
        </span><span class="hl-comment">#Dirty no more
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">dirty</span><span class="hl-default"> = </span><span class="hl-reserved">False

    return </span><span class="hl-identifier">rect_return</span></pre></div></div>
<p>Finally we get to the draw code, and thankfully it&#8217;s pretty simple.  The draw() function take a <a href="http://www.pygame.org/docs/ref/surface.html">pygame.Surface</a> as the only parameter, this is the surface that we will be drawing to, probably the main surface in your game or application.  The draw function will also return a  <a href="http://www.pygame.org/docs/ref/rect.html">pygame.Rect</a> object (or None) which represents the area of the surface that needs to be updated (or not).</p>
<p>The first thing we do in the function is make sure that we actually have something to draw.  We check our rectangle (self.tect) and our surface (self.image) to make sure that everything has been initialized, and then we check self.dirty to see if we <em>should</em> draw anything. </p>
<p>Then we check to see if self.bold_rect is valid, if it is we return it as the rectangle to update and invalidate the bold_rect, since it only needed once, when moving from highlighted to non-highlighted). If self.bold_rect is not valid then we will return self.rect as the rectangle to update.</p>
<p>Then we <a href="http://www.pygame.org/docs/ref/surface.html#Surface.blit">blit</a> our text to the screen (which is stored in self.image as a pygame.Surface object) and set self.dirty to False so that we do not needlessly draw in the future.</p>
<h2>Events</h2>
<p>If you remember way back at the beginning of this tutorial we create a define for a future event that would be fired when the TextWidget was clicked on:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-identifier">TEXT_WIDGET_CLICK</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">locals</span><span class="hl-default">.</span><span class="hl-identifier">USEREVENT</span><span class="hl-default"> + </span><span class="hl-number">1</span></pre></div></div>
<p>Now is where we use it and let the main class know when a TextWidget has been clicked.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">on_mouse_button_down</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-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Called by the main application when the
    MOUSEBUTTONDOWN event fires.
    @param event - Pygame Event object
    MOUSEBUTTONDOWN  pos, button
    </span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-comment">#Check for collision
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">tracking</span><span class="hl-default"> = </span><span class="hl-reserved">False
    </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">rect</span><span class="hl-code">.</span><span class="hl-identifier">collidepoint</span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">pos</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">tracking</span><span class="hl-default"> = </span><span class="hl-reserved">True</span></pre></div></div>
<p>First we have the on_mouse_button_down() function, this should be called by your main application during the event loop.  If you come across a MOUSEBUTTONDOWN event, then you should pass it to all of your TextWidget objects, or at least the TextWidgets that you want to be interactive.</p>
<p>All this function does is check to see if the mouse button down event occurred within the TextWidgets rect, if it did, self.Tracking is set to True to let the TextWidget know that it is “tracking” or listening for the MOUSEBUTTONUP event to complete the click.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">on_mouse_button_up</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-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Called by the main application when the
    MOUSEBUTTONDOWN event fires.
    @param event - Pygame Event object
    MOUSEBUTTONDOWN  pos, button
    </span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-comment">#Check for collision
    </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">tracking</span><span class="hl-brackets">) </span><span class="hl-identifier">and </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">rect</span><span class="hl-code">.</span><span class="hl-identifier">collidepoint</span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">pos</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">on_mouse_click</span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-brackets">)
    </span><span class="hl-comment">#Not Tracking anymore
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">tracking</span><span class="hl-default"> = </span><span class="hl-reserved">False</span></pre></div></div>
<p>Next we have the on_mouse_button_up function which called by your main application for all of your interactive TextWidget objects when the MOUSEBUTTONDOWN event is found.  </p>
<p>This function is very simple, it first checks to see if we are currently tracking, if we are then we check to see if the event has occurred within the TextWidget&#8217;s rect.  If it has we call self.one_mouse_click() and pass it the event.</p>
<p>Then we set self.tracking to False, since whatever happened we are no longer tracking.  Now if you don&#8217;t fully understand what the self.tracking boolean does, it simply makes sure that the TextWidget behaves like a standard button, which means that it only registers MOUSEBUTTONUP events as a &#8220;click&#8221; if the MOUSEBUTTONDOWN also occurred within the buttons rectangle.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">on_mouse_click</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-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Called by the main application when the
    MOUSEBUTTONDOWN event fires, and the text widget
    has been clicked on.  You can either let
    this post the event (default) or you can override this
    function call in your app.
    ie. myTextWidget.on_mouse_click = my_click_handler
    @param event - Pygame Event object
    MOUSEBUTTONDOWN  pos, button
    </span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-comment">#Create the TEXT_WIDGET_CLICK event
    </span><span class="hl-identifier">event_attrib</span><span class="hl-default"> = {}
    </span><span class="hl-identifier">event_attrib</span><span class="hl-brackets">[</span><span class="hl-quotes">&quot;</span><span class="hl-string">button</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">]</span><span class="hl-default"> = </span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">button
    event_attrib</span><span class="hl-brackets">[</span><span class="hl-quotes">&quot;</span><span class="hl-string">pos</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">]</span><span class="hl-default"> = </span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">pos
    event_attrib</span><span class="hl-brackets">[</span><span class="hl-quotes">&quot;</span><span class="hl-string">text_widget</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">]</span><span class="hl-default"> = </span><span class="hl-identifier">self
    e</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">Event</span><span class="hl-brackets">(</span><span class="hl-identifier">TEXT_WIDGET_CLICK</span><span class="hl-code">, </span><span class="hl-identifier">event_attrib</span><span class="hl-brackets">)
    </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">post</span><span class="hl-brackets">(</span><span class="hl-identifier">e</span><span class="hl-brackets">)</span></pre></div></div>
<p>Finally in our event handling we have the on_mouse_click() function.  By default this function will <a href="http://www.pygame.org/docs/ref/event.html#pygame.event.post">post</a> a TEXT_WIDGET_CLICK event to the PyGame event queue which you can handle in whichever manner you like.</p>
<p>The event has the following attributes:</p>
<ul>
<li>button &#8211; The button of the mouse that was used for the click.  This is the same as the MOUSEBUTTONUP event&#8217;s button attribute.</li>
<li>pos (x,y) &#8211; The position of the mouse cursor when the click occurred.  The is the same as the MOUSEBUTTONUP event&#8217;s pos attribute.</li>
<li>text_widget &#8211; The TextWidget object that was clicked on, so you know how to handle the event.</li>
</ul>
<p>That is the default way that the on_mouse_click() function functions, in general it is probably easier to override the on_mouse_click() function in all of the TextWidget instances that you want to be interactive.</p>
<p>As an example, in your main application you might use the following for an Exit TextWidget:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">exit_text</span><span class="hl-default">.</span><span class="hl-identifier">on_mouse_click</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">on_exit_clicked</span></pre></div></div>
<h2>Using the TextWidget</h2>
<p>In order to make it easy to see how to use the TextWidget object (if it is not already) I whipped up a quick sample application, the short flash capture is to show you what it looks like without having to run it:</p>
<p>
<embed src="http://www.learningpython.com/flash/textwidget.swf" width="320" height="240" play="true" loop="True" quality="low" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"><br />
</embed>
</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-reserved">import </span><span class="hl-identifier">os</span><span class="hl-default">, </span><span class="hl-identifier">sys
</span><span class="hl-reserved">import </span><span class="hl-identifier">pygame
</span><span class="hl-reserved">from </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">locals </span><span class="hl-reserved">import</span><span class="hl-default"> *

</span><span class="hl-reserved">import </span><span class="hl-identifier">TextWidget

</span><span class="hl-reserved">class </span><span class="hl-identifier">PyGameText</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">An example class used to illustrate possible ways to work
    with the TextWidget text in a pygame 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">width</span><span class="hl-code">=</span><span class="hl-number">640</span><span class="hl-code">, </span><span class="hl-identifier">height</span><span class="hl-code">=</span><span class="hl-number">480</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Initialize
        @param width=640 - The width of the pygame window
        @param height=480 - The height of the pygame window.
        </span><span class="hl-quotes">&quot;&quot;&quot;
        
        </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">init</span><span class="hl-brackets">()
        </span><span class="hl-comment">#create the screen
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">display</span><span class="hl-default">.</span><span class="hl-identifier">set_mode</span><span class="hl-brackets">((</span><span class="hl-identifier">width</span><span class="hl-code">, </span><span class="hl-identifier">height</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">) 
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">background</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">Surface</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">screen</span><span class="hl-code">.</span><span class="hl-identifier">get_size</span><span class="hl-brackets">()</span><span class="hl-code">, </span><span class="hl-identifier">SWSURFACE</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">background</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">background</span><span class="hl-default">.</span><span class="hl-identifier">convert</span><span class="hl-brackets">()
        </span><span class="hl-identifier">image</span><span class="hl-default">, </span><span class="hl-identifier">rect</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">load_image</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">background.png</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
        </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">image</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">background</span><span class="hl-default">.</span><span class="hl-identifier">blit</span><span class="hl-brackets">(</span><span class="hl-identifier">image</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-brackets">))
        </span><span class="hl-reserved">else</span><span class="hl-default">:
            </span><span class="hl-comment">#Just fill with a solid colour
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">background</span><span class="hl-default">.</span><span class="hl-identifier">fill</span><span class="hl-brackets">((</span><span class="hl-number">124</span><span class="hl-code">,</span><span class="hl-number">124</span><span class="hl-code">,</span><span class="hl-number">124</span><span class="hl-brackets">))
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default">.</span><span class="hl-identifier">blit</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">background</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-brackets">))
        
    </span><span class="hl-reserved">def </span><span class="hl-identifier">main_loop</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">The main Python loop</span><span class="hl-quotes">&quot;&quot;&quot;
        
        </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">display</span><span class="hl-default">.</span><span class="hl-identifier">update</span><span class="hl-brackets">()
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">timer</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">time</span><span class="hl-default">.</span><span class="hl-identifier">Clock</span><span class="hl-brackets">()
        
        </span><span class="hl-comment"># Text Widget list
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</span><span class="hl-default"> = </span><span class="hl-brackets">[]
        </span><span class="hl-comment">#Create our Text WIdgets
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">new_game_text</span><span class="hl-default"> = </span><span class="hl-identifier">TextWidget</span><span class="hl-default">.</span><span class="hl-identifier">TextWidget</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">New Game</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-number">200</span><span class="hl-code">,</span><span class="hl-number">200</span><span class="hl-code">,</span><span class="hl-number">200</span><span class="hl-brackets">))
        </span><span class="hl-comment">#make this the biggest text
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">new_game_text</span><span class="hl-default">.</span><span class="hl-identifier">size</span><span class="hl-default"> = </span><span class="hl-number">96
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">new_game_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">center</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default">.</span><span class="hl-identifier">get_rect</span><span class="hl-brackets">()</span><span class="hl-default">.</span><span class="hl-identifier">center
        self</span><span class="hl-default">.</span><span class="hl-identifier">new_game_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">top</span><span class="hl-default"> = </span><span class="hl-number">0</span><span class="hl-default">;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</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">new_game_text</span><span class="hl-brackets">)
        
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">high_score_text</span><span class="hl-default"> = </span><span class="hl-identifier">TextWidget</span><span class="hl-default">.</span><span class="hl-identifier">TextWidget</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">High Scores</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-number">255</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-number">64</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">high_score_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">center</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default">.</span><span class="hl-identifier">get_rect</span><span class="hl-brackets">()</span><span class="hl-default">.</span><span class="hl-identifier">center
        self</span><span class="hl-default">.</span><span class="hl-identifier">high_score_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">top</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">new_game_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">bottom</span><span class="hl-default"> + </span><span class="hl-number">30
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</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">high_score_text</span><span class="hl-brackets">)
        
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">website_text</span><span class="hl-default"> = </span><span class="hl-identifier">TextWidget</span><span class="hl-default">.</span><span class="hl-identifier">TextWidget</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Website</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-number">255</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">255</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-number">64</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">website_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">center</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default">.</span><span class="hl-identifier">get_rect</span><span class="hl-brackets">()</span><span class="hl-default">.</span><span class="hl-identifier">center
        self</span><span class="hl-default">.</span><span class="hl-identifier">website_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">top</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">high_score_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">bottom</span><span class="hl-default"> + </span><span class="hl-number">30
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</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">website_text</span><span class="hl-brackets">)
        
        </span><span class="hl-comment"># Different font for the last one, and let's make it increase
        # more
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">exit_text</span><span class="hl-default"> = </span><span class="hl-identifier">TextWidget</span><span class="hl-default">.</span><span class="hl-identifier">TextWidget</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Exit</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">255</span><span class="hl-code">,</span><span class="hl-number">255</span><span class="hl-brackets">)</span><span class="hl-code">
                                , </span><span class="hl-number">64</span><span class="hl-code">, </span><span class="hl-number">40</span><span class="hl-code">
                                , </span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">font</span><span class="hl-code">.</span><span class="hl-identifier">match_font</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">sans</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-reserved">False</span><span class="hl-code">, </span><span class="hl-reserved">True</span><span class="hl-brackets">))
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">exit_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">center</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default">.</span><span class="hl-identifier">get_rect</span><span class="hl-brackets">()</span><span class="hl-default">.</span><span class="hl-identifier">center
        self</span><span class="hl-default">.</span><span class="hl-identifier">exit_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">top</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">website_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">bottom</span><span class="hl-default"> + </span><span class="hl-number">30
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">exit_text</span><span class="hl-default">.</span><span class="hl-identifier">on_mouse_click</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">on_exit_clicked
        self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</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">exit_text</span><span class="hl-brackets">)
           
        </span><span class="hl-reserved">while </span><span class="hl-number">1</span><span class="hl-default">: 
            </span><span class="hl-comment">#Tick of the timer
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">timer</span><span class="hl-default">.</span><span class="hl-identifier">tick</span><span class="hl-brackets">()
            
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">event_loop</span><span class="hl-brackets">()
            
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">()
                
    </span><span class="hl-reserved">def </span><span class="hl-identifier">event_loop</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">Perform the event loop.</span><span class="hl-quotes">&quot;&quot;&quot;
        
        </span><span class="hl-reserved">for </span><span class="hl-identifier">event </span><span class="hl-reserved">in </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">get</span><span class="hl-brackets">()</span><span class="hl-default">:
            </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">type</span><span class="hl-code"> == </span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">QUIT</span><span class="hl-brackets">)</span><span class="hl-default">:
                </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">quit</span><span class="hl-brackets">()
                </span><span class="hl-identifier">sys</span><span class="hl-default">.</span><span class="hl-identifier">exit</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">type</span><span class="hl-code"> == </span><span class="hl-identifier">ACTIVEEVENT</span><span class="hl-brackets">)</span><span class="hl-default">:
                </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">gain</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-reserved">for </span><span class="hl-identifier">text </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</span><span class="hl-default">:
                        </span><span class="hl-identifier">text</span><span class="hl-default">.</span><span class="hl-identifier">dirty</span><span class="hl-default"> = </span><span class="hl-reserved">True
                    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">draw</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">state</span><span class="hl-code"> ==</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-default">: 
                    </span><span class="hl-comment">#We are hidden so wait for the next event
                    </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">post</span><span class="hl-brackets">(</span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">wait</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">type</span><span class="hl-code"> == </span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">MOUSEMOTION</span><span class="hl-brackets">)</span><span class="hl-default">:
                </span><span class="hl-reserved">for </span><span class="hl-identifier">text </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</span><span class="hl-default">:
                    </span><span class="hl-identifier">text</span><span class="hl-default">.</span><span class="hl-identifier">highlight</span><span class="hl-default"> = </span><span class="hl-identifier">text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">collidepoint</span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">pos</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">type</span><span class="hl-code"> == </span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">MOUSEBUTTONDOWN</span><span class="hl-brackets">)</span><span class="hl-default">:
                </span><span class="hl-reserved">for </span><span class="hl-identifier">text </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</span><span class="hl-default">:
                    </span><span class="hl-identifier">text</span><span class="hl-default">.</span><span class="hl-identifier">on_mouse_button_down</span><span class="hl-brackets">(</span><span class="hl-identifier">event</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">type</span><span class="hl-code"> == </span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">MOUSEBUTTONUP</span><span class="hl-brackets">)</span><span class="hl-default">:
                </span><span class="hl-reserved">for </span><span class="hl-identifier">text </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</span><span class="hl-default">:
                    </span><span class="hl-identifier">text</span><span class="hl-default">.</span><span class="hl-identifier">on_mouse_button_up</span><span class="hl-brackets">(</span><span class="hl-identifier">event</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">type</span><span class="hl-code"> == </span><span class="hl-identifier">TextWidget</span><span class="hl-code">.</span><span class="hl-identifier">TEXT_WIDGET_CLICK</span><span class="hl-brackets">)</span><span class="hl-default">:
                </span><span class="hl-reserved">print </span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">text_widget             
    
    </span><span class="hl-reserved">def </span><span class="hl-identifier">draw</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">Draw everything</span><span class="hl-quotes">&quot;&quot;&quot;        
        </span><span class="hl-identifier">rects</span><span class="hl-default"> = </span><span class="hl-brackets">[]
        </span><span class="hl-identifier">rects</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">timer_update</span><span class="hl-brackets">())
        </span><span class="hl-reserved">for </span><span class="hl-identifier">text </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</span><span class="hl-default">:
            </span><span class="hl-identifier">rect</span><span class="hl-default"> = </span><span class="hl-identifier">text</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">screen</span><span class="hl-brackets">)
            </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">rect</span><span class="hl-brackets">)</span><span class="hl-default">:
                </span><span class="hl-identifier">rects</span><span class="hl-default">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-identifier">rect</span><span class="hl-brackets">)
        </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">display</span><span class="hl-default">.</span><span class="hl-identifier">update</span><span class="hl-brackets">(</span><span class="hl-identifier">rects</span><span class="hl-brackets">)        
        
    </span><span class="hl-reserved">def </span><span class="hl-identifier">timer_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">Update the Timer
        returns - pygame.rect - The rect that the timer
        needs to be redrawn, or None on error</span><span class="hl-quotes">&quot;&quot;&quot;
        
        </span><span class="hl-identifier">rect_return</span><span class="hl-default"> = </span><span class="hl-reserved">None
        
        </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">font</span><span class="hl-brackets">)</span><span class="hl-default">:     
            </span><span class="hl-identifier">timer_string</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">%.2f</span><span class="hl-quotes">&quot;</span><span class="hl-default"> % </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">timer</span><span class="hl-default">.</span><span class="hl-identifier">get_fps</span><span class="hl-brackets">()
            </span><span class="hl-comment">#basic font
            </span><span class="hl-identifier">font</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">font</span><span class="hl-default">.</span><span class="hl-identifier">Font</span><span class="hl-brackets">(</span><span class="hl-reserved">None</span><span class="hl-code">, </span><span class="hl-number">36</span><span class="hl-brackets">)
            </span><span class="hl-identifier">message</span><span class="hl-default"> = </span><span class="hl-identifier">font</span><span class="hl-default">.</span><span class="hl-identifier">render</span><span class="hl-brackets">(</span><span class="hl-identifier">timer_string</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-number">147</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">16</span><span class="hl-brackets">))
            </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">message</span><span class="hl-brackets">)</span><span class="hl-default">:
                </span><span class="hl-identifier">rect_return</span><span class="hl-default"> = </span><span class="hl-identifier">message</span><span class="hl-default">.</span><span class="hl-identifier">get_rect</span><span class="hl-brackets">(</span><span class="hl-identifier">left</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-brackets">)
                </span><span class="hl-identifier">rect_return</span><span class="hl-default">.</span><span class="hl-identifier">width</span><span class="hl-default"> += </span><span class="hl-number">25
                </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default">.</span><span class="hl-identifier">blit</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">background</span><span class="hl-code">, </span><span class="hl-identifier">rect_return</span><span class="hl-brackets">)
                </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default">.</span><span class="hl-identifier">blit</span><span class="hl-brackets">(</span><span class="hl-identifier">message</span><span class="hl-code">, </span><span class="hl-identifier">rect_return</span><span class="hl-brackets">) 
        
        </span><span class="hl-reserved">return </span><span class="hl-identifier">rect_return
    
    </span><span class="hl-reserved">def </span><span class="hl-identifier">load_image</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">name</span><span class="hl-code">, </span><span class="hl-identifier">colorkey</span><span class="hl-code">=</span><span class="hl-reserved">None</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">full_path</span><span class="hl-default"> = </span><span class="hl-identifier">os</span><span class="hl-default">.</span><span class="hl-identifier">path</span><span class="hl-default">.</span><span class="hl-identifier">realpath</span><span class="hl-brackets">(</span><span class="hl-identifier">os</span><span class="hl-code">.</span><span class="hl-identifier">path</span><span class="hl-code">.</span><span class="hl-identifier">dirname</span><span class="hl-brackets">(</span><span class="hl-identifier">sys</span><span class="hl-code">.</span><span class="hl-identifier">argv</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]))
        </span><span class="hl-identifier">full_path</span><span class="hl-default"> = </span><span class="hl-identifier">os</span><span class="hl-default">.</span><span class="hl-identifier">path</span><span class="hl-default">.</span><span class="hl-identifier">join</span><span class="hl-brackets">(</span><span class="hl-identifier">full_path</span><span class="hl-code">, </span><span class="hl-identifier">name</span><span class="hl-brackets">)
        </span><span class="hl-reserved">try</span><span class="hl-default">:
            </span><span class="hl-identifier">image</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">image</span><span class="hl-default">.</span><span class="hl-identifier">load</span><span class="hl-brackets">(</span><span class="hl-identifier">full_path</span><span class="hl-brackets">)
        </span><span class="hl-reserved">except </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">error</span><span class="hl-default">, </span><span class="hl-identifier">message</span><span class="hl-default">:
            </span><span class="hl-reserved">print </span><span class="hl-quotes">'</span><span class="hl-string">Cannot load image:</span><span class="hl-quotes">'</span><span class="hl-default">, </span><span class="hl-identifier">full_path
            </span><span class="hl-reserved">return None</span><span class="hl-default">, </span><span class="hl-reserved">None
        </span><span class="hl-identifier">image</span><span class="hl-default"> = </span><span class="hl-identifier">image</span><span class="hl-default">.</span><span class="hl-identifier">convert</span><span class="hl-brackets">()
        </span><span class="hl-reserved">if </span><span class="hl-identifier">colorkey </span><span class="hl-reserved">is not None</span><span class="hl-default">:
            </span><span class="hl-reserved">if </span><span class="hl-identifier">colorkey </span><span class="hl-reserved">is</span><span class="hl-default"> -</span><span class="hl-number">1</span><span class="hl-default">:
                </span><span class="hl-identifier">colorkey</span><span class="hl-default"> = </span><span class="hl-identifier">image</span><span class="hl-default">.</span><span class="hl-identifier">get_at</span><span class="hl-brackets">((</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-brackets">))
            </span><span class="hl-identifier">image</span><span class="hl-default">.</span><span class="hl-identifier">set_colorkey</span><span class="hl-brackets">(</span><span class="hl-identifier">colorkey</span><span class="hl-code">, </span><span class="hl-identifier">RLEACCEL</span><span class="hl-brackets">)
        </span><span class="hl-reserved">return </span><span class="hl-identifier">image</span><span class="hl-default">, </span><span class="hl-identifier">image</span><span class="hl-default">.</span><span class="hl-identifier">get_rect</span><span class="hl-brackets">()
    
    </span><span class="hl-reserved">def </span><span class="hl-identifier">on_exit_clicked</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-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">quit</span><span class="hl-brackets">()
        </span><span class="hl-identifier">sys</span><span class="hl-default">.</span><span class="hl-identifier">exit</span><span class="hl-brackets">()

</span><span class="hl-reserved">if </span><span class="hl-identifier">__name__</span><span class="hl-default"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">__main__</span><span class="hl-quotes">&quot;</span><span class="hl-default">:
    </span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-identifier">PyGameText</span><span class="hl-brackets">()
    </span><span class="hl-identifier">text</span><span class="hl-default">.</span><span class="hl-identifier">main_loop</span><span class="hl-brackets">()</span></pre></div></div>
<p>Now most of the code isn&#8217;t that exciting and is a pretty basic example of a short PyGame application so I won&#8217;t explain every line of this code.  I will highlight three portions:</p>
<h3>Text Widget Creation</h3>
<p>Here is how I created the four TextWidgets in the example application, each is slightly different so you might want to look at each and try to understand what it is doing:</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-comment"># Text Widget list
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</span><span class="hl-default"> = </span><span class="hl-brackets">[]
</span><span class="hl-comment">#Create our Text WIdgets
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">new_game_text</span><span class="hl-default"> = </span><span class="hl-identifier">TextWidget</span><span class="hl-default">.</span><span class="hl-identifier">TextWidget</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">New Game</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-number">200</span><span class="hl-code">,</span><span class="hl-number">200</span><span class="hl-code">,</span><span class="hl-number">200</span><span class="hl-brackets">))
</span><span class="hl-comment">#make this the biggest text
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">new_game_text</span><span class="hl-default">.</span><span class="hl-identifier">size</span><span class="hl-default"> = </span><span class="hl-number">96
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">new_game_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">center</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default">.</span><span class="hl-identifier">get_rect</span><span class="hl-brackets">()</span><span class="hl-default">.</span><span class="hl-identifier">center
self</span><span class="hl-default">.</span><span class="hl-identifier">new_game_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">top</span><span class="hl-default"> = </span><span class="hl-number">0</span><span class="hl-default">;
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</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">new_game_text</span><span class="hl-brackets">)

</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">high_score_text</span><span class="hl-default"> = </span><span class="hl-identifier">TextWidget</span><span class="hl-default">.</span><span class="hl-identifier">TextWidget</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">High Scores</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-number">255</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-number">64</span><span class="hl-brackets">)
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">high_score_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">center</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default">.</span><span class="hl-identifier">get_rect</span><span class="hl-brackets">()</span><span class="hl-default">.</span><span class="hl-identifier">center
self</span><span class="hl-default">.</span><span class="hl-identifier">high_score_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">top</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">new_game_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">bottom</span><span class="hl-default"> + </span><span class="hl-number">30</span><span class="hl-default">;
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</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">high_score_text</span><span class="hl-brackets">)

</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">website_text</span><span class="hl-default"> = </span><span class="hl-identifier">TextWidget</span><span class="hl-default">.</span><span class="hl-identifier">TextWidget</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Website</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-number">255</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">255</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-number">64</span><span class="hl-brackets">)
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">website_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">center</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default">.</span><span class="hl-identifier">get_rect</span><span class="hl-brackets">()</span><span class="hl-default">.</span><span class="hl-identifier">center
self</span><span class="hl-default">.</span><span class="hl-identifier">website_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">top</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">high_score_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">bottom</span><span class="hl-default"> + </span><span class="hl-number">30</span><span class="hl-default">;
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</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">website_text</span><span class="hl-brackets">)

</span><span class="hl-comment"># Different font for the last one, and let's make it increase
# more
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">exit_text</span><span class="hl-default"> = </span><span class="hl-identifier">TextWidget</span><span class="hl-default">.</span><span class="hl-identifier">TextWidget</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Exit</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">255</span><span class="hl-code">,</span><span class="hl-number">255</span><span class="hl-brackets">)</span><span class="hl-code">
                        , </span><span class="hl-number">64</span><span class="hl-code">, </span><span class="hl-number">40</span><span class="hl-code">
                        , </span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">font</span><span class="hl-code">.</span><span class="hl-identifier">match_font</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">sans</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-reserved">False</span><span class="hl-code">, </span><span class="hl-reserved">True</span><span class="hl-brackets">))
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">exit_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">center</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default">.</span><span class="hl-identifier">get_rect</span><span class="hl-brackets">()</span><span class="hl-default">.</span><span class="hl-identifier">center
self</span><span class="hl-default">.</span><span class="hl-identifier">exit_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">top</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">website_text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">bottom</span><span class="hl-default"> + </span><span class="hl-number">30</span><span class="hl-default">;
</span><span class="hl-comment"># Override the on_mouse_click function
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">exit_text</span><span class="hl-default">.</span><span class="hl-identifier">on_mouse_click</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">on_exit_clicked
self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</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">exit_text</span><span class="hl-brackets">)</span></pre></div></div>
<p>In general this code is pretty simple, it mostly involves creating our TextWidget objects, settings some of their properties and then adding them to a list.  It really should be pretty self explanatory how this code works, one thing that I will point out is how the in the exit_text TextWidget the pygame.font.match_font() function is used to get the path to the font, and the on_mouse_click() function is overridden.</p>
<h3>The Event Loop</h3>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">event_loop</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">Perform the event loop.</span><span class="hl-quotes">&quot;&quot;&quot;

</span><span class="hl-reserved">for </span><span class="hl-identifier">event </span><span class="hl-reserved">in </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">get</span><span class="hl-brackets">()</span><span class="hl-default">:
    </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">type</span><span class="hl-code"> == </span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">QUIT</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">quit</span><span class="hl-brackets">()
        </span><span class="hl-identifier">sys</span><span class="hl-default">.</span><span class="hl-identifier">exit</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">type</span><span class="hl-code"> == </span><span class="hl-identifier">ACTIVEEVENT</span><span class="hl-brackets">)</span><span class="hl-default">:
        </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">gain</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-reserved">for </span><span class="hl-identifier">text </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</span><span class="hl-default">:
                </span><span class="hl-identifier">text</span><span class="hl-default">.</span><span class="hl-identifier">dirty</span><span class="hl-default"> = </span><span class="hl-reserved">True
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">draw</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">state</span><span class="hl-code"> ==</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-default">: 
            </span><span class="hl-comment">#We are hidden so wait for the next event
            </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">post</span><span class="hl-brackets">(</span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">wait</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">type</span><span class="hl-code"> == </span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">MOUSEMOTION</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-reserved">for </span><span class="hl-identifier">text </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</span><span class="hl-default">:
            </span><span class="hl-identifier">text</span><span class="hl-default">.</span><span class="hl-identifier">highlight</span><span class="hl-default"> = </span><span class="hl-identifier">text</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">collidepoint</span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">pos</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">type</span><span class="hl-code"> == </span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">MOUSEBUTTONDOWN</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-reserved">for </span><span class="hl-identifier">text </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</span><span class="hl-default">:
            </span><span class="hl-identifier">text</span><span class="hl-default">.</span><span class="hl-identifier">on_mouse_button_down</span><span class="hl-brackets">(</span><span class="hl-identifier">event</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">type</span><span class="hl-code"> == </span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">MOUSEBUTTONUP</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-reserved">for </span><span class="hl-identifier">text </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</span><span class="hl-default">:
            </span><span class="hl-identifier">text</span><span class="hl-default">.</span><span class="hl-identifier">on_mouse_button_up</span><span class="hl-brackets">(</span><span class="hl-identifier">event</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">type</span><span class="hl-code"> == </span><span class="hl-identifier">TextWidget</span><span class="hl-code">.</span><span class="hl-identifier">TEXT_WIDGET_CLICK</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-reserved">print </span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">text_widget</span></pre></div></div>
<p>The first thing you should notice is that we respond to the ACTIVEEVENT, we do this so that when the PyGame window gets focus back (after loosing it) we redraw all of the text widgets.  We force the TextWidgets to redraw by setting the dirty member to True.</p>
<p>Next we set the TextWidget.highlight property by checking for a collision with each TextWidget rect when we get the MOUSEMOTION event.</p>
<p>If you want your TextWidgets to be responsive to a click whenever you get a MOUSEBUTTONDOWN or MOUSEBUTTONUP event you can simply pass it forward to the on_mouse_button_down() and on_mouse_button_up() functions in each widget, the TextWidget will take care of the rest of the processing.</p>
<p>The example also shows how to handle the TEXT_WIDGIT_CLICK event:</p>
<div class="hl-surround" ><div class="hl-main"><pre><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">type</span><span class="hl-code"> == </span><span class="hl-identifier">TextWidget</span><span class="hl-code">.</span><span class="hl-identifier">TEXT_WIDGET_CLICK</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-reserved">print </span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">text_widget</span></pre></div></div>
<p>In the example I simply print out the TextWidget, but you could do whatever you wanted to.</p>
<h3>The Draw Loop</h3>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">draw</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">Draw everything</span><span class="hl-quotes">&quot;&quot;&quot;        
    </span><span class="hl-identifier">rects</span><span class="hl-default"> = </span><span class="hl-brackets">[]
    </span><span class="hl-identifier">rects</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">timer_update</span><span class="hl-brackets">())
    </span><span class="hl-reserved">for </span><span class="hl-identifier">text </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">text_widgets</span><span class="hl-default">:
        </span><span class="hl-identifier">rect</span><span class="hl-default"> = </span><span class="hl-identifier">text</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">screen</span><span class="hl-brackets">)
        </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">rect</span><span class="hl-brackets">)</span><span class="hl-default">:
            </span><span class="hl-identifier">rects</span><span class="hl-default">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-identifier">rect</span><span class="hl-brackets">)
    </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">display</span><span class="hl-default">.</span><span class="hl-identifier">update</span><span class="hl-brackets">(</span><span class="hl-identifier">rects</span><span class="hl-brackets">)</span></pre></div></div>
<p>The draw loop is actually pretty simple.  I use a list to keep track of rectangles that I want to update at the end of the draw using <a href="http://www.pygame.org/docs/ref/display.html#pygame.display.update">pygame.display.update()</a> function.</p>
<p>In order to draw the TextWidget&#8217;s I simply loop through the list of them and call their draw() function.  If the draw() function returns a rectangle, then I know that the TextWdiget has drawn so I add it to the list of rectangles that need to be updated.  </p>
<p>If None is returned then I know that the TextWidget has not drawn and that it does not need to be drawn. </p>
<h2>Conclusion</h2>
<p>The full source and necessary files for this tutorial can be downloaded <a href="http://www.learningpython.com/sources/TextWidget.tar.gz">here</a>.</p>
<p>So that&#8217;s it for this tutorial, as I said early on I&#8217;m not saying that this is the best or only way to work with text, I&#8217;m simply presenting this as a possible way to work with text, or as an example that might help get you started with your own text processing.</p>
<p>If you have any suggestions, problems, or notice anything that is incorrect, as always, add a comment! </p>
<p>Feel free to use this in whatever manner you want (it&#8217;s licensed under the LGPL), but if you do end up using it, I would appreciate it if you sent me an email and let me know.</p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2006/12/13/textwidget-a-simple-text-class-for-pygame/&title=TextWidget - A simple text class for PyGame&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/2006/12/13/textwidget-a-simple-text-class-for-pygame/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Novemeber</title>
		<link>http://www.learningpython.com/2006/11/04/novemeber/</link>
		<comments>http://www.learningpython.com/2006/11/04/novemeber/#comments</comments>
		<pubDate>Sat, 04 Nov 2006 17:40:14 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[PyGTK]]></category>
		<category><![CDATA[glade]]></category>
		<category><![CDATA[pygame]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/2006/11/04/novemeber/</guid>
		<description><![CDATA[
			
				
			
		
Hi everyone just a quick post to let you know that I probably won&#8217;t be posting much during this month as I have decided to participate in nanowrimo which leaves me almost no time to come up with tutorials.
I as mentioned in the past Satoshi Tanabe over at satolog has translated a few of my [...]]]></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%2F2006%2F11%2F04%2Fnovemeber%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F11%2F04%2Fnovemeber%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Hi everyone just a quick post to let you know that I probably won&#8217;t be posting much during this month as I have decided to participate in <a href="http://www.nanowrimo.org/">nanowrimo</a> which leaves me almost no time to come up with tutorials.</p>
<p>I as mentioned in the past Satoshi Tanabe over at <a href="http://po3a.blogspot.com/2006/09/pygtk.html">satolog</a> has translated a few of my tutorials over into Japanese, which is very cool.  It also turns out that someone over at <a href="http://blog.chinaunix.net">blog.chinaunix.net</a> has also translated one of my tutorials (<a href="http://www.learningpython.com/2006/05/30/building-an-application-with-pygtk-and-glade/">Building an application with PyGTK and Glade</a>) into <a href="http://blog.chinaunix.net/u/5462/showart.php?id=157696">Chinese</a>, again very cool!  Thanks a lot for the translations guys. </p>
<p>I&#8217;ve also been working on some pyGame in my spare time over the last few months, I&#8217;ve been working on a level editor and a widget set:</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/pygame/pygame_scrollbars.png" alt="PyGame Window" border="0" /></p>
<p>Of course that doesn&#8217;t show very much but it show show my scrollbars!  The problem with building the level editor is that y you always have to keep working on the smallest details (like a scrollable area0 before you can get to the real heart of the problem.  Oh well it&#8217;s till fun!</p>
<p>Talk to you all soon!</p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2006/11/04/novemeber/&title=Novemeber&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/2006/11/04/novemeber/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PyWeek!</title>
		<link>http://www.learningpython.com/2006/09/13/pyweek/</link>
		<comments>http://www.learningpython.com/2006/09/13/pyweek/#comments</comments>
		<pubDate>Thu, 14 Sep 2006 02:35:21 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[pygame]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/2006/09/13/pyweek/</guid>
		<description><![CDATA[
			
				
			
		
Recently I&#8217;ve been playing around in PyGame for the past  PyWeek and having a blast!  Sadly my PyWeek entry was mostly a bust as real life got the better of me, but it reminded my how much I enjoy python and doing simple game programming.
Hopefully I&#8217;ll continue on with both my PyGTK and [...]]]></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%2F2006%2F09%2F13%2Fpyweek%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F09%2F13%2Fpyweek%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Recently I&#8217;ve been playing around in <a href="http://www.pygame.org/news.html">PyGame</a> for the past  <a href="http://www.pyweek.org/">PyWeek</a> and having a blast!  Sadly my PyWeek entry was mostly a bust as real life got the better of me, but it reminded my how much I enjoy python and doing simple game programming.</p>
<p>Hopefully I&#8217;ll continue on with both my PyGTK and PyGame work!  If you like Python and are interested in creating games, I definately think that you should check out <a href="http://www.pygame.org/news.html">PyGame</a>.</p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2006/09/13/pyweek/&title=PyWeek!&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/2006/09/13/pyweek/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creating a Game in Python Using PyGame &#8211; Part 3 &#8211; Adding the bad guys</title>
		<link>http://www.learningpython.com/2006/04/16/creating-a-game-in-python-using-pygame-part-3-adding-the-bad-guys/</link>
		<comments>http://www.learningpython.com/2006/04/16/creating-a-game-in-python-using-pygame-part-3-adding-the-bad-guys/#comments</comments>
		<pubDate>Sun, 16 Apr 2006 22:36:46 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[pygame]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=21</guid>
		<description><![CDATA[
			
				
			
		
All right in this section of the tutorial we are going to start adding the bad guys.  If you are familiar with the changes that we made in part two it should be pretty clear to you how are are going to create these bad guys.  If you haven&#8217;t already you should check [...]]]></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%2F2006%2F04%2F16%2Fcreating-a-game-in-python-using-pygame-part-3-adding-the-bad-guys%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F04%2F16%2Fcreating-a-game-in-python-using-pygame-part-3-adding-the-bad-guys%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>All right in this section of the tutorial we are going to start adding the bad guys.  If you are familiar with the changes that we made in part two it should be pretty clear to you how are are going to create these bad guys.  If you haven&#8217;t already you should check out <a href="http://www.learningpython.com/2006/03/12/creating-a-game-in-python-using-pygame-part-one/">part one</a> and <a href="http://www.learningpython.com/2006/03/19/creating-a-game-in-python-using-pygame-part-two-creating-a-level/">part two</a>.  If you would like the full source and all of the images you can get it <a href="http://www.learningpython.com/sources/PyMan_3.zip">here</a>.</p>
<p>First we are going have to create a new class based on our basicSprite class.  I created a new file called basicMonster.py and inside of that I created a class called Monster:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">Monster</span><span class="hl-brackets">(</span><span class="hl-identifier">basicSprite</span><span class="hl-code">.</span><span class="hl-identifier">Sprite</span><span class="hl-brackets">)</span><span class="hl-default">:</span></pre></div></div>
<p>For now we&#8217;re not going to go too crazy in our enemy AI, we&#8217;re just going to get them to choose a random direction to move and move in that direction until they hit a wall.   We&#8217;ll also add in another option where after a certain amount of moves the monster will choose another random direction.  This is just to add a little but more randomness into the Monster, it&#8217;s not really what we&#8217;d like to use at the end of the day but it works out all right.</p>
<p>Now we&#8217;ll take a look at the Monsters __init__ function:</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">centerPoint</span><span class="hl-code">, </span><span class="hl-identifier">image</span><span class="hl-code">, </span><span class="hl-identifier">scared_image</span><span class="hl-code">=</span><span class="hl-reserved">None</span><span class="hl-brackets">)</span><span class="hl-default">:
    
    </span><span class="hl-identifier">basicSprite</span><span class="hl-default">.</span><span class="hl-identifier">Sprite</span><span class="hl-default">.</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">centerPoint</span><span class="hl-code">, </span><span class="hl-identifier">image</span><span class="hl-brackets">)
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Save the original rect</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">original_rect</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">Rect</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">rect</span><span class="hl-brackets">)
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">normal_image</span><span class="hl-default"> = </span><span class="hl-identifier">image
    </span><span class="hl-reserved">if </span><span class="hl-identifier">scared_image</span><span class="hl-default"> !=</span><span class="hl-reserved">None</span><span class="hl-default">:
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">scared_image</span><span class="hl-default"> = </span><span class="hl-identifier">scared_image
    </span><span class="hl-reserved">else</span><span class="hl-default">:
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">scared_image</span><span class="hl-default"> = </span><span class="hl-identifier">image
    
    self</span><span class="hl-default">.</span><span class="hl-identifier">scared</span><span class="hl-default"> = </span><span class="hl-reserved">False
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Initialize the direction</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">direction</span><span class="hl-default"> = </span><span class="hl-identifier">random</span><span class="hl-default">.</span><span class="hl-identifier">randint</span><span class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-code">,</span><span class="hl-number">4</span><span class="hl-brackets">)
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">dist</span><span class="hl-default"> = </span><span class="hl-number">3
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">moves</span><span class="hl-default"> = </span><span class="hl-identifier">random</span><span class="hl-default">.</span><span class="hl-identifier">randint</span><span class="hl-brackets">(</span><span class="hl-number">100</span><span class="hl-code">,</span><span class="hl-number">200</span><span class="hl-brackets">)
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">moveCount</span><span class="hl-default"> = </span><span class="hl-number">0</span><span class="hl-default">;</span></pre></div></div>
<p><span id="more-21"></span></p>
<p>You can see that there is a bit more happening here then there is in out basic sprites.  The first thing you&#8217;ll notice is the fact that the Monster is going to have two images, a normal image and a scared image.  The scared image is what we will be drawn as the monster when the snake is in it&#8217;s &#8220;super&#8221; state, which is caused when it eats a super pellet. </p>
<p>You&#8217;ll also notice that we save a copy of the original rect, the reason that we do this is because that is where the Monster will go if it has been eaten by the snake.  This probably isn&#8217;t how we will want to handle it in the end, but it&#8217;s a pretty good start.</p>
<p>Then we start laying out the Monster AI, the first thing to do is choose an initial direction:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">direction</span><span class="hl-default"> = </span><span class="hl-identifier">random</span><span class="hl-default">.</span><span class="hl-identifier">randint</span><span class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-code">,</span><span class="hl-number">4</span><span class="hl-brackets">)</span></pre></div></div>
<p>The direction is simply a random integer between 1 and 4, each integer being a direction from the Monster to travel in (1=left, 2=up, 3=right, and 4=down) the monster will then travel self.dist pixels every-time it is updated until it hits a wall, at which point it will choose another random direction.</p>
<p>After that we initialize the idea of moves and moveCount, this is the second degree of randomness that we add into the Monster.  The idea is that after 100-200 moves the Monster will choose another random direction.</p>
<p>If you&#8217;ve read part 2 of this PyGame series the structure of our Monsters update function should come as no surprise:</p>
<div class="hl-surround" style="height:280px;"><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-code">,</span><span class="hl-identifier">block_group</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 Monster sprit should update itself</span><span class="hl-quotes">&quot;&quot;&quot;        
    </span><span class="hl-identifier">xMove</span><span class="hl-default">,</span><span class="hl-identifier">yMove</span><span class="hl-default"> = </span><span class="hl-number">0</span><span class="hl-default">,</span><span class="hl-number">0
    
    </span><span class="hl-reserved">if </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">direction</span><span class="hl-default">==</span><span class="hl-number">1</span><span class="hl-default">:
        </span><span class="hl-identifier">xMove</span><span class="hl-default"> = -</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">dist
    </span><span class="hl-reserved">elif </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">direction</span><span class="hl-default">==</span><span class="hl-number">2</span><span class="hl-default">:
        </span><span class="hl-identifier">yMove</span><span class="hl-default"> = -</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">dist
    </span><span class="hl-reserved">elif </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">direction</span><span class="hl-default">==</span><span class="hl-number">3</span><span class="hl-default">:
        </span><span class="hl-identifier">xMove</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">dist
    </span><span class="hl-reserved">elif </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">direction</span><span class="hl-default">==</span><span class="hl-number">4</span><span class="hl-default">:
        </span><span class="hl-identifier">yMove</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">dist
    
    self</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">move_ip</span><span class="hl-brackets">(</span><span class="hl-identifier">xMove</span><span class="hl-code">,</span><span class="hl-identifier">yMove</span><span class="hl-brackets">) </span><span class="hl-comment">#Move the Rect
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">moveCount</span><span class="hl-default"> += </span><span class="hl-number">1 </span><span class="hl-comment">#Update the Move count
    
    </span><span class="hl-reserved">if </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">spritecollideany</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">block_group</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">IF we hit a block, don't move - reverse the movement</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">move_ip</span><span class="hl-brackets">(</span><span class="hl-code">-</span><span class="hl-identifier">xMove</span><span class="hl-code">,-</span><span class="hl-identifier">yMove</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">direction</span><span class="hl-default"> = </span><span class="hl-identifier">random</span><span class="hl-default">.</span><span class="hl-identifier">randint</span><span class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-code">,</span><span class="hl-number">4</span><span class="hl-brackets">)
    </span><span class="hl-reserved">elif </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">moves</span><span class="hl-default"> == </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">moveCount</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">If we have moved enough, choose a new direction</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">direction</span><span class="hl-default"> = </span><span class="hl-identifier">random</span><span class="hl-default">.</span><span class="hl-identifier">randint</span><span class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-code">,</span><span class="hl-number">4</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">moves</span><span class="hl-default"> = </span><span class="hl-identifier">random</span><span class="hl-default">.</span><span class="hl-identifier">randint</span><span class="hl-brackets">(</span><span class="hl-number">100</span><span class="hl-code">,</span><span class="hl-number">200</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">moveCount</span><span class="hl-default"> = </span><span class="hl-number">0</span><span class="hl-default">;</span></pre></div></div>
<p>The next thing that we are going to create is a function that will let us set the Monsters scared state:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">SetScared</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">scared</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Tell the monster to be scared or not</span><span class="hl-quotes">&quot;&quot;&quot;
    &quot;&quot;&quot;</span><span class="hl-string">Should we update out scared image?</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-reserved">if </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">scared</span><span class="hl-default"> != </span><span class="hl-identifier">scared</span><span class="hl-default">:
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">scared</span><span class="hl-default"> = </span><span class="hl-identifier">scared
        </span><span class="hl-reserved">if </span><span class="hl-identifier">scared</span><span class="hl-default">:
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">image</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">scared_image
        </span><span class="hl-reserved">else</span><span class="hl-default">:
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">image</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">normal_image</span></pre></div></div>
<p>This function is pretty self-explanatory, we just set the current state and the current image for the monster based on that state.</p>
<p>The next function that we need is a &#8220;eaten&#8221; function, this is the function that will be called when the monster has been eaten by the snake.  This function is, again, very simply and simply moves the monster back to it&#8217;s original position and resets its state:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">Eaten</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">Well looks like we've been eaten!, reset to the original
    position and stop being scared</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">original_rect
    self</span><span class="hl-default">.</span><span class="hl-identifier">scared</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">image</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">normal_image</span></pre></div></div>
<p>The next thing that we need to do is make sure that the snakes update function takes into account these new monsters:</p>
<div class="hl-surround" style="height:280px;"><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-code">,</span><span class="hl-identifier">block_group</span><span class="hl-code">,</span><span class="hl-identifier">pellet_group</span><span class="hl-code">,</span><span class="hl-identifier">super_pellet_group</span><span class="hl-code">,</span><span class="hl-identifier">monster_group</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 Snake sprit should update itself</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">xMove</span><span class="hl-code">==</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-identifier">and</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">yMove</span><span class="hl-code">==</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">If we arn'te moving just get out of here</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-reserved">return
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">All right we must be moving!</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">move_ip</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">xMove</span><span class="hl-code">,</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">yMove</span><span class="hl-brackets">)
    
    </span><span class="hl-reserved">if </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">spritecollideany</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">block_group</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">IF we hit a block, don't move - reverse the movement</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">move_ip</span><span class="hl-brackets">(</span><span class="hl-code">-</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">xMove</span><span class="hl-code">,-</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">yMove</span><span class="hl-brackets">)
    
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Check to see if we hit a Monster!</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">lst_monsters</span><span class="hl-default"> =</span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">spritecollide</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">monster_group</span><span class="hl-code">, </span><span class="hl-reserved">False</span><span class="hl-brackets">)
    </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">lst_monsters</span><span class="hl-brackets">)</span><span class="hl-code">&gt;</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Allright we have hit a Monster!</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">MonsterCollide</span><span class="hl-brackets">(</span><span class="hl-identifier">lst_monsters</span><span class="hl-brackets">)
    </span><span class="hl-reserved">else</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Alright we did move, so check collisions</span><span class="hl-quotes">&quot;&quot;&quot;
        &quot;&quot;&quot;</span><span class="hl-string">Check for a snake collision/pellet collision</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">lstCols</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">spritecollide</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">
                                             , </span><span class="hl-identifier">pellet_group</span><span class="hl-code">
                                             , </span><span class="hl-reserved">True</span><span class="hl-brackets">)
        </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">lstCols</span><span class="hl-brackets">)</span><span class="hl-code">&gt;</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-default">:
            </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Update the amount of pellets eaten</span><span class="hl-quotes">&quot;&quot;&quot;
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">pellets</span><span class="hl-default"> += </span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">lstCols</span><span class="hl-brackets">)
            </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">if we didn't hit a pellet, maybe we hit a SUper Pellet?</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">elif </span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">sprite</span><span class="hl-code">.</span><span class="hl-identifier">spritecollide</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">super_pellet_group</span><span class="hl-code">, </span><span class="hl-reserved">True</span><span class="hl-brackets">))</span><span class="hl-code">&gt;</span><span class="hl-number">0</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 collided with a super pellet! Time to become Super!</span><span class="hl-quotes">&quot;&quot;&quot;
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">superState</span><span class="hl-default"> = </span><span class="hl-reserved">True
            </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">post</span><span class="hl-brackets">(</span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">Event</span><span class="hl-brackets">(</span><span class="hl-identifier">SUPER_STATE_START</span><span class="hl-code">,{}</span><span class="hl-brackets">))
            </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Start a timer to figure out when the super state ends</span><span class="hl-quotes">&quot;&quot;&quot;
            </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">time</span><span class="hl-default">.</span><span class="hl-identifier">set_timer</span><span class="hl-brackets">(</span><span class="hl-identifier">SUPER_STATE_OVER</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-brackets">)
            </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">time</span><span class="hl-default">.</span><span class="hl-identifier">set_timer</span><span class="hl-brackets">(</span><span class="hl-identifier">SUPER_STATE_OVER</span><span class="hl-code">,</span><span class="hl-number">3000</span><span class="hl-brackets">)</span></pre></div></div>
<p>You&#8217;ll notice that this function is relatively similar to what happened in part 2, except that some of the code has been moved here, and we now check to see if we have collided with any monsters. </p>
<p>The first thing we check is if we have collided with a wall, if so we reverse our movement, so that the snake doesn&#8217;t move.  Instead of leaving the function, we continue to test to see if the snake has hit a monster.  If the snake has hit a monster we call the MonsterCollide function to see what should happen.  If no monster was hit we then check to see if any pellets or super pellets were hit.</p>
<p>If the snake eats a super pellet, we set its state to be super, then we post an event to our main loop telling it that the snake has become super, and then we start a three second time.  The three second timer is basically how long the snake will stay in it&#8217;s super state and be able to eat monsters.</p>
<p>The MonsterCollide function is detailed below:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">MonsterCollide</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">lstMonsters</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This Function is called when the snake collides with the a Monster
    lstMonsters is a list of Monster sprites that it has hit.</span><span class="hl-quotes">&quot;&quot;&quot;
    
    </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">lstMonsters</span><span class="hl-brackets">)</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-quotes">&quot;&quot;&quot;</span><span class="hl-string">If the list is empty, just get out of here</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-reserved">return

    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Loop through the monsters and see what should happen</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-reserved">for </span><span class="hl-identifier">monster </span><span class="hl-reserved">in </span><span class="hl-identifier">lstMonsters</span><span class="hl-default">:
        </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">monster</span><span class="hl-code">.</span><span class="hl-identifier">scared</span><span class="hl-brackets">)</span><span class="hl-default">:
            </span><span class="hl-identifier">monster</span><span class="hl-default">.</span><span class="hl-identifier">Eaten</span><span class="hl-brackets">()
        </span><span class="hl-reserved">else</span><span class="hl-default">:
            </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Looks like we're dead</span><span class="hl-quotes">&quot;&quot;&quot;
            </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">post</span><span class="hl-brackets">(</span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">Event</span><span class="hl-brackets">(</span><span class="hl-identifier">SNAKE_EATEN</span><span class="hl-code">,{}</span><span class="hl-brackets">))</span></pre></div></div>
<p>The MonsterCollide function is relatively simple, It loops trough the list of monsters that the snake has collided with and decides what to do with each of them.  If the monster is scared it gets eaten, and if the monster is not scared then we post the snake eaten even.</p>
<p>No we need to look at the changes that we need to make to the LoadSprites function in the PyMan.py file.  The only think that we are going to change is add the code to load the monsters, it will be identical to the code that we use to load the other sprites, except that monsters have a normal and scared image, and we will also switch to using <a href="http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.RenderUpdates">RenderUpdate<a href="http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.RenderUpdates">s</a> for our pygame groups, RenderUpdates are just like normal PyGame sprite groups except that they keep track of the rects that they have drawn to.  So when you call RenderUpdates.Draw it will draw all of the sprites in the groups and then return a list of &#8220;dirty&#8221; rects.  You then take the list of rects and pass it into pygame.display.update.</p>
<p>This way you save time by only redrawing only those sections of the screen that have changed.</p>
<p>The next changes that we need to make are in the MainLoop function.  The first thing we have to do is pay attention to the events and timers that we have created in out other code. </p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">elif </span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">type</span><span class="hl-default"> == </span><span class="hl-identifier">SUPER_STATE_OVER</span><span class="hl-default">:
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">snake</span><span class="hl-default">.</span><span class="hl-identifier">superState</span><span class="hl-default"> = </span><span class="hl-reserved">False
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Stop the timer</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">time</span><span class="hl-default">.</span><span class="hl-identifier">set_timer</span><span class="hl-brackets">(</span><span class="hl-identifier">SUPER_STATE_OVER</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-brackets">)
    </span><span class="hl-reserved">for </span><span class="hl-identifier">monster </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">monster_sprites</span><span class="hl-default">.</span><span class="hl-identifier">sprites</span><span class="hl-brackets">()</span><span class="hl-default">:
        </span><span class="hl-identifier">monster</span><span class="hl-default">.</span><span class="hl-identifier">SetScared</span><span class="hl-brackets">(</span><span class="hl-reserved">False</span><span class="hl-brackets">)
</span><span class="hl-reserved">elif </span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">type</span><span class="hl-default"> == </span><span class="hl-identifier">SUPER_STATE_START</span><span class="hl-default">:
    </span><span class="hl-reserved">for </span><span class="hl-identifier">monster </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">monster_sprites</span><span class="hl-default">.</span><span class="hl-identifier">sprites</span><span class="hl-brackets">()</span><span class="hl-default">:
        </span><span class="hl-identifier">monster</span><span class="hl-default">.</span><span class="hl-identifier">SetScared</span><span class="hl-brackets">(</span><span class="hl-reserved">True</span><span class="hl-brackets">)
</span><span class="hl-reserved">elif </span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">type</span><span class="hl-default"> == </span><span class="hl-identifier">SNAKE_EATEN</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">The snake is dead!</span><span class="hl-quotes">&quot;&quot;&quot;
    &quot;&quot;&quot;</span><span class="hl-string">For now kist quit</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">sys</span><span class="hl-default">.</span><span class="hl-identifier">exit</span><span class="hl-brackets">()</span></pre></div></div>
<p>Handling these events is pretty straight forward, the only one that will eventually change is the SNAKE_EATEN event.  For now we just quite the game but in the future we will use up lives and things like that. </p>
<p>The next, and final change for this tutorial is the section of the code where we actually draw the sprites.  As detailed above we are using RenderUpdate groups this time instead of the normal groups that we have used in the past.  As a result our drawing code has to change slightly in order to pay attention to the rects that we have dirtied:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Do the Drawing</span><span class="hl-quotes">&quot;&quot;&quot;     
</span><span class="hl-identifier">textpos</span><span class="hl-default"> = </span><span class="hl-number">0          
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default">.</span><span class="hl-identifier">blit</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">background</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">))     
</span><span class="hl-reserved">if </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">font</span><span class="hl-default">:
    </span><span class="hl-identifier">font</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">font</span><span class="hl-default">.</span><span class="hl-identifier">Font</span><span class="hl-brackets">(</span><span class="hl-reserved">None</span><span class="hl-code">, </span><span class="hl-number">36</span><span class="hl-brackets">)
    </span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-identifier">font</span><span class="hl-default">.</span><span class="hl-identifier">render</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Pellets %s</span><span class="hl-quotes">&quot;</span><span class="hl-code"> % </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">snake</span><span class="hl-code">.</span><span class="hl-identifier">pellets</span><span class="hl-code">
                        , </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-number">255</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">))
    </span><span class="hl-identifier">textpos</span><span class="hl-default"> = </span><span class="hl-identifier">text</span><span class="hl-default">.</span><span class="hl-identifier">get_rect</span><span class="hl-brackets">(</span><span class="hl-identifier">centerx</span><span class="hl-code">=</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">background</span><span class="hl-code">.</span><span class="hl-identifier">get_width</span><span class="hl-brackets">()</span><span class="hl-code">/</span><span class="hl-number">2</span><span class="hl-brackets">)
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default">.</span><span class="hl-identifier">blit</span><span class="hl-brackets">(</span><span class="hl-identifier">text</span><span class="hl-code">, </span><span class="hl-identifier">textpos</span><span class="hl-brackets">)

</span><span class="hl-identifier">reclist</span><span class="hl-default"> = </span><span class="hl-brackets">[</span><span class="hl-identifier">textpos</span><span class="hl-brackets">]  
</span><span class="hl-identifier">reclist</span><span class="hl-default"> += </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">pellet_sprites</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">screen</span><span class="hl-brackets">)
</span><span class="hl-identifier">reclist</span><span class="hl-default"> += </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">super_pellet_sprites</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">screen</span><span class="hl-brackets">)
</span><span class="hl-identifier">reclist</span><span class="hl-default"> += </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">snake_sprites</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">screen</span><span class="hl-brackets">)
</span><span class="hl-identifier">reclist</span><span class="hl-default"> +=  </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">monster_sprites</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">screen</span><span class="hl-brackets">)
</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Now Update the Display</span><span class="hl-quotes">&quot;&quot;&quot;
</span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">display</span><span class="hl-default">.</span><span class="hl-identifier">update</span><span class="hl-brackets">(</span><span class="hl-identifier">reclist</span><span class="hl-brackets">)</span></pre></div></div>
<p>As you can see not much has actually changed except for the fact that we are now paying attention to the rectangles that we have dirtied and are only updating those rather then redrawing the entire screen.</p>
<p>If you would like the full source of this tutorial, you can get it <a href="http://www.learningpython.com/sources/PyMan_3.zip">here</a>.</p>
<p>Well that&#8217;s it for this tutorial, sorry it&#8217;s been such a long time coming, it seems that real life got in the way of my learning python.  Hopefully I will get back on my schedule of updating this site once a week soon.</p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2006/04/16/creating-a-game-in-python-using-pygame-part-3-adding-the-bad-guys/&title=Creating a Game in Python Using PyGame - Part 3 - Adding the bad guys&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/2006/04/16/creating-a-game-in-python-using-pygame-part-3-adding-the-bad-guys/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Creating a Game in Python Using PyGame &#8211; Part Two &#8211; Creating a level</title>
		<link>http://www.learningpython.com/2006/03/19/creating-a-game-in-python-using-pygame-part-two-creating-a-level/</link>
		<comments>http://www.learningpython.com/2006/03/19/creating-a-game-in-python-using-pygame-part-two-creating-a-level/#comments</comments>
		<pubDate>Sun, 19 Mar 2006 21:26:02 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[beginnings]]></category>
		<category><![CDATA[pygame]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=20</guid>
		<description><![CDATA[
			
				
			
		
All right in Part one we actually created a semi-working almost-game, in part two we&#8217;re going to go a bit further, in part two we&#8217;re going to add the walls that will make help make PyMan (our python based PacMan clone) an actual game.
Note: Part one has been mistakenly half-deleted by me, so it is [...]]]></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%2F2006%2F03%2F19%2Fcreating-a-game-in-python-using-pygame-part-two-creating-a-level%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F03%2F19%2Fcreating-a-game-in-python-using-pygame-part-two-creating-a-level%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://www.learningpython.com/images/pyMan_02_01.png"><img style="margin: 0pt 10px 10px 0pt; cursor: pointer; float: left" src="http://www.learningpython.com/images/small_pyMan_02_01.png" alt="Python RSS Reader" border="0" /></a>All right in <a href="http://www.learningpython.com/2006/03/12/creating-a-game-in-python-using-pygame-part-one/">Part one</a> we actually created a semi-working almost-game, in part two we&#8217;re going to go a bit further, in part two we&#8217;re going to add the walls that will make help make PyMan (our python based PacMan clone) an actual game.</p>
<p>Note: <a href="http://www.learningpython.com/2006/03/12/creating-a-game-in-python-using-pygame-part-one/">Part one</a> has been mistakenly half-deleted by me, so it is not fully available at this time. I am working to re-write it so it should be up soon.</p>
<p><img style="margin: 0pt 10px 10px 0pt; cursor: pointer; float: left" src="http://www.learningpython.com/images/snake02.png" alt="Python RSS Reader" border="0" />Note: I also changed the images that I used in Part one so now we have a new smaller snake.</p>
<p>You can download the full source for Part Two <a href="http://www.learningpython.com/sources/PyMan_2.zip">here</a>.</p>
<p>What we are going to do is base some of our level code on a great PyGame tutorial over at <a href="http://www.devshed.com/c/a/Python/A-PyGame-Working-Example-Starting-a-Game/">DevShed</a> specifically the idea of the level layout.  This is something that has always puzzled me about games, how they create their levels?  I&#8217;ve read some articles and beginnings of books but I&#8217;ve never really be able to understand it, but the multidimensional array approach discussed in the DevShed article is nothing if not simple.</p>
<p>So the idea is that our game world, or game board, will basically be a grid of 24&#215;24 pixel items, and we will have a multidimensional array or list that we will used to describe it.</p>
<p>So for example lets say we have a grid like this:</p>
<p><code><br />
0 0 0 0 0<br />
1 1 1 1 1<br />
1 0 0 0 1<br />
1 1 1 1 1<br />
0 0 0 0 0<br />
</code></p>
<p>Now that grid may result in the following:</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/grid_01.png" alt="PyGame Window" border="0" /></p>
<p>Where 0 is nothing and where 1 is a blue square.  We could then add 2&#8217;s if we wanted to and make those red squares, or anything else that we wanted.  So that is basically how our levels are going to be defined.</p>
<p><span id="more-20"></span></p>
<p>The first thing we are going to do is add a new file to our project called levelBase.Py, in that we will define a class called level which is as follows:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">Level</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">The Base Class for Levels</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-reserved">def </span><span class="hl-identifier">getLayout</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">Get the Layout of the level</span><span class="hl-quotes">&quot;&quot;&quot;
        &quot;&quot;&quot;</span><span class="hl-string">Returns a [][] list</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-reserved">pass
    def </span><span class="hl-identifier">getImages</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">Get a list of all the images used by the level</span><span class="hl-quotes">&quot;&quot;&quot;
        &quot;&quot;&quot;</span><span class="hl-string">Returns a list of all the images used.  The indices 
        in the layout refer to sprites in the list returned by
        this function</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-reserved">pass</span></pre></div></div>
<p>Pretty simple not much going on there, getLayout returns a multidimensional list of numbers that represents the level, and getImages returns a list of the images used by the level.</p>
<p>It may seem complicated but once you see our first level it will start to make some sense.    The next thing to do is add a file called level001.py to our project.  I waffled on this a bit, I was unsure if it was better to have one file called levels.py that contained classes called level001, or make each level a separate file.  In then end I went with each level being a separate file, just because it separates things, but I don&#8217;t really know if one method is superior over the other.  Here is the code for level001.py:</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-comment">#! /usr/bin/env python

</span><span class="hl-reserved">import </span><span class="hl-identifier">levelBase
</span><span class="hl-reserved">from </span><span class="hl-identifier">helpers </span><span class="hl-reserved">import </span><span class="hl-identifier">load_image

</span><span class="hl-reserved">class </span><span class="hl-identifier">level</span><span class="hl-brackets">(</span><span class="hl-identifier">levelBase</span><span class="hl-code">.</span><span class="hl-identifier">Level</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Level 1 of the PyMan 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-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">BLOCK</span><span class="hl-default"> = </span><span class="hl-number">1
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">SNAKE</span><span class="hl-default"> = </span><span class="hl-number">2
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">PELLET</span><span class="hl-default"> = </span><span class="hl-number">0
    
    </span><span class="hl-reserved">def </span><span class="hl-identifier">getLayout</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-reserved">return </span><span class="hl-brackets">[[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code"> ,</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code"> ,</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code"> ,</span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code"> ,</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code"> ,</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">2</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code"> ,</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code"> ,</span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code"> ,</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]</span><span class="hl-code">,\
                </span><span class="hl-brackets">[</span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-brackets">]]         
        
    </span><span class="hl-reserved">def </span><span class="hl-identifier">getSprites</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-identifier">block</span><span class="hl-default">, </span><span class="hl-identifier">rect</span><span class="hl-default"> = </span><span class="hl-identifier">load_image</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">block.png</span><span class="hl-quotes">'</span><span class="hl-brackets">)
        </span><span class="hl-identifier">pellet</span><span class="hl-default">, </span><span class="hl-identifier">rect</span><span class="hl-default"> = </span><span class="hl-identifier">load_image</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">pellet.png</span><span class="hl-quotes">'</span><span class="hl-code">,-</span><span class="hl-number">1</span><span class="hl-brackets">)
        </span><span class="hl-identifier">snake</span><span class="hl-default">, </span><span class="hl-identifier">rect</span><span class="hl-default"> = </span><span class="hl-identifier">load_image</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">snake.png</span><span class="hl-quotes">'</span><span class="hl-code">,-</span><span class="hl-number">1</span><span class="hl-brackets">)
        </span><span class="hl-reserved">return </span><span class="hl-brackets">[</span><span class="hl-identifier">pellet</span><span class="hl-code">,</span><span class="hl-identifier">block</span><span class="hl-code">,</span><span class="hl-identifier">snake</span><span class="hl-brackets">]</span></pre></div></div>
<p>You can see that in the __init__ function the level defines three values: BLOCK, SNAKE, and PELLET.  These values correspond to the numbers in the list returned by getLayout().  So wherever there is a 1 in the list, that will correspond to a BLOCK, a 0 is a PELLET, and a 2 is our snake.  In this example I use 9 to represent a blank square.</p>
<p>You&#8217;ll also notice that the getImages() function returns a list of images that are indexed by those same values.  So in the list returned by getImages(), position [BLOCK] is the block image.  </p>
<p>So far so good, the next thing we are going to do is add a base class for all of our sprites.  In <a href="http://www.learningpython.com/2006/03/12/creating-a-game-in-python-using-pygame-part-one/">Part one</a> we didn&#8217;t need to do this since we only had two sprites, but in this lesson (as we can see above) we are going to have at least three and that number will continue to grow.  So we will add another file to our project called basicSprite.py, this will be the base class that we use for all of our sprites.  It doesn&#8217;t do much besides add a few initialization features to pygame.sprite.Sprite class.  The code for basicSprite.py is as follows:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#! /usr/bin/env python

</span><span class="hl-reserved">import </span><span class="hl-identifier">pygame

</span><span class="hl-reserved">class </span><span class="hl-identifier">Sprite</span><span class="hl-brackets">(</span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">sprite</span><span class="hl-code">.</span><span class="hl-identifier">Sprite</span><span class="hl-brackets">)</span><span class="hl-default">:
        
    </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">centerPoint</span><span class="hl-code">, </span><span class="hl-identifier">image</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">Sprite</span><span class="hl-default">.</span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">) 
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Set the image and the rect</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">image</span><span class="hl-default"> = </span><span class="hl-identifier">image
        self</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default"> = </span><span class="hl-identifier">image</span><span class="hl-default">.</span><span class="hl-identifier">get_rect</span><span class="hl-brackets">()
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Move the rect into the correct position</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">center</span><span class="hl-default"> = </span><span class="hl-identifier">centerPoint</span></pre></div></div>
<p>As you can see all that basicSprite.Sprite does is really add a few initialization values: centerPoint and image.  Image is the image that the sprite will use and centerPoint is the center point of the rect, it controls where the image will be placed.  The nice thing about using the center point is that it is correct regardless of the images size.  So if you have a 16&#215;16 image and a 24&#215;24 image the center point will be the same, but the topleft point (which we used in part one) will be different for both.</p>
<p>Because of the addition of the basicSprite.Sprite we are going to have to change our snake sprite from part one, we are also going to move the sprite into it&#8217;s own file since it will start getting a bit complicated as time goes on.  So we will add the file snakeSprite.py to our project. snakeSprite.py will contain a class called Snake, which will be based off of our basicSprite.Sprite class:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">Snake</span><span class="hl-brackets">(</span><span class="hl-identifier">basicSprite</span><span class="hl-code">.</span><span class="hl-identifier">Sprite</span><span class="hl-brackets">)</span><span class="hl-default">:</span></pre></div></div>
<p>Snake will initialize itself as follows:</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">centerPoint</span><span class="hl-code">, </span><span class="hl-identifier">image</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">initialize base class</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">basicSprite</span><span class="hl-default">.</span><span class="hl-identifier">Sprite</span><span class="hl-default">.</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">centerPoint</span><span class="hl-code">, </span><span class="hl-identifier">image</span><span class="hl-brackets">)
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Initialize the number of pellets eaten</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">pellets</span><span class="hl-default"> = </span><span class="hl-number">0
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Set the number of Pixels to move each time</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">x_dist</span><span class="hl-default"> = </span><span class="hl-number">3
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">y_dist</span><span class="hl-default"> = </span><span class="hl-number">3 
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Initialize how much we are moving</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">xMove</span><span class="hl-default"> = </span><span class="hl-number">0
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">yMove</span><span class="hl-default"> = </span><span class="hl-number">0</span></pre></div></div>
<p>You&#8217;ll notice that the initialization is similar to the initialization in part one except this time we are using the basicSprite.Sprite class to set up our image and rect.  Then we initialize the same values, except we have introduce two new ones, self.xMove and self.yMove.  The reason for this is we are going to change the way that we move our snake.</p>
<p>If you tried out the last tutorial you&#8217;ll see that just moving the character on the key down events didn&#8217;t work all that well.  It worked well enough but if you start pressing more then one key are the same time pyGame seemed to get a bit confused.</p>
<p>In order to combat this we will use another tip taken from the <a href="http://www.devshed.com/c/a/Python/A-PyGame-Working-Example-Starting-a-Game/">DevShed tutorial</a> and pay attention to both the DOWN and UP events associated with keystrokes.  We will also switch to using the update() function to update the snake.</p>
<p>So what we are going to is add two functions to the Snake class MoveKeyUp() and MoveKeyDown().  What these functions will do is adjust  the xMove and yMove variables depending on what key has been pressed.  Then when the update function is called the Snake will update it&#8217;s positions according the values of xMove and yMove.  It may seem a bit complicated but it&#8217;s actually pretty simple:</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">MoveKeyDown</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">key</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This function sets the xMove or yMove variables that will
    then move the snake when update() function is called.  The
    xMove and yMove values will be returned to normal when this 
    keys MoveKeyUp function is called.</span><span class="hl-quotes">&quot;&quot;&quot;
    
    </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_RIGHT</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">xMove</span><span class="hl-default"> += </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">x_dist
    elif </span><span class="hl-brackets">(</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_LEFT</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">xMove</span><span class="hl-default"> += -</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">x_dist
    elif </span><span class="hl-brackets">(</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_UP</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">yMove</span><span class="hl-default"> += -</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">y_dist
    elif </span><span class="hl-brackets">(</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_DOWN</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">yMove</span><span class="hl-default"> += </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">y_dist
    
</span><span class="hl-reserved">def </span><span class="hl-identifier">MoveKeyUp</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">key</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This function resets the xMove or yMove variables that will
    then move the snake when update() function is called.  The
    xMove and yMove values will be returned to normal when this 
    keys MoveKeyUp function is called.</span><span class="hl-quotes">&quot;&quot;&quot;
    
    </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_RIGHT</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">xMove</span><span class="hl-default"> += -</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">x_dist
    elif </span><span class="hl-brackets">(</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_LEFT</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">xMove</span><span class="hl-default"> += </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">x_dist
    elif </span><span class="hl-brackets">(</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_UP</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">yMove</span><span class="hl-default"> += </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">y_dist
    elif </span><span class="hl-brackets">(</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_DOWN</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">yMove</span><span class="hl-default"> += -</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">y_dist</span></pre></div></div>
<p>As you can see when we press the K_LEFT key down we add -x_dist pixels to the xMove variable.  This will then be used to move the snake in the update() function.  Then when we let up the K_LEFT key we remove the -x_dist values that we added to xMove.  For example if you hold down K_LEFT for a moment xMove will before -3 (move three pixels to the left every time the update() function is called), then when you let the K_LEFT key up xMove will become 0 (don&#8217;t move).</p>
<p>Then all we have to do is add the update function:</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-code">,</span><span class="hl-identifier">block_group</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 Snake sprit should 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">rect</span><span class="hl-default">.</span><span class="hl-identifier">move_ip</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">xMove</span><span class="hl-code">,</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">yMove</span><span class="hl-brackets">)
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">IF we hit a block, don't move - reverse the movement</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-reserved">if </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">spritecollide</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">block_group</span><span class="hl-code">, </span><span class="hl-reserved">False</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">rect</span><span class="hl-default">.</span><span class="hl-identifier">move_ip</span><span class="hl-brackets">(</span><span class="hl-code">-</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">xMove</span><span class="hl-code">,-</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">yMove</span><span class="hl-brackets">)</span></pre></div></div>
<p>Now the <a href="http://www.pygame.org/docs/ref/sprite.html#Sprite.update">update()</a> function is function in pyGames Sprite class.  It really doesn&#8217;t do anything besides providing a convenient way to update a group of sprites.  It will also pass the same parameters to all of your sprites in the group.  So if you have a bunch of sprites that need to know the current time when you update them it&#8217;s a good idea to add them to the same group and then update them all at once by calling the group.update() function and pass the time.  </p>
<p>You&#8217;ll also notice that there is a second parameter passed to this function called block_group, this is the group of all of our block sprites.  We use that group to do a hit test.  If the snake has collided with a block after we have adjusted it&#8217;s rect, we reverse the movement.</p>
<p>No we are going to go back to our PyManMain class and edit the LoadSprites() function.  We are going to use this function to load the level that we created in level001.py and all of the sprites that we need:</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">LoadSprites</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">Load all of the sprites that we need</span><span class="hl-quotes">&quot;&quot;&quot;
    &quot;&quot;&quot;</span><span class="hl-string">calculate the center point offset</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">x_offset</span><span class="hl-default"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">BLOCK_SIZE</span><span class="hl-code">/</span><span class="hl-number">2</span><span class="hl-brackets">)
    </span><span class="hl-identifier">y_offset</span><span class="hl-default"> = </span><span class="hl-brackets">(</span><span class="hl-identifier">BLOCK_SIZE</span><span class="hl-code">/</span><span class="hl-number">2</span><span class="hl-brackets">)
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Load the level</span><span class="hl-quotes">&quot;&quot;&quot;        
    </span><span class="hl-identifier">level1</span><span class="hl-default"> = </span><span class="hl-identifier">level001</span><span class="hl-default">.</span><span class="hl-identifier">level</span><span class="hl-brackets">()
    </span><span class="hl-identifier">layout</span><span class="hl-default"> = </span><span class="hl-identifier">level1</span><span class="hl-default">.</span><span class="hl-identifier">getLayout</span><span class="hl-brackets">()
    </span><span class="hl-identifier">img_list</span><span class="hl-default"> = </span><span class="hl-identifier">level1</span><span class="hl-default">.</span><span class="hl-identifier">getSprites</span><span class="hl-brackets">()
    
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Create the Pellet group</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">pellet_sprites</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">Group</span><span class="hl-brackets">()
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Create the block group</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">block_sprites</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">Group</span><span class="hl-brackets">()
    
    </span><span class="hl-reserved">for </span><span class="hl-identifier">y </span><span class="hl-reserved">in </span><span class="hl-builtin">xrange</span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">layout</span><span class="hl-brackets">))</span><span class="hl-default">:
        </span><span class="hl-reserved">for </span><span class="hl-identifier">x </span><span class="hl-reserved">in </span><span class="hl-builtin">xrange</span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">layout</span><span class="hl-brackets">[</span><span class="hl-identifier">y</span><span class="hl-brackets">]))</span><span class="hl-default">:
            </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Get the center point for the rects</span><span class="hl-quotes">&quot;&quot;&quot;
            </span><span class="hl-identifier">centerPoint</span><span class="hl-default"> = </span><span class="hl-brackets">[(</span><span class="hl-identifier">x</span><span class="hl-code">*</span><span class="hl-identifier">BLOCK_SIZE</span><span class="hl-brackets">)</span><span class="hl-code">+</span><span class="hl-identifier">x_offset</span><span class="hl-code">,</span><span class="hl-brackets">(</span><span class="hl-identifier">y</span><span class="hl-code">*</span><span class="hl-identifier">BLOCK_SIZE</span><span class="hl-code">+</span><span class="hl-identifier">y_offset</span><span class="hl-brackets">)]
            </span><span class="hl-reserved">if </span><span class="hl-identifier">layout</span><span class="hl-brackets">[</span><span class="hl-identifier">y</span><span class="hl-brackets">][</span><span class="hl-identifier">x</span><span class="hl-brackets">]</span><span class="hl-default">==</span><span class="hl-identifier">level1</span><span class="hl-default">.</span><span class="hl-identifier">BLOCK</span><span class="hl-default">:
                </span><span class="hl-identifier">block</span><span class="hl-default"> = </span><span class="hl-identifier">basicSprite</span><span class="hl-default">.</span><span class="hl-identifier">Sprite</span><span class="hl-brackets">(</span><span class="hl-identifier">centerPoint</span><span class="hl-code">, </span><span class="hl-identifier">img_list</span><span class="hl-brackets">[</span><span class="hl-identifier">level1</span><span class="hl-code">.</span><span class="hl-identifier">BLOCK</span><span class="hl-brackets">])
                </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">block_sprites</span><span class="hl-default">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-identifier">block</span><span class="hl-brackets">)
            </span><span class="hl-reserved">elif </span><span class="hl-identifier">layout</span><span class="hl-brackets">[</span><span class="hl-identifier">y</span><span class="hl-brackets">][</span><span class="hl-identifier">x</span><span class="hl-brackets">]</span><span class="hl-default">==</span><span class="hl-identifier">level1</span><span class="hl-default">.</span><span class="hl-identifier">SNAKE</span><span class="hl-default">:
                </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">snake</span><span class="hl-default"> = </span><span class="hl-identifier">Snake</span><span class="hl-brackets">(</span><span class="hl-identifier">centerPoint</span><span class="hl-code">,</span><span class="hl-identifier">img_list</span><span class="hl-brackets">[</span><span class="hl-identifier">level1</span><span class="hl-code">.</span><span class="hl-identifier">SNAKE</span><span class="hl-brackets">])
            </span><span class="hl-reserved">elif </span><span class="hl-identifier">layout</span><span class="hl-brackets">[</span><span class="hl-identifier">y</span><span class="hl-brackets">][</span><span class="hl-identifier">x</span><span class="hl-brackets">]</span><span class="hl-default">==</span><span class="hl-identifier">level1</span><span class="hl-default">.</span><span class="hl-identifier">PELLET</span><span class="hl-default">:
                </span><span class="hl-identifier">pellet</span><span class="hl-default"> = </span><span class="hl-identifier">basicSprite</span><span class="hl-default">.</span><span class="hl-identifier">Sprite</span><span class="hl-brackets">(</span><span class="hl-identifier">centerPoint</span><span class="hl-code">, </span><span class="hl-identifier">img_list</span><span class="hl-brackets">[</span><span class="hl-identifier">level1</span><span class="hl-code">.</span><span class="hl-identifier">PELLET</span><span class="hl-brackets">])
                </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">pellet_sprites</span><span class="hl-default">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-identifier">pellet</span><span class="hl-brackets">)  
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Create the Snake group</span><span class="hl-quotes">&quot;&quot;&quot;            
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">snake_sprites</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">RenderPlain</span><span class="hl-brackets">((</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">snake</span><span class="hl-brackets">))</span></pre></div></div>
<p>Since this function is a bit more complicated I will try to explain it in more detail.  The first thing that we do is calculate the x_offset and the y_offset, using the value BLOCK_SIZE.  BLOCK_SIZE is simple a global created in pyMan.py that refers to the size that each block will be.  So in this case, BLOCK_SIZE = 24.  The x_offset and y_offsets are the center points of a block.  We use these offsets to position the sprites that we will create.</p>
<p>After that we load the level: we create an instance of the level001 class, and then get it&#8217;s layout and it&#8217;s image list.  Then we create our pellet and block groups, these will be sprite groups that contain all the pellet or block sprites.</p>
<p>The next step is to loop through the layout of the level and create the necessary sprites at each position.  We first calculate the current blocks center point, just to make the code a bit cleaner, and then we check to see what the current position in the layout is.  It it&#8217;s a BLOCK position we create new block sprite and add it to the block_sprites group.  If it&#8217;s a PELLET position, we create a new pellet sprite and add it to the pellet_sprites group. </p>
<p>The only sprite that we treat a bit differently is the snake sprite, since we will only have one snake per level, we only create one.  And if the level designer mistakenly added two snakes, we will use the second snake as the snakes position.</p>
<p>You&#8217;ll also notice that both the pellet and the block sprites are simply basicSprites.Sprite&#8217;s since they don&#8217;t really do anything else besides get drawn to the screen.</p>
<p>Now we simply have to adjust our MainLoop() function to take into account all of the changes that we have made to our game:</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">MainLoop</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 the Main Loop of the Game</span><span class="hl-quotes">&quot;&quot;&quot;
        
        &quot;&quot;&quot;</span><span class="hl-string">Load All of our Sprites</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">LoadSprites</span><span class="hl-brackets">()</span><span class="hl-default">;
        
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Create the background</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">background</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">Surface</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">screen</span><span class="hl-code">.</span><span class="hl-identifier">get_size</span><span class="hl-brackets">())
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">background</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">background</span><span class="hl-default">.</span><span class="hl-identifier">convert</span><span class="hl-brackets">()
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">background</span><span class="hl-default">.</span><span class="hl-identifier">fill</span><span class="hl-brackets">((</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-brackets">))
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Draw the blocks onto the background, since they only need to be 
        drawn once</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">block_sprites</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">background</span><span class="hl-brackets">)
        
        </span><span class="hl-reserved">while </span><span class="hl-number">1</span><span class="hl-default">:
            </span><span class="hl-reserved">for </span><span class="hl-identifier">event </span><span class="hl-reserved">in </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">get</span><span class="hl-brackets">()</span><span class="hl-default">:
                </span><span class="hl-reserved">if </span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">type</span><span class="hl-default"> == </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">QUIT</span><span class="hl-default">: 
                    </span><span class="hl-identifier">sys</span><span class="hl-default">.</span><span class="hl-identifier">exit</span><span class="hl-brackets">()
                </span><span class="hl-reserved">elif </span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">type</span><span class="hl-default"> == </span><span class="hl-identifier">KEYDOWN</span><span class="hl-default">:
                    </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">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_RIGHT</span><span class="hl-brackets">)
                    </span><span class="hl-identifier">or </span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_LEFT</span><span class="hl-brackets">)
                    </span><span class="hl-identifier">or </span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_UP</span><span class="hl-brackets">)
                    </span><span class="hl-identifier">or </span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_DOWN</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">snake</span><span class="hl-default">.</span><span class="hl-identifier">MoveKeyDown</span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">key</span><span class="hl-brackets">)
                </span><span class="hl-reserved">elif </span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">type</span><span class="hl-default"> == </span><span class="hl-identifier">KEYUP</span><span class="hl-default">:
                    </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">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_RIGHT</span><span class="hl-brackets">)
                    </span><span class="hl-identifier">or </span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_LEFT</span><span class="hl-brackets">)
                    </span><span class="hl-identifier">or </span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_UP</span><span class="hl-brackets">)
                    </span><span class="hl-identifier">or </span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_DOWN</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">snake</span><span class="hl-default">.</span><span class="hl-identifier">MoveKeyUp</span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">key</span><span class="hl-brackets">)
            </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Update the snake sprite</span><span class="hl-quotes">&quot;&quot;&quot;        
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">snake_sprites</span><span class="hl-default">.</span><span class="hl-identifier">update</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">block_sprites</span><span class="hl-brackets">)
                        
            </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Check for a snake collision/pellet collision</span><span class="hl-quotes">&quot;&quot;&quot;
            </span><span class="hl-identifier">lstCols</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">spritecollide</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">snake</span><span class="hl-code">
                                                 , </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">pellet_sprites</span><span class="hl-code">
                                                 , </span><span class="hl-reserved">True</span><span class="hl-brackets">)
            </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Update the amount of pellets eaten</span><span class="hl-quotes">&quot;&quot;&quot;
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">snake</span><span class="hl-default">.</span><span class="hl-identifier">pellets</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">snake</span><span class="hl-default">.</span><span class="hl-identifier">pellets</span><span class="hl-default"> + </span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">lstCols</span><span class="hl-brackets">)
                        
            </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Do the Drawing</span><span class="hl-quotes">&quot;&quot;&quot;               
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default">.</span><span class="hl-identifier">blit</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">background</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">))     
            </span><span class="hl-reserved">if </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">font</span><span class="hl-default">:
                </span><span class="hl-identifier">font</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">font</span><span class="hl-default">.</span><span class="hl-identifier">Font</span><span class="hl-brackets">(</span><span class="hl-reserved">None</span><span class="hl-code">, </span><span class="hl-number">36</span><span class="hl-brackets">)
                </span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-identifier">font</span><span class="hl-default">.</span><span class="hl-identifier">render</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Pellets %s</span><span class="hl-quotes">&quot;</span><span class="hl-code"> % </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">snake</span><span class="hl-code">.</span><span class="hl-identifier">pellets</span><span class="hl-code">
                                    , </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-number">255</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">))
                </span><span class="hl-identifier">textpos</span><span class="hl-default"> = </span><span class="hl-identifier">text</span><span class="hl-default">.</span><span class="hl-identifier">get_rect</span><span class="hl-brackets">(</span><span class="hl-identifier">centerx</span><span class="hl-code">=</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">background</span><span class="hl-code">.</span><span class="hl-identifier">get_width</span><span class="hl-brackets">()</span><span class="hl-code">/</span><span class="hl-number">2</span><span class="hl-brackets">)
                </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default">.</span><span class="hl-identifier">blit</span><span class="hl-brackets">(</span><span class="hl-identifier">text</span><span class="hl-code">, </span><span class="hl-identifier">textpos</span><span class="hl-brackets">)
               
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">pellet_sprites</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">screen</span><span class="hl-brackets">)
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">snake_sprites</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">screen</span><span class="hl-brackets">)
            
            </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">display</span><span class="hl-default">.</span><span class="hl-identifier">flip</span><span class="hl-brackets">()</span></pre></div></div>
<p>It&#8217;s a lot of code, but it&#8217;s not really that different then the code was in part one.  An obvious difference is the way in which we adjust the snakes position, using the MoveKeyUp, MoveKeyDown, and update function.  Everything else is basically identical to the way that it was in part one.</p>
<p>The only other difference is the fact that we have to draw the block sprites.  Since the blocks are static and will never be updated, we are going to draw then to the background surface.  This will save us a bit of drawing time when we draw.</p>
<p><a href="http://www.learningpython.com/images/pyMan_02_01.png"><img style="margin: 0pt 10px 10px 0pt; cursor: pointer; float: left" src="http://www.learningpython.com/images/small_pyMan_02_01.png" alt="Python RSS Reader" border="0" /></a>When we run the code now we are greeted with the simple PyMan level that we saw at the beginning of this post.  Now this isn&#8217;t close to being a finished game as there is no level two, and there are no bad guys, but our simple game it starting to take shape.</p>
<p>In the next part of this pyGame series I hope to add the bad guys to the level and the idea of the super pellets.  If you want to download the code for this post you can do so <a href="http://www.learningpython.com/sources/PyMan_2.zip">here</a>.</p>
<p>If you like this post remember to <a href="http://digg.com/programming/Creating_a_Game_in_Python_Using_PyGame_-_Part_Two_-_Creating_a_level">digg it</a></p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2006/03/19/creating-a-game-in-python-using-pygame-part-two-creating-a-level/&title=Creating a Game in Python Using PyGame - Part Two - Creating a level&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/2006/03/19/creating-a-game-in-python-using-pygame-part-two-creating-a-level/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Creating a Game in Python Using PyGame &#8211; Part One</title>
		<link>http://www.learningpython.com/2006/03/12/creating-a-game-in-python-using-pygame-part-one/</link>
		<comments>http://www.learningpython.com/2006/03/12/creating-a-game-in-python-using-pygame-part-one/#comments</comments>
		<pubDate>Mon, 13 Mar 2006 00:46:04 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[beginnings]]></category>
		<category><![CDATA[pygame]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=19</guid>
		<description><![CDATA[
			
				
			
		
Prerequisites
In order to follow along with this tutorial you will need Python 2.4 and PyGame 1.7.0+ installed.  Since I&#8217;m doing this all on a Mac and Python 2.4.2 is not available from MacPython or any other site as a disk image I decided to install from source.
If you are try to build Python on [...]]]></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%2F2006%2F03%2F12%2Fcreating-a-game-in-python-using-pygame-part-one%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F03%2F12%2Fcreating-a-game-in-python-using-pygame-part-one%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<h2>Prerequisites</h2>
<p>In order to follow along with this tutorial you will need Python 2.4 and PyGame 1.7.0+ installed.  Since I&#8217;m doing this all on a Mac and Python 2.4.2 is not available from <a href="http://homepages.cwi.nl/~jack/macpython/">MacPython</a> or any other site as a disk image I decided to install from source.</p>
<p>If you are try to build Python on a Mac as well you should probably install into the /Library/Frameworks path as explained by this <a href="http://xanana.ucsc.edu/~wgscott/xtal/wiki/index.php/Unix_and_OS_X:_The_Power_and_the_Glory">site</a>.</p>
<p>Basically once you have downloaded the source from <a href="http://www.python.org/download/">python.org</a> unpack the tar ball as follows:</p>
<p><code>tar -zxvf Python-2.4.2.tgz</code></p>
<p>Once that has finished change to the Python-2.4.2 directory that was just created and install it by issuing the following commands:</p>
<p><code>./configure --enable-framework<br />
make<br />
sudo make frameworkinstall<br />
</code></p>
<p>For Windows or Linux you should be able to install easily given the instructions on the Python site.</p>
<p>This will install Python in the /Library/Frameworks path, and create a symlink to the python executable in /use/local/bin, which is not part of the PATH environment variable on new OS X builds, so you will probably want to add it:</p>
<p><code>export PATH=/usr/local/bin:$PATH</code></p>
<h2>Part One</h2>
<p>The full source of this tutorial can be downloaded <a href="http://www.learningpython.com/sources/PyMan.zip">here</a>.</p>
<p><img style="margin: 0pt 10px 10px 0pt; float: left;" src="http://www.learningpython.com/images/snake.png" alt="Python Snake" border="0" />So let&#8217;s actually start creating this game using PyGame.  For our snake image in the game I&#8217;m going to use the snake to the left for now.  As you can see I&#8217;m not artist, but I was able to install the gimp on my Mac and get that image to a point that I think is relatively acceptable.  If anyone out there has any graphics skills and can whip me up a better looking python in a 64&#215;64 png I&#8217;d gladly use it!</p>
<p>So the first thing we are going to do is create a new PyDev project in Eclipse.  I&#8217;m going to use Python 2.4 for this project because it is the version that is compatible with my PyGame Installation.</p>
<p><img style="margin: 0pt 10px 10px 0pt; float: left;" src="http://www.learningpython.com/images/pygameFolder.png" alt="Python folders" border="0" />Then I&#8217;m going to create a new file called PyMan.py, this will be the main file of our game for now.  The architecture may change as I go through this and discover better ways to use the files in my projects but for now this will work as a main file.  In the directory that I create my project I will also create a subfolder entitled &#8220;data&#8221; and in that subfolder I will have another folder entitled &#8220;images&#8221; where I will store the above snake.png image.</p>
<p><strong>Note:</strong> A lot of the information in this post was taken from the Pete Shinner&#8217;s great <a href="http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html">Line by Line Chimp</a> and <a href="http://www.pygame.org/docs/tut/intro/intro.html">Python Pygame Introduction</a> tutorials.<br />
<span id="more-19"></span><br />
Now the first thing that we are going to have to do is import the libraries that we are going to need, we&#8217;ll also warn the user if the font or sound mixers are not available:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">import </span><span class="hl-identifier">os</span><span class="hl-default">, </span><span class="hl-identifier">sys
</span><span class="hl-reserved">import </span><span class="hl-identifier">pygame
</span><span class="hl-reserved">from </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">locals </span><span class="hl-reserved">import</span><span class="hl-default"> *

</span><span class="hl-reserved">if not </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">font</span><span class="hl-default">: </span><span class="hl-reserved">print </span><span class="hl-quotes">'</span><span class="hl-string">Warning, fonts disabled</span><span class="hl-quotes">'
</span><span class="hl-reserved">if not </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">mixer</span><span class="hl-default">: </span><span class="hl-reserved">print </span><span class="hl-quotes">'</span><span class="hl-string">Warning, sound disabled</span><span class="hl-quotes">'</span></pre></div></div>
<p>Then we&#8217;re going to create a class called PyManMain, this is going to be the main class of our game, it will handle all of the main functions in our game, things like the game loop, the screen creation, and keeping track of all of our sprites:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">PyManMain</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">The Main PyMan Class - This class handles the main 
    initialization and creating of the 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">width</span><span class="hl-code">=</span><span class="hl-number">640</span><span class="hl-code">,</span><span class="hl-identifier">height</span><span class="hl-code">=</span><span class="hl-number">480</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Initialize</span><span class="hl-quotes">&quot;&quot;&quot;
        &quot;&quot;&quot;</span><span class="hl-string">Initialize PyGame</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">init</span><span class="hl-brackets">()
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Set the window Size</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">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
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Create the Screen</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">display</span><span class="hl-default">.</span><span class="hl-identifier">set_mode</span><span class="hl-brackets">((</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">width</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></pre></div></div>
<p>As you can see our __init__ function takes two optional parameters height and width, this will be the height and the width of the screen that we create. The __init__ function basically initializes pygame (pygame.init()) and then creates our main screen using the <a href="http://www.pygame.org/docs/ref/display.html#pygame.display.set_mode">pygame.display.set_mode function</a>.</p>
<p>So far so good! The next thing that we are going to need is a game loop, this is the loop that will process all of the events that PyGame sends our way. To do that, we’re going to add a function to our PyManMain class called MainLoop:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">MainLoop</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 the Main Loop of the Game</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-reserved">while </span><span class="hl-number">1</span><span class="hl-default">:
        </span><span class="hl-reserved">for </span><span class="hl-identifier">event </span><span class="hl-reserved">in </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">get</span><span class="hl-brackets">()</span><span class="hl-default">:
            </span><span class="hl-reserved">if </span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">type</span><span class="hl-default"> == </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">QUIT</span><span class="hl-default">: 
                </span><span class="hl-identifier">sys</span><span class="hl-default">.</span><span class="hl-identifier">exit</span><span class="hl-brackets">()</span></pre></div></div>
<p>We’ll also add the code to our class that will start out game:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">if </span><span class="hl-identifier">__name__</span><span class="hl-default"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">__main__</span><span class="hl-quotes">&quot;</span><span class="hl-default">:
    </span><span class="hl-identifier">MainWindow</span><span class="hl-default"> = </span><span class="hl-identifier">PyManMain</span><span class="hl-brackets">()
    </span><span class="hl-identifier">MainWindow</span><span class="hl-default">.</span><span class="hl-identifier">MainLoop</span><span class="hl-brackets">()</span></pre></div></div>
<p><img style="margin: 0pt 10px 10px 0pt; float: left;" src="http://www.learningpython.com/images/small_blankpygame.png" alt="PyGame Window" border="0" />If you run this now you will be with a very uninspiring black screen. It may not be much but for the amount of code that we’ve written it’s pretty good.</p>
<p>The next thing that we need to create is out snake sprite. We are going to wrap our snake sprite in it’s own class called snake (of course) and it will be based off of the <a href="http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite">pygame.sprite.Sprite</a> class. If you want to read more about PyGames sprite and group classes you should read <a href="http://kai.vm.bytemark.co.uk/~piman/writing/sprite-tutorial.shtml">piman’s great sprite tutorial</a>:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">Snake</span><span class="hl-brackets">(</span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">sprite</span><span class="hl-code">.</span><span class="hl-identifier">Sprite</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 our snake that will move around the screen</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-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">Sprite</span><span class="hl-default">.</span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">) 
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">image</span><span class="hl-default">, </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default"> = </span><span class="hl-identifier">load_image</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">snake.png</span><span class="hl-quotes">'</span><span class="hl-code">,-</span><span class="hl-number">1</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">pellets</span><span class="hl-default"> = </span><span class="hl-number">0</span></pre></div></div>
<p>As you can see the Snake class really isn’t doing much besides initializing the Sprite base class and loading the snake image. It also sets pellets to zero, this variable isn’t used yet but it is the number of pellets that our snake has eaten.</p>
<p>You’ll notice that we use load_image to load our snake image. This function is taken from the <a href="http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html">Line by Line Chimp tutorial</a>. I created a new file for my project called helpers.py which will contain all helper functions. I put load_image in this file and added the following to the top of PyMan.py:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-reserved">from </span><span class="hl-identifier">helpers </span><span class="hl-reserved">import</span><span class="hl-default"> *</span></pre></div></div>
<p>Now we have to create an instance of our snake sprite and display it. To do this I created another function in the PyManMain class called LoadSprites, this function will take care of loading all of our sprites:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">LoadSprites</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">Load the sprites that we need</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">snake</span><span class="hl-default"> = </span><span class="hl-identifier">Snake</span><span class="hl-brackets">()
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">snake_sprites</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">RenderPlain</span><span class="hl-brackets">((</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">snake</span><span class="hl-brackets">))</span></pre></div></div>
<p>This function creates the sprite (self.snake = Snake) and then creates a group that contains our snake sprite (self.snake_sprites = pygame.sprite.RenderPlain((self.snake))).</p>
<p>We are also going to have to make some changes to the MainLoop function, we are going to have to load all of our sprites before we enter the while loop:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Load All of our Sprites</span><span class="hl-quotes">&quot;&quot;&quot;
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">LoadSprites</span><span class="hl-brackets">()</span><span class="hl-default">;</span></pre></div></div>
<p>Then after our event for loop but within the while loop we need to tell PyGame to draw our sprit:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">snake_sprites</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">screen</span><span class="hl-brackets">)
</span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">display</span><span class="hl-default">.</span><span class="hl-identifier">flip</span><span class="hl-brackets">()</span></pre></div></div>
<p><a href="http://www.learningpython.com/images/pygame_01_01.png"><img style="margin: 0pt 10px 10px 0pt; float: left;" src="http://www.learningpython.com/images/small_pygame_01_01.png" alt="Python Snake" border="0" /></a>Now we’re getting somewhere! We’ve got our little PyMan snake displaying himself in the top left corner of our screen. Not too bad.</p>
<p>The next thing that we’re going to want to do is create and display all of our pellets, this isn’t very difficult if you understood the snake sprite above you’ll understand this:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">Pellet</span><span class="hl-brackets">(</span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">sprite</span><span class="hl-code">.</span><span class="hl-identifier">Sprite</span><span class="hl-brackets">)</span><span class="hl-default">:
        
    </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">rect</span><span class="hl-code">=</span><span class="hl-reserved">None</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">Sprite</span><span class="hl-default">.</span><span class="hl-identifier">__init__</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">) 
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">image</span><span class="hl-default">, </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default"> = </span><span class="hl-identifier">load_image</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">pellet.png</span><span class="hl-quotes">'</span><span class="hl-code">,-</span><span class="hl-number">1</span><span class="hl-brackets">)
        </span><span class="hl-reserved">if </span><span class="hl-identifier">rect</span><span class="hl-default"> != </span><span class="hl-reserved">None</span><span class="hl-default">:
            </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default"> = </span><span class="hl-identifier">rect</span></pre></div></div>
<p><img style="margin: 0pt 10px 10px 0pt; float: left;" src="http://www.learningpython.com/images/pellet.png" alt="Python pellet" border="0" />The only difference you’ll see in the pellet class is the fact that we have an optional rect parameter, which lets us place the pellet where ever we want. The pellets I’ve created aren’t the best pellets that I’ve ever seen but just like the snake.png they’ll do for this example.</p>
<p>We then need to load the pellet sprites in our LoadSprites function:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">figure out how many pellets we can display</span><span class="hl-quotes">&quot;&quot;&quot;
</span><span class="hl-identifier">nNumHorizontal</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">width</span><span class="hl-code">/</span><span class="hl-number">64</span><span class="hl-brackets">)
</span><span class="hl-identifier">nNumVertical</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">height</span><span class="hl-code">/</span><span class="hl-number">64</span><span class="hl-brackets">)       
</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Create the Pellet group</span><span class="hl-quotes">&quot;&quot;&quot;
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">pellet_sprites</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">Group</span><span class="hl-brackets">()
</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Create all of the pellets and add them to the 
pellet_sprites group</span><span class="hl-quotes">&quot;&quot;&quot;
</span><span class="hl-reserved">for </span><span class="hl-identifier">x </span><span class="hl-reserved">in </span><span class="hl-builtin">range</span><span class="hl-brackets">(</span><span class="hl-identifier">nNumHorizontal</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-reserved">for </span><span class="hl-identifier">y </span><span class="hl-reserved">in </span><span class="hl-builtin">range</span><span class="hl-brackets">(</span><span class="hl-identifier">nNumVertical</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">pellet_sprites</span><span class="hl-default">.</span><span class="hl-identifier">add</span><span class="hl-brackets">(</span><span class="hl-identifier">Pellet</span><span class="hl-brackets">(</span><span class="hl-identifier">pygame</span><span class="hl-code">.</span><span class="hl-identifier">Rect</span><span class="hl-brackets">(</span><span class="hl-identifier">x</span><span class="hl-code">*</span><span class="hl-number">64</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-code">*</span><span class="hl-number">64</span><span class="hl-code">, </span><span class="hl-number">64</span><span class="hl-code">, </span><span class="hl-number">64</span><span class="hl-brackets">)))</span></pre></div></div>
<p>Then we have to draw the pellets in out MainLoop:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">pellet_sprites</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">screen</span><span class="hl-brackets">)
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">snake_sprites</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">screen</span><span class="hl-brackets">)
</span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">display</span><span class="hl-default">.</span><span class="hl-identifier">flip</span><span class="hl-brackets">()</span></pre></div></div>
<p>Now it’s time to make that snake move, to do so we’re going to have to look at the event loop in our MainLoop. ( for event in pygame.event.get():) What we are going to do is move the snake around when the arrow keys are pressed. To do that we are going to check to see if the event is a KEYDOWN event, and if it is a key down event whether the key being pressed is one of the arrow keys:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">if </span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">type</span><span class="hl-default"> == </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">QUIT</span><span class="hl-default">: 
    </span><span class="hl-identifier">sys</span><span class="hl-default">.</span><span class="hl-identifier">exit</span><span class="hl-brackets">()
</span><span class="hl-reserved">elif </span><span class="hl-identifier">event</span><span class="hl-default">.</span><span class="hl-identifier">type</span><span class="hl-default"> == </span><span class="hl-identifier">KEYDOWN</span><span class="hl-default">:
    </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">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_RIGHT</span><span class="hl-brackets">)
    </span><span class="hl-identifier">or </span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_LEFT</span><span class="hl-brackets">)
    </span><span class="hl-identifier">or </span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_UP</span><span class="hl-brackets">)
    </span><span class="hl-identifier">or </span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_DOWN</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">snake</span><span class="hl-default">.</span><span class="hl-identifier">move</span><span class="hl-brackets">(</span><span class="hl-identifier">event</span><span class="hl-code">.</span><span class="hl-identifier">key</span><span class="hl-brackets">)</span></pre></div></div>
<p>Then we have to add a move function to our snake class that will actually move our snake:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">move</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">key</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Move your self in one of the 4 directions according to key</span><span class="hl-quotes">&quot;&quot;&quot;
    &quot;&quot;&quot;</span><span class="hl-string">Key is the pyGame define for either up,down,left, or right key
    we will adjust ourselves in that direction</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">xMove</span><span class="hl-default"> = </span><span class="hl-number">0</span><span class="hl-default">;
    </span><span class="hl-identifier">yMove</span><span class="hl-default"> = </span><span class="hl-number">0</span><span class="hl-default">;
    
    </span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_RIGHT</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">xMove</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">x_dist
    elif </span><span class="hl-brackets">(</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_LEFT</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">xMove</span><span class="hl-default"> = -</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">x_dist
    elif </span><span class="hl-brackets">(</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_UP</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">yMove</span><span class="hl-default"> = -</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">y_dist
    elif </span><span class="hl-brackets">(</span><span class="hl-identifier">key</span><span class="hl-code"> == </span><span class="hl-identifier">K_DOWN</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-identifier">yMove</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">y_dist
    self</span><span class="hl-default">.</span><span class="hl-identifier">rect</span><span class="hl-default">.</span><span class="hl-identifier">move_ip</span><span class="hl-brackets">(</span><span class="hl-identifier">xMove</span><span class="hl-code">,</span><span class="hl-identifier">yMove</span><span class="hl-brackets">)</span><span class="hl-default">;</span></pre></div></div>
<p>You’ll notice that the move function references self.x_dist and self.y_dist, these are basically two integers that I set in the snake’s __init__ function, they are the number of pixels that we want the snake to move in the x or y direction. I set mine to 5.</p>
<p>To move the snake we adjust it’s rect so that the next time it is displayed it will be drawn in a different direction. To do that we call the <a href="http://www.pygame.org/docs/ref/rect.html#Rect.move_ip">rect.move_ip</a> function with the amount of pixels in the x and y directions to move.</p>
<p>Now we are going to have to add some collision detection to make our snake eat our pellets. Thankfully the spirt and group classes have built in collision detection that we are able to use:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Check for collision</span><span class="hl-quotes">&quot;&quot;&quot;
</span><span class="hl-identifier">lstCols</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">spritecollide</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">snake</span><span class="hl-code">
                                     , </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">pellet_sprites</span><span class="hl-code">
                                     , </span><span class="hl-reserved">True</span><span class="hl-brackets">)
</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Update the amount of pellets eaten</span><span class="hl-quotes">&quot;&quot;&quot;
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">snake</span><span class="hl-default">.</span><span class="hl-identifier">pellets</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">snake</span><span class="hl-default">.</span><span class="hl-identifier">pellets</span><span class="hl-default"> + </span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">lstCols</span><span class="hl-brackets">)</span></pre></div></div>
<p>We use the <a href="http://www.pygame.org/docs/ref/pygame_sprite.html#spritecollide">pygame.sprite.spritecollide</a> function to see if our snake sprite collided with any of the pellet sprites. If they did we kill which ever pellet that was hit by passing True as the third parameter. pygame.sprite.spritecollide basically goes through all of the sprites in the group passes and sees if the sprites rect intersects with the sprit passed in as parameter one.</p>
<p>pygame.sprite.spritecollide also returns a list of all the sprites that were collided by our snake, so we use that to update the number of pellets that we have eaten.</p>
<p>The last thing that we our going to do in this tutorial is display the number of pellets that we have eaten to the user:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">if </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">font</span><span class="hl-default">:
    </span><span class="hl-identifier">font</span><span class="hl-default"> = </span><span class="hl-identifier">pygame</span><span class="hl-default">.</span><span class="hl-identifier">font</span><span class="hl-default">.</span><span class="hl-identifier">Font</span><span class="hl-brackets">(</span><span class="hl-reserved">None</span><span class="hl-code">, </span><span class="hl-number">36</span><span class="hl-brackets">)
    </span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-identifier">font</span><span class="hl-default">.</span><span class="hl-identifier">render</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Pellets %s</span><span class="hl-quotes">&quot;</span><span class="hl-code"> % </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">snake</span><span class="hl-code">.</span><span class="hl-identifier">pellets</span><span class="hl-code">
                        , </span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-number">255</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-number">0</span><span class="hl-brackets">))
    </span><span class="hl-identifier">textpos</span><span class="hl-default"> = </span><span class="hl-identifier">text</span><span class="hl-default">.</span><span class="hl-identifier">get_rect</span><span class="hl-brackets">(</span><span class="hl-identifier">centerx</span><span class="hl-code">=</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">width</span><span class="hl-code">/</span><span class="hl-number">2</span><span class="hl-brackets">)
    </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen</span><span class="hl-default">.</span><span class="hl-identifier">blit</span><span class="hl-brackets">(</span><span class="hl-identifier">text</span><span class="hl-code">, </span><span class="hl-identifier">textpos</span><span class="hl-brackets">)</span></pre></div></div>
<p>This code is taken from the line by line chimp example with very few changes. Instead of telling the user to hit the chimp for cash, we’re telling them how many pellets that the snake has eaten so far. We need to put the code right right before we draw our sprites in the game loop.</p>
<p><a href="http://www.learningpython.com/images/pygame_01_02.png"><img style="margin: 0pt 10px 10px 0pt; float: left;" src="http://www.learningpython.com/images/small_pygame_01_02.png" alt="Python Snake" border="0" /></a>That’s about it for this example, there is a bit of code in the finished “product” that I didn’t discuss but if you download the <a href="http://www.learningpython.com/sources/PyMan.zip">full source</a> it the new code should be pretty self explanatory. Now this isn’t a full example by any means and probably isn’t something that you’d really want to base any game off of, but it does give you a pretty basic idea about what is required to create a game in using Python and PyGame.</p>
<p>If you are really interested in creating a game I’d suggest you download the <a href="http://www.learningpython.com/sources/PyMan.zip">full source</a> and play around with it a bit. I’m also suggest (insist?) that you read the documentation and tutorials provided for you on the main <a href="http://www.pygame.org/">PyGame site</a>, if it wasn’t for them I would have been unable to write this simple tutorial.</p>
<p>Whew, that was a lot a typing, it’s time for me to go make some dinner and have a glass of wine.</p>
<p>Useful links:</p>
<li><a href="http://www.pygame.org">http://www.<b style="color:black;background-color:#ffff66">pygame</b>.org</a></li>
<li><a href="http://www.pygame.org/docs/">http://www.<b style="color:black;background-color:#ffff66">pygame</b>.org/docs/</a></li>
<li><a href="http://www.pygame.org/wiki/tutorials">http://www.<b style="color:black;background-color:#ffff66">pygame</b>.org/wiki/tutorials</a></li>
<li><a href="http://www.pygame.org/docs/tut/intro/intro.html">http://www.<b style="color:black;background-color:#ffff66">pygame</b>.org/docs/tut/intro/intro.html</a></li>
<li><a href="http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html">http://www.<b style="color:black;background-color:#ffff66">pygame</b>.org/docs/tut/chimp/ChimpLineByLine.html</a></li>
<li><a href="http://kai.vm.bytemark.co.uk/~piman/writing/sprite-tutorial.shtml">http://kai.vm.bytemark.co.uk/~piman/writing/sprite-tutorial.shtml</a></li>
<li><a href="http://docs.python.org">http://docs.python.org</a></li>
<li><a href="http://www.python.org/">http://www.python.org/</a></li>
<p>	If you like this post remember to <a href="http://digg.com/programming/Creating_a_Game_in_Python_Using_PyGame_-_Part_One">digg it</a>.</p>
<p><strong>Note</strong>: Whew! Many thanks to <a href="http://frem.wordpress.com/">James</a> for finding the google cahce for me!  I looked for it when I first deleted it but couldn&#8217;t fine it.  Much appreciated!</p>
<p>Here are some alternate images that were sent to me by a kind reader named Jordan:</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/pygame/Python.png" alt="Python Snake" border="0" /><br />
<img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/pygame/Pellet.png" alt="Python Snake" border="0" /></p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2006/03/12/creating-a-game-in-python-using-pygame-part-one/&title=Creating a Game in Python Using PyGame - Part One&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/2006/03/12/creating-a-game-in-python-using-pygame-part-one/feed/</wfw:commentRss>
		<slash:comments>57</slash:comments>
		</item>
		<item>
		<title>Creating a Game in Python Using PyGame &#8211; Introduction</title>
		<link>http://www.learningpython.com/2006/03/11/creating-a-game-in-python-using-pygame-introduction/</link>
		<comments>http://www.learningpython.com/2006/03/11/creating-a-game-in-python-using-pygame-introduction/#comments</comments>
		<pubDate>Sat, 11 Mar 2006 19:10:05 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[beginnings]]></category>
		<category><![CDATA[pygame]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=18</guid>
		<description><![CDATA[
			
				
			
		
Well as someone who programs a lot and likes using computers creating a viedo game is always something that I&#8217;ve wanted to do.  In the past I spent a lot of time learning OpenGL and actually went quite far in the creation of a simple/not-so-simple game.  The game was put on hold indefinitely [...]]]></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%2F2006%2F03%2F11%2Fcreating-a-game-in-python-using-pygame-introduction%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F03%2F11%2Fcreating-a-game-in-python-using-pygame-introduction%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Well as someone who programs a lot and likes using computers creating a viedo game is always something that I&#8217;ve wanted to do.  In the past I spent a lot of time learning OpenGL and actually went quite far in the creation of a simple/not-so-simple game.  The game was put on hold indefinitely once real life responsibilities started to encroach on my evenings.  Since then I&#8217;ve longed to try again.</p>
<p>Every since I heard about <a href="http://www.pygame.org/news.html">PyGame</a> from <a href="http://thinkhole.org/wp/">import this</a>, I thought it looked promising.  It&#8217;s cross platform, which is something that is very important to me as a developer, and it lets me stratch my Python skills which is the whole reason I have this website.  Here is some of the about information from the PyGame website:</p>
<blockquote><p>Pygame is a set of Python modules designed for writing games. It is written on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the python language. Pygame is highly portable and runs on nearly every platform and operating system.</p></blockquote>
<p><span id="more-18"></span><br />
So I decided to test my skills and create a simple game using PyGame.  My first instinct was to create a cool game, but I soom realized that creating cool game right away probably wasn&#8217;t going to be the easiest thing to do so I decided to try something really simple and went with the idea of a PacMan clone, which I decided to call PyMan.</p>
<p>Now I assume that there have been many PacMan clones written using PyGame, but I&#8217;m honestly not trying to copy them or steal their thunder, I&#8217;m simply trying to learn how PyGame works.</p>
<p>The first thing that you should do is visit the <a href="http://www.pygame.org/">PyGame</a> site and read some of their <a href="http://www.pygame.org/docs/">documentation</a> and <a href="http://www.pygame.org/wiki/tutorials">tutorials</a>.  So I&#8217;m going to assume that you have a limited understanding of what PyGame is and how to program in Python, not too much becuase I don&#8217;t, but still a little bit. </p>
<p>Since this project is probably going to be a bit more complicated I&#8217;m going to use <a href="http://www.eclipse.org/">Eclipse</a> with the <a href="http://pydev.sourceforge.net/">PyDev</a> plugin.  I&#8217;m also going to use the <a href="http://www.gimp.org/">gimp</a>  for all of my graphics, since it&#8217;s the only graphics program that I know how to use, now I just need to figure out how to install it on OS X.</p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2006/03/11/creating-a-game-in-python-using-pygame-introduction/&title=Creating a Game in Python Using PyGame - Introduction&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/2006/03/11/creating-a-game-in-python-using-pygame-introduction/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
