首页 > 解决方案 > 如何防止玩家在给定区域地图之外移动?

问题描述

我使用名为 zonemap 的字典创建了一个 4x6 区域地图。我在该字典中嵌套了多个字典;每个代表一个玩家可以访问和互动的区域。我希望能够将玩家移动限制在该 4x6 区域,并重新显示他们尝试访问“禁止”区域的位置。只有当玩家尝试访问由空字符串标记的区域时,才会出现此限制。下面的示例代码:

import cmd
import textwrap
import sys
import os
import time
import random

class player:
    def __init__(self):
        self.name = ''
        self.job = ''
        self.hp = 0
        self.mp = 0
        self.stamina = 0
        self.faith = 0
        self.status_effects = []
        self.location = "a2"
        self.gameOver = False
myPlayer = player()

ZONENAME = ''
DESCRIPTION = 'description'
EXAMINATION = 'examine'
SOLVED = False
UP = 'up', 'north'
DOWN = 'down', 'south'
LEFT = 'left', 'west'
RIGHT = 'right', 'east'

solved_places = {'a1': False, 'a2': False, # more places}

zonemap = {
    'a1': {
        ZONENAME: "West Mounatains",
        DESCRIPTION: "A mountain range.",
        EXAMINATION: "The slope is too steep to climb.",
        SOLVED: False,
        UP: '',
        DOWN: "Western Forest",
        LEFT: '', 
        RIGHT: "Central Mountains",
    },
    'a2': {
        ZONENAME: "Central Mountains",
        DESCRIPTION: "A mountain range",
        EXAMINATION: "These mountains go on for miles.",
        SOLVED: False,
        UP: '',
        DOWN: "Eastern Forest",
        LEFT: "Western Mountains",
        RIGHT: "Mountain Cave",
    },
    # more dictionaries
}

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

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

def movement_handler(destination):
    print("\n" + "You have moved to the " + destination + ".")
    myPlayer.location = destination
    print_location()

我在想一个 if 语句会起作用,但我不知道把它放在哪里。任何帮助,将不胜感激。如果需要,我还可以提供更多代码。

标签: pythondictionary

解决方案


是的,一个if会工作。检查用户想去的地方。如果无效,打印一条消息并且不要移动播放器。否则移动它们。

def player_move(player_action):
    ask = "Where would you like to move?\n"
    dest = input(ask).lower()
    if dest in ['up', 'north']:
        destination = UP
    elif dest in ['down', 'south']:
        destination = DOWN
    elif dest in ['left', 'west']:
        destination = LEFT
    elif dest in ['right', 'east']:
        destination = RIGHT
    else:
        print("Bad entry")
        return

    if "" == zonemap[myPlayer.location][destination]:
        print("Can't go there")
    else:
        movement_handler(zonemap[myPlayer.location][destination])

推荐阅读