添加摄像机以跟踪 ASCII 地牢地图中的'player'动



在我的游戏中,每回合都会显示整个地图,但我希望它只显示玩家的位置,也许还会显示玩家周围的3x3网格。任何关于如何在下面的代码中做到这一点的建议都将是非常好的。

我假设你必须更改显示地图的功能,但我不太确定如何打印出玩家x和y周围的3x3网格。

import os 
#map
dungeonMap = [["0","0","0","0","0","0","0","0","0"],
["0",".",".","0",".",".",".",".","0"],
["0",".",".",".",".",".",".","0","0"],
["0",".",".",".",".",".",".",".","0"],
["0",".",".",".","0",".",".",".","0"],
["0",".",".","0","*",".",".",".","0"],
["0",".",".",".",".",".",".",".","0"],
["0",".",".",".",".",".",".",".","0"],
["0",".",".",".",".",".",".",".","0"],
["0",".",".",".",".",".",".",".","0"],
["0",".",".",".",".",".",".",".","0"],
["0","0","0","0","0","0","0","0","0"]]
playerMap  = [["0","0","0","0","0","0","0","0","0"],
["0",".",".","0",".",".",".",".","0"],
["0",".",".",".",".",".",".","0","0"],
["0","S",".",".",".",".",".",".","0"],
["0",".",".",".","0",".",".",".","0"],
["0",".",".","0",".",".",".",".","0"],
["0",".",".",".",".",".",".",".","0"],
["0",".",".",".",".",".",".",".","0"],
["0",".",".",".",".",".",".",".","0"],
["0",".",".",".",".",".",".",".","0"],
["0",".",".",".",".",".",".",".","0"],
["0","0","0","0","0","0","0","0","0"]]

#Displaying the map
def displayMap(maps):
for x in range(0,12):
print(maps[x])
#selecting a map
mapChoice = dungeonMap
displayMap(playerMap)
#initialising the players position
position = mapChoice[0][0]

x = 1
y = 3
print(mapChoice[y][x])
while position != "E":
os.system('cls' if os.name == 'nt' else 'clear')
displayMap(playerMap)
previousX = x
previousY = y
playerMap[y][x] = "."
movement = input("W,S,D,A,MAP").upper()
if movement == "W":
y = y-1
position = mapChoice[y][x]
playerMap[y][x] = "S"

if movement == "S":
y = y+1
position = mapChoice[y][x]
playerMap[y][x] = "S"

if movement == "D":
x = x+1
position = mapChoice[y][x]
playerMap[y][x] = "S"

if movement == "A":
x = x-1
position = mapChoice[y][x]
playerMap[y][x] = "S"

position = mapChoice[y][x]
playerMap[y][x] = "S"

if position == "0" or position == "1":
print("You hit a wall, you stumble in the darkness back to your previous position...")
playerMap[y][x] = "0"
x = previousX
y = previousY
playerMap[y][x] = "S"


真的吗?

def displayMapAround(maps,x,y):
for dy in (-1,0,1):
print( maps[y+dy][x-1:x+2] )

编辑以显示更大的区域
使用更大区域的棘手之处在于,您必须检查是否经过了边缘。那里只有一个填充元素。你通常只想要奇数尺寸,否则焦点不在中心。

def displayMapAround(maps,size,x,y):
assert( size & 1 ) # is size odd?
x0 = max( 0, x-size//2 )
x1 = min( x+size//2, len(maps[0]))
y0 = max( 0, y-size//2 )
y1 = min( y+size//2, len(maps))
for dy in range(y0,y1+1):
print( maps[dy][x0:x1+1] )

最新更新