您如何称呼列表内的字符串位置?

时间:2018-12-20 22:56:27

标签: python list

我一直在从事迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。

我试图将其放在列表内和列表外。

maze = ["o","*","*","*","*","*",".",#row one is over
        ".",".",".",".",".","*","*",#Row two is over
        "*","*","*","*",".","*",".",#Row three is over
        ".",".",".","*",".","*",".",#Row four is over
        ".",".",".","*",".","*",".",#Row five is over
        ".",".",".","*",".","*",".",#Row Six is over
        ".",".",".","*",".","@"]

mazeOne = maze[0:6]
mazeTwo = maze[7:13]
mazeThree = maze[14:20]
mazeFour = maze[21:27]
mazeFive = maze[28:34]
mazeSix = maze[35:41]
mazeSeven = maze[42:48]
playerAre = maze[47]

for a,b,c,d,e,f,g in zip (mazeOne, mazeTwo, mazeThree, mazeFour, 
mazeFive, mazeSix, mazeSeven):
    print (a,b,c,d,e,f,g)

keypress = str(input("""W, A, S, D: """))
if keypress == 'w' or keypress == 'W':
    playerAre = '.'
    playerAre = playerAre + 7

1 个答案:

答案 0 :(得分:0)

您在这里有两个直接的问题。

  • 您已经将maze(用于显示迷宫的*。@字符)的内容 index (整数0-48)混淆了)。
  • 您似乎正在尝试通过在该位置添加 7来向上移动播放器。请注意,这将为您提供位于迷宫外部的54位置。我想你想减去。

尝试以下快速修复方法:

if keypress.lower() == 'w':
    maze[playerAre] = '.'
    playerAre = playerAre - 7

我强烈建议您在线查看几个许多迷宫示例。从迷宫布局的2D组织开始,看看他们如何更“自然”地处理移动。