亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用列表索引在地圖上移動一個字符

使用列表索引在地圖上移動一個字符

陪伴而非守候 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 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號