简体   繁体   English

如何使怪物移动?

[英]How do I make my monster move?

It says this 这样说

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

When the game starts, the monster stays in it's position but I'm not sure why. 游戏开始时,怪物停留在原处,但我不确定为什么。

import random

BOARD = [(0, 0), (0, 1), (0, 2),
         (1, 0), (1, 1), (1, 2),
         (2, 0), (2, 1), (2, 2)]

def get_locations():
  monster = random.choice(BOARD)
  start = random.choice(BOARD)
  door = random.choice(BOARD)

  #If player, door or monster are random, do it all over again.
  if monster == door or monster == start or door == start:
    return get_locations()

  return monster, door, start

def move_player(player, move):
  x, y = player
  if move == 'LEFT':
    y -= 1
  elif move == 'RIGHT':
    y += 1
  elif move == 'UP':
    x -= 1
  elif move == 'DOWN':
    x +=1
  else:
    print("That move doesn't exist PLEASE.")
  return x, y

def move_monster(monster, steps):
  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  


def get_moves(player):
  moves = ['LEFT', 'RIGHT', 'UP', 'DOWN']
  if player[1] == 0:
    moves.remove('LEFT')
  if player[1] == 2:
    moves.remove('RIGHT')
  if player[0] == 0:
    moves.remove('UP')
  if player[0] == 2:
    moves.remove('DOWN')
  return moves


def draw_map(player, monster):
  print('_ _ _')
  tile = '|{}'  
  for idx, cell in enumerate(BOARD):
    if idx in [0, 1, 3, 4, 6, 7]:
      if cell == player:
        print(tile.format("X"), end ='') 
      elif cell == monster:
        print(tile.format("M"), end = '')
      else:
        print(tile.format("_"), end ='')
    else:
      if cell == player:
        print(tile.format("X|"))
      elif cell == monster:
        print(tile.format("M|"))
      else:
        print(tile.format("_|"))    


monster, player, door = get_locations()

print("Welcome to the dungeon! *evil laugh*")

while True:
  moves = get_moves(player)

  draw_map(player, monster)              
  print("You are currently in room {}".format(player)) #fill in with player position
  print("You can move {}".format(moves)) # fill in with available positions
  print("Enter 'GIVEUP' to quit")

  move = input("> ")
  move = move.upper()
  move_monster(monster, steps)                                       LINE 93

  if move == "GIVEUP":
      print("Giving up, you wait sadly for the Beast to find you. It does, and makes a tasty meal of you...")
      print("You lose.")
      break

  if move in moves:
        player = move_player(player, move)
  else:
        print("Walls are hard, stop walking into them!")
        continue
  if player == door:
    print("You narrowly escaped the beast and escaped")
    print("You win!")
    break
  elif player == monster:
    print("The beast found you!")
    print("You lose!")
    print("Game over")
    break
  # If it's a good move, change the player's position
  # If it's a bad move, don't change anything
  # If the new position is the door, they win!
  # If the new positon is the Beast's, they lose!      
  # Otherwise, continue

and it looks like this 它看起来像这样

|X|_|_|                                                                                                                     
|_|_|_|                                                                                                                     
|_|_|M|  where M is player and X is monster
>>> monster = (2,3)
>>> x,y=monster
>>> x+=1
>>> x, y
(3, 3)
>>> monster
(2, 3)

As you can see here, assigning the monster's position to new variables and changing the new variables does not change the original monster's position. 如您在此处看到的,将怪物的位置分配给新变量并更改新变量不会更改原始怪物的位置。 After changing the function to not take steps as a parameter (as explained in Mureinik's answer), you would have to move the monster the way you did with the player, by changing your code to the following: 更改功能以不采取步骤作为参数后(如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

and

monster = move_monster(monster)

There's no point in passing steps to move_monster , as you overwrite it in the third line, and never use the original value. 没有必要传递steps move_monster ,因为您在第三行覆盖了它,并且从不使用原始值。 Just drop it from the function's definition: 只需将其从函数的定义中删除:

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  

And stop trying to pass it when you call the function: 并在调用函数时停止尝试传递它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM