我在Player类中有两个名为Play1()和play2()的函数,它们都调用位于同一函数下的函数。当我尝试使用“ Player.Play1()”从类外部调用它们时,计算机找不到函数play1()或play2()。为什么在播放器下找不到“ play1()”?
完整代码:
import random
from time import *
class Colors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class Player:
r = random
coins = 100
health = 100
p = 3 #potions.
def __init__(self):
random.seed()
print("init instance variables here")
def hk1():
print("Du sprang iväg")
def hk2():
print("Du sprang iväg")
sleep(3)
print("men du trillar och slår i knät: -20 HP")
self.health = self.health - 20
print("Your health is: " + str(self.health), " HP")
def hkf1():
if hakan.health <= 0:
hakan.health = 100
print("Du besegrade håkan")
print("Du tar hans droger och säljer det själv till Eleverna: +150 coins")
print("Your health is ", str(self.health), " HP")
self.coins = self.coins + 150
print("You have: ", str(self.coins), " coins")
else:
print("Du slår Håkan och gör -25 HP skada på honom.")
print("Your health is ", str(self.health), " HP")
print("You have: ", str(self.coins), " coins")
print("Håkan har ", hakan.health, "left")
def hkf2():
print("Håkan besegrade dig: -50 HP")
print("Your health is ", str(self.health), " HP")
print("You have: ", str(self.coins), " coins")
self.coins = self.coins + 150
def play1():
choice = random.choice(['flee', 'fall'])
if choice == 'flee':
hk1()
elif choice == 'fall':
hk2()
def play2():
choice = random.choice(['win', 'lose'])
if choice == 'win':
hkf1()
elif choice == "lose":
hkf2()
def report_status(self):
status_message = "Your health is %s, \nCoins left %s" % (self.health, self.coins)
return status_message
william = Player()
hakan = Player()
def buypotion():
if william.coins < 0:
print("You can't afford a potion")
else:
william.coins = william.coins - 25
william.p = Player.p + 1
print("You bought a potion for 25 coins")
print("You have ", william.coins, " coins left")
print("you have ", william.p, " potions")
print("Your health is now ", str(william.health), " HP")
def drinkpotion():
if Player.p == 0:
print("You can't drink any potions.")
print("You have zero potions left!")
print("Your health is ", str(william.health), " HP")
elif william.health >= 250:
print("Your health is already maxed out. You can't drink a potion.")
print("you have ", str(william.p), " potions")
print("Your health is ", str(william.health), " HP")
else:
william.health = william.health + 20
william.p = william.p - 1
print("you have ", william.p, " potions")
print("Your health is ", str(william.health), " HP")
def quests():
quest = input("Choose from quest: 1, 2 or 3 ")
if quest == "1" or quest == "option 1" or quest == "opt 1":
quest1()
elif quest == "2" or quest == "option 2" or quest == "opt 2":
print(Colors.RED + "ERROR: ERROR: ERROR:" + Colors.ENDC)
print(Colors.YELLOW + "Quest 2 is not yet added" + Colors.ENDC)
elif quest == "3" or quest == "option 3" or quest == "opt 3":
print(Colors.RED + "ERROR: ERROR: ERROR" + Colors.ENDC)
print(Colors.YELLOW + "Quest 3 is not yet added" + Colors.ENDC)
def quest1():
print("Du vandrar runt på Rudbeck när du ser Håkan, din samhällslärare, i ett mörkt hörn")
print("Du väljer att gå närmare för att investigera.")
print("Håkan står och säljer knark till eleverna.")
while hakan.health > 0 or william.health > 0 or hk != "Spring" or hk != "spring":
hk = input("Vad gör du? Spring: Slåss:")
if hk == "Spring" or hk == "spring":
Player.play1()
if hk == "slåss" or hk == "Slåss" or hk == "s":
Player.play2()
print("Welcome to a poorly made text-based game:")
print("you have ", william.p, " potions")
print("Your health is ", str(william.health), " HP")
print("You have: ", str(william.coins), " coins")
while True:
print("Commands: add, drink, coins, sleep, quest")
a = input("Enter a command: ")
if a == "add" or a == "Add" or a == "buy" or a == "Buy":
buypotion()
if a == "drink":
drinkpotion()
if a == "quest":
quests()