Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我如何让我的怪物移动?_Python_Nameerror - Fatal编程技术网

Python 我如何让我的怪物移动?

Python 我如何让我的怪物移动?,python,nameerror,Python,Nameerror,上面写着 Traceback (most recent call last): File "dungeon2.py", line 93, in <module> move_monster(monster, steps) NameError: name 'steps' is not defined 看起来是这样的 |X|_|_|

上面写着

Traceback (most recent call last):
File "dungeon2.py", line 93, in <module>
move_monster(monster, steps)
NameError: name 'steps' is not defined
看起来是这样的

|X|_|_|                                                                                                                     
|_|_|_|                                                                                                                     
|_|_|M|  where M is player and X is monster

移动怪物的步骤没有意义,因为您在第三行中覆盖了它,并且从不使用原始值。只需将其从函数的定义中删除:

def move_monster(monster):
  # here ---------------^
  x, y = monster
  moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
  steps = random.choice(moves) # steps is now a local variable
  if steps == 'LEFT':
    y -= 1
  elif steps == 'RIGHT':
    y += 1
  elif steps == 'UP':
    x -= 1
  elif steps == 'DOWN':
    x +=1  
并在调用函数时停止尝试传递它:

move_monster(monster)
# here -------------^

移动怪物的步骤没有意义,因为您在第三行中覆盖了它,并且从不使用原始值。只需将其从函数的定义中删除:

def move_monster(monster):
  # here ---------------^
  x, y = monster
  moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
  steps = random.choice(moves) # steps is now a local variable
  if steps == 'LEFT':
    y -= 1
  elif steps == 'RIGHT':
    y += 1
  elif steps == 'UP':
    x -= 1
  elif steps == 'DOWN':
    x +=1  
并在调用函数时停止尝试传递它:

move_monster(monster)
# here -------------^
如您所见,将怪物的位置分配给新变量并更改新变量不会更改原始怪物的位置。如Mureinik的回答中所述,将函数更改为“不采取步骤”作为参数后,您必须像对待玩家一样移动怪物,将代码更改为以下内容:

def move_monster(monster):
  x, y = monster
  moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
  steps = random.choice(moves)
  if steps == 'LEFT':
    y -= 1
  elif steps == 'RIGHT':
    y += 1
  elif steps == 'UP':
    x -= 1
  elif steps == 'DOWN':
    x +=1
  return x,y

如您所见,将怪物的位置分配给新变量并更改新变量不会更改原始怪物的位置。如Mureinik的回答中所述,将函数更改为“不采取步骤”作为参数后,您必须像对待玩家一样移动怪物,将代码更改为以下内容:

def move_monster(monster):
  x, y = monster
  moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
  steps = random.choice(moves)
  if steps == 'LEFT':
    y -= 1
  elif steps == 'RIGHT':
    y += 1
  elif steps == 'UP':
    x -= 1
  elif steps == 'DOWN':
    x +=1
  return x,y


如果cell==player:printtile.formatX,end=,您可能需要修复标记的第93行之后不一致的间距,其中M是player,X是monster;elif cell==monster:printtile.formatM,end=您是否混淆了X是什么和M是什么?对不起,我的意思是另一种方式,如果cell==player:printtile.formatX,end=,您可能需要修复标记的第93行后不一致的间距,其中M是player,X是monster;elif cell==monster:printtile.formatM,end=你是否混淆了X和M是什么?对不起,我的意思是另一种方式虽然这是真的,但我认为这不是问题的答案。谢谢你,这是真的,但是即使在这些更改之后,怪物也不会移动。你是对的,我误读这个问题是因为怪物不动,不知何故我错过了顶部贴出的错误。虽然这是真的,但我不认为这是问题的答案。谢谢你,这是真的,但是即使在这些改变之后怪物也不动。你是对的,我误读这个问题是因为怪物不动,不知何故,我错过了顶部张贴的错误。是的,这解决了OP即将遇到的问题,因为他们解决了问题,但没有定义步骤。是的,不知何故,我错过了顶部的错误。我将编辑我的答案以包含该修复。是的,这解决了OP即将遇到的问题,因为他们用未定义的步骤解决了问题。是的,不知何故,我错过了顶部的错误。我将编辑我的答案以包含该修复。