<?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; beginnings</title>
	<atom:link href="http://www.learningpython.com/category/python/beginnings/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>Level Editor 0.3 (Dodger)</title>
		<link>http://www.learningpython.com/2008/06/22/level-editor-03-dodger/</link>
		<comments>http://www.learningpython.com/2008/06/22/level-editor-03-dodger/#comments</comments>
		<pubDate>Mon, 23 Jun 2008 03:23:27 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[beginnings]]></category>
		<category><![CDATA[game engine]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[Dodger]]></category>
		<category><![CDATA[level editor]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=77</guid>
		<description><![CDATA[
			
				
			
		
I know it&#8217;s been a while and for that I apologize the last few months have been pretty crazy around here&#8230;although I&#8217;m starting to see a trend with life in general lately, namely that it&#8217;s always crazy.
I&#8217;ve been busy with work, Python Magazine, my wife, trips to Dallas, and yes whenever I get a chance [...]]]></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%2F06%2F22%2Flevel-editor-03-dodger%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2008%2F06%2F22%2Flevel-editor-03-dodger%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>I know it&#8217;s been a while and for that I apologize the last few months have been pretty crazy around here&#8230;although I&#8217;m starting to see a trend with life in general lately, namely that it&#8217;s always crazy.</p>
<p>I&#8217;ve been busy with work, <a href="http://pymag.phparch.com/">Python Magazine</a>, my wife, trips to Dallas, and yes whenever I get a chance this slowly growing level editor. Let&#8217;s see what I&#8217;ve been working on for the last little while:</p>
<p><strong>Name</strong>: There has a been a name for the editor ever since I started working on it. I wasn&#8217;t sure if I was going to think up something really cool and change, or leave it. Turns out I just left it.</p>
<p>So from this point forward this project is christened: &#8220;Dodger&#8221;, or probably more correctly: &#8220;Dodger Level Editor&#8221;.</p>
<p>The name has its roots in the name of one of my cats and a history in the multitudes of level editors and game engines that I have tried to create in the past, but I wont&#8217; go into that. So Dodger it is.</p>
<p><strong>Saving and Loading</strong>: Saving and loading in the default YAML project type now really works. I still need to put in support for optional project types: XML, JSON, and other formats</p>
<p><a href="http://www.learningpython.com/wp-content/uploads/2008/06/editor_03.png"><img class="alignnone size-medium wp-image-78" title="welcome dialog" src="http://www.learningpython.com/wp-content/uploads/2008/06/editor_03-300x210.png" alt="dodger editor welcome dialog" width="300" height="210" /></a></p>
<p><strong>Welcome Dialog</strong>: This was a real pain, but it&#8217;s made the last while so much easier. I added a working (at least I hope) welcome dialog with a way to create new projects, open old ones, and a recent file list. The recent file list really makes testing easier for me.</p>
<p><a href="http://www.learningpython.com/wp-content/uploads/2008/06/editor_04.png"><img class="alignnone size-medium wp-image-79" title="rect tracking" src="http://www.learningpython.com/wp-content/uploads/2008/06/editor_04-300x210.png" alt="dodger editor rect tracking" width="300" height="210" /></a></p>
<p><strong>Rect tracking</strong>: Rect tracking is finally working properly. I&#8217;ve had the rect tracker in there for a while but it didn&#8217;t really do anything until now.</p>
<p><a href="http://www.learningpython.com/wp-content/uploads/2008/06/editor_05.png"><img class="alignnone size-medium wp-image-80" title="Multiple Selection" src="http://www.learningpython.com/wp-content/uploads/2008/06/editor_05-300x210.png" alt="dodger editor multiple selection" width="300" height="210" /></a></p>
<p><strong>Multiple Selection/Multiple Properties</strong>: I&#8217;ve also finally got multiple selection going, which is what makes the rect tracker actually useful. You can select multiple sprties, move them around and add properties to all of them.</p>
<p><strong>Remove Properties</strong>: Now you can remove custom properties that you have added. This was a must but I was lazy and left it for a while.</p>
<p><strong>Steps towards being made public</strong>: A lot of the changes (and I do mean a lot) that I&#8217;ve been making have been behind the scenes. There has been a lot of refactoring and reorganizing of the code, often the result of quick and dirty implementations that I made earlier (sigh). I&#8217;ve also started working on getting the distribution of this editor going so that other people can use/develop it.</p>
<p>I&#8217;ve added support for zc.Buildout so that if anyone wants to develop they can quickly gather dependencies and won&#8217;t have to install the editor system wide, not that you have to anyways but zc.Buildout is really neat.</p>
<p>I&#8217;ve also worked on the license (GPLv3) and setup.py and README and all of that. None of it&#8217;s done but it&#8217;s working its way forward.</p>
<p><strong>Faster</strong>: It&#8217;s also much faster now. None of you have used it so you&#8217;ll probably think it&#8217;s slow, but trust me it&#8217;s much faster then it was before.</p>
<p><strong>Bugs</strong>: There have been loads of bugs that have been fixed and created. Plus one doozy related to<br />
changes made to pyglet. Not pyglets fault but it took a long time to figure out what the issue was.</p>
<p><strong>Google Code</strong>: The project has a temporary homepage over at google code: <a href="http://code.google.com/p/dodger-editor/">http://code.google.com/p/dodger-editor/</a> There&#8217;s nothing there yet but over time I will start to host the project there so that people can easily download it. I&#8217;ll still post updates here until the site has a full-time home.</p>
<p>I&#8217;m going to use Mercurial for the revision control system for the project so the CVS support at the google code site will just be for downloading. Eventually I will want to host the project on some space of my own and get a nice web interface for the mercurial repository going. I&#8217;ll have to find a new hosting company so it will take a while.</p>
<p>So that&#8217;s it, that&#8217;s what&#8217;s happened to the Dodger Level Editor over the last few months. I know I promised to make it public earlier but given the shape it was in at that time there really was no point. I want this to be at a point where people can actually almost use it before I make it public.</p>
<p>I know it&#8217;s been a while, and I know the few of you that actually care about this project have probably moved onto bigger and better things, but hopefully if you stick with me there will be something out soon.</p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2008/06/22/level-editor-03-dodger/&title=Level Editor 0.3 (Dodger)&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/06/22/level-editor-03-dodger/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Using Menus in Tkinter</title>
		<link>http://www.learningpython.com/2006/03/27/using-menus-in-tkinter/</link>
		<comments>http://www.learningpython.com/2006/03/27/using-menus-in-tkinter/#comments</comments>
		<pubDate>Tue, 28 Mar 2006 03:59:53 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[beginnings]]></category>
		<category><![CDATA[gui]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tkinter]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=22</guid>
		<description><![CDATA[
			
				
			
		
This is a quick post on how to use menu&#8217;s in Tkinter, if you are unfamiliar with  Tkinter you might want to check out some of my other Tkinter related posts.
We are going to start of with a basic python Tkinter app:
#! /usr/bin/env python
from Tkinter import *

class App(Frame):

	def __init__(self, master):
		
		&#34;&#34;&#34;Initialise the base class&#34;&#34;&#34;
  [...]]]></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%2F27%2Fusing-menus-in-tkinter%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F03%2F27%2Fusing-menus-in-tkinter%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>This is a quick post on how to use menu&#8217;s in Tkinter, if you are unfamiliar with  Tkinter you might want to check out some of my other Tkinter related <a href="http://www.learningpython.com/category/python/tkinter/">posts</a>.</p>
<p>We are going to start of with a basic python Tkinter app:</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">from </span><span class="hl-identifier">Tkinter </span><span class="hl-reserved">import</span><span class="hl-default"> *

</span><span class="hl-reserved">class </span><span class="hl-identifier">App</span><span class="hl-brackets">(</span><span class="hl-identifier">Frame</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">master</span><span class="hl-brackets">)</span><span class="hl-default">:
		
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Initialise the base class</span><span class="hl-quotes">&quot;&quot;&quot;
        	</span><span class="hl-identifier">Frame</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">master</span><span class="hl-brackets">)
        
        	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Set the Window Title</span><span class="hl-quotes">&quot;&quot;&quot;
        	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">master</span><span class="hl-default">.</span><span class="hl-identifier">title</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">TkInter Menus</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">configure</span><span class="hl-brackets">(</span><span class="hl-identifier">height</span><span class="hl-code">=</span><span class="hl-number">200</span><span class="hl-code">,</span><span class="hl-identifier">width</span><span class="hl-code">=</span><span class="hl-number">200</span><span class="hl-brackets">)
        	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Display the main window
        	with a little bit of padding</span><span class="hl-quotes">&quot;&quot;&quot;
        	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">grid</span><span class="hl-brackets">(</span><span class="hl-identifier">padx</span><span class="hl-code">=</span><span class="hl-number">15</span><span class="hl-code">, </span><span class="hl-identifier">pady</span><span class="hl-code">=</span><span class="hl-number">15</span><span class="hl-code">,</span><span class="hl-identifier">sticky</span><span class="hl-code">=</span><span class="hl-identifier">N</span><span class="hl-code">+</span><span class="hl-identifier">S</span><span class="hl-code">+</span><span class="hl-identifier">E</span><span class="hl-code">+</span><span class="hl-identifier">W</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">root</span><span class="hl-default"> = </span><span class="hl-identifier">Tk</span><span class="hl-brackets">()
	</span><span class="hl-identifier">app</span><span class="hl-default"> = </span><span class="hl-identifier">App</span><span class="hl-brackets">(</span><span class="hl-identifier">root</span><span class="hl-brackets">)
	</span><span class="hl-identifier">root</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/menu_01.png" alt="Python Tkinter Window" border="0" />What this gives us is basically a plain old blank Tkinter window, not too exciting.  The next thing we are going to do is add create our root menu, before going any further you might want to read the excellent information about <a href="http://www.pythonware.com/library/tkinter/introduction/menu.htm">menu widgets</a> in <a href="http://www.pythonware.com/library/tkinter/introduction/index.htm">PythonWare&#8217;s Tkinter introduction</a>.  You don&#8217;t have to but it&#8217;s an excellent source of information.</p>
<p><span id="more-22"></span></p>
<p>To create the root menu we simply add the following command to our __init__ function:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#Create the Menu base
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">menu</span><span class="hl-default"> = </span><span class="hl-identifier">Menu</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)
</span><span class="hl-comment">#Add the Menu
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">master</span><span class="hl-default">.</span><span class="hl-identifier">config</span><span class="hl-brackets">(</span><span class="hl-identifier">menu</span><span class="hl-code">=</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">menu</span><span class="hl-brackets">)</span></pre></div></div>
<p>No we are greeted with, well a blank window with a blank menu, the reason for this is because we haven&#8217;t added anything to it.  What we are going to add to it is a menu, but not just any menu, a menu who&#8217;s parent is our menu base:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#Create our Python menu
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">tkMenu</span><span class="hl-default"> = </span><span class="hl-identifier">Menu</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">menu</span><span class="hl-brackets">)
</span><span class="hl-comment">#Add our Menu to the Base Menu
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">menu</span><span class="hl-default">.</span><span class="hl-identifier">add_cascade</span><span class="hl-brackets">(</span><span class="hl-identifier">label</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">TkMenu</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">menu</span><span class="hl-code">=</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">tkMenu</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/menu_02.png" alt="Python Tkinter Window" border="0" />Now we&#8217;re getting somewhere at least we have a menu now!  The next step is to add items to our TkMenu:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#Add items to the menu
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">tkMenu</span><span class="hl-default">.</span><span class="hl-identifier">add_command</span><span class="hl-brackets">(</span><span class="hl-identifier">label</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Simple</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">command</span><span class="hl-code">=</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">Simple</span><span class="hl-brackets">)
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">tkMenu</span><span class="hl-default">.</span><span class="hl-identifier">add_separator</span><span class="hl-brackets">()
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">tkMenu</span><span class="hl-default">.</span><span class="hl-identifier">add_command</span><span class="hl-brackets">(</span><span class="hl-identifier">label</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Menu</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">command</span><span class="hl-code">=</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">Menu</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/menu_03.png" alt="Python Tkinter Menu" border="0" />That&#8217;s basically it!  Adding the menu item&#8217;s is pretty simple, we specify it&#8217;s text and then tell it what function to call when the user clicks on the menu item.  The two functions that I used don&#8217;t really do anything besides show a message box:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">Simple</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">tkMessageBox</span><span class="hl-default">.</span><span class="hl-identifier">showinfo</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Simple</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">Simple</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
</span><span class="hl-reserved">def </span><span class="hl-identifier">Menu</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">tkMessageBox</span><span class="hl-default">.</span><span class="hl-identifier">showinfo</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Menu</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">Menu</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span></pre></div></div>
<p>Well that&#8217;s it for this post, here is the code in its entirety:</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">from </span><span class="hl-identifier">Tkinter </span><span class="hl-reserved">import</span><span class="hl-default"> *

</span><span class="hl-reserved">import </span><span class="hl-identifier">tkMessageBox

</span><span class="hl-reserved">class </span><span class="hl-identifier">App</span><span class="hl-brackets">(</span><span class="hl-identifier">Frame</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">master</span><span class="hl-brackets">)</span><span class="hl-default">:
		
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Initialise the base class</span><span class="hl-quotes">&quot;&quot;&quot;
        	</span><span class="hl-identifier">Frame</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">master</span><span class="hl-brackets">)
        
        	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Set the Window Title</span><span class="hl-quotes">&quot;&quot;&quot;
        	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">master</span><span class="hl-default">.</span><span class="hl-identifier">title</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">TkInter Menus</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">configure</span><span class="hl-brackets">(</span><span class="hl-identifier">height</span><span class="hl-code">=</span><span class="hl-number">200</span><span class="hl-code">,</span><span class="hl-identifier">width</span><span class="hl-code">=</span><span class="hl-number">200</span><span class="hl-brackets">)
        	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Display the main window
        	with a little bit of padding</span><span class="hl-quotes">&quot;&quot;&quot;
        	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">grid</span><span class="hl-brackets">(</span><span class="hl-identifier">padx</span><span class="hl-code">=</span><span class="hl-number">15</span><span class="hl-code">, </span><span class="hl-identifier">pady</span><span class="hl-code">=</span><span class="hl-number">15</span><span class="hl-code">,</span><span class="hl-identifier">sticky</span><span class="hl-code">=</span><span class="hl-identifier">N</span><span class="hl-code">+</span><span class="hl-identifier">S</span><span class="hl-code">+</span><span class="hl-identifier">E</span><span class="hl-code">+</span><span class="hl-identifier">W</span><span class="hl-brackets">)       
 
       		</span><span class="hl-comment">#Create the Menu base
        	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">menu</span><span class="hl-default"> = </span><span class="hl-identifier">Menu</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)
        	</span><span class="hl-comment">#Add the Menu
        	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">master</span><span class="hl-default">.</span><span class="hl-identifier">config</span><span class="hl-brackets">(</span><span class="hl-identifier">menu</span><span class="hl-code">=</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">menu</span><span class="hl-brackets">)
        	
		</span><span class="hl-comment">#Create our Python menu
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">tkMenu</span><span class="hl-default"> = </span><span class="hl-identifier">Menu</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">menu</span><span class="hl-brackets">)
		</span><span class="hl-comment">#Add our Menu to the Base Menu
        	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">menu</span><span class="hl-default">.</span><span class="hl-identifier">add_cascade</span><span class="hl-brackets">(</span><span class="hl-identifier">label</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">TkMenu</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">menu</span><span class="hl-code">=</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">tkMenu</span><span class="hl-brackets">)

		</span><span class="hl-comment">#Add items to the menu
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">tkMenu</span><span class="hl-default">.</span><span class="hl-identifier">add_command</span><span class="hl-brackets">(</span><span class="hl-identifier">label</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Simple</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">command</span><span class="hl-code">=</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">Simple</span><span class="hl-brackets">)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">tkMenu</span><span class="hl-default">.</span><span class="hl-identifier">add_separator</span><span class="hl-brackets">()
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">tkMenu</span><span class="hl-default">.</span><span class="hl-identifier">add_command</span><span class="hl-brackets">(</span><span class="hl-identifier">label</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Menu</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">command</span><span class="hl-code">=</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">Menu</span><span class="hl-brackets">)
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">Simple</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">tkMessageBox</span><span class="hl-default">.</span><span class="hl-identifier">showinfo</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Simple</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">Simple</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
	</span><span class="hl-reserved">def </span><span class="hl-identifier">Menu</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">tkMessageBox</span><span class="hl-default">.</span><span class="hl-identifier">showinfo</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Menu</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">Menu</span><span class="hl-quotes">&quot;</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">root</span><span class="hl-default"> = </span><span class="hl-identifier">Tk</span><span class="hl-brackets">()
	</span><span class="hl-identifier">app</span><span class="hl-default"> = </span><span class="hl-identifier">App</span><span class="hl-brackets">(</span><span class="hl-identifier">root</span><span class="hl-brackets">)
	</span><span class="hl-identifier">root</span><span class="hl-default">.</span><span class="hl-identifier">mainloop</span><span class="hl-brackets">()</span></pre></div></div>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2006/03/27/using-menus-in-tkinter/&title=Using Menus in Tkinter&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/27/using-menus-in-tkinter/feed/</wfw:commentRss>
		<slash:comments>9</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>
		<item>
		<title>Creating a GUI in Python using Tkinter</title>
		<link>http://www.learningpython.com/2006/02/08/creating-a-gui-in-python-using-tkinter/</link>
		<comments>http://www.learningpython.com/2006/02/08/creating-a-gui-in-python-using-tkinter/#comments</comments>
		<pubDate>Wed, 08 Feb 2006 05:42:53 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[beginnings]]></category>
		<category><![CDATA[gui]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[reference]]></category>
		<category><![CDATA[tkinter]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=11</guid>
		<description><![CDATA[
			
				
			
		
Up until now we&#8217;ve focused on python applications that only run in the command line.  For a lot of tasks this is great, but in order to be really impressive we&#8217;re going to want to create a GUI for some of our python applications.
There are a lot of python GUI toolkits out there, but [...]]]></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%2F02%2F08%2Fcreating-a-gui-in-python-using-tkinter%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F02%2F08%2Fcreating-a-gui-in-python-using-tkinter%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Up until now we&#8217;ve focused on python applications that only run in the command line.  For a lot of tasks this is great, but in order to be really impressive we&#8217;re going to want to create a GUI for some of our python applications.</p>
<p>There are a lot of python GUI toolkits out there, but for our first application we are going to use Tkinter, which is considered somewhat of a <em>standard</em> right now and is installed when pyhon is installed.  Another nice thing about the Tkinter GUI is that it is cross platform and offers a native look and feel on *nix, Windows, and OS X.</p>
<h3>Tkinter Example One</h3>
<p>So lets start off with a simply &#8220;Hello World&#8221; GUI app:</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">from </span><span class="hl-identifier">Tkinter </span><span class="hl-reserved">import</span><span class="hl-default"> *

</span><span class="hl-reserved">class </span><span class="hl-identifier">GUIFramework</span><span class="hl-brackets">(</span><span class="hl-identifier">Frame</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 GUI</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">master</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-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 the base class</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">Frame</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">master</span><span class="hl-brackets">)
        
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Display the main window</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">grid</span><span class="hl-brackets">()
        
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Create the Text</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">HelloLabel</span><span class="hl-default"> = </span><span class="hl-identifier">Label</span><span class="hl-brackets">(</span><span class="hl-identifier">master</span><span class="hl-code">, </span><span class="hl-identifier">text</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Hello World!</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">HelloLabel</span><span class="hl-default">.</span><span class="hl-identifier">grid</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">guiFrame</span><span class="hl-default"> = </span><span class="hl-identifier">GUIFramework</span><span class="hl-brackets">()
    </span><span class="hl-identifier">guiFrame</span><span class="hl-default">.</span><span class="hl-identifier">mainloop</span><span class="hl-brackets">()</span></pre></div></div>
<p>Running this on OS X results in the following:</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/HelloWorld.png" alt="hello world python gui" /></p>
<p>And running it in Windows Xp gives you:</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/WinPyGUI1.png" alt="hello world python gui" /><br />
<span id="more-11"></span><br />
As you can see the code used to create the GUI is pretty simple and easy to understand, but it is actually more complicated then need be because it uses classes.  But since I&#8217;ll be using classes whenever building a GUI I thought that this was the best starting point.  We start off by importing the Tkinter library into our program using this following:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-reserved">from </span><span class="hl-identifier">Tkinter </span><span class="hl-reserved">import</span><span class="hl-default"> *</span></pre></div></div>
<p>After that we create a class called <em>GUIFramework</em>, which is the class that will be used to great the GUI elements in Tkinter and uses the Tkinter <a href="http://www.pythonware.com/library/tkinter/introduction/frame.htm#AEN4819">frame</a> class as its base.  For now all the work that we do is done in the <em>__init__</em> function. </p>
<p>There we first initialize the base class (which will initialize Tkinter) and then tell ourselves to be visible:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Initialise the base class</span><span class="hl-quotes">&quot;&quot;&quot;
</span><span class="hl-identifier">Frame</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">master</span><span class="hl-brackets">)

</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Display the main window</span><span class="hl-quotes">&quot;&quot;&quot;
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">grid</span><span class="hl-brackets">()</span></pre></div></div>
<p>To display ourselves we use the <a href="http://www.pythonware.com/library/tkinter/introduction/grid.htm">grid geometry manager</a>, the grid geometry manager is a straight forward way to display widgets and position them.  Since we have no other widgets in this example the defaults are fine, take a look at example two for for information on widget placement using the grid function.</p>
<p>Next is the process of creating the <em>HelloWorld</em> <a href="http://www.pythonware.com/library/tkinter/introduction/label.htm">label widget</a>,  which is a member of the <em>GUIFramework</em> class, and setting it to be visible.  It&#8217;s a good idea to make your widgets members of the class in case you need to access them later, but if you&#8217;re sure that you&#8217;ll never have to access them later, you can just create them locally.</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">HelloLabel</span><span class="hl-default"> = </span><span class="hl-identifier">Label</span><span class="hl-brackets">(</span><span class="hl-identifier">master</span><span class="hl-code">, </span><span class="hl-identifier">text</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">Hello World!</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">HelloLabel</span><span class="hl-default">.</span><span class="hl-identifier">grid</span><span class="hl-brackets">()</span></pre></div></div>
<p>After that all the other processing is done in the &#8220;main&#8221; section of our code where we initialize our class and tell it to enter the &#8220;mainloop&#8221;.    The &#8220;mainloop&#8221; is basically the application responding to events:</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">guiFrame</span><span class="hl-default"> = </span><span class="hl-identifier">GUIFramework</span><span class="hl-brackets">()
    </span><span class="hl-identifier">guiFrame</span><span class="hl-default">.</span><span class="hl-identifier">mainloop</span><span class="hl-brackets">()</span></pre></div></div>
<h3>Tkinter Example Two</h3>
<p>Next I&#8217;ll show you a more complicated example, I won&#8217;t go into specifics with this one since it&#8217;s still pretty simple.  But if you have any questions about the following code feel free to ask via a comment.</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">from </span><span class="hl-identifier">Tkinter </span><span class="hl-reserved">import</span><span class="hl-default"> *
</span><span class="hl-reserved">import </span><span class="hl-identifier">tkMessageBox

</span><span class="hl-reserved">class </span><span class="hl-identifier">GUIFramework</span><span class="hl-brackets">(</span><span class="hl-identifier">Frame</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 GUI</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">master</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-quotes">&quot;&quot;&quot;</span><span class="hl-string">Initialize yourself</span><span class="hl-quotes">&quot;&quot;&quot;
        
        &quot;&quot;&quot;</span><span class="hl-string">Initialise the base class</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">Frame</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">master</span><span class="hl-brackets">)
        
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Set the Window Title</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">master</span><span class="hl-default">.</span><span class="hl-identifier">title</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Type Some Text</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
        
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Display the main window&quot;
        with a little bit of padding</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">grid</span><span class="hl-brackets">(</span><span class="hl-identifier">padx</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-code">,</span><span class="hl-identifier">pady</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">CreateWidgets</span><span class="hl-brackets">()
       
    </span><span class="hl-reserved">def </span><span class="hl-identifier">CreateWidgets</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 all the widgets that we need</span><span class="hl-quotes">&quot;&quot;&quot;
                
        &quot;&quot;&quot;</span><span class="hl-string">Create the Text</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lbText</span><span class="hl-default"> = </span><span class="hl-identifier">Label</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;</span><span class="hl-string">Enter Text:</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">lbText</span><span class="hl-default">.</span><span class="hl-identifier">grid</span><span class="hl-brackets">(</span><span class="hl-identifier">row</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-identifier">column</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">Create the Entry, set it to be a bit wider</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">enText</span><span class="hl-default"> = </span><span class="hl-identifier">Entry</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">enText</span><span class="hl-default">.</span><span class="hl-identifier">grid</span><span class="hl-brackets">(</span><span class="hl-identifier">row</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-identifier">column</span><span class="hl-code">=</span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-identifier">columnspan</span><span class="hl-code">=</span><span class="hl-number">3</span><span class="hl-brackets">)
        
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Create the Button, set the text and the 
        command that will be called when the button is clicked</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">btnDisplay</span><span class="hl-default"> = </span><span class="hl-identifier">Button</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;</span><span class="hl-string">Display!</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">command</span><span class="hl-code">=</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">Display</span><span class="hl-brackets">)
        </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">btnDisplay</span><span class="hl-default">.</span><span class="hl-identifier">grid</span><span class="hl-brackets">(</span><span class="hl-identifier">row</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">, </span><span class="hl-identifier">column</span><span class="hl-code">=</span><span class="hl-number">4</span><span class="hl-brackets">)
        
    </span><span class="hl-reserved">def </span><span class="hl-identifier">Display</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
        </span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Called when btnDisplay is clicked, displays the contents of self.enText</span><span class="hl-quotes">&quot;&quot;&quot;
        </span><span class="hl-identifier">tkMessageBox</span><span class="hl-default">.</span><span class="hl-identifier">showinfo</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Text</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">You typed: %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">enText</span><span class="hl-code">.</span><span class="hl-identifier">get</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">guiFrame</span><span class="hl-default"> = </span><span class="hl-identifier">GUIFramework</span><span class="hl-brackets">()
    </span><span class="hl-identifier">guiFrame</span><span class="hl-default">.</span><span class="hl-identifier">mainloop</span><span class="hl-brackets">()</span></pre></div></div>
<p>This creates the following window:<br />
<img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/GuiWnd2.png" alt="hello world python gui" /></p>
<p>That shows this dialog when the button is pressed:<br />
<img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/GuiWnd2Res.png" alt="hello world python gui" /></p>
<p>Links that helped me get through this post:</p>
<ul>
<li><a href="http://www.pythonware.com/library/tkinter/introduction/">http://www.pythonware.com/library/tkinter/introduction/</a></li>
<li><a href="http://infohost.nmt.edu/tcc/help/pubs/tkinter/">http://infohost.nmt.edu/tcc/help/pubs/tkinter/</a></li>
</ul>
<p>If you like this post remember to <a href="http://digg.com/programming/Creating_a_GUI_in_Python_using_Tkinter">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/02/08/creating-a-gui-in-python-using-tkinter/&title=Creating a GUI in Python using Tkinter&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/02/08/creating-a-gui-in-python-using-tkinter/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>RSS reader &#8211; Part Three &#8211; Generator Class</title>
		<link>http://www.learningpython.com/2006/01/30/rss-reader-part-three-generator-class/</link>
		<comments>http://www.learningpython.com/2006/01/30/rss-reader-part-three-generator-class/#comments</comments>
		<pubDate>Tue, 31 Jan 2006 04:51:01 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[beginnings]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rss reader]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=13</guid>
		<description><![CDATA[
			
				
			
		
Please remember to read part one and part two.
Classes and Generators
All right, now that we have split our RSS reader up into functions, we&#8217;re going to go one step further and put our code into a class.
We&#8217;re also going to do something a bit more advanced with our class and create a generator.  The [...]]]></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%2F01%2F30%2Frss-reader-part-three-generator-class%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F01%2F30%2Frss-reader-part-three-generator-class%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Please remember to read <a href="http://www.learningpython.com/2006/01/14/rss-reader-part-one/">part one</a> and <a href="http://www.learningpython.com/2006/01/24/rss-reader-part-two-and-functions/">part two</a>.</p>
<h3>Classes and Generators</h3>
<p>All right, now that we have split our RSS reader up into <a href="http://www.learningpython.com/2006/01/24/rss-reader-part-two-and-functions/">functions</a>, we&#8217;re going to go one step further and put our code into a <a href="http://www.learningpython.com/2006/01/28/classes/">class</a>.</p>
<p>We&#8217;re also going to do something a bit more advanced with our class and create a generator.  The reason that we are going to do this is because python has such nice iteration handling and because in the future we&#8217;ll probably want to handle each RSS item individually rather then simply dumping it out to the terminal.</p>
<p><a href="http://www.python.org/doc/2.4.2/tut/node11.html#SECTION00111000000000000000000">Generators</a> are basically a type of <a href="http://www.python.org/doc/2.4.2/tut/node11.html#SECTION0011900000000000000000">iterator</a>, except their syntax is slightly different.  In fact anything you can accomplish in a generator you can accomplish in a standard iterator. </p>
<p>Here is an example generator that yields all the factors of the specified number:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">factors </span><span class="hl-brackets">(</span><span class="hl-identifier">num</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-identifier">count</span><span class="hl-default"> = </span><span class="hl-number">1</span><span class="hl-default">;
	</span><span class="hl-reserved">while </span><span class="hl-identifier">count</span><span class="hl-default"> &lt; = </span><span class="hl-identifier">num</span><span class="hl-default">/</span><span class="hl-number">2</span><span class="hl-default">:
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">num</span><span class="hl-code"> % </span><span class="hl-identifier">count</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-reserved">yield </span><span class="hl-identifier">count
		count</span><span class="hl-default"> = </span><span class="hl-identifier">count</span><span class="hl-default"> + </span><span class="hl-number">1
	</span><span class="hl-reserved">yield </span><span class="hl-identifier">num</span></pre></div></div>
<p><span id="more-13"></span></p>
<p>The special statements that make this function a generator are those two yield statements.  (The second yield statement is simply to return the number itself, since a number is always a factor of itself.)  The yield statement basically returns the data to the caller and the next time the function is iterated on, the function continues exactly where it left off.</p>
<p>You call the generator in the following way:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">for </span><span class="hl-identifier">res </span><span class="hl-reserved">in </span><span class="hl-identifier">factors</span><span class="hl-brackets">(</span><span class="hl-number">30</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-reserved">print </span><span class="hl-identifier">res</span></pre></div></div>
<p>With the following results:</p>
<p><code><br />
1<br />
2<br />
3<br />
5<br />
6<br />
10<br />
15<br />
30</p>
<h3>Code</h3>
<p>As you can see having something like this would be very useful in our RSS reader.  In order to accomplish this the first new addition to the code that we created in <a href="http://www.learningpython.com/2006/01/24/rss-reader-part-two-and-functions/">part two</a>  is the <em>RSSItem</em> class.  The <em>RSSItem</em> class basically represents one RSS item.  For the time being it is going to contain only the title and the description.  The nice thing about having this in a separate class is that we can add new members at any time without affecting existing code:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">RSSItem</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This is an RSS item, it contain all the RSS info like Tile and Description</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">title</span><span class="hl-code">=</span><span class="hl-quotes">&quot;&quot;</span><span class="hl-code">,</span><span class="hl-identifier">description</span><span class="hl-code">=</span><span class="hl-quotes">&quot;&quot;</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">title</span><span class="hl-default"> = </span><span class="hl-identifier">title
		self</span><span class="hl-default">.</span><span class="hl-identifier">description</span><span class="hl-default"> = </span><span class="hl-identifier">description</span></pre></div></div>
<p>You'll notice that the RSSItem class has no functions besides the <em>__init__</em> which takes two optional parameters the title and the description, and then sets two data members equal to those values.</p>
<p>The next thing that we are going to define is out main class, we'll call it <em>RSSReader</em> for lack of a better name:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">RSSReader</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This class is an RSS reader, it should have a better docstring</span><span class="hl-quotes">&quot;&quot;&quot;</span></pre></div></div>
<p>Here is the <em>__init__</em> function, which takes one parameter, the <em>RSSUrl</em>.  The <em>__init__</em> function also does some "pre-processing" and gets the XML document:</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">RSSUrl</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 class</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">RSSUrl</span><span class="hl-default"> = </span><span class="hl-identifier">RSSUrl</span><span class="hl-default">;
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">xmldoc</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">GetXMLDocument</span><span class="hl-brackets">(</span><span class="hl-identifier">RSSUrl</span><span class="hl-brackets">)
	</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-reserved">not </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">xmldoc</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error Getting XML Document!</span><span class="hl-quotes">&quot;</span></pre></div></div>
<p>The RSSUrl class has two member variables, <em>RSSUrl</em> and <em>xmldoc</em>.  You'll also notice that the <em>__init__</em> function calls  a member function of <em>RSSReader</em> called <em>GetXMLDocument</em>, which does exactly what it says:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">GetXMLDocument</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">,</span><span class="hl-identifier">RSSUrl</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 reads in a RSS URL and then</span><span class="hl-quotes">&quot;&quot;&quot;
	&quot;&quot;&quot;</span><span class="hl-string">returns the XML document on success</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">url_info</span><span class="hl-default"> = </span><span class="hl-identifier">urllib2</span><span class="hl-default">.</span><span class="hl-identifier">urlopen</span><span class="hl-brackets">(</span><span class="hl-identifier">RSSUrl</span><span class="hl-brackets">)
	</span><span class="hl-identifier">xmldoc</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">url_info</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">xmldoc</span><span class="hl-default"> = </span><span class="hl-identifier">minidom</span><span class="hl-default">.</span><span class="hl-identifier">parse</span><span class="hl-brackets">(</span><span class="hl-identifier">url_info</span><span class="hl-brackets">)
	</span><span class="hl-reserved">else</span><span class="hl-default">	:
		</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error Getting URL</span><span class="hl-quotes">&quot;
	</span><span class="hl-reserved">return </span><span class="hl-identifier">xmldoc</span></pre></div></div>
<p>The next function that I'm going to show you is the generator, and how simply it actually is:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">GetItems</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">Generator to get items</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-reserved">for </span><span class="hl-identifier">item_node </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">xmldoc</span><span class="hl-default">.</span><span class="hl-identifier">documentElement</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
			</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">item</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:
				</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">All right we have an item</span><span class="hl-quotes">&quot;&quot;&quot;
				</span><span class="hl-identifier">rss_item</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">CreateRSSItem</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-brackets">)
				</span><span class="hl-reserved">yield </span><span class="hl-identifier">rss_item</span></pre></div></div>
<p>You'll see that all this generator does is iterate through all of the nodes in the XML document until it comes to a node named "Item".  Once a node named "item" is encountered a member function called <em>CreateRSSItem</em> (Which creates an RSSItem) is called and then that RSSItem is yielded to the caller.</p>
<p><em>CreateRSSItem</em> is another simply function:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">CreateRSSItem</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">,</span><span class="hl-identifier">item_node</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Create an RSS item and return it</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">title</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">GetChildText</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">,</span><span class="hl-quotes">&quot;</span><span class="hl-string">title</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
	</span><span class="hl-identifier">description</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">GetChildText</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">,</span><span class="hl-quotes">&quot;</span><span class="hl-string">description</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
	</span><span class="hl-reserved">return </span><span class="hl-identifier">RSSItem</span><span class="hl-brackets">(</span><span class="hl-identifier">title</span><span class="hl-code">,</span><span class="hl-identifier">description</span><span class="hl-brackets">)</span></pre></div></div>
<p>All this function does is call the member function <em>GetChildText</em> for the "title" and "description" values.  <em>GetChildText</em> basically searches through the passed xml node's children searching for a match to the second parameter, and if one is found  that item's text is returned.</p>
<p>Then <em>CreateRSS</em> item call the constructor of the <em>RSSItem</em> class and returns the created class: </code><code>return RSSItem(title,description)</code></p>
<p>There are only two more functions left, both of which are (as you might have guessed) very simply.  This is the beauty of using functions, you get to break up the code into manageable and reusable tasks.  The two functions that are left are <em>GetChildText</em> and <em>GetItemText</em>.</p>
<p>Where <em>GetChildText</em> searched through childNodes for a matching item, <em>GetItemText</em> simply returns an XML node&#8217;s text:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">GetItemText</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">,</span><span class="hl-identifier">xml_node</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 text from an xml item</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;&quot;
	</span><span class="hl-reserved">for </span><span class="hl-identifier">text_node </span><span class="hl-reserved">in </span><span class="hl-identifier">xml_node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">text_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeType</span><span class="hl-code"> == </span><span class="hl-identifier">Node</span><span class="hl-code">.</span><span class="hl-identifier">TEXT_NODE</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-identifier">text</span><span class="hl-default"> += </span><span class="hl-identifier">text_node</span><span class="hl-default">.</span><span class="hl-identifier">nodeValue
	</span><span class="hl-reserved">return </span><span class="hl-identifier">text

</span><span class="hl-reserved">def </span><span class="hl-identifier">GetChildText</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">xml_node</span><span class="hl-code">, </span><span class="hl-identifier">child_name</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 child node from the xml node</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-reserved">not </span><span class="hl-identifier">xml_node</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error GetChildNode: No xml_node</span><span class="hl-quotes">&quot;
		</span><span class="hl-reserved">return </span><span class="hl-quotes">&quot;&quot;
	</span><span class="hl-reserved">for </span><span class="hl-identifier">item_node </span><span class="hl-reserved">in </span><span class="hl-identifier">xml_node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code">==</span><span class="hl-identifier">child_name</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">GetItemText</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-brackets">)
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Return Nothing</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-reserved">return </span><span class="hl-quotes">&quot;&quot;</span></pre></div></div>
<p>All that&#8217;s left is to show how we create an instance of the RSSReader class, and then use the generator to iterate through all of the <em>RSSItems</em>:</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">rss_reader</span><span class="hl-default"> = </span><span class="hl-identifier">RSSReader</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">http://rss.slashdot.org/Slashdot/slashdot</span><span class="hl-quotes">'</span><span class="hl-brackets">)
	</span><span class="hl-reserved">for </span><span class="hl-identifier">rss_item </span><span class="hl-reserved">in </span><span class="hl-identifier">rss_reader</span><span class="hl-default">.</span><span class="hl-identifier">GetItems</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">rss_item</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-reserved">print </span><span class="hl-identifier">rss_item</span><span class="hl-default">.</span><span class="hl-identifier">title
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;&quot;
			</span><span class="hl-reserved">print </span><span class="hl-identifier">rss_item</span><span class="hl-default">.</span><span class="hl-identifier">description
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;&quot;</span></pre></div></div>
<p>Here is the code in its entirety, it call also be downloaded from here:</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">urllib2
</span><span class="hl-reserved">from </span><span class="hl-identifier">xml</span><span class="hl-default">.</span><span class="hl-identifier">dom </span><span class="hl-reserved">import </span><span class="hl-identifier">minidom</span><span class="hl-default">, </span><span class="hl-identifier">Node

</span><span class="hl-reserved">class </span><span class="hl-identifier">RSSItem</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This is an RSS item, it contain all the RSS info like Tile and Description</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">title</span><span class="hl-code">=</span><span class="hl-quotes">&quot;&quot;</span><span class="hl-code">,</span><span class="hl-identifier">description</span><span class="hl-code">=</span><span class="hl-quotes">&quot;&quot;</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">title</span><span class="hl-default"> = </span><span class="hl-identifier">title
		self</span><span class="hl-default">.</span><span class="hl-identifier">description</span><span class="hl-default"> = </span><span class="hl-identifier">description

</span><span class="hl-reserved">class </span><span class="hl-identifier">RSSReader</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">This class is an RSS reader, it should have a better docstring</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">RSSUrl</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 class</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">RSSUrl</span><span class="hl-default"> = </span><span class="hl-identifier">RSSUrl</span><span class="hl-default">;
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">xmldoc</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">GetXMLDocument</span><span class="hl-brackets">(</span><span class="hl-identifier">RSSUrl</span><span class="hl-brackets">)
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-reserved">not </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">xmldoc</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error Getting XML Document!</span><span class="hl-quotes">&quot;
		
	</span><span class="hl-reserved">def </span><span class="hl-identifier">GetXMLDocument</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">,</span><span class="hl-identifier">RSSUrl</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 reads in a RSS URL and then</span><span class="hl-quotes">&quot;&quot;&quot;
		&quot;&quot;&quot;</span><span class="hl-string">returns the XML documentn on success</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">url_info</span><span class="hl-default"> = </span><span class="hl-identifier">urllib2</span><span class="hl-default">.</span><span class="hl-identifier">urlopen</span><span class="hl-brackets">(</span><span class="hl-identifier">RSSUrl</span><span class="hl-brackets">)
		</span><span class="hl-identifier">xmldoc</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">url_info</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-identifier">xmldoc</span><span class="hl-default"> = </span><span class="hl-identifier">minidom</span><span class="hl-default">.</span><span class="hl-identifier">parse</span><span class="hl-brackets">(</span><span class="hl-identifier">url_info</span><span class="hl-brackets">)
		</span><span class="hl-reserved">else</span><span class="hl-default">	:
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error Getting URL</span><span class="hl-quotes">&quot;
		</span><span class="hl-reserved">return </span><span class="hl-identifier">xmldoc
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">GetItemText</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">,</span><span class="hl-identifier">xml_node</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 text from an xml item</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;&quot;
		</span><span class="hl-reserved">for </span><span class="hl-identifier">text_node </span><span class="hl-reserved">in </span><span class="hl-identifier">xml_node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
			</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">text_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeType</span><span class="hl-code"> == </span><span class="hl-identifier">Node</span><span class="hl-code">.</span><span class="hl-identifier">TEXT_NODE</span><span class="hl-brackets">)</span><span class="hl-default">:
				</span><span class="hl-identifier">text</span><span class="hl-default"> += </span><span class="hl-identifier">text_node</span><span class="hl-default">.</span><span class="hl-identifier">nodeValue
		</span><span class="hl-reserved">return </span><span class="hl-identifier">text
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">GetChildText</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">xml_node</span><span class="hl-code">, </span><span class="hl-identifier">child_name</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 child node from the xml node</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-reserved">not </span><span class="hl-identifier">xml_node</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error GetChildNode: No xml_node</span><span class="hl-quotes">&quot;
			</span><span class="hl-reserved">return </span><span class="hl-quotes">&quot;&quot;
		</span><span class="hl-reserved">for </span><span class="hl-identifier">item_node </span><span class="hl-reserved">in </span><span class="hl-identifier">xml_node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
			</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code">==</span><span class="hl-identifier">child_name</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">GetItemText</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-brackets">)
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Return Nothing</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-reserved">return </span><span class="hl-quotes">&quot;&quot;
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">CreateRSSItem</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">,</span><span class="hl-identifier">item_node</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Create an RSS item and return it</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">title</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">GetChildText</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">,</span><span class="hl-quotes">&quot;</span><span class="hl-string">title</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
		</span><span class="hl-identifier">description</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">GetChildText</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">,</span><span class="hl-quotes">&quot;</span><span class="hl-string">description</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
		</span><span class="hl-reserved">return </span><span class="hl-identifier">RSSItem</span><span class="hl-brackets">(</span><span class="hl-identifier">title</span><span class="hl-code">,</span><span class="hl-identifier">description</span><span class="hl-brackets">)
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">GetItems</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">Generator to get items</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-reserved">for </span><span class="hl-identifier">item_node </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">xmldoc</span><span class="hl-default">.</span><span class="hl-identifier">documentElement</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
				</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">item</span><span class="hl-quotes">&quot;</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 an item</span><span class="hl-quotes">&quot;&quot;&quot;
					</span><span class="hl-identifier">rss_item</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">CreateRSSItem</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-brackets">)
					</span><span class="hl-reserved">yield </span><span class="hl-identifier">rss_item
					
