跨多维数组、基于文本的游戏的"移动"问题



我现在正在弄乱代码以解决运动中的一些问题。理想情况下,我希望能够将角色移动到数组(worldMap(中包含"的任何位置,并用"P"标记玩家的位置。

我对我遇到的问题是什么感到迷茫,我遇到的一些问题如下...... - 当我移动E时,它总是转到世界地图[3][3] - N 只从世界地图[3][x] 向上移动到世界地图 [2][x] - S 不起作用

下面是我当前的代码...


import math
import random
import sys
import os
import time

gameplay= True
#world map lay out, X--> wall, player can't go that way. ' ', possible area for player to move, 'P', player indicator.
worldMap = [['X','X','X','X','X'],
['X',' ',' ',' ','X'],
['X',' ',' ',' ','X'],
['X','P',' ',' ','X'],
['X','X','X','X','X']]
west=str("w" or "W")
east=str("e" or "E")
north=str('n' or 'N')
south=str('s' or 'S')
for row in worldMap:
for column in row:
print(column, end=' ')
print()

while gameplay == True:
x=str((input("nWhat direction would you like to move?n ")))
if x==west:
for row in worldMap:
for i, column in enumerate(row):
if column == 'P':
if((i>0) and (i<4) and row[i-1]) == ' ':
row[i-1] = 'P'
row[i]=' '
else:
print("nCan't do that.n")
for row in worldMap:
for column in row:
print(column, end=' ')
print()
elif x==east:
for row in worldMap:
for i, column in enumerate(row):
if column == 'P':
if((i>0) and (i<4) and row[i+1]) == ' ':
row[i+1] = 'P'
row[i]=' '
else:
print("nCan't do that.n")
for row in worldMap:
for column in row:
print(column, end=' ')
print()
elif x == north: #move north
for column in worldMap:
for i, row in enumerate(column):
if row == 'P':
if((i>0) and (i<4) and column[i-1]) == ' ':
column[i-1] = 'P'
column[i]=' '
else:
print("nCan't do that.n")
for row in worldMap:
for column in row:
print(column, end=' ')
print()
elif x== south: #move south
for column in worldMap:
for i, row in enumerate(column):
if column == 'P':
if((i>0) and (i<4) and column[i+1]) == ' ':
column[i+1] = 'P'
column[i]=' '
else:
print("nCan't do that.n")
for row in worldMap:
for column in row:
print(column, end=' ')
print()
else:
for row in worldMap:
for column in row:
print(column, end=' ')
print()
print("nCan't do that.n")

我已经对你的代码进行了一些整理。需要重构代码的一个很好的线索是,如果你在代码中写了很多同样的东西。在这些情况下,最好编写一个函数,然后从代码中调用该函数。下面的代码只是一个示例,可帮助您走上正确的道路。

在这个例子中,我不是扫描整个地图的玩家,而是跟踪玩家的位置并计算新位置,如果它是一个空格,那么移动玩家。我们可以通过在玩家的 x 或 y 坐标上加或减 1 来轻松移动玩家。

我还清理了打印映射函数,现在仅在每个循环开始时调用它。

from random import randint
def generate_map(size):
# world map lay out, X--> wall, player can't go that way. ' ', possible area for player to move, 'P', player indicator.
world_map = [[wall for _ in range(size)]]
for i in range(1, size - 1):
world_map.append([wall])
for j in range(1, size - 1):
#generate random walls in our map (currently set to genearete spaces 90% of the time
if randint(1,100) < 90:
world_map[i].append(space)
else:
world_map[i].append(wall)
world_map[i].append(wall)
world_map.append([wall for _ in range(size)])
return world_map

def print_map():
for row in worldMap:
print(" ".join(row))

def move_player():
x, y = player_position
if direction == "W":
y -= 1
elif direction == "E":
y += 1
elif direction == "N":
x -= 1
elif direction == "S":
x += 1
if worldMap[x][y] == space:
worldMap[player_position[0]][player_position[1]] = space
worldMap[x][y] = player
player_position[0] = x
player_position[1] = y
else:
print("Cannot more there")
#Genearte a map and position the player
wall, space, player = "X", " ", "P"
map_size = 10
worldMap = generate_map(map_size)
player_position = [1, 1]
worldMap[player_position[0]][player_position[1]] = player
#Run the game
gameplay = True
while gameplay:
print_map()
direction = input("nWhat direction would you like to move? ").upper()
if direction:
move_player()
else:
break

最新更新