Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added a GUI maze program, updated credits and book structure.

  • Loading branch information...
commit e2ed122ba5ab79d2eb144e758a8591957fa249e7 1 parent 968ca11
@jmhobbs jmhobbs authored
View
3  book.xml
@@ -30,6 +30,9 @@
<!-- Chapters -->
<!-- Create a chapter file, write it up, and include it! -->
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="chapter_rock_paper_scissors.xml"/>
+ <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="chapter_hangman.xml"/>
+ <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="chapter_maze.xml"/>
+ <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="chapter_better_maze.xml"/>
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="credits.xml"/>
View
209 chapter_better_maze.xml
@@ -0,0 +1,209 @@
+mv<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE chapter [
+<!ENTITY % allent SYSTEM "w3centities-f.ent">
+%allent;
+]>
+<chapter version="5.0" xmlns="http://docbook.org/ns/docbook"
+ xmlns:xlink="http://www.w3.org/1999/xlink"
+ xmlns:xi="http://www.w3.org/2001/XInclude"
+ xmlns:svg="http://www.w3.org/2000/svg"
+ xmlns:mml="http://www.w3.org/1998/Math/MathML"
+ xmlns:html="http://www.w3.org/1999/xhtml"
+ xmlns:db="http://docbook.org/ns/docbook">
+
+ <title>Better Maze</title>
+ <sect1>
+ <title>The Game Plan</title>
+ <para>Coming Soon</para>
+ <figure xml:id="fig.">
+ <title></title>
+ <screenshot>
+ <mediaobject>
+ <imageobject>
+ <imagedata fileref="chapter_better_maze.python.png" align="center" width="" />
+ </imageobject>
+ <caption><para>An Example Maze</para></caption>
+ </mediaobject>
+ </screenshot>
+ </figure>
+ </sect1>
+ <sect2>
+ <title>Game Rules</title>
+ <para>Coming Soon</para>
+ </sect2>
+ <sect3>
+ <title>Program Listing - maze.txt</title>
+ <para>
+ Your program needs maze to run, this file provides that maze.
+ </para>
+ <para>
+ You can use our maze shown below, or add your own if you like. Just be sure
+ to include an "S" and an "E" in the maze for the start and end points.
+ </para>
+ <programlisting language="text"><![CDATA[
+##########
+#S# #
+# #### #
+# # #### #
+# # ## #
+# ## # # #
+# # # #
+## # #####
+# # E#
+##########
+ ]]></programlisting>
+ </sect3>
+ <sect4 xml:id="code.py.better.maze">
+ <title>Program Listing - Python</title>
+ <programlisting language="python"><![CDATA[
+# -*- coding: utf-8 -*-
+
+import pygame
+
+class Maze:
+
+ start = ( 0, 0 )
+ end = ( 0, 0 )
+ user = ( 0, 0 )
+ maze = []
+ moves = 0
+ dirty = []
+
+ user_surface = None
+ maze_surface = None
+
+ def __init__ ( self, maze_file ):
+ self.load_maze( maze_file )
+
+ def load_maze ( self, maze_file ):
+
+ self.maze = []
+
+ f = open( maze_file, 'r' )
+ row = 0
+ for line in f:
+ col = 0
+ self.maze.append( [] )
+ for char in line:
+ if char == 'S':
+ self.start = ( row, col )
+ elif char == 'E':
+ self.end = ( row, col )
+ self.maze[row].append( char )
+ col = col + 1
+ row = row + 1
+ f.close()
+
+ self.size = ( col * 10, row * 10 )
+
+ self.user = self.start
+
+ def init_window ( self ):
+ self.window = pygame.display.set_mode( self.size )
+ pygame.display.set_caption( 'Maze' )
+ pygame.display.update()
+
+ self.user_surface = pygame.Surface( ( 10, 10 ) )
+ self.user_surface.fill( pygame.Color( 255, 255, 255 ) )
+
+ self.maze_surface = pygame.Surface( self.size )
+
+ def render_maze ( self ):
+ self.maze_surface.fill( pygame.Color( 0, 0, 0 ) )
+
+ red_block = pygame.Surface( ( 10, 10 ) )
+ red_block.fill( pygame.Color( 255, 0, 0 ) )
+
+ green_block = pygame.Surface( ( 10, 10 ) )
+ green_block.fill( pygame.Color( 0, 255, 0 ) )
+
+ gray_block = pygame.Surface( ( 10, 10 ) )
+ gray_block.fill( pygame.Color( 200, 200, 200 ) )
+
+ row = 0
+ for row_data in self.maze:
+ col = 0
+ for char in row_data:
+ if 'S' == char:
+ self.maze_surface.blit( red_block, ( col * 10, row * 10 ) )
+ elif 'E' == char:
+ self.maze_surface.blit( green_block, ( col * 10, row * 10 ) )
+ elif ' ' == char:
+ pass
+ else:
+ self.maze_surface.blit( gray_block, ( col * 10, row * 10 ) )
+ col = col + 1
+ row = row + 1
+
+ def run ( self ):
+ self.init_window()
+ self.render_maze()
+
+ self.window.blit( self.maze_surface, ( 0, 0, self.size[1], self.size[0] ) )
+ self.dirty.append( ( 0, 0, self.size[1], self.size[0] ) )
+
+ self.window.blit( self.user_surface, ( self.start[1] * 10, self.start[0] * 10 ) )
+
+ self.update()
+
+ while True:
+ for event in pygame.event.get():
+ if event.type == pygame.QUIT:
+ pygame.quit()
+ exit()
+ elif event.type == pygame.KEYUP:
+ goto = self.user
+
+ if event.key == pygame.K_DOWN:
+ goto = ( self.user[0]+1, self.user[1] )
+ elif event.key == pygame.K_UP:
+ goto = ( self.user[0]-1, self.user[1] )
+ elif event.key == pygame.K_LEFT:
+ goto = ( self.user[0], self.user[1]-1 )
+ elif event.key == pygame.K_RIGHT:
+ goto = ( self.user[0], self.user[1]+1 )
+
+ if self.maze[goto[0]][goto[1]] == 'E':
+ print "You escaped in %d moves!" % self.moves
+ pygame.quit()
+ exit()
+ elif self.maze[goto[0]][goto[1]] != ' ' and self.maze[goto[0]][goto[1]] != 'S':
+ goto = self.user
+
+ if self.user != goto:
+
+ self.window.blit( self.user_surface, ( goto[1] * 10, goto[0] * 10 ) )
+ self.window.blit( self.maze_surface, ( self.user[1] * 10, self.user[0] * 10 ), ( self.user[1] * 10, self.user[0] * 10, 10, 10 ) )
+
+ self.dirty.append( ( self.user[1] * 10, self.user[0] * 10, 10, 10 ) )
+ self.dirty.append( ( goto[1] * 10, goto[0] * 10, 10, 10 ) )
+
+ self.user = goto
+ self.moves = self.moves + 1
+ pygame.display.set_caption( 'Maze - %d' % self.moves )
+
+ self.update()
+
+ def update ( self ):
+ if 0 != len( self.dirty ):
+ pygame.display.update( self.dirty )
+ self.dirty = []
+
+if __name__ == '__main__':
+ import traceback, sys
+
+ if 1 >= len( sys.argv ):
+ print "You must specify a maze file!"
+ exit(1)
+
+ try:
+ pygame.init()
+ maze = Maze( sys.argv[1] )
+ maze.run()
+ except SystemExit, e:
+ pass
+ except:
+ traceback.print_exc()
+ ]]></programlisting>
+ </sect4>
+</chapter>
View
2  chapter_hangman.xml
@@ -37,7 +37,7 @@ snake
lizard
]]></programlisting>
</sect3>
- <sect4>
+ <sect4 xml:id="code.py.hangman">
<title>Program Listing - Python</title>
<programlisting language="python"><![CDATA[
# -*- coding: utf-8 -*-
View
2  chapter_maze.xml
@@ -48,7 +48,7 @@
'------------++-+-+---------------'
]]></programlisting>
</sect3>
- <sect4>
+ <sect4 xml:id="code.py.maze">
<title>Program Listing - Python</title>
<programlisting language="python"><![CDATA[
# -*- coding: utf-8 -*-
View
148 code/chapter_better_maze/better_maze.py
@@ -0,0 +1,148 @@
+# -*- coding: utf-8 -*-
+
+import pygame
+
+class Maze:
+
+ start = ( 0, 0 )
+ end = ( 0, 0 )
+ user = ( 0, 0 )
+ maze = []
+ moves = 0
+ dirty = []
+
+ user_surface = None
+ maze_surface = None
+
+ def __init__ ( self, maze_file ):
+ self.load_maze( maze_file )
+
+ def load_maze ( self, maze_file ):
+
+ self.maze = []
+
+ f = open( maze_file, 'r' )
+ row = 0
+ for line in f:
+ col = 0
+ self.maze.append( [] )
+ for char in line:
+ if char == 'S':
+ self.start = ( row, col )
+ elif char == 'E':
+ self.end = ( row, col )
+ self.maze[row].append( char )
+ col = col + 1
+ row = row + 1
+ f.close()
+
+ self.size = ( col * 10, row * 10 )
+
+ self.user = self.start
+
+ def init_window ( self ):
+ self.window = pygame.display.set_mode( self.size )
+ pygame.display.set_caption( 'Maze' )
+ pygame.display.update()
+
+ self.user_surface = pygame.Surface( ( 10, 10 ) )
+ self.user_surface.fill( pygame.Color( 255, 255, 255 ) )
+
+ self.maze_surface = pygame.Surface( self.size )
+
+ def render_maze ( self ):
+ self.maze_surface.fill( pygame.Color( 0, 0, 0 ) )
+
+ red_block = pygame.Surface( ( 10, 10 ) )
+ red_block.fill( pygame.Color( 255, 0, 0 ) )
+
+ green_block = pygame.Surface( ( 10, 10 ) )
+ green_block.fill( pygame.Color( 0, 255, 0 ) )
+
+ gray_block = pygame.Surface( ( 10, 10 ) )
+ gray_block.fill( pygame.Color( 200, 200, 200 ) )
+
+ row = 0
+ for row_data in self.maze:
+ col = 0
+ for char in row_data:
+ if 'S' == char:
+ self.maze_surface.blit( red_block, ( col * 10, row * 10 ) )
+ elif 'E' == char:
+ self.maze_surface.blit( green_block, ( col * 10, row * 10 ) )
+ elif ' ' == char:
+ pass
+ else:
+ self.maze_surface.blit( gray_block, ( col * 10, row * 10 ) )
+ col = col + 1
+ row = row + 1
+
+ def run ( self ):
+ self.init_window()
+ self.render_maze()
+
+ self.window.blit( self.maze_surface, ( 0, 0, self.size[1], self.size[0] ) )
+ self.dirty.append( ( 0, 0, self.size[1], self.size[0] ) )
+
+ self.window.blit( self.user_surface, ( self.start[1] * 10, self.start[0] * 10 ) )
+
+ self.update()
+
+ while True:
+ for event in pygame.event.get():
+ if event.type == pygame.QUIT:
+ pygame.quit()
+ exit()
+ elif event.type == pygame.KEYUP:
+ goto = self.user
+
+ if event.key == pygame.K_DOWN:
+ goto = ( self.user[0]+1, self.user[1] )
+ elif event.key == pygame.K_UP:
+ goto = ( self.user[0]-1, self.user[1] )
+ elif event.key == pygame.K_LEFT:
+ goto = ( self.user[0], self.user[1]-1 )
+ elif event.key == pygame.K_RIGHT:
+ goto = ( self.user[0], self.user[1]+1 )
+
+ if self.maze[goto[0]][goto[1]] == 'E':
+ print "You escaped in %d moves!" % self.moves
+ pygame.quit()
+ exit()
+ elif self.maze[goto[0]][goto[1]] != ' ' and self.maze[goto[0]][goto[1]] != 'S':
+ goto = self.user
+
+ if self.user != goto:
+
+ self.window.blit( self.user_surface, ( goto[1] * 10, goto[0] * 10 ) )
+ self.window.blit( self.maze_surface, ( self.user[1] * 10, self.user[0] * 10 ), ( self.user[1] * 10, self.user[0] * 10, 10, 10 ) )
+
+ self.dirty.append( ( self.user[1] * 10, self.user[0] * 10, 10, 10 ) )
+ self.dirty.append( ( goto[1] * 10, goto[0] * 10, 10, 10 ) )
+
+ self.user = goto
+ self.moves = self.moves + 1
+ pygame.display.set_caption( 'Maze - %d' % self.moves )
+
+ self.update()
+
+ def update ( self ):
+ if 0 != len( self.dirty ):
+ pygame.display.update( self.dirty )
+ self.dirty = []
+
+if __name__ == '__main__':
+ import traceback, sys
+
+ if 1 >= len( sys.argv ):
+ print "You must specify a maze file!"
+ exit(1)
+
+ try:
+ pygame.init()
+ maze = Maze( sys.argv[1] )
+ maze.run()
+ except SystemExit, e:
+ pass
+ except:
+ traceback.print_exc()
View
10 code/chapter_better_maze/maze.txt
@@ -0,0 +1,10 @@
+##########
+#S# #
+# #### #
+# # #### #
+# # ## #
+# ## # # #
+# # # #
+## # #####
+# # E#
+##########
View
3  credits.xml
@@ -15,6 +15,9 @@
<para>Many talented programmers have contributed their time and code to this effort.</para>
<itemizedlist>
<listitem><para>Rock, Paper, Scissors (Python), <xref linkend="code.py.rock.paper.scissors" />, copyright 2010 John Hobbs</para></listitem>
+ <listitem><para>Hangman (Python), <xref linkend="code.py.hangman" />, copyright 2010 John Hobbs</para></listitem>
+ <listitem><para>Maze (Python), <xref linkend="code.py.maze" />, copyright 2010 John Hobbs</para></listitem>
+ <listitem><para>Better Maze (Python), <xref linkend="code.py.better.maze" />, copyright 2010 John Hobbs</para></listitem>
</itemizedlist>
View
BIN  images/png/chapter_better_maze.python.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Please sign in to comment.
Something went wrong with that request. Please try again.