</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">rss_reader</span><span class="hl-default"> = </span><span class="hl-identifier">RSSReader</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">http://rss.slashdot.org/Slashdot/slashdot</span><span class="hl-quotes">'</span><span class="hl-brackets">)
	</span><span class="hl-reserved">for </span><span class="hl-identifier">rss_item </span><span class="hl-reserved">in </span><span class="hl-identifier">rss_reader</span><span class="hl-default">.</span><span class="hl-identifier">GetItems</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">rss_item</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-reserved">print </span><span class="hl-identifier">rss_item</span><span class="hl-default">.</span><span class="hl-identifier">title
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;&quot;
			</span><span class="hl-reserved">print </span><span class="hl-identifier">rss_item</span><span class="hl-default">.</span><span class="hl-identifier">description
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;&quot;</span></pre></div></div>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2006/01/30/rss-reader-part-three-generator-class/&title=RSS reader - Part Three - Generator Class&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/01/30/rss-reader-part-three-generator-class/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>RSS reader &#8211; Part Two (and Functions)</title>
		<link>http://www.learningpython.com/2006/01/24/rss-reader-part-two-and-functions/</link>
		<comments>http://www.learningpython.com/2006/01/24/rss-reader-part-two-and-functions/#comments</comments>
		<pubDate>Tue, 24 Jan 2006 22:55:34 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[beginnings]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rss reader]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=10</guid>
		<description><![CDATA[
			
				
			
		
This post is my continuation of my Python based RSS reader that I wrote in part one.  As I said the code written in part one is not something that you would ever really want to use or maintain since it wasn&#8217;t broken up in to functions properly.  So, in this part we&#8217;re [...]]]></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%2F01%2F24%2Frss-reader-part-two-and-functions%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F01%2F24%2Frss-reader-part-two-and-functions%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>This post is my continuation of my Python based RSS reader that I wrote in <a href="http://www.learningpython.com/2006/01/14/rss-reader-part-one/">part one</a>.  As I said the code written in part one is not something that you would ever really want to use or maintain since it wasn&#8217;t broken up in to functions properly.  So, in this part we&#8217;re going to work on breaking the old script up into functions.</p>
<h3>Functions</h3>
<p>Functions are defined in python using the <em>def</em> keyword.  So if I wanted to define a function called &#8220;count&#8221; that counts from 1 to a certain number I would do so like so:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">count</span><span class="hl-brackets">(</span><span class="hl-identifier">nNum</span><span class="hl-brackets">)</span><span class="hl-default">:</span></pre></div></div>
<p>Where <em>count</em> is the name of the function and <em>nNum</em> is a parameter that is being passed into the function.  If I wanted to call the function I would do so like this:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-identifier">count</span><span class="hl-brackets">(</span><span class="hl-number">10</span><span class="hl-brackets">)</span></pre></div></div>
<p><span id="more-10"></span><br />
Here is the entire code for the count function:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">count</span><span class="hl-brackets">(</span><span class="hl-identifier">nNum</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-identifier">x</span><span class="hl-default"> = </span><span class="hl-number">1
    </span><span class="hl-reserved">while </span><span class="hl-identifier">x</span><span class="hl-default"> &amp;</span><span class="hl-identifier">lt</span><span class="hl-default">: </span><span class="hl-identifier">nNum</span><span class="hl-default"> :
        </span><span class="hl-reserved">print </span><span class="hl-identifier">x
        x</span><span class="hl-default"> = </span><span class="hl-identifier">x</span><span class="hl-default"> +</span><span class="hl-number">1</span></pre></div></div>
<p>Another interesting feature that python uses are <a href="http://www.python.org/doc/2.4.2/tut/node6.html#SECTION006760000000000000000">Documentation Strings</a>, these are basically comments that help document the code for you and can also be read by documentation tools like <a href="http://lfw.org/python/pydoc.html">pyDoc</a> to create formatted documentation.  Since this is becoming a standard it&#8217;s a good way for other people, and you, to be able to easily see what your program does.</p>
<p>So for the count function we might add something like this:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">count</span><span class="hl-brackets">(</span><span class="hl-identifier">nNum</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 prints out the numbers from 1 to nNum</span><span class="hl-quotes">&quot;&quot;&quot;
    </span><span class="hl-identifier">x</span><span class="hl-default"> = </span><span class="hl-number">1
    </span><span class="hl-reserved">while </span><span class="hl-identifier">x</span><span class="hl-default"> &amp;</span><span class="hl-identifier">lt</span><span class="hl-default">: </span><span class="hl-identifier">nNum</span><span class="hl-default"> :
        </span><span class="hl-reserved">print </span><span class="hl-identifier">x
        x</span><span class="hl-default"> = </span><span class="hl-identifier">x</span><span class="hl-default"> +</span><span class="hl-number">1</span></pre></div></div>
<h3>The Code</h3>
<p>We will be reusing the code that was already written in Part One for this section.</p>
<p>Now what we need to do is break up the code that we used in <a href="http://www.learningpython.com/2006/01/14/rss-reader-part-one/">part one</a> so that it uses functions.  The first thing that we are going to do is instead of always using the same location for our RSS we are going to write a function that takes an RSS url as it parameter and then attempts to retrieve it&#8217;s RSS information.  We&#8217;ll call that function <em>GetRSS</em>:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">GetRSS</span><span class="hl-brackets">(</span><span class="hl-identifier">RSSurl</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 attempts to get the RSS info using RSSurl as the RSS url</span><span class="hl-quotes">&quot;&quot;&quot;

	</span><span class="hl-identifier">url_info</span><span class="hl-default"> = </span><span class="hl-identifier">urllib2</span><span class="hl-default">.</span><span class="hl-identifier">urlopen</span><span class="hl-brackets">(</span><span class="hl-identifier">RSSurl</span><span class="hl-brackets">)
	</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">url_info</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 retrieve the RSS url properly, now let's parse it up</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">xmldoc</span><span class="hl-default"> = </span><span class="hl-identifier">minidom</span><span class="hl-default">.</span><span class="hl-identifier">parse</span><span class="hl-brackets">(</span><span class="hl-identifier">url_info</span><span class="hl-brackets">)
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">xmldoc</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Loop through all children of the main document</span><span class="hl-quotes">&quot;&quot;&quot;
			</span><span class="hl-reserved">for </span><span class="hl-identifier">item_node </span><span class="hl-reserved">in </span><span class="hl-identifier">xmldoc</span><span class="hl-default">.</span><span class="hl-identifier">documentElement</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
				</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">item</span><span class="hl-quotes">&quot;</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 have found an item print out the title 
					and description</span><span class="hl-quotes">&quot;&quot;&quot;
					</span><span class="hl-identifier">PrintNodeItems</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">, </span><span class="hl-brackets">[</span><span class="hl-quotes">&quot;</span><span class="hl-string">title</span><span class="hl-quotes">&quot;</span><span class="hl-code">,</span><span class="hl-quotes">&quot;</span><span class="hl-string">description</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">])
		</span><span class="hl-reserved">else</span><span class="hl-default">:
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error parsing url into xml</span><span class="hl-quotes">&quot;
	</span><span class="hl-reserved">else</span><span class="hl-default">:
        	</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error! Getting URL</span><span class="hl-quotes">&quot;</span></pre></div></div>
