为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

陪伴而非守候 2023-05-09 10:37:57
对于我需要制作的游戏功能,需要有一张网格形式的地图。它是一个嵌套列表,其位置称为_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]))
查看完整描述

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


查看完整回答
反对 回复 2023-05-09
?
哔哔one

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)


查看完整回答
反对 回复 2023-05-09
  • 2 回答
  • 0 关注
  • 143 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信