使用列表索引在地图上移动一个字符

对于我需要制作的游戏功能,需要有一张网格形式的地图。它是一个嵌套列表,其位置称为_map。为了以网格形式显示地图,我使用了函数map_grid。


其次,游戏应允许用户通过提示用户输入w、和键(分别为上、下、左、右)来将角色从一个单元格移动到A另一个单元格。字符从, 在左上角开始,例如,如果输入是,则字符的位置将更改为并根据该位置显示在网格中。问题是我不确定如何增加内部和外部列表中的值。SD_map[0][0]S_map[1][0]


此外,如果有人可以建议一种更好的方式来显示地图(也许可能带有循环?)而不是手动打印所有内容,我们将不胜感激,因为我知道我编写的代码冗长且难以阅读。


_map = [

    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\

    [' ', ' ', ' ', 'T', ' ', ' ', ' ', ' '],\

    [' ', ' ', ' ', ' ', ' ', 'T', ' ', ' '],\

    [' ', 'T', ' ', ' ', ' ', ' ', ' ', ' '],\

    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\

    [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],\

    [' ', ' ', ' ', ' ', 'T', ' ', ' ', ' '],\

    [' ', ' ', ' ', ' ', ' ', ' ', ' ', 'K']\

]


def map_grid(_map):

    print("+---+---+---+---+---+---+---+---+\n| {} | {} | {} | {} | {} | {} | {} | {} |"

          .format(_map[0][0],_map[0][1],_map[0][2],_map[0][3],_map[0][4],_map[0][5],_map[0][6],_map[0][7]))

    print("+---+---+---+---+---+---+---+---+\n| {} | {} | {} | {} | {} | {} | {} | {} |\n+---+---+---+---+---+---+---+---+"

          .format(_map[1][0],_map[1][1],_map[1][2],_map[1][3],_map[1][4],_map[1][5],_map[1][6],_map[1][7]))

    print("| {} | {} | {} | {} | {} | {} | {} | {} |\n+---+---+---+---+---+---+---+---+"

          .format(_map[2][0],_map[2][1],_map[2][2],_map[2][3],_map[2][4],_map[2][5],_map[2][6],_map[2][7]))

    print("| {} | {} | {} | {} | {} | {} | {} | {} |\n+---+---+---+---+---+---+---+---+"

          .format(_map[3][0],_map[3][1],_map[3][2],_map[3][3],_map[3][4],_map[3][5],_map[3][6],_map[3][7]))

    print("| {} | {} | {} | {} | {} | {} | {} | {} |\n+---+---+---+---+---+---+---+---+"

          .format(_map[4][0],_map[4][1],_map[4][2],_map[4][3],_map[4][4],_map[4][5],_map[4][6],_map[4][7]))



陪伴而非守候
浏览 144回答 2
2回答

慕斯王

