Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.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
While循环不工作-文本冒险python游戏_Python_Python 3.x - Fatal编程技术网

While循环不工作-文本冒险python游戏

While循环不工作-文本冒险python游戏,python,python-3.x,Python,Python 3.x,我的while循环不工作。我试着让它检查一下,看看玩家是死了还是敌人死了,如果是这样,战斗就结束了。如果他们还活着,那就继续战斗,直到有人死。现在用我的代码,即使一个或两个都死了,战斗仍在继续。我试着做出if-else语句,但仍然做同样的事情 def fight(): Monster.reduce_health(Player.weapon) Monster_check_dead() Player.reduce_health(Monster.damage) Player_check

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

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")
随机导入
导入时间
职业球员:
定义初始(自我、姓名、武器、生命值、护甲):
self.name=名称
自我武器
自我健康
自我保护=盔甲
def减少_健康(自我):
Player.health=Player.health-怪物伤害
类怪物:
定义初始(自我、姓名、生命值、伤害、护甲):
self.name=名称
自我健康
自我保护=盔甲
自毁
def减少_健康(自我):
怪物健康=怪物健康-玩家武器
def Monster_check_dead():
如果Monster.health<1==True:
打印(Monster.name,“已被杀死”)
其他:
打印(Monster.name,“当前生命:”,Monster.health)
def Player_check_dead():
如果Player.health<1==True:
打印(Player.name,“你已经死了”)
其他:
打印(Player.name,“当前运行状况:”,Player.health)
定义战斗():
怪物。降低生命值(玩家。武器)
怪物死了
玩家。降低生命值(怪物伤害)
玩家检查死亡()
当怪物死亡==真或玩家死亡==真时:
打印(“你的战斗结束了”)
打破
其他:
再战
再打一次()
如果询问(“您是否希望再次攻击”):
打架
其他:
打印(“你跑掉了”)
提问:
回答=输入(问题+“[Y/N]”)
返回[“y”、“y”、“是”、“是”、“是”中的答案
打印(“欢迎来到纳迪亚冒险-纳迪亚文本冒险游戏!”)
如果问(“你想玩这个游戏吗?”):
打印(“让伟大的冒险开始吧!”)
Player.name=“娜迪亚”
Player.health=100
玩家武器=10
Player.armor=0
打印(“Nadia您当前的健康状况”,Player.health)
时间。睡眠(1.0)
打印(“娜迪亚,你被传送到一个陌生的新世界……)
如果问(“你想四处看看吗?”):
打印(“你开始环顾四周,发现了一个洞穴”)
问(“你想进入洞穴吗?”)
其他:
打印(“一只巨大的蜘蛛出现了!”)
Monster.name=“大蜘蛛”
怪物健康=25
怪物伤害=5
如果问(“你想打架吗?”):
打架
其他:
打印(“你跑掉了”)

从您的代码中我可以看出,您只需打印出怪物或人是死了

我建议让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

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

恐怕您的代码已损坏无法修复。您希望
如果
,而不是
,而
。。是的,除此之外,他说:^
而Monster\u check\u dead==True或Player\u check\u dead==True:
在将函数对象与布尔值进行比较时总是错误的。您需要阅读更多python资源,如@EugeneSh。起初我确实使用了If,但它仍然使用相同的方法。因为代码中有几个错误。这些函数不返回任何内容,然后您正在测试函数本身,然后出现这个while/else(带break)循环代码。。。同时停止针对
==True
的测试,虽然它“修复”了一些问题,但我不想这样编写代码。@shodmoth它是否有效。我最初在没有为else语句设置return False的情况下尝试了此操作。我认为,如果我将if语句返回True,那么else将自动为false,而无需在代码中包含return Flase。谢天谢地,这段代码非常不符合Python。并不能解决所有的问题。。。例如,
当Monster\u check\u dead()==True或Player\u check\u dead()==True
应该是
当Monster\u check\u dead()或Player\u check\u dead()
时,有很多分号,这不是C,它们是无用的。与
相比,True
是多余的,不应该这样做…@Jean-Françoisfare感谢您的输入。这只是我在python中的第二个项目,你让我思考更干净的代码,而不是多余的。对于我添加的问题,我感到抱歉。我只是用Java编程,完全忘记了python语法。我编辑了我的答案来解决这些问题。
  while Monster_check_dead() or Player_check_dead():
    print ("Your fight is over")
    break
  else:
    fight_again()