<?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>Silveira Neto &#187; pygame</title>
	<atom:link href="http://silveiraneto.net/tag/pygame/feed/" rel="self" type="application/rss+xml" />
	<link>http://silveiraneto.net</link>
	<description>the world is a pixel</description>
	<lastBuildDate>Sun, 08 Jan 2012 05:17:57 +0000</lastBuildDate>
	<language>pt-br</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Python Fast XML Parsing</title>
		<link>http://silveiraneto.net/2009/12/25/python-fast-xml-parsing/</link>
		<comments>http://silveiraneto.net/2009/12/25/python-fast-xml-parsing/#comments</comments>
		<pubDate>Fri, 25 Dec 2009 18:04:50 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[dtd]]></category>
		<category><![CDATA[expat]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[pygame]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[sax]]></category>
		<category><![CDATA[schema]]></category>
		<category><![CDATA[urllib]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3081</guid>
		<description><![CDATA[Here is a useful tip on Python XML decoding. I was extending xml.sax.ContentHandler class in a example to decode maps for a Pygame application when my connection went down and I noticed that the program stop working raising a exception regarded a call to urlib (a module for retrieve resources by url). I noticed that [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="size-full wp-image-3082 aligncenter" title="monty python bunny toy" src="http://silveiraneto.net/wp-content/uploads/2009/12/monty_python_bunny_toy.jpg" alt="" width="175" height="178" /></p>
<p>Here is a useful tip on Python XML decoding.</p>
<p>I was extending <a title="Python Documentation on XML SAX" href="http://docs.python.org/library/xml.sax.html">xml.sax.ContentHandler</a> class in <a title="Tiled TMX Map Loader for Pygame" href="http://silveiraneto.net/2009/12/19/tiled-tmx-map-loader-for-pygame/">a example to decode maps for a Pygame application</a> when my connection went down and I noticed that the program stop working raising a exception regarded a call to <a title="Python Documentation on urllib" href="http://docs.python.org/library/urllib.html">urlib</a> (a module for retrieve resources by url). I noticed that the module was getting the remote <a title="Wikipedia on Document Type Definition" href="http://en.wikipedia.org/wiki/Document_Type_Definition">DTD schema</a> to validate the XML.</p>
<div class="wp_syntax">
<div class="code">
<pre class="xml xml" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE map SYSTEM &quot;http://mapeditor.org/dtd/1.0/map.dtd&quot;&gt;</span></pre>
</div>
</div>
<p>This is not a requirement for my applications and it&#8217;s a huge performance overhead when works (almost 1 second for each map loaded) and when the applications is running in a environment without Internet it just waits for almost a minute and then fail with the remain decoding. A dirty workaround is open the XML file and get rid of the line containing the DTD reference.</p>
<p>But the correct way to programming XML decoding when we are not concerned on validate a XML schema is just the <a href="http://docs.python.org/library/pyexpat.html">xml.parsers.expat</a>. Instead of using a interface you just have to set some callback functions with the behaviors we want. This is a example from the documentation:</p>
<div class="wp_syntax">
<div class="code">
<pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">parsers</span>.<span style="color: black;">expat</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># 3 handler functions</span>
<span style="color: #ff7700;font-weight:bold;">def</span> start_element<span style="color: black;">&#40;</span>name, attrs<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Start element:'</span>, name, attrs
<span style="color: #ff7700;font-weight:bold;">def</span> end_element<span style="color: black;">&#40;</span>name<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'End element:'</span>, name
<span style="color: #ff7700;font-weight:bold;">def</span> char_data<span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Character data:'</span>, <span style="color: #dc143c;">repr</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>
&nbsp;
p = <span style="color: #dc143c;">xml</span>.<span style="color: black;">parsers</span>.<span style="color: black;">expat</span>.<span style="color: black;">ParserCreate</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
p.<span style="color: black;">StartElementHandler</span> = start_element
p.<span style="color: black;">EndElementHandler</span> = end_element
p.<span style="color: black;">CharacterDataHandler</span> = char_data
&nbsp;
p.<span style="color: black;">Parse</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;&quot;&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;parent id=&quot;top&quot;&gt;&lt;child1 name=&quot;paul&quot;&gt;Text goes here&lt;/child1&gt;
&lt;child2 name=&quot;fred&quot;&gt;More text&lt;/child2&gt;
&lt;/parent&gt;&quot;&quot;&quot;</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span></pre>
</div>
</div>
<p>The output:</p>
<pre>
Start element: parent {'id': 'top'}
Start element: child1 {'name': 'paul'}
Character data: 'Text goes here'
End element: child1
Character data: '\n'
Start element: child2 {'name': 'fred'}
Character data: 'More text'
End element: child2
Character data: '\n'
End element: parent
</pre>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/12/25/python-fast-xml-parsing/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Pygame: Running Orcs</title>
		<link>http://silveiraneto.net/2009/12/11/pygame-running-orcs/</link>
		<comments>http://silveiraneto.net/2009/12/11/pygame-running-orcs/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 10:47:27 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[orc]]></category>
		<category><![CDATA[Pixelart]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[pygame]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3048</guid>
		<description><![CDATA[Here is a Pygame Sprite animation using the approach presented by Joe Wreschnig and Nicolas Crovatti. It&#8217;s not yet exactly what I need but is very suitable. import pygame, random from pygame.locals import * &#160; class Char&#40;pygame.sprite.Sprite&#41;: x,y = &#40;100,0&#41; def __init__&#40;self, img, frames=1, modes=1, w=32, h=32, fps=3&#41;: pygame.sprite.Sprite.__init__&#40;self&#41; original_width, original_height = img.get_size&#40;&#41; self._w = [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a <a href="http://www.pygame.org/docs/ref/sprite.html">Pygame Sprite</a> animation using the approach presented by <a href="http://www.sacredchao.net/~piman/writing/sprite-tutorial.shtml">Joe Wreschnig</a> and <a href="http://blog.shinylittlething.com/2009/07/21/pygame-and-animated-sprites/">Nicolas Crovatti</a>. It&#8217;s not yet exactly what I need but is very suitable.</p>
<div class="wp_syntax">
<div class="code">
<pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> pygame, <span style="color: #dc143c;">random</span>
<span style="color: #ff7700;font-weight:bold;">from</span> pygame.<span style="color: #008000;">locals</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Char<span style="color: black;">&#40;</span>pygame.<span style="color: black;">sprite</span>.<span style="color: black;">Sprite</span><span style="color: black;">&#41;</span>:
	x,y = <span style="color: black;">&#40;</span><span style="color: #ff4500;">100</span>,0<span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, img, frames=<span style="color: #ff4500;">1</span>, modes=<span style="color: #ff4500;">1</span>, w=<span style="color: #ff4500;">32</span>, h=<span style="color: #ff4500;">32</span>, fps=<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>:
		pygame.<span style="color: black;">sprite</span>.<span style="color: black;">Sprite</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
		original_width, original_height = img.<span style="color: black;">get_size</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #008000;">self</span>._w = w
		<span style="color: #008000;">self</span>._h = h
		<span style="color: #008000;">self</span>._framelist = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
		<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>original_width/w<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
			<span style="color: #008000;">self</span>._framelist.<span style="color: black;">append</span><span style="color: black;">&#40;</span>img.<span style="color: black;">subsurface</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>i<span style="color: #66cc66;">*</span>w,0,w,h<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">image</span> = <span style="color: #008000;">self</span>._framelist<span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span>
		<span style="color: #008000;">self</span>._start = pygame.<span style="color: #dc143c;">time</span>.<span style="color: black;">get_ticks</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #008000;">self</span>._delay = <span style="color: #ff4500;">1000</span> / fps
		<span style="color: #008000;">self</span>._last_update = 0
		<span style="color: #008000;">self</span>._frame = 0
		<span style="color: #008000;">self</span>.<span style="color: black;">update</span><span style="color: black;">&#40;</span>pygame.<span style="color: #dc143c;">time</span>.<span style="color: black;">get_ticks</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">100</span>, <span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> set_pos<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, x, y<span style="color: black;">&#41;</span>:
		<span style="color: #008000;">self</span>.<span style="color: black;">x</span> = x
		<span style="color: #008000;">self</span>.<span style="color: black;">y</span> = y
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> get_pos<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">x</span>,<span style="color: #008000;">self</span>.<span style="color: black;">y</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> update<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, t, width, height<span style="color: black;">&#41;</span>:
		<span style="color: #808080; font-style: italic;"># postion</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">y</span>+=<span style="color: #ff4500;">1</span>
		<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">y</span><span style="color: #66cc66;">&gt;</span>width<span style="color: black;">&#41;</span>:
			<span style="color: #008000;">self</span>.<span style="color: black;">x</span> = <span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span>0,height-<span style="color: #008000;">self</span>._w<span style="color: black;">&#41;</span>
			<span style="color: #008000;">self</span>.<span style="color: black;">y</span> = -<span style="color: #008000;">self</span>._h
&nbsp;
		<span style="color: #808080; font-style: italic;"># animation</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> t - <span style="color: #008000;">self</span>._last_update <span style="color: #66cc66;">&gt;</span> <span style="color: #008000;">self</span>._delay:
			<span style="color: #008000;">self</span>._frame += <span style="color: #ff4500;">1</span>
			<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>._frame <span style="color: #66cc66;">&gt;</span>= <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._framelist<span style="color: black;">&#41;</span>:
				<span style="color: #008000;">self</span>._frame = 0
			<span style="color: #008000;">self</span>.<span style="color: black;">image</span> = <span style="color: #008000;">self</span>._framelist<span style="color: black;">&#91;</span><span style="color: #008000;">self</span>._frame<span style="color: black;">&#93;</span>
			<span style="color: #008000;">self</span>._last_update = t
&nbsp;
SCREEN_W, SCREEN_H = <span style="color: black;">&#40;</span><span style="color: #ff4500;">320</span>, <span style="color: #ff4500;">320</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	pygame.<span style="color: black;">init</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	screen = pygame.<span style="color: black;">display</span>.<span style="color: black;">set_mode</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>SCREEN_W, SCREEN_H<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	background = pygame.<span style="color: black;">image</span>.<span style="color: black;">load</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;field.png&quot;</span><span style="color: black;">&#41;</span>
	img_orc = pygame.<span style="color: black;">image</span>.<span style="color: black;">load</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;orc.png&quot;</span><span style="color: black;">&#41;</span>
	orc = Char<span style="color: black;">&#40;</span>img_orc, <span style="color: #ff4500;">4</span>, <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">32</span>, <span style="color: #ff4500;">48</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">while</span> pygame.<span style="color: black;">event</span>.<span style="color: black;">poll</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: #008000;">type</span> <span style="color: #66cc66;">!</span>= KEYDOWN:
		screen.<span style="color: black;">blit</span><span style="color: black;">&#40;</span>background, <span style="color: black;">&#40;</span>0,0<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
		screen.<span style="color: black;">blit</span><span style="color: black;">&#40;</span>orc.<span style="color: black;">image</span>,  orc.<span style="color: black;">get_pos</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
		orc.<span style="color: black;">update</span><span style="color: black;">&#40;</span>pygame.<span style="color: #dc143c;">time</span>.<span style="color: black;">get_ticks</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, SCREEN_W, SCREEN_H<span style="color: black;">&#41;</span>
		pygame.<span style="color: black;">display</span>.<span style="color: black;">update</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		pygame.<span style="color: #dc143c;">time</span>.<span style="color: black;">delay</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>: main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
</div>
</div>
<p>Here is it working:</p>
<p><center><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/J3oSUa6oiuk&#038;hl=pt_BR&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/J3oSUa6oiuk&#038;hl=pt_BR&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></center></p>
<p><ins datetime="2011-08-24T18:33:48+00:00">Uptade: I put this source and images at the <a href="https://github.com/silveira/openpixels/tree/master/examples/python/running_orcs">OpenPixel project in Github</a></ins></p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/12/11/pygame-running-orcs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Pygame Simple Key Handling</title>
		<link>http://silveiraneto.net/2009/12/10/pygame-simple-key-handling/</link>
		<comments>http://silveiraneto.net/2009/12/10/pygame-simple-key-handling/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 03:50:39 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[pygame]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=3029</guid>
		<description><![CDATA[Here&#8217;s a simple key handle in Pygame wheres you move a circle using keyboard. import pygame from pygame.locals import * &#160; def main&#40;&#41;: x,y = &#40;100,100&#41; pygame.init&#40;&#41; screen = pygame.display.set_mode&#40;&#40;400, 400&#41;&#41; while 1: pygame.time.delay&#40;1000/60&#41; # exit handle for event in pygame.event.get&#40;&#41;: if event.type == QUIT: return elif event.type == KEYDOWN and event.key == K_ESCAPE: return [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a simple key handle in <a href="http://www.pygame.org/">Pygame</a> wheres you move a circle using keyboard.</p>
<div class="wp_syntax">
<div class="code">
<pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> pygame
<span style="color: #ff7700;font-weight:bold;">from</span> pygame.<span style="color: #008000;">locals</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	x,y = <span style="color: black;">&#40;</span><span style="color: #ff4500;">100</span>,<span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>
	pygame.<span style="color: black;">init</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	screen = pygame.<span style="color: black;">display</span>.<span style="color: black;">set_mode</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">400</span>, <span style="color: #ff4500;">400</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff4500;">1</span>:
		pygame.<span style="color: #dc143c;">time</span>.<span style="color: black;">delay</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1000</span>/<span style="color: #ff4500;">60</span><span style="color: black;">&#41;</span>
      <span style="color: #808080; font-style: italic;"># exit handle</span>
		<span style="color: #ff7700;font-weight:bold;">for</span> event <span style="color: #ff7700;font-weight:bold;">in</span> pygame.<span style="color: black;">event</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
			<span style="color: #ff7700;font-weight:bold;">if</span> event.<span style="color: #008000;">type</span> == QUIT:
				<span style="color: #ff7700;font-weight:bold;">return</span>
			<span style="color: #ff7700;font-weight:bold;">elif</span> event.<span style="color: #008000;">type</span> == KEYDOWN <span style="color: #ff7700;font-weight:bold;">and</span> event.<span style="color: black;">key</span> == K_ESCAPE:
				<span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
      <span style="color: #808080; font-style: italic;"># keys handle </span>
		key=pygame.<span style="color: black;">key</span>.<span style="color: black;">get_pressed</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> key<span style="color: black;">&#91;</span>K_LEFT<span style="color: black;">&#93;</span>:
			x-=<span style="color: #ff4500;">1</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> key<span style="color: black;">&#91;</span>K_RIGHT<span style="color: black;">&#93;</span>:
			x+=<span style="color: #ff4500;">1</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> key<span style="color: black;">&#91;</span>K_UP<span style="color: black;">&#93;</span>:
			y-=<span style="color: #ff4500;">1</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> key<span style="color: black;">&#91;</span>K_DOWN<span style="color: black;">&#93;</span>:
			y+=<span style="color: #ff4500;">1</span>
&nbsp;
		<span style="color: #808080; font-style: italic;"># fill background and draw a white circle</span>
		screen.<span style="color: black;">fill</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">255</span>,<span style="color: #ff4500;">255</span>,<span style="color: #ff4500;">255</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
		pygame.<span style="color: black;">draw</span>.<span style="color: black;">circle</span><span style="color: black;">&#40;</span>screen, <span style="color: black;">&#40;</span>0,0,0<span style="color: black;">&#41;</span>, <span style="color: black;">&#91;</span>x,y<span style="color: black;">&#93;</span>, <span style="color: #ff4500;">30</span><span style="color: black;">&#41;</span>
		pygame.<span style="color: black;">display</span>.<span style="color: black;">flip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>: main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
</div>
</div>
<p>Here&#8217;s a video of it working:</p>
<p><center><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/ehIqTEvAChI&#038;hl=pt_BR&#038;fs=1&#038;"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/ehIqTEvAChI&#038;hl=pt_BR&#038;fs=1&#038;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></center></p>
<p>Function <a href="http://www.pygame.org/docs/ref/key.html#pygame.key.get_pressed">pygame.key.get_pressed</a> Returns a sequence of boolean values representing the state of every key on the keyboard. It&#8217;s very useful because usually on others game platforms I have to create it by myself.</p>
<p>This approach allow me to handle more than one key at time. For example, left and up keys can be pressed and each one is handled separately creating a diagonal movement.</p>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/12/10/pygame-simple-key-handling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pygame, Simple Space Effect</title>
		<link>http://silveiraneto.net/2009/08/12/pygame-simple-space-effect/</link>
		<comments>http://silveiraneto.net/2009/08/12/pygame-simple-space-effect/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 10:14:53 +0000</pubDate>
		<dc:creator>Silveira</dc:creator>
				<category><![CDATA[english]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[game loop]]></category>
		<category><![CDATA[list comprehension]]></category>
		<category><![CDATA[pygame]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SDL]]></category>
		<category><![CDATA[Simple Direct Layer]]></category>
		<category><![CDATA[sliding stars]]></category>
		<category><![CDATA[Space]]></category>
		<category><![CDATA[space effect]]></category>
		<category><![CDATA[stars]]></category>

		<guid isPermaLink="false">http://silveiraneto.net/?p=2705</guid>
		<description><![CDATA[This is a simple space effect of sliding stars using Pygame. Direct link to video: simple_space_effect_01.ogv We set some constants like the screen size and the number N of star we want. N = 200 SCREEN_W, SCREEN_H = &#40;640, 480&#41; Using list comprehension we create a list of random points in the screen, that will [...]]]></description>
			<content:encoded><![CDATA[<p>This is a simple space effect of sliding stars using <a href="http://pygame.org">Pygame</a>.</p>
<p><center><object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/TXGV6guTOno&amp;rel=0&amp;color1=0x3a3a3a&amp;color2=0x999999"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/TXGV6guTOno&amp;rel=0&amp;color1=0x3a3a3a&amp;color2=0x999999" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></object></center></p>
<p><small>Direct link to video: <a href="http://silveiraneto.net/downloads/simple_space_effect_01.ogv">simple_space_effect_01.ogv</a></small></p>
<p>We set some constants like the screen size and the number N of star we want.</p>
<div class="wp_syntax">
<div class="code">
<pre class="python python" style="font-family:monospace;">N = <span style="color: #ff4500;">200</span>
SCREEN_W, SCREEN_H = <span style="color: black;">&#40;</span><span style="color: #ff4500;">640</span>, <span style="color: #ff4500;">480</span><span style="color: black;">&#41;</span></pre>
</div>
</div>
<p>Using <a href="http://en.wikipedia.org/wiki/List_comprehension#Python">list comprehension</a> we create a list of random points in the screen, that will be our stars. The size of this list is N.</p>
<div class="wp_syntax">
<div class="code">
<pre class="python python" style="font-family:monospace;">stars = <span style="color: black;">&#91;</span>
  <span style="color: black;">&#91;</span><span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span>0, SCREEN_W<span style="color: black;">&#41;</span>,<span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span>0, SCREEN_H<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>N<span style="color: black;">&#41;</span>
<span style="color: black;">&#93;</span></pre>
</div>
</div>
<p>Each star is represented by one tuple on the stars list. The first star is on stars[0] and is a touple with [x, y] positions.</p>
<p>At each step from the <a href="http://en.wikipedia.org/wiki/Game_programming#The_game_loop">game loop</a> we draw and update the position of each star. A star is draw as a white line of one pixel. See the <a href="http://www.pygame.org/docs/ref/draw.html#pygame.draw.line">pygame.draw.line doc</a>.</p>
<div class="wp_syntax">
<div class="code">
<pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">for</span> star <span style="color: #ff7700;font-weight:bold;">in</span> stars:
  pygame.<span style="color: black;">draw</span>.<span style="color: black;">line</span><span style="color: black;">&#40;</span>background,
    <span style="color: black;">&#40;</span><span style="color: #ff4500;">255</span>, <span style="color: #ff4500;">255</span>, <span style="color: #ff4500;">255</span><span style="color: black;">&#41;</span>, <span style="color: black;">&#40;</span>star<span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span>, star<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: black;">&#40;</span>star<span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span>, star<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  star<span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span> = star<span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span> - <span style="color: #ff4500;">1</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> star<span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span> <span style="color: #66cc66;">&lt;</span> 0:
      star<span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span> = SCREEN_W
      star<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> = <span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span>0, SCREEN_H<span style="color: black;">&#41;</span></pre>
</div>
</div>
<p>In this example we update the position of a star by decreasing its horizontal position. When the horizontal position is less than zero, it&#8217;s not displayed on the screen anymore so we replace its horizontal position (star[0]) by the screen width (SCREEN_W) and the vertical position (star[1]) by a new random position. This will be like create a new star and guarantee always a different pattern of sliding stars. </p>
<p>The complete code:</p>
<div class="wp_syntax">
<div class="code">
<pre class="python python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># A simple effect of sliding stars to create a deep space sensation.</span>
<span style="color: #808080; font-style: italic;"># by Silveira Neto &lt;me@silveiraneto.net&gt;</span>
<span style="color: #808080; font-style: italic;"># Free under the terms of GPLv3 license</span>
<span style="color: #808080; font-style: italic;"># See http://silveiraneto.net/2009/08/12/pygame-simple-space-effect/</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>,<span style="color: #dc143c;">sys</span>,<span style="color: #dc143c;">random</span>
<span style="color: #ff7700;font-weight:bold;">import</span> pygame
<span style="color: #ff7700;font-weight:bold;">from</span> pygame.<span style="color: #008000;">locals</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Constants </span>
N = <span style="color: #ff4500;">200</span>
SCREEN_W, SCREEN_H = <span style="color: black;">&#40;</span><span style="color: #ff4500;">640</span>, <span style="color: #ff4500;">480</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	<span style="color: #808080; font-style: italic;"># basic start</span>
	pygame.<span style="color: black;">init</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	screen = pygame.<span style="color: black;">display</span>.<span style="color: black;">set_mode</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>SCREEN_W,SCREEN_H<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	pygame.<span style="color: black;">display</span>.<span style="color: black;">set_caption</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Simple Space Effect by Silveira Neto'</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;"># create background</span>
	background = pygame.<span style="color: black;">Surface</span><span style="color: black;">&#40;</span>screen.<span style="color: black;">get_size</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	background = background.<span style="color: black;">convert</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;"># generate N stars</span>
	stars = <span style="color: black;">&#91;</span>
		<span style="color: black;">&#91;</span><span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span>0, SCREEN_W<span style="color: black;">&#41;</span>,<span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span>0, SCREEN_H<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
		<span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>N<span style="color: black;">&#41;</span>
	<span style="color: black;">&#93;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;"># main loop</span>
	clock = pygame.<span style="color: #dc143c;">time</span>.<span style="color: black;">Clock</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff4500;">1</span>:
		clock.<span style="color: black;">tick</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">22</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">for</span> event <span style="color: #ff7700;font-weight:bold;">in</span> pygame.<span style="color: black;">event</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
			<span style="color: #ff7700;font-weight:bold;">if</span> event.<span style="color: #008000;">type</span> == QUIT:
				<span style="color: #ff7700;font-weight:bold;">return</span>
			<span style="color: #ff7700;font-weight:bold;">elif</span> event.<span style="color: #008000;">type</span> == KEYDOWN <span style="color: #ff7700;font-weight:bold;">and</span> event.<span style="color: black;">key</span> == K_ESCAPE:
				<span style="color: #ff7700;font-weight:bold;">return</span>
		background.<span style="color: black;">fill</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>0,0,0<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">for</span> star <span style="color: #ff7700;font-weight:bold;">in</span> stars:
			pygame.<span style="color: black;">draw</span>.<span style="color: black;">line</span><span style="color: black;">&#40;</span>background,
				<span style="color: black;">&#40;</span><span style="color: #ff4500;">255</span>, <span style="color: #ff4500;">255</span>, <span style="color: #ff4500;">255</span><span style="color: black;">&#41;</span>, <span style="color: black;">&#40;</span>star<span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span>, star<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: black;">&#40;</span>star<span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span>, star<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
			star<span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span> = star<span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span> - <span style="color: #ff4500;">1</span>
			<span style="color: #ff7700;font-weight:bold;">if</span> star<span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span> <span style="color: #66cc66;">&lt;</span> 0:
				star<span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span> = SCREEN_W
				star<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> = <span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span>0, SCREEN_H<span style="color: black;">&#41;</span>
		screen.<span style="color: black;">blit</span><span style="color: black;">&#40;</span>background, <span style="color: black;">&#40;</span>0,0<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
		pygame.<span style="color: black;">display</span>.<span style="color: black;">flip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>: main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://silveiraneto.net/2009/08/12/pygame-simple-space-effect/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

