未定义变量';玩家伤害';-Python文本库游戏
这是我在python中的第二个小项目,因为我还在学习,我正在使用参考代码并提出问题来完成这个项目,但我在第49-52行不断收到一个错误“未定义变量‘player_damage’”未定义变量';玩家伤害';-Python文本库游戏,python,python-3.x,Python,Python 3.x,这是我在python中的第二个小项目,因为我还在学习,我正在使用参考代码并提出问题来完成这个项目,但我在第49-52行不断收到一个错误“未定义变量‘player_damage’” Sample_FIGHT = { player_damage: "You desperately try to stop the %s for %i damage", enemy_damage: "%s gores you for %i damage", player_win: "The %s collapses wi
Sample_FIGHT = {
player_damage: "You desperately try to stop the %s for %i damage",
enemy_damage: "%s gores you for %i damage",
player_win: "The %s collapses with a thunderous boom",
enemy_win: "You are squished"
}
但我确实在我的函数战斗中定义了变量
def combat (player, enemy):
player_damage = random.randint (*WEAPONS[player["weapon"]])
enemy_damage = random.randint(*enemy["attack"])
player_win = player_damage > enemy["health"]
enemy_win= enemy_damage > player["health"]
return player_damage, player_win , enemy_damage, enemy_win
这就是全部代码
import random
#initial question to start
print ("The Adventures Of Magical Nadia")
Answer = input ("Do you wish to embark on this adventures?")
#Question to start game or end game
if Answer == "Yes" or Answer == "yes":
print ("You have accepted the adventure. God Speed my young rass!")
else:
print ("You are a coward and shall not in bark on a great adventure!")
#function for questions avoide repeat
def ask(question):
answer = input(question + " [y/n]")
return answer in ["y", "Y", "Yes", "YES", "yes"]
#Dic of all the weapons in the game
WEAPONS = {
"Spear": (3, 10), None:(1,3), "knife":(4,16), "Gun":(16,25), "Glass Bottle":(4,16)
}
#Tracking weapon and player health
player = {"weapon":None, "health": (100)}
#to give the player weapons code
player["weapon"] = "Spear"
#Enemys type
enemy = {"name":None, "health":None, "attack":None }
Gaint_spider = {"health":(45), "attack":(7, 10) }
Dogs = {"health": (50), "attack":(4,15)}
Dragon = {"health": (150), "attack":(35,45)}
#Function each fight gives random dmg, have a player and enemy, winner and loser
def combat (player, enemy):
player_damage = random.randint (*WEAPONS[player["weapon"]])
enemy_damage = random.randint(*enemy["attack"])
player_win = player_damage > enemy["health"]
enemy_win= enemy_damage > player["health"]
return player_damage, player_win , enemy_damage, enemy_win
#Structure of a fight
Sample_FIGHT = {
player_damage: "You desperately try to stop the %s for %i damage",
enemy_damage: "%s gores you for %i damage",
player_win: "The %s collapses with a thunderous boom",
enemy_win: "You are squished"
}
# describe the fight in a function
def describe_combat(player, enemy, fight_description):
player_damage, player_win , enemy_damage, enemy_win = combat(player, enemy)
print (fight_description["player_damage"] % (enemy["name"], player_damage))
print (fight_description["enemy_damage"] % (enemy["name"], enemy_damage))
if player_win:
print (fight_description["player_win"] % enemy["name"])
return True
if enemy_win:
print (fight_description["player_win"] % enemy["name"])
return False
return None # fight is a draw
fight_result = describe_combat(player, Gaint_spider, Sample_FIGHT)
if fight_result is None:
print ("This is a draw")
elif fight_result:
print ("You have won the fight")
else:
print ("You lost")
您在这里定义了一个字典,因此为此您需要传递一个应该是字符串的键变量,如:
Sample_FIGHT = {
"player_damage": "You desperately try to stop the %s for %i damage",
"enemy_damage": "%s gores you for %i damage",
"player_win": "The %s collapses with a thunderous boom",
"enemy_win": "You are squished"
}
您没有任何已定义的关键变量。这就是错误的原因。 因此,只需在下面的代码之前初始化它们
Sample_FIGHT = {
player_damage: "You desperately try to stop the %s for %i damage",
enemy_damage: "%s gores you for %i damage",
player_win: "The %s collapses with a thunderous boom",
enemy_win: "You are squished"
}
所以dict的使用方式,它认为玩家的伤害是一种功能。事实并非如此。这就是为什么会出现错误。您可以像使用字典一样使用字典,但我认为使用类是更好的代码。例如,定义一个玩家类,然后你可以访问像
player.wearm
这样的信息,而不是player[“wearm”]
@bendl我正计划从头开始重新制作基于文本的游戏,并使用python进入OOP,因为它看起来确实像是更好的代码。谢谢你的提示。