虽然循环不起作用 - 文本冒险python游戏

时间:2018-05-23 19:02:17

标签: python python-3.x

我的while循环不起作用。我试图让它检查玩家是死了还是敌人,如果是这样,那么战斗就结束了。如果他们还活着,那就再战斗直到有人死了。现在我的代码即使其中一个或两个都死了,战斗仍然继续。我试着制作if if语句并仍然做同样的事情。

def fight():
  Monster.reduce_health(Player.weapon)
  Monster_check_dead()
  Player.reduce_health(Monster.damage)
  Player_check_dead()
  while Monster_check_dead == True or Player_check_dead == True:
    print ("Your fight is over")
    break
  else:
    fight_again()

完整代码:

import random
import time

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

  def reduce_health(self):
    Player.health = Player.health - Monster.damage


class Monster:
  def __init__(self, name, health, damage, armor):
    self.name = name
    self.health = health
    self.armor = armor
    self.damage = damage  

  def reduce_health(self):
    Monster.health = Monster.health - Player.weapon

def Monster_check_dead():
  if Monster.health < 1 == True :
    print(Monster.name, "has been killed")
  else:
    print (Monster.name, "current health: ", Monster.health)


def Player_check_dead():
  if Player.health < 1 == True :
    print(Player.name, "You have died")
  else:
    print (Player.name, "current health: ", Player.health)


def fight():
  Monster.reduce_health(Player.weapon)
  Monster_check_dead()
  Player.reduce_health(Monster.damage)
  Player_check_dead()
  while Monster_check_dead == True or Player_check_dead == True:
    print ("Your fight is over")
    break
  else:
    fight_again()

def fight_again():
  if ask("Do you wish to attack again"):
    fight()
  else:
    print ("You have ran away") 

def ask(question):
    answer = input(question + " [Y/N]")
    return answer in ["y", "Y", "Yes", "YES", "yes"]

print("Welcome to Nadia adventures the Nadia text adventure game!")

if ask("Do you wish to play the game?"):
    print ("Lets the great adventure start!")
    Player.name = "Nadia"
    Player.health = 100
    Player.weapon = 10
    Player.armor = 0
    print ("Nadia your current health is", Player.health)
time.sleep(1.0)

print("Nadia you have been teleported to a strange new world ....... ")

if ask("Do you wish to look around?"):
  print ("You start to look around and you have spotted a cave")
  ask("Do you wish to enter the cave?")
else: 
  print ("A gaint spider has appeared!")
  Monster.name = "Gaint spider"
  Monster.health = 25
  Monster.damage = 5
  if ask("Do you wish to fight?"):
    fight() 
  else:
    print("You have ran away")

1 个答案:

答案 0 :(得分:2)

从我的代码中可以看出,你只需打印出怪物或人是否死亡。

我建议让Monster_Check_Dead和Person等效返回True或False,如下所示。

def Monster_check_dead():
  if Monster.health < 1 == True :
    print(Monster.name, "has been killed")
    return True
  else:
    print (Monster.name, "current health: ", Monster.health)
    return False


def Player_check_dead():
  if Player.health < 1 == True :
    print(Player.name, "You have died")
    return True
  else:
    print (Player.name, "current health: ", Player.health)
    return False

然后在您展示的代码的第一部分中,在每个方法的末尾添加括号

  while Monster_check_dead() or Player_check_dead():
    print ("Your fight is over")
    break
  else:
    fight_again()

如果这不起作用,请通知我,我可以重新查看我做错了什么。