<?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; PyGlet</title>
	<atom:link href="http://www.learningpython.com/category/python/pyglet/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>Pyglet Level Editor</title>
		<link>http://www.learningpython.com/2008/03/22/pyglet-level-editor/</link>
		<comments>http://www.learningpython.com/2008/03/22/pyglet-level-editor/#comments</comments>
		<pubDate>Sat, 22 Mar 2008 18:18:04 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[PyGlet]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[game engine]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=72</guid>
		<description><![CDATA[
			
				
			
		
Hey Everyone,
Sorry I&#8217;ve been away for a bit, work and trips and writing for Python Magazine had me pretty busy and I wasn&#8217;t able to reply to everyone&#8217;s comments on the simple Python game engine. I really do appreciate the comments though so please keep them coming.
I have been thinking about the simple game engine [...]]]></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%2F03%2F22%2Fpyglet-level-editor%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2008%2F03%2F22%2Fpyglet-level-editor%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Hey Everyone,</p>
<p>Sorry I&#8217;ve been away for a bit, work and trips and writing for Python Magazine had me pretty busy and I wasn&#8217;t able to reply to everyone&#8217;s comments on the simple Python game engine. I really do appreciate the comments though so please keep them coming.</p>
<p>I have been thinking about the simple game engine quite a bit though and wondering where to start on it all, and whether or not it makes sense to start on it at all!  After some thinking I decided that what I would want most (for a variety of reasons) would be an easy to use level editor. So with a day off from work and life yesterday I started to do some hacking with PyGTK and pyglet to see if I couldn&#8217;t get a simple level editor going.</p>
<p>The results are still quite crude, but the basics are starting to get in there:</p>
<p><a href="http://www.learningpython.com/images/dodger/editor.png"><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/dodger/editor.png" alt="Python Game pyglet editor" border="0"/><img /></a></p>
<p>As you can see it&#8217;s a PyGTK application with an OpenGL window that displays pyglet sprites. There is a properties list, where you can add and edit properties or the sprite. There is also a &#8220;content&#8221; list that displays all of the graphics in your project&#8217;s &#8220;content&#8221; directory. Then you can add any of those images to your level. You can also select sprites and move them around, or edit their properties (notice the monster with the yellow border around it?).</p>
<p>The idea is that eventually this will save the information out into a human readable file type (yaml, xml, whatever) that games (your game?) will then read in for their levels. The properties will be saved with each sprite and then applied when you load the level. That way you can add specific properties to specific sprites.</p>
<p>This is still very much a work in progress, but when it gets a little bit more stable and if people are interested I think I&#8217;ll create a project on sourceforge or google code so the other people can start working with it or hacking it.</p>
<p>So&#8230;ideas? Comments? Thoughts?</p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2008/03/22/pyglet-level-editor/&title=Pyglet Level Editor&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/03/22/pyglet-level-editor/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Creating a game with PyGlet and Python</title>
		<link>http://www.learningpython.com/2007/11/10/creating-a-game-with-pyglet-and-python/</link>
		<comments>http://www.learningpython.com/2007/11/10/creating-a-game-with-pyglet-and-python/#comments</comments>
		<pubDate>Sat, 10 Nov 2007 18:31:44 +0000</pubDate>
		<dc:creator>selsine</dc:creator>
				<category><![CDATA[PyGlet]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.learningpython.com/?p=67</guid>
		<description><![CDATA[
			
				
			
		
For this tutorial I&#8217;m going to take a look at PyGlet: &#8220;a cross-platform windowing and multimedia library for Python.&#8221; The reason that I decided to take a look at PyGlet is because it is an alternative to PyGame in the Python gaming world and: &#8220;No external dependencies or installation requirements. For most application and game [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.learningpython.com%2F2007%2F11%2F10%2Fcreating-a-game-with-pyglet-and-python%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.learningpython.com%2F2007%2F11%2F10%2Fcreating-a-game-with-pyglet-and-python%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>For this tutorial I&#8217;m going to take a look at PyGlet: &#8220;a cross-platform windowing and multimedia library for Python.&#8221; The reason that I decided to take a look at PyGlet is because it is an alternative to PyGame in the Python gaming world and: &#8220;No external dependencies or installation requirements. For most application and game requirements, pyglet needs nothing else besides Python, simplifying distribution and installation.&#8221;[1]</p>
<p>The first step to using PyGlet is to actually download it and install it (http://pyglet.org/download.html) as of writing this PyGlet is at version 1.0 alpha 2 (as I finished this tutorial Beta 1 was released but I have been unable to try it out), so between now and the final release there are bound to be a few changes. Once you have download the correct version for your system install it an you are ready to go. I&#8217;m writing this on a Debian box so I downloaded the source distribution and 	installed it using the following, as per the instructions:</p>
<p><code><br />
python setup.py install<br />
</code></p>
<p>Now like PyGame, PyGlet is a framework for developing games or other applications, it is not a game engine, therefore if you are looking to create a full game you will need to create your own, or use someone else&#8217;s. This tutorial will not going into full game creation, instead it will introduce PyGlet using a small sample application, hopefully giving you enough of the basics, or enough of a taste to continue on with it.</p>
<p>You can download the full source to this tutorial <a href="http://www.learningpython.com/sources/pygletSpace.tar.gz">here</a>.</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/pyglet_01/pyglet_02.png" alt="Python Game PyGlet" border="0"/><br />
<img /></p>
<p><span id="more-67"></span></p>
<h2>Creating a Window</h2>
<p>The first step is to get an actual window displaying. In this initial code I&#8217;m going to create a window with a main game loop and display the Frames Per Second that the game is currently running at. In order to do this I am going to subclass from the <a href="http://pyglet.org/doc/api/pyglet.window.Window-class.html">pyglet.window.Window</a> class. You don&#8217;t have to do this in order to display a window, but given the way that events are handled this seemed to make the most sense to me.</p>
<p>I added the following to a file named &#8220;PyGletSpace.py&#8221;</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-reserved">from </span><span class="hl-identifier">pyglet </span><span class="hl-reserved">import </span><span class="hl-identifier">window
</span><span class="hl-reserved">from </span><span class="hl-identifier">pyglet </span><span class="hl-reserved">import </span><span class="hl-identifier">clock
</span><span class="hl-reserved">from </span><span class="hl-identifier">pyglet </span><span class="hl-reserved">import </span><span class="hl-identifier">font

</span><span class="hl-reserved">class </span><span class="hl-identifier">SpaceGameWindow</span><span class="hl-brackets">(</span><span class="hl-identifier">window</span><span class="hl-code">.</span><span class="hl-identifier">Window</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">args</span><span class="hl-code">, **</span><span class="hl-identifier">kwargs</span><span class="hl-brackets">)</span><span class="hl-default">:

		</span><span class="hl-comment">#Let all of the standard stuff pass through
		</span><span class="hl-identifier">window</span><span class="hl-default">.</span><span class="hl-identifier">Window</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">args</span><span class="hl-code">, **</span><span class="hl-identifier">kwargs</span><span class="hl-brackets">)

	</span><span class="hl-reserved">def </span><span class="hl-identifier">main_loop</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:

		</span><span class="hl-comment">#Create a font for our FPS clock
		</span><span class="hl-identifier">ft</span><span class="hl-default"> = </span><span class="hl-identifier">font</span><span class="hl-default">.</span><span class="hl-identifier">load</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">Arial</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-number">28</span><span class="hl-brackets">)
		</span><span class="hl-comment">#The pyglet.font.Text object to display the FPS
		</span><span class="hl-identifier">fps_text</span><span class="hl-default"> = </span><span class="hl-identifier">font</span><span class="hl-default">.</span><span class="hl-identifier">Text</span><span class="hl-brackets">(</span><span class="hl-identifier">ft</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-brackets">)

		</span><span class="hl-reserved">while not </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">has_exit</span><span class="hl-default">:
			</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">dispatch_events</span><span class="hl-brackets">()
			</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">clear</span><span class="hl-brackets">()

			</span><span class="hl-comment">#Tick the clock
			</span><span class="hl-identifier">clock</span><span class="hl-default">.</span><span class="hl-identifier">tick</span><span class="hl-brackets">()
			</span><span class="hl-comment">#Gets fps and draw it
			</span><span class="hl-identifier">fps_text</span><span class="hl-default">.</span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">fps: %d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default"> % </span><span class="hl-brackets">(</span><span class="hl-identifier">clock</span><span class="hl-code">.</span><span class="hl-identifier">get_fps</span><span class="hl-brackets">())
			</span><span class="hl-identifier">fps_text</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">()

			</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">flip</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-comment"># Someone is launching this directly
	</span><span class="hl-identifier">space</span><span class="hl-default"> = </span><span class="hl-identifier">SpaceGameWindow</span><span class="hl-brackets">()
	</span><span class="hl-identifier">space</span><span class="hl-default">.</span><span class="hl-identifier">main_loop</span><span class="hl-brackets">()</span></pre></div></div>
<p>So we are not doing anything that fancy here, first we import three things from the PyGlet library, the window, clock, and font modules. We then create a class called SpaceGameWindow which is a subclass of the <a href="http://pyglet.org/doc/api/pyglet.window.Window-class.html">pyglet.window.Window</a> class. </p>
<p>Then we have a simple initialization function where for now we simply pass all of the keywords and arguments to the pyglet.window.Window&#8217;s __init__() function. </p>
<p>Next we have our main_loop() function which will serve as the main loop for the game. Here we create a <a href="http://pyglet.org/doc/api/pyglet.font.base.Font-class.html">pyglet.font.base.Font</a> object and a <a href="http://pyglet.org/doc/api/pyglet.font.Text-class.html">pyglet.font.Text</a> object that will serve as the font for our &#8220;Frames Per Second&#8221; text, and the class that will actually write out the &#8220;Frames Per Second&#8221; text.</p>
<p>After that we have the actual game loop where we continue to loop until the window&#8217;s has_exit boolean member variable becomes true.</p>
<p>In the loop we first dispatch all of the event to the event handlers, and then we clear the window to prepare it for drawing.  Then we tick the clock, get our FPS text and then draw it out to the screen.  Then, since windows in PyGlet are double buffered we flip the display.</p>
<p>Hopefully that is pretty straight forward, if you run the code you should see something similar to the following:</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/pyglet_01/pyglet_01.png" alt="Python Game PyGlet" border="0"/><br />
<img /></p>
<h2>Sprites</h2>
<p>Pretty good so far but this doesn&#8217;t really help us or do anything, so the next step is to get some stuff draw onto the screen. To do that I&#8217;m going to create a simple Sprite class.  This class will be loosely based off of the PyGame&#8217;s Sprite class, but for the most part will serve as a helper for drawing and the action on the screen.</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">Sprite</span><span class="hl-brackets">(</span><span class="hl-identifier">object</span><span class="hl-brackets">)</span><span class="hl-default">:

	</span><span class="hl-reserved">def </span><span class="hl-identifier">__get_left</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-reserved">return </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">x
	left</span><span class="hl-default"> = </span><span class="hl-builtin">property</span><span class="hl-brackets">(</span><span class="hl-identifier">__get_left</span><span class="hl-brackets">)

	</span><span class="hl-reserved">def </span><span class="hl-identifier">__get_right</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-reserved">return </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">x</span><span class="hl-default"> + </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">image</span><span class="hl-default">.</span><span class="hl-identifier">width
	right</span><span class="hl-default"> = </span><span class="hl-builtin">property</span><span class="hl-brackets">(</span><span class="hl-identifier">__get_right</span><span class="hl-brackets">)

	</span><span class="hl-reserved">def </span><span class="hl-identifier">__get_top</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-reserved">return </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">y</span><span class="hl-default"> + </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">image</span><span class="hl-default">.</span><span class="hl-identifier">height
	top</span><span class="hl-default"> = </span><span class="hl-builtin">property</span><span class="hl-brackets">(</span><span class="hl-identifier">__get_top</span><span class="hl-brackets">)

	</span><span class="hl-reserved">def </span><span class="hl-identifier">__get_bottom</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-reserved">return </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">y
	bottom</span><span class="hl-default"> = </span><span class="hl-builtin">property</span><span class="hl-brackets">(</span><span class="hl-identifier">__get_bottom</span><span class="hl-brackets">)

	</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">image_file</span><span class="hl-code">, </span><span class="hl-identifier">image_data</span><span class="hl-code">=</span><span class="hl-reserved">None</span><span class="hl-code">, **</span><span class="hl-identifier">kwargs</span><span class="hl-brackets">)</span><span class="hl-default">:

		</span><span class="hl-comment">#init standard variables
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">image_file</span><span class="hl-default"> = </span><span class="hl-identifier">image_file
		if </span><span class="hl-brackets">(</span><span class="hl-identifier">image_data </span><span class="hl-reserved">is None</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">image</span><span class="hl-default"> = </span><span class="hl-identifier">helper</span><span class="hl-default">.</span><span class="hl-identifier">load_image</span><span class="hl-brackets">(</span><span class="hl-identifier">image_file</span><span class="hl-brackets">)
		</span><span class="hl-reserved">else</span><span class="hl-default">:
			</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">image</span><span class="hl-default"> = </span><span class="hl-identifier">image_data
		self</span><span class="hl-default">.</span><span class="hl-identifier">x</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">y</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">dead</span><span class="hl-default"> = </span><span class="hl-reserved">False
		</span><span class="hl-comment">#Update the dict if they sent in any keywords
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">__dict__</span><span class="hl-default">.</span><span class="hl-identifier">update</span><span class="hl-brackets">(</span><span class="hl-identifier">kwargs</span><span class="hl-brackets">)

	</span><span class="hl-reserved">def </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">image</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">x</span><span class="hl-code">, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">y</span><span class="hl-brackets">)

	</span><span class="hl-reserved">def </span><span class="hl-identifier">update</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-reserved">pass

	def </span><span class="hl-identifier">intersect</span><span class="hl-brackets">(</span><span class="hl-identifier">self</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">Do the two sprites intersect?
		@param sprite - Sprite - The Sprite to test
		</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-reserved">return </span><span class="hl-identifier">not </span><span class="hl-brackets">((</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">left</span><span class="hl-code"> &gt; </span><span class="hl-identifier">sprite</span><span class="hl-code">.</span><span class="hl-identifier">right</span><span class="hl-brackets">)
			</span><span class="hl-identifier">or </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">right</span><span class="hl-code"> &lt; </span><span class="hl-identifier">sprite</span><span class="hl-code">.</span><span class="hl-identifier">left</span><span class="hl-brackets">)
			</span><span class="hl-identifier">or </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">top</span><span class="hl-code"> &lt; </span><span class="hl-identifier">sprite</span><span class="hl-code">.</span><span class="hl-identifier">bottom</span><span class="hl-brackets">)
			</span><span class="hl-identifier">or </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">bottom</span><span class="hl-code"> &gt; </span><span class="hl-identifier">sprite</span><span class="hl-code">.</span><span class="hl-identifier">top</span><span class="hl-brackets">))

	</span><span class="hl-reserved">def </span><span class="hl-identifier">collide</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">sprite_list</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Determing ther are collisions with this
		sprite and the list of sprites
		@param sprite_list - A list of sprites
		@returns list - List of collisions</span><span class="hl-quotes">&quot;&quot;&quot;

		</span><span class="hl-identifier">lst_return</span><span class="hl-default"> = </span><span class="hl-brackets">[]
		</span><span class="hl-reserved">for </span><span class="hl-identifier">sprite </span><span class="hl-reserved">in </span><span class="hl-identifier">sprite_list</span><span class="hl-default">:
			</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">intersect</span><span class="hl-brackets">(</span><span class="hl-identifier">sprite</span><span class="hl-brackets">))</span><span class="hl-default">:
				</span><span class="hl-identifier">lst_return</span><span class="hl-default">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-identifier">sprite</span><span class="hl-brackets">)
		</span><span class="hl-reserved">return </span><span class="hl-identifier">lst_return

	</span><span class="hl-reserved">def </span><span class="hl-identifier">collide_once</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">sprite_list</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-quotes">&quot;&quot;&quot;</span><span class="hl-string">Determine if there is at least one
		collision between this sprite and the list
		@param sprite_list - A list of sprites
		@returns - None - No Collision, or the first
		sprite to collide
		</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-reserved">for </span><span class="hl-identifier">sprite </span><span class="hl-reserved">in </span><span class="hl-identifier">sprite_list</span><span class="hl-default">:
			</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">intersect</span><span class="hl-brackets">(</span><span class="hl-identifier">sprite</span><span class="hl-brackets">))</span><span class="hl-default">:
				</span><span class="hl-reserved">return </span><span class="hl-identifier">sprite
		</span><span class="hl-reserved">return None</span></pre></div></div>
<p>So it may look complicated but it&#8217;s really not that difficult.  First off you will see that I create some positioning properties, left, right, top, and bottom. I do this so that the collision code is easier to read, plus it separates the positioning information from the implementation, in this case the <a href="http://pyglet.org/doc/api/pyglet.image.AbstractImage-class.html">pyglet.image.AbstractImage</a> member. If, in the future we were to add a rectangle class to the Sprite we don&#8217;t have to change a the code that interacts with the positioning, just what the position properties interact with.</p>
<p>You will also notice that in the Sprite class can either be initialized using an image file or the actual image data, which is a <a href="http://pyglet.org/doc/api/pyglet.image.AbstractImage-class.html">pyglet.image.AbstractImage</a>. The reason for this is instead of having to load the same image from the hard drive time and time again, and to avoid having the same image duplicated in memory in countless locations, we let the user decide which makes more sense.</p>
<p>Other then that it&#8217;s pretty simple stuff, some positioning information, and whether or not the sprite is Alive. If a sprite is dead it will be removed from the window.  There is also a draw method where the sprites image is blited to the window, and an update function where (if necessary) the sprite should update itself (position, image, etc).</p>
<p>Next we have a few collision functions that will help us to determine when out sprites collide. Now that we have the sprite class we need to add three more classes based off of the Sprite class. A SpaceShip class, a Bullet class which will be fired by the SpaceShip, and a Monster class.</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/pyglet_01/ship.png" alt="Python Game PyGlet" border="0"/><br />
<img /></p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">SpaceShip</span><span class="hl-brackets">(</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">text_x</span><span class="hl-code">, </span><span class="hl-identifier">text_y</span><span class="hl-code">, **</span><span class="hl-identifier">kwargs</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">kills</span><span class="hl-default"> = </span><span class="hl-number">0
		</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-quotes">&quot;</span><span class="hl-string">ship.png</span><span class="hl-quotes">&quot;</span><span class="hl-code">, **</span><span class="hl-identifier">kwargs</span><span class="hl-brackets">)

		</span><span class="hl-comment">#Create a font for kill message
		</span><span class="hl-identifier">self</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">load</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">Arial</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-number">28</span><span class="hl-brackets">)
		</span><span class="hl-comment">#The pyglet.font.Text object to display the FPS
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">kill_text</span><span class="hl-default"> = </span><span class="hl-identifier">font</span><span class="hl-default">.</span><span class="hl-identifier">Text</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">font</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-code">=</span><span class="hl-identifier">text_y</span><span class="hl-code">, </span><span class="hl-identifier">x</span><span class="hl-code">=</span><span class="hl-identifier">text_x</span><span class="hl-brackets">)

	</span><span class="hl-reserved">def </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">Sprite</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-brackets">)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">kill_text</span><span class="hl-default">.</span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Kills: %d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default"> % </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">kills</span><span class="hl-brackets">)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">kill_text</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">()

	</span><span class="hl-reserved">def </span><span class="hl-identifier">on_kill</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">kills</span><span class="hl-default"> += </span><span class="hl-number">1</span></pre></div></div>
<p>The SpaceShip class doesn&#8217;t do that much above and beyond the base class. It has a data member &#8220;kills&#8221; that keeps track of the number of monsters that it kills, and a <a href="http://pyglet.org/doc/api/pyglet.font.Text-class.html">pyglet.font.Text</a> member that will display the number of kills on the screen. The position of the &#8220;kill text&#8221; is also passed into the SpaceShip constructor.</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/pyglet_01/bullet.png" alt="Python Game PyGlet" border="0"/><br />
<img /></p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">Bullet</span><span class="hl-brackets">(</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">parent_ship</span><span class="hl-code">, </span><span class="hl-identifier">image_data</span><span class="hl-code">, </span><span class="hl-identifier">top</span><span class="hl-code">, **</span><span class="hl-identifier">kwargs</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">velocity</span><span class="hl-default"> = </span><span class="hl-number">5
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">screen_top</span><span class="hl-default"> = </span><span class="hl-identifier">top
		self</span><span class="hl-default">.</span><span class="hl-identifier">parent_ship</span><span class="hl-default"> = </span><span class="hl-identifier">parent_ship
		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-quotes">&quot;&quot;</span><span class="hl-code">, </span><span class="hl-identifier">image_data</span><span class="hl-code">, **</span><span class="hl-identifier">kwargs</span><span class="hl-brackets">)

	</span><span class="hl-reserved">def </span><span class="hl-identifier">update</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">y</span><span class="hl-default"> += </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">velocity
		</span><span class="hl-comment">#Have we gone off the screen?
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">bottom</span><span class="hl-code"> &gt; </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">screen_top</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">dead</span><span class="hl-default"> = </span><span class="hl-reserved">True

	def </span><span class="hl-identifier">on_kill</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">We have hit a monster let the parent know</span><span class="hl-quotes">&quot;&quot;&quot;
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">parent_ship</span><span class="hl-default">.</span><span class="hl-identifier">on_kill</span><span class="hl-brackets">()</span></pre></div></div>
<p>The Bullet class will represent bullets fired by the SpaceShip class, so it adds three new data members the:</p>
<p>1) The velocity &#8211; which controls how fast the bullet moves.<br />
2) The screen_top &#8211; Which represents the top of the screen is and will be used to destroy the bullet when the bullet moves past the top of the screen.<br />
3) The parent_ship &#8211; This is the SpaceShip that fired the bullet. This will be used to let the ship know when it has made a kill.</p>
<p>The final sprite is the Monster sprite:</p>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/pyglet_01/monster.png" alt="Python Game PyGlet" border="0"/><br />
<img /></p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">Monster</span><span class="hl-brackets">(</span><span class="hl-identifier">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">image_data</span><span class="hl-code">, **</span><span class="hl-identifier">kwargs</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">y_velocity</span><span class="hl-default"> = </span><span class="hl-number">5
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">set_x_velocity</span><span class="hl-brackets">()
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">x_move_count</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">x_velocity
		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-quotes">&quot;&quot;</span><span class="hl-code">, </span><span class="hl-identifier">image_data</span><span class="hl-code">, **</span><span class="hl-identifier">kwargs</span><span class="hl-brackets">)

	</span><span class="hl-reserved">def </span><span class="hl-identifier">update</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">y</span><span class="hl-default"> -= </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">y_velocity
		self</span><span class="hl-default">.</span><span class="hl-identifier">x</span><span class="hl-default"> += </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">x_velocity</span><span class="hl-comment">#random.randint(-3,3)
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">x_move_count</span><span class="hl-default"> += </span><span class="hl-number">1
		</span><span class="hl-comment">#Have we gone beneath the botton of the screen?
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">y</span><span class="hl-code"> &lt; </span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">dead</span><span class="hl-default"> = </span><span class="hl-reserved">True

		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">x_move_count</span><span class="hl-code"> &gt;=</span><span class="hl-number">30</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">x_move_count</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">set_x_velocity</span><span class="hl-brackets">()

	</span><span class="hl-reserved">def </span><span class="hl-identifier">set_x_velocity</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">x_velocity</span><span class="hl-default"> = </span><span class="hl-identifier">random</span><span class="hl-default">.</span><span class="hl-identifier">randint</span><span class="hl-brackets">(</span><span class="hl-code">-</span><span class="hl-number">3</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-brackets">)</span></pre></div></div>
<p>The Monster sprite will be moving from the top of the screen to the bottom of the screen, which is the opposite of the Bullet and is controlled by the y_velocity data member. But unlike the bullet the Monster sprite will also have some horizontal motion built it, which is controlled by the x_velocity data member, and set in the set_x_velocity() function. What is interesting about the x_velocity is that there is a bit of randomness built into it, it will be a random number from -3 in the left direct to 3 in the right direction. It will also change every thirty updates, giving the Monster a lazy side to side motion making it a bit more difficult for the SpaceShip to shoot it.</p>
<p>The Monster sprite does not have a bottom data member to tell it when it has moved off of the screen. The reason for this is that since the Monster will be moving down from the top of the screen, and we will can tell if it&#8217;s off the screen when it&#8217;s y, or top, data members are less then zero.</p>
<h2>Adding the Sprites to the Game</h2>
<p>Now that we have our sprite classes we need to use them in the main SpaceGameWindow class. There are a few ways to do this, but what I ended up doing was creating three new data members: &#8216;bullets&#8217; and &#8216;monsters&#8217;, both lists of sprites, and &#8217;ship&#8217; which will be the one and only ShapShip sprite.</p>
<p>The Sprites and some other data is initialized in the init_sprites() function:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">init_sprites</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">bullets</span><span class="hl-default"> = </span><span class="hl-brackets">[]
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">monsters</span><span class="hl-default"> = </span><span class="hl-brackets">[]
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">ship</span><span class="hl-default"> = </span><span class="hl-identifier">SpaceShip</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">150</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-identifier">x</span><span class="hl-code">=</span><span class="hl-number">100</span><span class="hl-code">,</span><span class="hl-identifier">y</span><span class="hl-code">=</span><span class="hl-number">100</span><span class="hl-brackets">)
	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">bullet_image</span><span class="hl-default"> = </span><span class="hl-identifier">helper</span><span class="hl-default">.</span><span class="hl-identifier">load_image</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">bullet.png</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">monster_image</span><span class="hl-default"> = </span><span class="hl-identifier">helper</span><span class="hl-default">.</span><span class="hl-identifier">load_image</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">monster.png</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span></pre></div></div>
<p>All we do is create the two lists to store our sprites, the SpaceShip sprites, and use our helper to load the images that we will use for the Bullet and Monster sprites.</p>
<p>The next step is to change the draw loop so that we actually draw our sprites. There are a few more changes to be made in the main_loop() function but overall it remains 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">main_loop</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:

	</span><span class="hl-comment">#Create a font for our FPS clock
	</span><span class="hl-identifier">ft</span><span class="hl-default"> = </span><span class="hl-identifier">font</span><span class="hl-default">.</span><span class="hl-identifier">load</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">Arial</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-number">28</span><span class="hl-brackets">)
	</span><span class="hl-comment">#The pyglet.font.Text object to display the FPS
	</span><span class="hl-identifier">fps_text</span><span class="hl-default"> = </span><span class="hl-identifier">font</span><span class="hl-default">.</span><span class="hl-identifier">Text</span><span class="hl-brackets">(</span><span class="hl-identifier">ft</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-brackets">)

	</span><span class="hl-comment">#Schedule the Monster creation
	</span><span class="hl-identifier">clock</span><span class="hl-default">.</span><span class="hl-identifier">schedule_interval</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">create_monster</span><span class="hl-code">, </span><span class="hl-number">0.3</span><span class="hl-brackets">)
	</span><span class="hl-identifier">clock</span><span class="hl-default">.</span><span class="hl-identifier">set_fps_limit</span><span class="hl-brackets">(</span><span class="hl-number">30</span><span class="hl-brackets">)

	</span><span class="hl-reserved">while not </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">has_exit</span><span class="hl-default">:
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">dispatch_events</span><span class="hl-brackets">()
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">clear</span><span class="hl-brackets">()

		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">update</span><span class="hl-brackets">()
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">()

		</span><span class="hl-comment">#Tick the clock
		</span><span class="hl-identifier">clock</span><span class="hl-default">.</span><span class="hl-identifier">tick</span><span class="hl-brackets">()
		</span><span class="hl-comment">#Gets fps and draw it
		</span><span class="hl-identifier">fps_text</span><span class="hl-default">.</span><span class="hl-identifier">text</span><span class="hl-default"> = </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">fps: %d</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-default"> % </span><span class="hl-brackets">(</span><span class="hl-identifier">clock</span><span class="hl-code">.</span><span class="hl-identifier">get_fps</span><span class="hl-brackets">())
		</span><span class="hl-identifier">fps_text</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">()
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">flip</span><span class="hl-brackets">()</span></pre></div></div>
<p>There is quite a bit happening here so I&#8217;ll take a little bit of time to explain everything that is new. One of the things that I want to happen is to have monsters created after a certain amount of time has passed. To to this I use the <a href="http://www.pyglet.org/doc/api/pyglet.clock-module.html#schedule_interval">clock.schedule_interval</a> function.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#Schedule the Monster creation
</span><span class="hl-identifier">clock</span><span class="hl-default">.</span><span class="hl-identifier">schedule_interval</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">create_monster</span><span class="hl-code">, </span><span class="hl-number">0.3</span><span class="hl-brackets">)</span></pre></div></div>
<p>What this will do is call the create_monster() function every 0.3 seconds. The create_monster() function is pretty simple, it simply creates a Monster sprite in a random location provided that there is less then the maximum amount of Monsters currently around:</p>
<p><code><br />
def create_monster(self, interval):<br />
	if (len(self.monsters) &lt; self.max_monsters):<br />
		self.monsters.append(Monster(self.monster_image<br />
			, x=random.randint(0, self.width) , y=self.height))<br />
</code></p>
<p>max_monsters is defined in the __int__() function to be 30. The next new feature is the limiting of the frame rate. For such a simple game I limit it to 30 frames per second using the <a href="http://www.pyglet.org/doc/api/pyglet.clock-module.html#set_fps_limit">set_fps_limit </a>  function.</p>
<p>Other then that there are only two new calls in the main main loop and that is:</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">update</span><span class="hl-brackets">()
</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">()</span></pre></div></div>
<p>update() is basically a function that is used to update everything that is happening on the screen: sprite positions, scores whatever you want. Then the draw() function is basically the function where everything that needs to be drawn onto the screen gets drawn.</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">update</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:

	</span><span class="hl-identifier">to_remove</span><span class="hl-default"> = </span><span class="hl-brackets">[]
	</span><span class="hl-reserved">for </span><span class="hl-identifier">sprite </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">monsters</span><span class="hl-default">:
		</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">update</span><span class="hl-brackets">()
		</span><span class="hl-comment">#Is it dead?
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">sprite</span><span class="hl-code">.</span><span class="hl-identifier">dead</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-identifier">to_remove</span><span class="hl-default">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-identifier">sprite</span><span class="hl-brackets">)
	</span><span class="hl-comment">#Remove dead sprites
	</span><span class="hl-reserved">for </span><span class="hl-identifier">sprite </span><span class="hl-reserved">in </span><span class="hl-identifier">to_remove</span><span class="hl-default">:
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">monsters</span><span class="hl-default">.</span><span class="hl-identifier">remove</span><span class="hl-brackets">(</span><span class="hl-identifier">sprite</span><span class="hl-brackets">)

	</span><span class="hl-comment">#Bullet update and collision
	</span><span class="hl-identifier">to_remove</span><span class="hl-default"> = </span><span class="hl-brackets">[]
	</span><span class="hl-reserved">for </span><span class="hl-identifier">sprite </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">bullets</span><span class="hl-default">:
		</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">update</span><span class="hl-brackets">()
		</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-reserved">not </span><span class="hl-identifier">sprite</span><span class="hl-code">.</span><span class="hl-identifier">dead</span><span class="hl-brackets">)</span><span class="hl-default">:
			</span><span class="hl-identifier">monster_hit</span><span class="hl-default"> = </span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">collide_once</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">monsters</span><span class="hl-brackets">)
			</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">monster_hit </span><span class="hl-reserved">is not None</span><span class="hl-brackets">)</span><span class="hl-default">:
				</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">on_kill</span><span class="hl-brackets">()
				</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">monsters</span><span class="hl-default">.</span><span class="hl-identifier">remove</span><span class="hl-brackets">(</span><span class="hl-identifier">monster_hit</span><span class="hl-brackets">)
				</span><span class="hl-identifier">to_remove</span><span class="hl-default">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-identifier">sprite</span><span class="hl-brackets">)
		</span><span class="hl-reserved">else</span><span class="hl-default">:
			</span><span class="hl-identifier">to_remove</span><span class="hl-default">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-identifier">sprite</span><span class="hl-brackets">)
	</span><span class="hl-comment">#Remove bullets that hit monsters
	</span><span class="hl-reserved">for </span><span class="hl-identifier">sprite </span><span class="hl-reserved">in </span><span class="hl-identifier">to_remove</span><span class="hl-default">:
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">bullets</span><span class="hl-default">.</span><span class="hl-identifier">remove</span><span class="hl-brackets">(</span><span class="hl-identifier">sprite</span><span class="hl-brackets">)

	</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">ship</span><span class="hl-default">.</span><span class="hl-identifier">update</span><span class="hl-brackets">()
	</span><span class="hl-comment">#Is it dead?
	</span><span class="hl-identifier">monster_hit</span><span class="hl-default"> = </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">ship</span><span class="hl-default">.</span><span class="hl-identifier">collide_once</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">monsters</span><span class="hl-brackets">)
	</span><span class="hl-identifier">if </span><span class="hl-brackets">(</span><span class="hl-identifier">monster_hit </span><span class="hl-reserved">is not None</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">ship</span><span class="hl-default">.</span><span class="hl-identifier">dead</span><span class="hl-default"> = </span><span class="hl-reserved">True
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">has_exit</span><span class="hl-default"> = </span><span class="hl-reserved">True</span></pre></div></div>
<p>I won&#8217;t explain this function too much since in general it&#8217;s pretty straight forward. We are basically updating all of the sprites, checking for collisions or them being &#8220;dead&#8221; and if the sprites need to be removed then we do. </p>
<p>The first step is to update all of the monsters. The second step is to update all of the Bullet sprites and if they are not dead (off the screen) check for a collision between them at the Monster sprites. If the Bullet hits a Monster sprite then it and the monster are both dead and will be removed. </p>
<p>Then we check to see if the SpaceShip sprite has collided with any of the Monster sprites. If it has we signal the end of the game by setting the <a href="http://www.pyglet.org/doc/api/pyglet.window.event.WindowExitHandler-class.html#has_exit">has_exit</a> window handler.</p>
<p>The draw function is very simple, we simply draw all of the sprites:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">draw</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-brackets">)</span><span class="hl-default">:

	</span><span class="hl-reserved">for </span><span class="hl-identifier">sprite </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">bullets</span><span class="hl-default">:
		</span><span class="hl-identifier">sprite</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">()
	</span><span class="hl-reserved">for </span><span class="hl-identifier">sprite </span><span class="hl-reserved">in </span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">monsters</span><span class="hl-default">:
		</span><span class="hl-identifier">sprite</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-default">.</span><span class="hl-identifier">ship</span><span class="hl-default">.</span><span class="hl-identifier">draw</span><span class="hl-brackets">()</span></pre></div></div>
