0
我無法弄清楚在戰鬥中擊敗敵人後如何添加兩種藥劑。讓我瘋狂,我知道這可能很簡單。任何幫助表示讚賞。我想要做的就是開始戰鬥,如果你能夠按1來掠奪敵人並在你的庫存中增加兩種藥劑。python 3.x - AttributeError:'用戶'對象沒有屬性'__plusPotion'
這是錯誤:
Traceback (most recent call last):
File "C:/Users/kensai/Desktop/lab3/project/fight.py", line 137, in <module>
firstfight()
File "C:/Users/kensai/Desktop/lab3/project/fight.py", line 72, in firstfight
fight(soldier, player1, name1)
File "C:/Users/kensai/Desktop/lab3/project/fight.py", line 100, in fight
loot(soldier, player1, name1)
File "C:/Users/kensai/Desktop/lab3/project/fight.py", line 59, in loot
player1.__plusPotion(number1)
AttributeError: 'user' object has no attribute '__plusPotion'
這裏是代碼:
import sys
import random
import time
time.sleep(.002)
number = random.randint(0,20)
number1 = 2
name1 = input("What would you like your player to be named?")
class user:
def __init__ (self, player_name, player_health, health_potion):
self.__player_health = player_health
self.__player_name = player_name
self.__health_potion = health_potion
def getName(self):
return self.__player_name
def getHealth(self):
return self.__player_health
def getPotion(self):
return self.__health_potion
def setHealth(self):
self.__player_health = 100
return self.player_health
def minusHealth(self, number):
self.__player_health -= number
return self.__player_health
def minusPotion(self):
self.__health_potion -= 1
return self.__health_potion
def plusPotion(self, number1):
self.__health_potion += number1
return self.__health_potion
def plusHealth(self):
self.__player_health += 20
return self.__player_health
def loot(soldier, player1, name1):
player1 = user(name1, player1.getHealth(), player1.getPotion())
soldier = user('Soldier', soldier.getHealth(), soldier.getPotion())
booty1 = input("Type ' 1 ' to loot the enemy")
if booty1 == "1":
print("You find two first-aid kits")
player1.__plusPotion(number1)
def firstfight():
time.sleep(1)
player1 = user(name1, 100, 0)
soldier = user('Soldier', 80, 0)
print("Your characters stats are: Health:", player1.getHealth())
time.sleep(2)
fight(soldier, player1, name1)
def fight(soldier, player1, name1):
import random
import sys
player1 = user(name1, player1.getHealth(), player1.getPotion())
soldier = user('Soldier', soldier.getHealth(), soldier.getPotion())
p = 0
while p == 0:
attack = input("1 = Attack with weapon, 2 = use potion")
if attack == "1":
number = random.randint(5,20)
time.sleep(1)
print("You strike at your enemy. Soldier's health now is", soldier.minusHealth(number))
time.sleep(.05)
number = random.randint(5,20)
print("Soldier hits you back and your health is now at", player1.minusHealth(number))
time.sleep(.003)
if soldier.getHealth() <= 0:
print("You have won the fight!")
loot(soldier, player1, name1)
p = 2
elif player1.getHealth() <= 0:
print("You died.")
time.sleep(2)
print("\n\n\n\n\n\n\n\n\n\n")
print("Thanks for playing")
time.sleep(5)
print("\n\n\n\n\n\n\n\n\n\n")
sys.exit(0)
elif attack == "2":
time.sleep(1)
if player1.getPotion() == 0:
print("You don't have any first-aid kits")
else:
print("You use a first aid-kit!")
player1.plusHealth(), player1.minusPotion()
print("Your characters stats are: Health:", player1.getHealth(), "Health Potion:", player1.getPotion())
if player1.getHealth() > 100:
player1.setHealth()
print("Soldier hits you back and your health is now at", player.minusHealth(number))
else:
fight(soldier, player1, name1)
firstfight()