简体   繁体   English

有没有办法阻止用户“走出”我的“地图”?

[英]Is there a way to stop the user from going 'outside' of my 'map'?

'd4': {
        ZONENAME : 'Kingdom Church',
        DESCRIPTION : 'description',
        EXAMINATION : 'examine',
        SOLVED : False,
        UP : 'c4',
        DOWN : '',
        LEFT : 'd3',
        RIGHT : ''
}

This dictionary (part of a nested dictionary) representing the 'map'.此字典(嵌套字典的一部分)表示“地图”。 D4 is the last tile down and the last tile right. D4 是向下的最后一格和右侧的最后一格。 It seems I can't figure it out how to deal with the app crash when someone tries to go right or down when at location d4.当有人在位置 d4 尝试向右或向下移动时,我似乎无法弄清楚如何处理应用程序崩溃。 I need something like this:我需要这样的东西:

if myPlayer.location == 'd4' and zonemap['d4'][RIGHT = '']
   print("You can't go there!")

Some more code that may be helpful and may let u understand my project better:更多代码可能会有所帮助,并且可以让您更好地理解我的项目:

def print_location():
    print('\n'+'#' + myPlayer.location.upper() + '#')
    print('#' + zonemap[myPlayer.location][DESCRIPTION] + '#' + '\n')
    
def prompt():
    print('================================')
    print('\n' + 'What would you like to do?')
    action = input('> ').lower()
    acceptable_actions = ['move', 'go', 'travel', 'walk', 'quit', 'examine', 'inspect', 'interact','look']
    while action not in acceptable_actions:
        print('Unknown action, please try again.\n')
        action = input('> ').lower()
    if action == 'quit':
        sys.exit()
    elif action in ['move', 'go', 'travel', 'walk']:
        player_move(action)
    elif action in ['examine', 'inspect', 'interact', 'look']:
        player_examine(action)

def player_move(myAction):
    ask = 'Where would you like to move to?\n> '
    dest = input(ask)
    if dest in ['up', 'north']:
        destination = zonemap[myPlayer.location][UP]
        movement_handler(destination)
    elif dest in ['left', 'west']:
        destination = zonemap[myPlayer.location][LEFT]
        movement_handler(destination)   
    elif dest in ['down', 'south']:
        destination = zonemap[myPlayer.location][DOWN]
        movement_handler(destination)
    elif dest in ['right', 'east']:
        destination = zonemap[myPlayer.location][RIGHT]
        movement_handler(destination)

def movement_handler(destination):
    myPlayer.location = destination
    print_location()

You have here the function that prints the location as A1 A2 etcetera and the description.您在这里有将位置打印为 A1 A2 等的功能和描述。 The function that prompts over and over as the main loop.作为主循环反复提示的函数。 Last but not least the player movement function and the movement handler.最后但并非最不重要的是玩家移动功能和移动处理程序。

Probably you already figured out what to do, but here is what I've would done.可能您已经想好要做什么了,但这就是我要做的。

def player_move(myAction):
    ask = 'Where would you like to move to?\n> '
    dest = input(ask)
    direction = None
    if dest in ['up', 'north']:
        direction = UP
    elif dest in ['left', 'west']:
        direction = LEFT
    elif dest in ['down', 'south']:
        direction = DOWN
    elif dest in ['right', 'east']:
        direction = RIGHT
    if not direction:
        # you need to check if the correct direction was entered
        print ("ERROR wrong direction ... ")
        return
    if not zonemap[myPlayer.location][direction]:
        # You use empty string if player cannot go in direction so negation of empty string is True
        print ("You cannot go there")
        return
    mPlayer.location = zonemap[myPlayer.location][direction]

This way you cover all cases - UP/LEFT/DOWN/RIGHT with single check.这样您就可以覆盖所有情况 - 上/左/下/右单检查。

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

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