我将从你提出的第二个问题开始。下面是使用循环重建地图绘制函数。world_map = [&nbsp; &nbsp; [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],&nbsp; &nbsp; [' ', ' ', ' ', 'T', ' ', ' ', ' ', ' '],&nbsp; &nbsp; [' ', ' ', ' ', ' ', ' ', 'T', ' ', ' '],&nbsp; &nbsp; [' ', 'T', ' ', ' ', ' ', ' ', ' ', ' '],&nbsp; &nbsp; [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],&nbsp; &nbsp; [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],&nbsp; &nbsp; [' ', ' ', ' ', ' ', 'T', ' ', ' ', ' '],&nbsp; &nbsp; [' ', ' ', ' ', ' ', ' ', ' ', ' ', 'K'],]def draw_map(world_map):&nbsp; &nbsp; width = len(world_map[0])&nbsp; &nbsp; header = "---".join(["+"] * (width + 1))&nbsp; # Compute header line.&nbsp; &nbsp; for row in world_map:&nbsp; &nbsp; &nbsp; &nbsp; print(header)&nbsp; # Print header leading each line.&nbsp; &nbsp; &nbsp; &nbsp; print("| {} |".format(" | ".join(row)))&nbsp; # Format and print the row.&nbsp; &nbsp; print(header)&nbsp; # Print final header (well, footer).draw_map(world_map)好的,酷豆,现在玩家角色呢?一般来说,像这样的游戏的结构方式是您的移动实体(例如角色、敌人等)是独立的实体,而静态地图存储在像您这样的数组中。首先,我们需要修改函数draw_map,以便在渲染地图时跟踪每个 X/Y 坐标:def draw_map(world_map):&nbsp; &nbsp; width = len(world_map[0])&nbsp; &nbsp; header = "---".join(["+"] * (width + 1))&nbsp; # Compute header line.&nbsp; &nbsp; for y, row in enumerate(world_map):&nbsp; &nbsp; &nbsp; &nbsp; print(header)&nbsp; # Print header leading each line.&nbsp; &nbsp; &nbsp; &nbsp; # Figure out which characters to print in each cell.&nbsp; &nbsp; &nbsp; &nbsp; chars = []&nbsp; &nbsp; &nbsp; &nbsp; for x, c in enumerate(row):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chars.append(str(c))&nbsp; &nbsp; &nbsp; &nbsp; print("| {} |".format(" | ".join(chars)))&nbsp; &nbsp; print(header)&nbsp; # Print final header (well, footer).(输出仍然相同。)现在让我们将英雄位置存储在一个变量中,hero_position听起来不错。我们也想好一个大邪魔的位置,想出一些适合两人的角色。现在是渲染魔法...因为地图的每个单元格只能渲染一个东西——地砖或英雄或邪恶的东西,我们可以将它们的坐标作为字典传递,(如果你有character_positions一个list字符,很容易形成这样的字典)。神奇之处在于character_positions.get()第二个参数;基本上,我们看看我们正在绘制的 x/y 坐标是否存在于坐标字典中,然后使用该字符代替。def draw_map(world_map, character_positions):&nbsp; &nbsp; width = len(world_map[0])&nbsp; &nbsp; header = "---".join(["+"] * (width + 1))&nbsp; # Compute header line.&nbsp; &nbsp; for y, row in enumerate(world_map):&nbsp; &nbsp; &nbsp; &nbsp; print(header)&nbsp; # Print header leading each line.&nbsp; &nbsp; &nbsp; &nbsp; # Figure out which characters to print in each cell.&nbsp; &nbsp; &nbsp; &nbsp; chars = []&nbsp; &nbsp; &nbsp; &nbsp; for x, c in enumerate(row):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chars.append(str(character_positions.get((x, y), c)))&nbsp; &nbsp; &nbsp; &nbsp; print("| {} |".format(" | ".join(chars)))&nbsp; &nbsp; print(header)&nbsp; # Print final header (well, footer).hero_position = (1, 1)&nbsp; # 0, 0 would be boring.evil_position = (5, 6)hero_character = '@'evil_character = '%'draw_map(world_map, character_positions={&nbsp; &nbsp; hero_position: hero_character,&nbsp; &nbsp; evil_position: evil_character,})现在的结果是+---+---+---+---+---+---+---+---+|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|+---+---+---+---+---+---+---+---+|&nbsp; &nbsp;| @ |&nbsp; &nbsp;| T |&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|+---+---+---+---+---+---+---+---+|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;| T |&nbsp; &nbsp;|&nbsp; &nbsp;|+---+---+---+---+---+---+---+---+|&nbsp; &nbsp;| T |&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|+---+---+---+---+---+---+---+---+|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|+---+---+---+---+---+---+---+---+|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|+---+---+---+---+---+---+---+---+|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;| T | % |&nbsp; &nbsp;|&nbsp; &nbsp;|+---+---+---+---+---+---+---+---+|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;| K |+---+---+---+---+---+---+---+---+如您所见,@和%出现在那里!现在,交互性——对于这里的简单情况,让我们input()在循环中使用来询问用户要做什么并进行hero_position相应的修改。while True:&nbsp; &nbsp; draw_map(world_map, character_positions={&nbsp; &nbsp; &nbsp; &nbsp; hero_position: hero_character,&nbsp; &nbsp; &nbsp; &nbsp; evil_position: evil_character,&nbsp; &nbsp; })&nbsp; &nbsp; command = input("WASDQ?").lower()&nbsp; &nbsp; if command == "w" and hero_position[1] > 0:&nbsp; &nbsp; &nbsp; &nbsp; hero_position = (hero_position[0], hero_position[1] - 1)&nbsp; &nbsp; if command == "a" and hero_position[0] > 0:&nbsp; &nbsp; &nbsp; &nbsp; hero_position = (hero_position[0] - 1, hero_position[1])&nbsp; &nbsp; if command == "s" and hero_position[1] < len(world_map) - 1:&nbsp; &nbsp; &nbsp; &nbsp; hero_position = (hero_position[0], hero_position[1] + 1)&nbsp; &nbsp; if command == "d" and hero_position[0] < len(world_map[0]) - 1:&nbsp; &nbsp; &nbsp; &nbsp; hero_position = (hero_position[0] + 1, hero_position[1])&nbsp; &nbsp; if command == "q":&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; if hero_position == evil_position:&nbsp; &nbsp; &nbsp; &nbsp; print("You were eaten!")&nbsp; &nbsp; &nbsp; &nbsp; break

哔哔one

我认为您的意思是存储coordinates英雄的位置,而不是该位置的内容:def move(world_map,map_grid):&nbsp; &nbsp; hero_position = (0, 0)&nbsp; &nbsp; move_direction = input("Press WASD to move: ")&nbsp; &nbsp; if move_direction == "d":&nbsp; &nbsp; &nbsp; &nbsp; hero_position = hero_position[0], hero_position[1] + 1&nbsp; &nbsp; &nbsp; &nbsp; print(hero_position)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python