Mon Sep 10 14:38:56 PDT 2001
Someone has adopted the PyRPG!
I didn't write the Python RPG, it is primarily the work of Brian Blackwell and John Michaelsome
However, I did pull the code down one evening, and cleaned it up a bit. The code I came up with is at the end of this web page.
I was proudest of these 3 lines:
images = {}
for filename in filter( lambda x: x[-4:] == ".gif", os.listdir( os.curdir ) ):
images[ filename[:-4] ] = Tkinter.PhotoImage( file = filename )
I wish that every game library that was striving towards user-friendly-ness would automatically load all the gif's (or whatevers) in the current directory. The code above shows how to do it.
Extra points if it's then just a matter of saying
images[ "robot1" ].draw( 50, 60 )
to draw the image robot1.gif to 50, 60.
Unfortunately, I didn't make it do that. =^_^=
Python is so cool, that if you don't want to have to use the images dictionary, you can just change...
images[ filename[:-4] ] = Tkinter.PhotoImage( file = filename )
...to...
globals()[ filename[:-4] ] = Tkinter.PhotoImage( file = filename )
...and all of the image files are automatically loaded, and assigned to global variables!
So if you have a file "robot.gif" in your directory, you would now be able to type:
robot.draw( 40, 34 )
and the robot would draw to 40, 34!
Tres cool!
# pyrpg - The Python Role-Playing Game Engine
# (C) 1999 Brian Blackwell and John Michelsen, distributed under the terms of the
# GNU General Public License.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# drawmap.py - simple 3D maze drawing module for pyrpg
ver = '0.0.4 lion'
import Tkinter
import os
import time
# GPL #
print "pyrpg", ver, "- (C) 1999 Brian Blackwell."
print "Distributed under the terms of the GNU General Public License."
print "See file COPYING for details."
# build map #
map = [[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,1,1,1],
[1,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,1,0,1,0,1,1,0,1,0,1,0,1,0,1,0,1],
[1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,1],
[1,1,1,1,0,0,0,1,0,0,1,1,5,1,1,0,0,1,1],
[1,0,7,0,0,0,0,0,1,0,1,0,1,0,0,1,0,1,1],
[1,0,1,0,0,1,1,0,0,0,0,0,0,0,1,0,1,0,1],
[1,0,1,1,0,1,0,1,2,0,1,1,1,1,0,0,1,0,1],
[1,0,0,0,0,0,0,1,0,0,0,0,1,0,1,1,1,1,1],
[1,0,1,0,0,1,0,1,0,1,0,0,1,0,1,0,1,0,1],
[1,0,0,0,0,1,0,0,0,0,1,0,0,1,1,0,0,1,1],
[1,0,1,1,0,0,1,0,1,1,0,0,1,0,0,0,1,0,1],
[1,1,0,0,0,1,0,0,0,1,0,0,0,0,1,1,1,0,1],
[1,0,0,1,0,0,1,1,0,1,0,1,0,0,0,1,0,1,1],
[1,4,0,1,0,1,0,0,0,1,1,0,1,0,1,0,1,1,1],
[1,4,1,0,0,1,0,1,1,1,0,0,1,0,1,0,1,0,1],
[1,0,1,0,1,1,0,1,0,0,6,1,0,0,1,8,1,3,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]]
map_height = len( map )
map_width = len( map[ 0 ] )
# turn map template into collection of cells #
def new_cell( state ):
return { "state": state,
"visited": 0,
"contents": [] }
templateDict = { 0: 'empty',
1: 'wall',
2: 'enemy',
3: 'treasure',
4: 'fake_wall',
5: 'teleport',
6: 'stairs_up',
7: 'stairs_down',
8: 'stairs_both' }
for y in range( len( map ) ):
for x in range( len( map[ y ] ) ):
map[ y ][ x ] = new_cell( templateDict[ map[ y ][ x ] ] )
# initial settings #
pos = [1, 1] # player pos: x, y
facing = 'S' # N,E,S,W?
# UI response functions #
def firstperson_click( event = None ):
(w,h) = ( canv.winfo_width(), canv.winfo_height() )
if event.x < w/3: move_left()
elif event.x > 2*w/3: move_right()
else:
if event.y < h/3: print "climb the stairs"
elif event.y > 2*h/3: print "decend the stairs"
else: move_forward()
def overhead_click(self, event):
pos = [int(event.x/canv2scale),int(event.y/canv2scale)]
drawmap()
# player motion functions #
def move_forward( event = None ):
global pos, map
(x,y) = (pos[0], pos[1])
dx = { "N": 0, "E": 1, "S": 0, "W": -1 }[ facing ]
dy = { "N": -1, "E": 0, "S": 1, "W": 0 }[ facing ]
if map[ y+dy ][ x+dx ][ "state" ] != 'wall':
pos = [ x+dx, y+dy ]
map[ y ][ x ][ "visited" ] = 1
drawmap()
def move_back( event = None ):
global pos, map
(x,y) = (pos[0], pos[1])
dx = { "N": 0, "E": -1, "S": 0, "W": 1 }[ facing ]
dy = { "N": 1, "E": 0, "S": -1, "W": 0 }[ facing ]
if map[ y+dy ][ x+dx ][ "state" ] != 'wall':
pos = [ x+dx, y+dy ]
map[ y ][ x ][ "visited" ] = 1
drawmap()
def turn_right( event = None ):
global facing
facing = { "N": "E", "E": "S", "S": "W", "W": "N" }[ facing ]
drawmap()
def turn_left( event = None ):
global facing
facing = { "N": "W", "E": "N", "S": "E", "W": "S" }[ facing ]
drawmap()
def turn_around( event = None ):
global facing
facing = { "N": "S", "E": "W", "S": "N", "W": "E" }[ facing ]
drawmap()
# UI #
root = Tkinter.Tk()
root.title( "PyRPG" )
images = {}
for filename in filter( lambda x: x[-4:] == ".gif", os.listdir( os.curdir ) ):
images[ filename[:-4] ] = Tkinter.PhotoImage( file = filename )
firstperson = Tkinter.Frame( root )
firstperson.pack( side='left', anchor=Tkinter.NW )
overhead = Tkinter.Frame( root )
overhead.pack( side='left', anchor=Tkinter.NW )
canv = Tkinter.Canvas( firstperson, width=253, height=253, relief='sunken', bd=1 )
canv.pack()
canv.bind( "