2016-06-14 16 views
1

Er sagt, dieseWie kann ich mein Monster bewegen?

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

Wenn das Spiel beginnt, bleibt das Monster in ihm Position, aber ich bin mir nicht sicher, warum.

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 

und es sieht aus wie diese

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

Sie könnten den inkonsistenten Abstand nach der Linie 93, die Sie –

+0

markiert reparieren wollen „wobei M Spieler und X Monster“ 'wenn Zelle == Spieler: print (tile.format („X“), Ende = ''); elif cell == monster: drucken (tile.format ("M"), end = '') 'verwirrst du dich selbst darüber, was" X "ist und" M "ist? –

+0

Sorry, ich meinte den umgekehrten Weg –

Antwort

2

Es gibt keinen Grund steps-move_monster nebenbei, wie Sie es in der dritten Zeile zu überschreiben, und nie den ursprünglichen Wert verwenden. legen Sie es einfach aus der Definition der Funktion:

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 

Und aufhören zu versuchen, es zu passieren, wenn Sie die Funktion aufrufen:

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

Während das stimmt, glaube ich nicht, dass dies eine Antwort auf die Frage ist. –

+0

Danke und das ist wahr, aber auch nach diesen Änderungen bewegt sich das Monster nicht. –

+0

Sie haben recht, ich habe die Frage falsch verstanden, dass das Monster sich nicht bewegt, irgendwie habe ich den Fehler übersehen, der an der Spitze steht. –

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

Wie man hier sehen kann, das Monster Position zu neuen Variablen zuweisen und das Ändern der neuen Variablen ändert nicht die Position des ursprünglichen Monsters.

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 

und: Nach Aufruf der Funktion Ändern Schritte als Parameter nicht nehmen (wie in Mureinik Antwort erklärt), würden Sie die Monster, wie Sie mit dem Spieler tat, indem Sie den Code der folgenden bewegen müssen

monster = move_monster(monster) 
+2

Ja, das löst das Problem, das das OP jetzt hat, nachdem es das Problem gelöst hat, wobei "Schritte" nicht definiert sind. –

+0

Yup, irgendwie habe ich den Fehler an der Spitze verpasst. Ich bearbeite meine Antwort, um diesen Fix hinzuzufügen. –

+0

Dies ist die richtige Antwort –