simple movement in ascii-based rpg
Stefan Antoni
sasoft at gmx.de
Mon Aug 12 19:20:43 EDT 2002
hello list,
i wrote this little "rpg" (it's very bad, i know. i don't have much
coding experience).
now i would like to add the methods for moving the "p" (player) on the
map (see game.player).
i just cannot find out how i could do this. i think even the entire
design is wrong (is it good to have the player class _in_ the game
class? i thought this would be a good idea, because a "player" lives in
a "game") and: is the way i handle the mapfile appropriate for further
development, or not?
my main problem is to build a solid way to move the p(layer) and to
connect this visual output with the game logic.
any opinions?
### sourcecode start ###
#!/usr/bin/env python
import re
class game:
def __init__(self):
# read the map file and remove the \n's
self.mapwithNL = open('nomoremorningmap.py', 'r').readlines()
self.map = []
for line in self.mapwithNL:
self.map.append(re.sub('\n', "", line))
# startpunkt des spielers finden. kann auch später ähnlich benutzt werden um
# den aktuellen aufenthalt des spielers zu finden
def getPlayerLocation(self):
currentLine = 0
for line in self.map:
currentLine += 1
match = str(line).find("p")
if match != -1:
return (match, currentLine)
else:
continue
def printPlayerStartPoint(self):
currentLine = 0
for line in self.map:
currentLine += 1
match = str(line).find("p")
if match != -1:
print line, "<-- spieler gefunden, bei position %s in zeile %s" % (match, currentLine)
else:
print line
continue
class player:
def __init__(self, name):
self.name = name
self.idchar = "p"
def moveUp(self):
pass
def moveDown(self):
pass
def moveLeft(self):
pass
def moveRight(self):
pass
if __name__ == '__main__':
g = game()
p = game.player("test")
g.printPlayerStartPoint()
p.moveUp()
### eof ###
a sample mapfile is attached. the "#"'s are walls and the "o"'s is
floor. "p" is the player.
(sorry for my terrible english and the german comments in the sources)
many thx in advance,
--
Stefan Antoni
"Lügen bringen den kleinen Jesus zum weinen."
spam email will be deleted without being read
html email will be deleted without being read
i prefer signed / encrypted email
-------------- next part --------------
#########################
#ooooooooooooooooooooooo#
#o#o#####o########oopooo#
###o#ooooooooooooooooooo#
#oooo#######o############
###oo#oooooooooooooooooo#
#oooo#oooooooooooooooooo#
#########################
-------------- next part --------------
#!/usr/bin/env python
import re
class game:
def __init__(self):
# read the map file and remove the \n's
self.mapwithNL = open('nomoremorningmap.py', 'r').readlines()
self.map = []
for line in self.mapwithNL:
self.map.append(re.sub('\n', "", line))
# startpunkt des spielers finden. kann auch später ähnlich benutzt werden um
# den aktuellen aufenthalt des spielers zu finden
def getPlayerLocation(self):
currentLine = 0
for line in self.map:
currentLine += 1
match = str(line).find("p")
if match != -1:
return (match, currentLine)
else:
continue
def printPlayerStartPoint(self):
currentLine = 0
for line in self.map:
currentLine += 1
match = str(line).find("p")
if match != -1:
print line, "<-- spieler gefunden, bei position %s in zeile %s" % (match, currentLine)
else:
print line
continue
class player:
def __init__(self, name):
self.name = name
self.idchar = "p"
def moveUp(self):
pass
def moveDown(self):
pass
def moveLeft(self):
pass
def moveRight(self):
pass
if __name__ == '__main__':
g = game()
p = game.player("test")
g.printPlayerStartPoint()
p.moveUp()
More information about the Python-list
mailing list