Skip to main content
 首页 » 编程设计

python之跟踪玩家健康基于文本的游戏之Python

2024年12月31日9exmyth

我很难跟踪和调整 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") 

返回这个:

The adventures Of Magical Nadia 
Do you wish to embark on this great adventure? [y/n] Y 
You have accepted the adventure. God Speed my young rass! 
You desperately try to stop the Spider for 2 damage 
Spider gores you for 7 damage 
You have won the fight 

目标:

The adventures Of Magical Nadia 
Do you wish to embark on this great adventure? [y/n] Y 
You have accepted the adventure. God Speed my young rass! 
You desperately try to stop the Spider for 2 damage 
Spider gores you for 7 damage 
Player health 93 
You desperately try to stop the Spider for 2 damage 
Spider gores you for 7 damage 
Player health 86  

请您参考如下方法:

为什么不使用类而不是使用字典来存储玩家属性呢?

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

您可以向 Player 类添加额外的方法,例如 calculate_damage(),以考虑玩家拥有的武器类型。如果您还创建了一个怪物类,您的战斗序列可能看起来像

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