<p>You&#8217;ll notice that <em>GetRSS</em> is very similar to the majority of code that we had in Part One.  Basically it gets the RSS XML from a specific URL, then it loops through all of the child nodes in the XML looking for <em>item</em> nodes.  Once an node with the name of &#8220;item” is found the following new code is called:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-identifier">PrintNodeItems</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">, </span><span class="hl-brackets">[</span><span class="hl-quotes">&quot;</span><span class="hl-string">title</span><span class="hl-quotes">&quot;</span><span class="hl-code">,</span><span class="hl-quotes">&quot;</span><span class="hl-string">description</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">])</span></pre></div></div>
<p>This line calls another new function in the code called <em>PrintNodeItems</em>, passing it our <em>item</em> node and a list of items:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">PrintNodeItems</span><span class="hl-brackets">(</span><span class="hl-identifier">XmlNode</span><span class="hl-code">, </span><span class="hl-identifier">items</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 prints out all children of XmlNode found in items</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-reserved">for </span><span class="hl-identifier">item_node </span><span class="hl-reserved">in </span><span class="hl-identifier">XmlNode</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
		</span><span class="hl-reserved">if </span><span class="hl-identifier">item_node</span><span class="hl-default">.</span><span class="hl-identifier">nodeName </span><span class="hl-reserved">in </span><span class="hl-identifier">items</span><span class="hl-default">:
			</span><span class="hl-identifier">PrintNodesText</span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-brackets">)</span></pre></div></div>
