Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
跟踪玩家健康状况基于文本的游戏-Python_Python_Python 3.x - Fatal编程技术网

跟踪玩家健康状况基于文本的游戏-Python

跟踪玩家健康状况基于文本的游戏-Python,python,python-3.x,Python,Python 3.x,我很难跟踪和调整player[“health”]我创建了一个函数reduce_health():,该函数根据敌人的攻击降低玩家的生命值,但当我进行战斗时,生命值不起作用。我只是简单地赢得了这场战斗,或者当我编辑代码时,它只是不断地来回攻击。我如何创建一个降低生命值的函数,并在每次攻击后跟踪它,直到玩家死亡?请注意,代码的结构可能不太好,因为我仍在处理它,在我弄明白这一点后,我会清理它 import random import time #Tracking weapon and player h

我很难跟踪和调整
player[“health”]
我创建了一个函数
reduce_health():
,该函数根据敌人的攻击降低玩家的生命值,但当我进行战斗时,生命值不起作用。我只是简单地赢得了这场战斗,或者当我编辑代码时,它只是不断地来回攻击。我如何创建一个降低生命值的函数,并在每次攻击后跟踪它,直到玩家死亡?请注意,代码的结构可能不太好,因为我仍在处理它,在我弄明白这一点后,我会清理它

import random
import time

#Tracking weapon and player health
player = {"weapon":None, "health": None}

#function for questions avoide repeat 
def ask(question):
    answer = input(question + " [y/n]")
    return answer in ["y", "Y", "Yes", "YES", "yes"]

def game_over():
  print ("You Lose")
#initial question to start
print ("The adventures Of Magical Nadia")

#Question to start game or end game
if ask("Do you wish to embark on this great adventure?"):
  print ("You have accepted the adventure. God Speed my young rass!")
  player["health"] = 100
else:
  print ("You are a coward and shall not in bark on a great adventure!")

#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)
}


#to give the player weapons code
#player["weapon"] = "Spear"


#Enemys type
enemy = {"name":None, "health":None, "attack":None }
Gaint_spider = {"name":"Spider","health":(10), "attack":(7, 10) } 
Dogs = {"name":"Dogs","health": (50), "attack":(4,15)}
Dragon = {"name":"Dragon","health": (150), "attack":(35,45)}

def reduce_health():
  healthcheck = int(player["health"])
  enemyattack = int("enemy_damage")
  player["health"] = healthcheck - enemyattack
  print (player["health"])
  if player["health"] <= 0:
    game_over()
#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,reduce_health):
   player_damage, player_win , enemy_damage, enemy_win = combat(player, enemy)

   print (fight_description["player_damage"] % (enemy["name"], player_damage))
   time.sleep(1.0) 
   print (fight_description["enemy_damage"] % (enemy["name"], enemy_damage) )
   return reduce_health

   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, reduce_health)
while fight_result is None:
   describe_combat(player, Gaint_spider, Sample_FIGHT,reduce_health)
if True: 
   print ("You have won the fight")
else:
   print ("You lost")
目标:


与其使用dict来存储播放器属性,为什么不使用类呢

class Player:
    def __init__(self, weapon, health):
        self.weapon = weapon
        self.health = health

    def reduce_health(amount):
        self.health -= amount
你可以在
Player
类中添加额外的方法,比如
calculate\u damage()
,考虑玩家拥有的武器类型。如果你也创建了一个怪物类,你的战斗序列可能看起来像

def fight():
    monster.reduce_health(player.calculate_damage())
    monster.check_dead()
    player.reduce_health(monster.calculate_damage())
    player.check_dead()
    ...

这意味着什么:
int(“敌方伤害”)
你带着
return-reduce\u-health
提前返回,而且
return-None
缩进严重。在你的方法中,你永远不会达到那条线。另外,
fight\u result
的值为
reduce\u health
,并且在
while
循环中没有更改。我试图不使用类,因为我计划使用类等来重建此项目,因为它的代码更干净。这是我学习python的第二个项目。谢谢,这样做更有意义。我将开始使用类。在您完成类的实现后,您可以前往并征求评论。
class Player:
    def __init__(self, weapon, health):
        self.weapon = weapon
        self.health = health

    def reduce_health(amount):
        self.health -= amount
def fight():
    monster.reduce_health(player.calculate_damage())
    monster.check_dead()
    player.reduce_health(monster.calculate_damage())
    player.check_dead()
    ...