2 回答

TA贡献1864条经验 获得超2个赞
我将从你提出的第二个问题开始。下面是使用循环重建地图绘制函数。
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_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_position听起来不错。我们也想好一个大邪魔的位置,想出一些适合两人的角色。现在是渲染魔法...因为地图的每个单元格只能渲染一个东西——地砖或英雄或邪恶的东西,我们可以将它们的坐标作为字典传递,(如果你有character_positions一个list字符,很容易形成这样的字典)。
神奇之处在于character_positions.get()第二个参数;基本上,我们看看我们正在绘制的 x/y 坐标是否存在于坐标字典中,然后使用该字符代替。
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_position相应的修改。
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

TA贡献1854条经验 获得超8个赞
我认为您的意思是存储coordinates英雄的位置,而不是该位置的内容:
def move(world_map,map_grid):
hero_position = (0, 0)
move_direction = input("Press WASD to move: ")
if move_direction == "d":
hero_position = hero_position[0], hero_position[1] + 1
print(hero_position)
添加回答
举报