<h2>Events</h2>
<p>The next step to setup some event handling, which was the whole reason that we made our main class use <a href="http://pyglet.org/doc/api/pyglet.window.Window-class.html">pyglet.window.Window</a> as the base class way back before we got sidetracked on these sprites and game play.</p>
<p>Fortunately for us adding event handlers is very simple, we simply have to define one of the <a href="http://www.pyglet.org/doc/api/pyglet.window.Window-class.html#section-Events">event functions</a></p>
<p>Since we want to use mouse motion to move the space ship we need to catch the <a href="http://www.pyglet.org/doc/api/pyglet.window.Window-class.html#section-Events">mouse motion event</a>.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">on_mouse_motion</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">x</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-code">, </span><span class="hl-identifier">dx</span><span class="hl-code">, </span><span class="hl-identifier">dy</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">ship</span><span class="hl-default">.</span><span class="hl-identifier">x</span><span class="hl-default"> = </span><span class="hl-identifier">x
	self</span><span class="hl-default">.</span><span class="hl-identifier">ship</span><span class="hl-default">.</span><span class="hl-identifier">y</span><span class="hl-default"> = </span><span class="hl-identifier">y</span></pre></div></div>
<p>Pretty simple! We just need to add the function for the even and as long as we call <a href="http://www.pyglet.org/doc/api/pyglet.window.Window-class.html#dispatch_events">dispatch_events</a> our event will be handed.</p>
<p>We also want the spaceship to fire when we click the left mouse button. So to do that we need to handle the <a href="http://www.pyglet.org/doc/api/pyglet.window.Window-class.html#on_mouse_press">mouse press event</a>.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">on_mouse_press</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">x</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-code">, </span><span class="hl-identifier">button</span><span class="hl-code">, </span><span class="hl-identifier">modifiers</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">button</span><span class="hl-code"> == </span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">self</span><span class="hl-default">.</span><span class="hl-identifier">bullets</span><span class="hl-default">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-identifier">Bullet</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">ship</span><span class="hl-code">
				, </span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">bullet_image</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-code">
				, </span><span class="hl-identifier">x</span><span class="hl-code">=</span><span class="hl-identifier">x</span><span class="hl-code"> + </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">ship</span><span class="hl-code">.</span><span class="hl-identifier">image</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-code"> - </span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">.</span><span class="hl-identifier">bullet_image</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-code">
				, </span><span class="hl-identifier">y</span><span class="hl-code">=</span><span class="hl-identifier">y</span><span class="hl-brackets">))</span></pre></div></div>