<p><em>PrintNodeItems</em> is a simple function that loops through all of the <em>childNodes</em> found in <em>XmlNode</em> and check to see if the childNodes <em>nodeName</em> is in the list <em>items</em>.  If it is in the list, then the function <em>PrintNodesText</em> is called.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">PrintNodesText</span><span class="hl-brackets">(</span><span class="hl-identifier">XmlNode</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 prints out an XML Nodes text nodes.</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;&quot;
	</span><span class="hl-reserved">for </span><span class="hl-identifier">text_node </span><span class="hl-reserved">in </span><span class="hl-identifier">XmlNode</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">text_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeType</span><span class="hl-code"> == </span><span class="hl-identifier">Node</span><span class="hl-code">.</span><span class="hl-identifier">TEXT_NODE</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-identifier">text</span><span class="hl-default"> += </span><span class="hl-identifier">text_node</span><span class="hl-default">.</span><span class="hl-identifier">nodeValue
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Noe print out the text</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">text</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-reserved">print </span><span class="hl-identifier">text
		</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;&quot;</span></pre></div></div>
<p><em>PrintNodeItems</em> is another simple function that basically loops through an XmlNode&#8217;s children and prints out all <em>TEXT_NODES</em>.  You&#8217;ll notice that this code is identical to the code that we used to print out the &#8220;title&#8221; and &#8220;description&#8221; nodes in Part One.  The difference this time is that instead of duplicating code in two spots we simple create one function that gets called twice.</p>
<p>Now we have the a simple <em>main()</em> function that calls <em>GetRSS</em>:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">main</span><span class="hl-brackets">()</span><span class="hl-default">:
	</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Main Function</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">GetRSS</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">http://rss.slashdot.org/Slashdot/slashdot</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span></pre></div></div>
