python - 如何防止玩家在给定区域地图之外移动?
问题描述
我使用名为 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 语句会起作用,但我不知道把它放在哪里。任何帮助,将不胜感激。如果需要,我还可以提供更多代码。
解决方案
是的,一个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])
推荐阅读
- oauth - WSo2 APIM 尝试生成密钥时出现错误 401
- python - Pyspark - 从两个不同的数据帧中减去列
- python - 应用于长整数的 pandas astype 返回截断的结果
- angular - Angular MQTT 如何从任何主题读取消息
- python - 获取 DataFrame 中每个日期时间列的频率
- python - 有没有一种方法可以创建中间帧以使视频更流畅?
- python - 使用 Tkinter 功能但看不到任何下拉菜单
- java - 在多线程的情况下初始化 SSLContext
- python - 当脚本运行 PyQt 时,TableView 不显示
- jquery - 如何使用 jQuery 检查至少一个字段是否应填写