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)
添加回答
舉報