ですから、暇なときにかなり基本的なRPGを作成してきましたが、つまずきになりました。プレイヤーが戦闘に出入りするたびにコマンドの辞書を変更することで、特定の機能だけが特定の時間にアクセスできるようにしたいと思います。ただし、辞書キーを検索するために設定したループは、最初に記述されたコマンドを除いて、どのコマンドでも機能しないようです。
メインファイル:
from commands import *
Commands = {
"travel": Player.travel,
"explore": Player.explore,
"help": Player.help,
}
p = Player()
while (john_hero.health > 0):
line = raw_input("=> ")
args = line.split()
if len(args) > 0:
commandFound = False
for c in Commands.keys():
if args[0] == c[:len(args[0])]:
Commands[c](p)
commandFound = True
break
if not commandFound:
print "John's too simple to understand your complex command."
command.py
class Player:
def __init__(self):
self.state = "normal"
john_hero = John()
self.location = "Town"
global Commands
Commands = {
"attack": Player.attack,
"flee": Player.flee,
"help": Player.help
}
def fight(self):
Player.state = "fight"
global Commands
enemy_a = Enemy()
enemy_name = enemy_a.name
print "You encounter %s!" % (enemy_name)
*注:ループは他の誰かのコードから取得されました。私は主に学習目的でゲームを作成しているので、それを使用しています。