<p>The final step is a method for getting the ball rolling, for starting the execution.  So far we&#8217;ve simply defined functions that perform tasks when called but how do we call the first function?</p>
<p>The answer is a simple check at the bottom of your python script to determine if the script was launched directly as a standalone script (which is all that we have been doing so far when we call out scripts via the command line):</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">main</span><span class="hl-brackets">()</span></pre></div></div>
<p>This is basically a way for the Python script to know if it has been launched directly, if it has not been launched directly and is been instantiated in a different manner __name__ will not equal &#8220;__main__&#8221; and the main function will not be called automatically.</p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2006/01/24/rss-reader-part-two-and-functions/&title=RSS reader - Part Two (and Functions)&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/01/24/rss-reader-part-two-and-functions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>RSS reader &#8211; Part One</title>
		<link>http://www.learningpython.com/2006/01/14/rss-reader-part-one/</link>
		<comments>http://www.learningpython.com/2006/01/14/rss-reader-part-one/#comments</comments>
		<pubDate>Sat, 14 Jan 2006 19:12:03 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[beginnings]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rss reader]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=9</guid>
		<description><![CDATA[
			
				
			
		
All right so I&#8217;ve already figured out how to write an executable script that writes out &#8220;Hello World!&#8221; to the command line, now I need to figure out how to do something interesting.
As a result I was surfing the Internet and reading some Python documentation, trying to come up with something to do but nothing [...]]]></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%2F01%2F14%2Frss-reader-part-one%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F01%2F14%2Frss-reader-part-one%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>All right so I&#8217;ve already figured out how to <a href="http://www.learningpython.com/2006/01/10/writing-my-first-python-script/">write an executable script</a> that writes out &#8220;Hello World!&#8221; to the command line, now I need to figure out how to do something interesting.</p>
<p>As a result I was surfing the Internet and reading some Python documentation, trying to come up with something to do but nothing seemed interesting enough or easy enough for me to do until I visited one of my favorite websites: <a href="http://slashdot.org/">slashdot.org</a>.</p>
<p>It was there that I noticed that RSS icon that <a href="http://www.mozilla.com/firefox/">Firefox</a> always shows me whenever I come across a website with an RSS feed (like this one.)</p>
<p>So I thought to myself, &#8216;Hmm I wonder how hard it would be to create a simple RSS reader in Python?&#8217;  Read on to discover the results.<span id="more-9"></span></p>
<p>First I had to look at Slashdots RSS XML to know what it looks like and what my code should be looking for.  If you copy and paste: <a href="http://rss.slashdot.org/Slashdot/slashdot">http://rss.slashdot.org/Slashdot/slashdot</a> into your web browser you will be able to take a look at the XML format.  The format is basically as follows:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-brackets">&lt;</span><span class="hl-code"> ?</span><span class="hl-reserved">xml </span><span class="hl-var">version</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">1.0</span><span class="hl-quotes">&quot; </span><span class="hl-var">encoding</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">UTF-8</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">?&gt;
&lt;</span><span class="hl-reserved">rdf </span><span class="hl-var">:RDF</span><span class="hl-brackets">&gt;
	
	&lt;</span><span class="hl-reserved">item </span><span class="hl-var">rdf:about</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">http://someulr</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;
		&lt;</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;</span><span class="hl-default">The Title</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;
		&lt;</span><span class="hl-reserved">link</span><span class="hl-brackets">&gt;</span><span class="hl-default">The Link</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">link</span><span class="hl-brackets">&gt;
		&lt;</span><span class="hl-reserved">description</span><span class="hl-brackets">&gt;</span><span class="hl-default">The Description</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">description</span><span class="hl-brackets">&gt;
		
	&lt;/</span><span class="hl-reserved">item</span><span class="hl-brackets">&gt;
	&lt;</span><span class="hl-reserved">item </span><span class="hl-var">rdf:about</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">http://someulr</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">&gt;
		&lt;</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;</span><span class="hl-default">The Title</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">title</span><span class="hl-brackets">&gt;
		&lt;</span><span class="hl-reserved">link</span><span class="hl-brackets">&gt;</span><span class="hl-default">The Link</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">link</span><span class="hl-brackets">&gt;
		&lt;</span><span class="hl-reserved">description</span><span class="hl-brackets">&gt;</span><span class="hl-default">The Description</span><span class="hl-brackets">&lt;/</span><span class="hl-reserved">description</span><span class="hl-brackets">&gt;
		
	&lt;/</span><span class="hl-reserved">item</span><span class="hl-brackets">&gt;
