这个问题确实太宽泛了,所以我把它缩小了一点。为了专注于我解决的问题,我将首先陈述问题:'我如何制作一张地图——主要是从一个列表中——包含可以与玩家交互的某些属性,即敌方 AI 和战利品图块,并保存关于像城镇和地牢这样的地方,为了基于文本的 RPG 的目的?'
我这样解决了这个问题:
class Player(object):
def __init__(self, name):
self.name = name
def movement(self):
while True:
print room.userpos
move = raw_input("[W], [A], [S], or [D]: ").lower() #Movement WASD.
while True:
if move == 'w': #Have only coded the 'w' of the wasd for simplicity.
x, y = (1, 0) #x, y are column, row respectively. This is done
break ##to apply changes to the player's position on the map.
a = room.column + x #a and b represent the changes that take place to player's
b = room.row + y ##placement index in the map, or 'tilemap' list.
room.userpos = tilemap[a][b]
if room.userpos == 3:
print "LOOT!" #Representing what happens when a player comes across
return room.userpos ##a tile with the value 3 or 'loot'.
break
class Room(object):
def __init__(self, column, row):
self.userpos = tilemap[column][row]
self.column = column #Column/Row dictates player position in tilemap.
self.row = row
floor = 0
entry = 1
exit = 2
loot = 3 #Tile map w/ vairbale names.
tilemap = [[0, 1, 0], #[floor, entry, floor],
[3, 0, 0], #[loot, floor, floor],
[0, 2, 0]] #[floor, exit, floor]
room = Room(0, 0) #Player position for 'room' and 'Room' class -- too similar names
user = Player('Bryce') #for larger exercices, but I figure I could get away with it here.
def main(): #Loads the rest of the program -- returns user position results.
inp = raw_input("Press any button to continue.: ")
if inp != '':
user.movement()
print room.userpos
main()
如果我要加载这个程序,并用 'w' 将字符值从 index[0][0] 向前“移动”到列表的 index[1][0] - 上一行 - - 它返回值 3。这是通过将 Room 类的 userpos 变量映射到地图列表中的特定索引并通过 Player 函数移动跟踪任何更改来完成的。