<p>Here we simply make sure that it was the left mouse button that was pressed, and it it was we create a bullet in the correct location using the correct image.</p>
<p>PyGlet does something interesting it gives you a mouse drag event, which is sent when a mouse button is down and the mouse is moved. During the &#8220;mouse drag&#8221; event the &#8220;mouse motion&#8221; event will not be sent. Sine the used will be clicking a lot we need to keep the Space Ship moving by handling the <a href="http://www.pyglet.org/doc/api/pyglet.window.Window-class.html#on_mouse_drag">mouse drag event</a>, if we don&#8217;t the ship will seem to pause if we move the mouse and click the left mouse button.</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">def </span><span class="hl-identifier">on_mouse_drag</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">x</span><span class="hl-code">, </span><span class="hl-identifier">y</span><span class="hl-code">, </span><span class="hl-identifier">dx</span><span class="hl-code">, </span><span class="hl-identifier">dy</span><span class="hl-code">, </span><span class="hl-identifier">buttons</span><span class="hl-code">, </span><span class="hl-identifier">modifiers</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">ship</span><span class="hl-default">.</span><span class="hl-identifier">x</span><span class="hl-default"> = </span><span class="hl-identifier">x
	self</span><span class="hl-default">.</span><span class="hl-identifier">ship</span><span class="hl-default">.</span><span class="hl-identifier">y</span><span class="hl-default"> = </span><span class="hl-identifier">y</span></pre></div></div>
