首页 > 解决方案 > 在 ascii 地牢地图中添加相机以跟随“玩家”移动

问题描述

目前在我的游戏中,它每回合都会显示整个地图,但我希望它只显示玩家的位置,并且可能是玩家周围的 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"
        


标签: pythoncameraasciigame-engine

解决方案


真的吗?

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

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

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] )

推荐阅读