Python 使用列表索引在地图上移动字符
对于游戏中我需要制作的功能,需要有一个网格形式的地图。它是一个嵌套列表,其位置称为Python 使用列表索引在地图上移动字符,python,Python,对于游戏中我需要制作的功能,需要有一个网格形式的地图。它是一个嵌套列表,其位置称为\u map。为了以网格形式显示地图,我使用了map\u grid函数 其次,游戏应该允许用户通过提示用户输入w、A、S和D键(分别为上、下、左、右)在一个单元格之间移动角色。字符从左上角的\u map[0][0]开始,例如,如果输入为S,则字符的位置将更改为\u map[1][0],并根据该位置在网格中表示。问题是我不确定如何增加内部和外部列表中的值 此外,如果有人能建议一种更好的方式来显示地图(可能是循环?),
\u map
。为了以网格形式显示地图,我使用了map\u grid
函数
其次,游戏应该允许用户通过提示用户输入w
、A
、S
和D
键(分别为上、下、左、右)在一个单元格之间移动角色。字符从左上角的\u map[0][0]
开始,例如,如果输入为S
,则字符的位置将更改为\u map[1][0]
,并根据该位置在网格中表示。问题是我不确定如何增加内部和外部列表中的值
此外,如果有人能建议一种更好的方式来显示地图(可能是循环?),而不是手动打印所有内容,我将不胜感激,因为我知道我编写的代码冗长且难以阅读
\u映射=[
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\
[“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”\
[“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”,“”\
['',T','','','','','','','','','',''\
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\
['','','','','T','','',''\
['','','','','','','',K']\
]
def映射网格(_映射):
打印(“+--+--+--+--+--+--+--+--+--+--+-+\n{}{}{}{}}{}}{}}}{}}{}}}}{}}}{}}}{}}}}
。格式(_-map[0][0]、_-map[0][1]、_-map[0][2]、_-map[0][3]、_-map[0][4]、_-map[0][5]、_-map[0][6]、_-map[0][7]))
打印(“+--+--+--+--+--+--+--+--+--+--+--+--+\n+--+--+--+--+--+--+--+--+--+--+--
。格式(_-map[1][0]、_-map[1][1]、_-map[1][2]、_-map[1][3]、_-map[1][4]、_-map[1][5]、_-map[1][6]、_-map[1][7]))
打印(“{}}{}{}{}{}{}{}{}{}{}}{}}{}}}\n+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--
。格式(_-map[2][0]、_-map[2][1]、_-map[2][2]、_-map[2][3]、_-map[2][4]、_-map[2][5]、_-map[2][6]、_-map[2][7]))
打印(“{}}{}{}{}{}{}{}{}{}{}}{}}{}}}\n+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--
。格式(_-map[3][0]、_-map[3][1]、_-map[3][2]、_-map[3][3]、_-map[3][4]、_-map[3][5]、_-map[3][6]、_-map[3][7]))
打印(“{}}{}{}{}{}{}{}{}{}{}}{}}{}}}\n+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--
。格式(_-map[4][0]、_-map[4][1]、_-map[4][2]、_-map[4][3]、_-map[4][4]、_-map[4][5]、_-map[4][6]、_-map[4][7]))
打印(“{}}{}{}{}{}{}{}{}{}{}}{}}{}}}\n+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--
。格式(_-map[5][0]、_-map[5][1]、_-map[5][2]、_-map[5][3]、_-map[5][4]、_-map[5][5]、_-map[5][6]、_-map[5][7]))
打印(“{}}{}{}{}{}{}{}{}{}{}}{}}{}}}\n+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--
。格式(_-map[6][0]、_-map[6][1]、_-map[6][2]、_-map[6][3]、_-map[6][4]、_-map[6][5]、_-map[6][6]、_-map[6][7]))
打印(“{}}{}{}{}{}{}{}{}{}{}}{}}{}}}\n+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--
。格式(_-map[7][0]、_-map[7][1]、_-map[7][2]、_-map[7][3]、_-map[7][4]、_-map[7][5]、_-map[7][6]、_-map[7][7]))
def移动(_映射,映射网格):
英雄位置=_地图[0][0]
移动方向=输入(“按WASD移动:”)
如果移动方向=“d”:
英雄位置=_地图[1][+1]
打印(英雄位置)
我想你是想存储英雄所在位置的坐标,而不是该位置的坐标:
def移动(世界地图、地图网格):
英雄位置=(0,0)
移动方向=输入(“按WASD移动:”)
如果移动方向=“d”:
英雄位置=英雄位置[0],英雄位置[1]+1
打印(英雄位置)
我将从你的第二个问题开始。下面是使用循环重建地图绘制功能
world_map = [
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', 'T', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', 'T', ' ', ' '],
[' ', 'T', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', 'T', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', 'K'],
]
def draw_map(world_map):
width = len(world_map[0])
header = "---".join(["+"] * (width + 1)) # Compute header line.
for row in world_map:
print(header) # Print header leading each line.
print("| {} |".format(" | ".join(row))) # Format and print the row.
print(header) # Print final header (well, footer).
draw_map(world_map)
好了,酷豆,那玩家角色呢
一般来说,像这样的游戏是以这样的方式构造的:移动的实体(如角色、敌人等)是独立的实体,而静态地图存储在像你这样的数组中
首先,我们需要修改draw\u map
函数,以便在渲染地图时跟踪每个X/Y坐标:
def draw_map(world_map):
width = len(world_map[0])
header = "---".join(["+"] * (width + 1)) # Compute header line.
for y, row in enumerate(world_map):
print(header) # Print header leading each line.
# Figure out which characters to print in each cell.
chars = []
for x, c in enumerate(row):
chars.append(str(c))
print("| {} |".format(" | ".join(chars)))
print(header) # Print final header (well, footer).
(输出仍然相同。)
现在让我们将英雄位置存储在一个变量中,hero\u position
听起来足够好了。让我们也找出一个大邪恶怪物的位置,并为这两个怪物想出一些合适的角色。现在是渲染魔法。。。由于地图的每个单元格只能呈现一个对象–地面平铺、英雄或邪恶对象,我们可以将它们的坐标作为dict传递,character\u positions
(如果有字符列表,很容易形成这样的dict)
神奇之处在于字符的位置。get()
这里有第二个参数;基本上,我们看到我们正在绘制的x/y坐标是否存在于坐标dict中,并使用该字符
def draw_map(world_map, character_positions):
width = len(world_map[0])
header = "---".join(["+"] * (width + 1)) # Compute header line.
for y, row in enumerate(world_map):
print(header) # Print header leading each line.
# Figure out which characters to print in each cell.
chars = []
for x, c in enumerate(row):
chars.append(str(character_positions.get((x, y), c)))
print("| {} |".format(" | ".join(chars)))
print(header) # Print final header (well, footer).
hero_position = (1, 1) # 0, 0 would be boring.
evil_position = (5, 6)
hero_character = '@'
evil_character = '%'
draw_map(world_map, character_positions={
hero_position: hero_character,
evil_position: evil_character,
})
现在的结果是
+---+---+---+---+---+---+---+---+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
| | @ | | T | | | | |
+---+---+---+---+---+---+---+---+
| | | | | | T | | |
+---+---+---+---+---+---+---+---+
| | T | | | | | | |
+---+---+---+---+---+---+---+---+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
| | | | | T | % | | |
+---+---+---+---+---+---+---+---+
| | | | | | | | K |
+---+---+---+---+---+---+---+---+
如您所见,@
和%
出现在那里
现在,互动性–对于这里的简单案例,让我们在循环中使用input()
,询问用户要做什么,并相应地修改hero\u位置
while True:
draw_map(world_map, character_positions={
hero_position: hero_character,
evil_position: evil_character,
})
command = input("WASDQ?").lower()
if command == "w" and hero_position[1] > 0:
hero_position = (hero_position[0], hero_position[1] - 1)
if command == "a" and hero_position[0] > 0:
hero_position = (hero_position[0] - 1, hero_position[1])
if command == "s" and hero_position[1] < len(world_map) - 1:
hero_position = (hero_position[0], hero_position[1] + 1)
if command == "d" and hero_position[0] < len(world_map[0]) - 1:
hero_position = (hero_position[0] + 1, hero_position[1])
if command == "q":
break
if hero_position == evil_position:
print("You were eaten!")
break
为True时:
绘制地图(世界地图、角色位置)={
英雄位置:英雄人物,
邪恶的位置:邪恶的角色,
})
命令=输入(“WASDQ?”).lower()
如果命令==“w”且英雄位置[1]>0:
英雄位置=(英雄位置[0],英雄位置[1]-1)
如果命令==“a”且英雄位置[0]>0:
英雄位置=(英雄位置[0]-1,英雄位置[1])
如果命令==“s”和英雄位置[1]