&lt;/</span><span class="hl-reserved">rdf</span><span class="hl-brackets">&gt;</span></pre></div></div>
<p>For this simple RSS reader I&#8217;m only going to pay attention to each &lt;item&gt; and ignore all other tags.  In each &lt;item&gt; I&#8217;m going to want to display the &lt;title&gt; and the &lt;description&gt;.  That way when we run this program well see the title and descriptions of all the items on Slashdot&#8217;s main page.</p>
<p>Now that I knew what I wanted to do I fired up my trusty copy of gedit and began searching the web for information.  By far the best site I&#8217;ve found for Python based information is the source, <a href="http://www.python.org/">python.org</a>.  There is tons of documentation and information on this site, everything a new programmer needs.  There may be sites out there that are better, but I haven&#8217;t found them yet.</p>
<p>Now that I knew what I was going to do, I needed to find some libraries to help me with my task, since there was no way I was going to do all of this myself.  Fortunately for me python has many <a href="http://docs.python.org/lib/lib.html">built-in libraries</a> that make doing complicated things a lot easier.</p>
<p>The first thing I needed to do was create a blank shell of a script based on my first executable python script.  I called my new file pythonRSS.py and edited it so that it only contained the following:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-comment">#! /usr/bin/env python</span></pre></div></div>
<p>After that I needed to include (sorry for the c++ terminology) the libraries that I needed to make my RSS reader:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">import </span><span class="hl-identifier">urllib2
</span><span class="hl-reserved">from </span><span class="hl-identifier">xml</span><span class="hl-default">.</span><span class="hl-identifier">dom </span><span class="hl-reserved">import </span><span class="hl-identifier">minidom</span><span class="hl-default">, </span><span class="hl-identifier">Node</span></pre></div></div>
<p>The first line gives me access to the library <a href="http://docs.python.org/lib/module-urllib2.html">urllib2</a>, and the second line imports the minidom and Node submodules from the<a href="http://www.python.org/doc/current/lib/module-xml.dom.html"> xml.dom</a> library and makes them available without the package prefix.  Had I just used the following line:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-reserved">import </span><span class="hl-identifier">xml</span><span class="hl-default">.</span><span class="hl-identifier">dom</span></pre></div></div>
<p>I would have had to reference the Node and minidom submodules using the package prefix: <code>xml.dom.Node</code>, instead I am able to reference them directly as <code>Node</code></p>
<p>Now to get down to the programming business, the first piece of the puzzle that I  needed was slashdot&#8217;s RSS XML, that&#8217;s pretty easy to get using the ulrlib2 library.  We download the RSS feed using the <a href="http://www.python.org/doc/current/lib/module-urllib.html">urlopen</a> function:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-identifier">url_info</span><span class="hl-default"> = </span><span class="hl-identifier">urllib2</span><span class="hl-default">.</span><span class="hl-identifier">urlopen</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">http://rss.slashdot.org/Slashdot/slashdot</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span></pre></div></div>
<p>This gives us a &#8220;file-like&#8221; object stored in url_info, if we wanted to run through the XML returned line-by-line we could actually use the following:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">for </span><span class="hl-identifier">lines </span><span class="hl-reserved">in </span><span class="hl-identifier">url_info</span><span class="hl-default">:
	</span><span class="hl-reserved">print </span><span class="hl-identifier">lines</span></pre></div></div>