<p>This is exactly the same as the mouse motion handler, and in truth it could call on_mouse_motion() itself.</p>
<h2>Helper</h2>
<p>You might have noticed that I use a helper to load this images. This is pretty simple stuff located in a file called &#8220;helper.py&#8221; in the same directory as PyGletSpace.py:</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-reserved">from </span><span class="hl-identifier">pyglet </span><span class="hl-reserved">import </span><span class="hl-identifier">image

</span><span class="hl-reserved">def </span><span class="hl-identifier">get_image_dir</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 directory used to store the images
	@returns string - the directory
	</span><span class="hl-quotes">&quot;&quot;&quot;
	</span><span class="hl-identifier">directory</span><span class="hl-default"> = </span><span class="hl-identifier">os</span><span class="hl-default">.</span><span class="hl-identifier">path</span><span class="hl-default">.</span><span class="hl-identifier">abspath</span><span class="hl-brackets">(</span><span class="hl-identifier">os</span><span class="hl-code">.</span><span class="hl-identifier">path</span><span class="hl-code">.</span><span class="hl-identifier">dirname</span><span class="hl-brackets">(</span><span class="hl-identifier">__file__</span><span class="hl-brackets">))
	</span><span class="hl-identifier">directory</span><span class="hl-default"> = </span><span class="hl-identifier">os</span><span class="hl-default">.</span><span class="hl-identifier">path</span><span class="hl-default">.</span><span class="hl-identifier">join</span><span class="hl-brackets">(</span><span class="hl-identifier">directory</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">data</span><span class="hl-quotes">'</span><span class="hl-brackets">)
	</span><span class="hl-reserved">return </span><span class="hl-identifier">directory

</span><span class="hl-reserved">def </span><span class="hl-identifier">load_image</span><span class="hl-brackets">(</span><span class="hl-identifier">image_file_name</span><span class="hl-brackets">)</span><span class="hl-default">:

	</span><span class="hl-identifier">full_path</span><span class="hl-default"> = </span><span class="hl-identifier">os</span><span class="hl-default">.</span><span class="hl-identifier">path</span><span class="hl-default">.</span><span class="hl-identifier">join</span><span class="hl-brackets">(</span><span class="hl-identifier">get_image_dir</span><span class="hl-brackets">()</span><span class="hl-code">, </span><span class="hl-identifier">image_file_name</span><span class="hl-brackets">)
	</span><span class="hl-reserved">return </span><span class="hl-identifier">image</span><span class="hl-default">.</span><span class="hl-identifier">load</span><span class="hl-brackets">(</span><span class="hl-identifier">full_path</span><span class="hl-brackets">)</span></pre></div></div>
<h2>Done!</h2>
<p><img style="margin: 0pt 10px 10px 0pt;" src="http://www.learningpython.com/images/pyglet_01/pyglet_02.png" alt="Python Game PyGlet" border="0"/><br />
<img /></p>
<p>You can download the full source to this tutorial <a href="http://www.learningpython.com/sources/pygletSpace.tar.gz">here</a>.</p>
<p>That&#8217;s it for this wonderfully short game and lengthy tutorial. What&#8217;s I&#8217;ve found is that PyGlet is an extremely interesting library that I will continue to keep an eye on. At a certain point I had heard that it was going to be including some Sprite classes and collision classes as well, but it seems as though this may be on the back burner for the initial release.</p>
<p>It will be interesting to see where this goes, what I would like to see would be a simple 2d (since that&#8217;s all I&#8217;m interested in fooling around with for the most part) engine built on top of PyGlet. A simple way to work with all of the different features that a 2d game needs. Hopefully something like that will come along at some point, heck maybe it&#8217;s already out there and I just don&#8217;t know about it yet. But if anyone is working on one, or interested in setting something like this up drop me a line.</p>
<p>[1] http://www.pyglet.org/</p>
<div style="float:right;margin:0px 0px 0px 0px;"><a href="http://www.google.com/reader/link?url=http://www.learningpython.com/2007/11/10/creating-a-game-with-pyglet-and-python/&title=Creating a game with PyGlet and Python&srcTitle=learning python&srcURL=http://www.learningpython.com"target="_blank" rel=""><img border="0" src="http://www.learningpython.com/wp-content/plugins/wp-google-buzz/icon/12.png" style="opacity:1;filter:alpha(opacity=100)" onmouseover="this.style.opacity=0.8;this.filters.alpha.opacity=70" onmouseout="this.style.opacity=1;this.filters.alpha.opacity=100"/> </a></div>]]></content:encoded>
			<wfw:commentRss>http://www.learningpython.com/2007/11/10/creating-a-game-with-pyglet-and-python/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>
