2016-06-14 10 views
1

これは、ゲームが起動すると、その中にモンスターの滞在は、位置だが、私がなぜわからないんだけど、このモンスターの移動方法を教えてください。

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

を言います。

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 

、それはそこには、3行目に、それを上書きするようmove_monsterstepsを渡すにはポイントは、いない、と元の値を使用することはありません。この

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

あなたは「Mはプレイヤーであり、Xはモンスターである」 'セルの場合==プレーヤーあなたが –

+0

をマークされた行93の後に一貫性のない間隔を修正する場合があります、印刷(tile.format(「X」) end = ''); elif cell ==モンスター:print(tile.format( "M")、end = '') 'あなたは' X 'と 'M'が何であるか混乱していますか? –

+0

申し訳ありませんが、私は他の方法を意味する –

答えて

2

のように見えます。新しい変数にモンスターの位置を割り当て、あなたがここに見ることができるように

​​
+1

これは本当ですが、私はこれが質問への答えだとは思わない。 –

+0

ありがとう、それは本当です、しかし、これらの変更の後でも、モンスターは動かない。 –

+0

あなたはそうです、私はモンスターが動かないという質問を誤解しました。どういうわけか、上に載っているエラーを逃しました。 –

3
>>> monster = (2,3) 
>>> x,y=monster 
>>> x+=1 
>>> x, y 
(3, 3) 
>>> monster 
(2, 3) 

:ちょうどその関数の定義から、それをドロップします。

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 

そして、あなたは関数を呼び出すときにそれを渡そうと停止新しい変数を変更しても元のモンスターの位置は変わりません。 (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 

monster = move_monster(monster) 
+2

はいこれは、OPが定義されていない 'steps'を使って問題を解決したので、OPが_about to have_する問題を解決します。 –

+0

うん、どういうわけか、私は上のエラーを逃した。私はその修正を含むように私の答えを編集します。 –

+0

これは正解です –

関連する問題