<p>But since we have a fancy XML library at our disposal manually running through the file wouldn&#8217;t make much sense.  </p>
<p>Now that we have the RSS XML we are going to pass it to our <a href="http://www.python.org/doc/current/lib/module-xml.dom.minidom.html">minidom</a> object and get it to parse it into a document:</p>
<div class="hl-surround" style="height:28px;"><div class="hl-main"><pre><span class="hl-identifier">xmldoc</span><span class="hl-default"> = </span><span class="hl-identifier">minidom</span><span class="hl-default">.</span><span class="hl-identifier">parse</span><span class="hl-brackets">(</span><span class="hl-identifier">url_info</span><span class="hl-brackets">)</span></pre></div></div>
<p>The parse function parses up the XML into the Document Object Model, or DOM (as in minidom or xml.dom), and returns the document to us.  For more information on this please see the <a href="http://www.w3.org/TR/REC-DOM-Level-1/">Document Object Model specification</a>.</p>
<p>Now that we have the document, were going to get the root node (&lt;rdf :RDF&gt;) and then loop through all of its children nodes looking for &lt;item&gt; nodes.  This is actually really simply and intuitive:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-identifier">rootNode</span><span class="hl-default"> = </span><span class="hl-identifier">xmldoc</span><span class="hl-default">.</span><span class="hl-identifier">documentElement
</span><span class="hl-reserved">for </span><span class="hl-identifier">node </span><span class="hl-reserved">in </span><span class="hl-identifier">rootNode</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
	</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">item</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:</span></pre></div></div>
<p>The above is something that I am definitely learning to love about python, the ability to iterate through things easily.  Instead of having to redo the same iteration code for difference classes and different types (like I have to in C++) python makes this easy and intuitive using for loops or while loops.</p>
<p>Once we&#8217;ve found an &lt;item&gt; node, were just going to do that exact same thing we did above except this time we are going to look for &lt;title&gt; and &lt;description&gt; nodes:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">for </span><span class="hl-identifier">item_node </span><span class="hl-reserved">in </span><span class="hl-identifier">node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
	</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">title</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">title</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;&quot;
		</span><span class="hl-reserved">for </span><span class="hl-identifier">text_node </span><span class="hl-reserved">in </span><span class="hl-identifier">item_node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
			</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">text_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeType</span><span class="hl-code"> == </span><span class="hl-identifier">node</span><span class="hl-code">.</span><span class="hl-identifier">TEXT_NODE</span><span class="hl-brackets">)</span><span class="hl-default">:
				</span><span class="hl-identifier">title</span><span class="hl-default"> += </span><span class="hl-identifier">text_node</span><span class="hl-default">.</span><span class="hl-identifier">nodeValue
		if </span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">title</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-reserved">print </span><span class="hl-identifier">title

	if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">description</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">description</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;&quot;
		</span><span class="hl-reserved">for </span><span class="hl-identifier">text_node </span><span class="hl-reserved">in </span><span class="hl-identifier">item_node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
			</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">text_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeType</span><span class="hl-code"> == </span><span class="hl-identifier">node</span><span class="hl-code">.</span><span class="hl-identifier">TEXT_NODE</span><span class="hl-brackets">)</span><span class="hl-default">:
				</span><span class="hl-identifier">description</span><span class="hl-default"> += </span><span class="hl-identifier">text_node</span><span class="hl-default">.</span><span class="hl-identifier">nodeValue
		if </span><span class="hl-brackets">(</span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">description</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-reserved">print </span><span class="hl-identifier">description</span><span class="hl-default"> + </span><span class="hl-quotes">&quot;</span><span class="hl-special">\n</span><span class="hl-quotes">&quot;</span></pre></div></div>
<p>The above code is basically just a variation of the code that we used to get the iterate through all the children of the root node.  The only difference in this case is what we do when we find them, which is to iterate through all of their child nodes that are text nodes and then store that value for printing.  This is the only part of the script that felt a bit weird to me, shouldn&#8217;t it be more like <code>node.Text</code> or something like that?  Either way it works and that is basically the end of the RSS reader.  Just feed it into python or make it executabe and that&#8217;s it:</p>
<p><code>$ python pythonRSS.py</code></p>
<p>All-in-all this code didn&#8217;t take that long to write, a few google searches and I was on my way.  Truthfully witting this blog post took longer to write then the code did.</p>
<p>Please keep in mind that there are many things wrong with this code, the most obvious is that it make no use of functions to simplify the code.  But this is only part one of my RSS reader and with time I will fix up the code.</p>
<p>Here is the code in its entirety with some comments thrown in, alternatively you could also <a href="http://www.learningpython.com/sources/pythonRSS.txt">download the code</a> as  a text file:</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">urllib2
</span><span class="hl-reserved">from </span><span class="hl-identifier">xml</span><span class="hl-default">.</span><span class="hl-identifier">dom </span><span class="hl-reserved">import </span><span class="hl-identifier">minidom</span><span class="hl-default">, </span><span class="hl-identifier">Node

</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string"> Get the XML </span><span class="hl-quotes">&quot;&quot;&quot;
</span><span class="hl-identifier">url_info</span><span class="hl-default"> = </span><span class="hl-identifier">urllib2</span><span class="hl-default">.</span><span class="hl-identifier">urlopen</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">http://rss.slashdot.org/Slashdot/slashdot</span><span class="hl-quotes">'</span><span class="hl-brackets">)

</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">url_info</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 the RSS XML lets try to parse it up </span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">xmldoc</span><span class="hl-default"> = </span><span class="hl-identifier">minidom</span><span class="hl-default">.</span><span class="hl-identifier">parse</span><span class="hl-brackets">(</span><span class="hl-identifier">url_info</span><span class="hl-brackets">)
	</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">xmldoc</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 the Doc, get the root node</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">rootNode</span><span class="hl-default"> = </span><span class="hl-identifier">xmldoc</span><span class="hl-default">.</span><span class="hl-identifier">documentElement
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string"> Iterate the child nodes </span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-reserved">for </span><span class="hl-identifier">node </span><span class="hl-reserved">in </span><span class="hl-identifier">rootNode</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
			</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string"> We only care about &quot;item&quot; entries</span><span class="hl-quotes">&quot;&quot;&quot;
			</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">item</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:
				</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string"> Now iterate through all of the &lt;item&gt;'s children </span><span class="hl-quotes">&quot;&quot;&quot;
				</span><span class="hl-reserved">for </span><span class="hl-identifier">item_node </span><span class="hl-reserved">in </span><span class="hl-identifier">node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
					</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">title</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:
						</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string"> Loop through the title Text nodes to get
						the actual title</span><span class="hl-quotes">&quot;&quot;&quot;
						</span><span class="hl-identifier">title</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;&quot;
						</span><span class="hl-reserved">for </span><span class="hl-identifier">text_node </span><span class="hl-reserved">in </span><span class="hl-identifier">item_node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
							</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">text_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeType</span><span class="hl-code"> == </span><span class="hl-identifier">node</span><span class="hl-code">.</span><span class="hl-identifier">TEXT_NODE</span><span class="hl-brackets">)</span><span class="hl-default">:
								</span><span class="hl-identifier">title</span><span class="hl-default"> += </span><span class="hl-identifier">text_node</span><span class="hl-default">.</span><span class="hl-identifier">nodeValue
						</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string"> Now print the title if we have one </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">title</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-reserved">print </span><span class="hl-identifier">title

					if </span><span class="hl-brackets">(</span><span class="hl-identifier">item_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeName</span><span class="hl-code"> == </span><span class="hl-quotes">&quot;</span><span class="hl-string">description</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default">:
						</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string"> Loop through the description Text nodes to get
						the actual description</span><span class="hl-quotes">&quot;&quot;&quot;
						</span><span class="hl-identifier">description</span><span class="hl-default"> = </span><span class="hl-quotes">&quot;&quot;
						</span><span class="hl-reserved">for </span><span class="hl-identifier">text_node </span><span class="hl-reserved">in </span><span class="hl-identifier">item_node</span><span class="hl-default">.</span><span class="hl-identifier">childNodes</span><span class="hl-default">:
							</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">text_node</span><span class="hl-code">.</span><span class="hl-identifier">nodeType</span><span class="hl-code"> == </span><span class="hl-identifier">node</span><span class="hl-code">.</span><span class="hl-identifier">TEXT_NODE</span><span class="hl-brackets">)</span><span class="hl-default">:
								</span><span class="hl-identifier">description</span><span class="hl-default"> += </span><span class="hl-identifier">text_node</span><span class="hl-default">.</span><span class="hl-identifier">nodeValue
						</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string"> Now print the title if we have one.
						Add a blank with </span><span class="hl-special">\n</span><span class="hl-string"> so that it looks better </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">description</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-reserved">print </span><span class="hl-identifier">description</span><span class="hl-default"> + </span><span class="hl-quotes">&quot;</span><span class="hl-special">\n</span><span class="hl-quotes">&quot;
	</span><span class="hl-reserved">else</span><span class="hl-default">:
		</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error getting XML document!</span><span class="hl-quotes">&quot;
</span><span class="hl-reserved">else</span><span class="hl-default">:
	</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Error! Getting URL</span><span class="hl-quotes">&quot;</span></pre></div></div>
<p>Links that helped me get through this post:</p>
<ul>
<li><a href="href="http://www.rexx.com/~dkuhlman/pyxmlfaq.html">Pyxml FAQ</a>
</li>
<li><a href="http://pyxml.sourceforge.net/topics/howto/xml-howto.html">Python XML howto</a></li>
<li><a href="http://docs.python.org/">Python Documentation</a></li>
<li><a href="http://www.google.com">http://www.google.com</a></li>
<li><a href="http://www.chroder.com/archives/2005/04/16/wordpress-code-highlight-plugin/">WordPress Code Highlight Plugin</a></li>
</ul>
<p></item></p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2006/01/14/rss-reader-part-one/&title=RSS reader - 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/01/14/rss-reader-part-one/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Writing my first python script</title>
		<link>http://www.learningpython.com/2006/01/10/writing-my-first-python-script/</link>
		<comments>http://www.learningpython.com/2006/01/10/writing-my-first-python-script/#comments</comments>
		<pubDate>Wed, 11 Jan 2006 03:52:41 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[beginnings]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=8</guid>
		<description><![CDATA[Well after reading the python documentation for a few days I decided it was time for me to write my first real script.  Now I've <a href="http://www.learningpython.com/2005/12/09/what-am-i-working-on/">written a few scripts before</a> in the python interpreter, but I wanted to try my hand at creating some stand-alone executable file.

Now I didn't want to go too crazy with my first script so I thought I would start off with the simplest program that everyone knows, the <a href="http://en.wikipedia.org/wiki/HelloWorld">Hello World program</a>.   For this example I'm going to use the <a href="http://www.gnome.org/projects/gedit/">gedit</a> since I already have it installed and it comes with built-in python syntax highlighting, but any standard or fancy text editor could be used in its place.]]></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%2F01%2F10%2Fwriting-my-first-python-script%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2006%2F01%2F10%2Fwriting-my-first-python-script%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Well after reading the python documentation for a few days I decided it was time for me to write my first real script.  Now I&#8217;ve <a href="http://www.learningpython.com/2005/12/09/what-am-i-working-on/">written a few scripts before</a> in the python interpreter, but I wanted to try my hand at creating some stand-alone executable file.</p>
<p>Now I didn&#8217;t want to go too crazy with my first script so I thought I would start off with the simplest program that everyone knows, the <a href="http://en.wikipedia.org/wiki/HelloWorld">Hello World program</a>.   For this example I&#8217;m going to use the <a href="http://www.gnome.org/projects/gedit/">gedit</a> since I already have it installed and it comes with built-in python syntax highlighting, but any standard or fancy text editor could be used in its place.<br />
<span id="more-8"></span><br />
So to start gedit simply type &#8216;gedit&#8217; in a terminal or find it in your gnome menu.  Once gedit has started put it into <em>pythong mode</em> by selecting <strong>View | Highlight Mode | Scripts | Python</strong> from the menu.</p>
<p><img style="margin: 0pt 10px 10px 0pt; float: left;" src="http://www.learningpython.com/images/gedit.png" alt="gedit" />Now I&#8217;m greeted with a blank gedit canvas and it&#8217;s time to write my first script.  In order for a script to be executable on a Linux/Unix based OS it must start with the following line (or something similar):</p>
<p><code>#! /usr/bin/env python</code></p>
<p>Basically whatever follows the #! characters must locate the interpreter needed to execute the script file, in our case that&#8217;s the python interpreter.  But instead of passing the path to the python interpreter (which we could have using something like <code>#! /usr/bin/python</code>) we use the env utility to search the PATH for the first executable named &#8220;python&#8221;.  (PATH is an environment variable containing   paths where executables can be found)</p>
<p>I found that there is a lot of debate about which form to use <code>#! /usr/bin/env python</code> or <code>#! /usr/bin/python</code> but for now I&#8217;m just going to stick with <code>#! /usr/bin/env python</code> since it appears to be the most portable.</p>
<p>After that I added a blank line to separate things and typed the following code:</p>
<p><code>print "Hello World!"</code></p>
<p><img style="margin: 10px 10px 10px 10px;" src="http://www.learningpython.com/images/gedit_listing.png" alt="gedit helloworld.py listing" /></p>
<p>Not very complicated but it will get the job done.  You&#8217;ll also notice, if you are working with gedit, the nice syntax highlighting.  After that I saved my files as helloworld.py.</p>
<p>Now to execute this file we can open up a terminal, browse to the directory containing our helloworld.py file and type:</p>
<p><code>$ python helloworld.py</code></p>
<p>But that doesn&#8217;t make much sense since we went through all that trouble to type the #! line at the beginning of our file.  So in order to make use of that we have to tell the system that helloworld.py is an executable file, to do that we need to modify its properties using <a href="http://catcode.com/teachmod/">chmod</a>.</p>
<p>To do that we simply type:</p>
<p><code>$ chmod +x helloworld.py</code></p>
<p>Now the system knows that helloworld.py is an executable file and in order to run  it we simple have to type the following in a terminal:</p>
<p><code>$ ./helloworld.py</code></p>
<p>(The ./ part of the above simple tells the terminal that we are looking in the current directory.)</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/helloworld_res.png" alt="helloworld.py results" /></p>
<p>Not very exciting or difficult but at least it worked!</p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2006/01/10/writing-my-first-python-script/&title=Writing my first python script&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/01/10/writing-my-first-python-script/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
