class GameMech:
def __init__(self ,aCharacter):
print("A battle is starting")
def getMP(self, aCharacter):
return aCharacter.mp
def getHP(aCharacter):
return aCharacter.hp
def getAtk(aCharacter):
return aCharacter.atk
def getDef(aCharacter):
return aCharacter.defense
def usePotion(aCharacter):
aCharacter.hp += 100
return aCharacter.hp
def useSpecial(self, aCharacter, target):
if aCharacter.mp >= 100:
target.hp -= 45
def dead(self, aCharacter):
return aCharacter.name +" is now dead"
def attack(self, aCharacter, target):
if target.hp - (aCharacter.atk/aCharacter.defense) <= 0:
dead(target)
else:
target.hp - aCharacter.atk/aCharacter.defense
print(getHP(target))
def guard(self, aCharacter):
print(aCharacter + "was unharmed")
if aCharacter.hp <= 50:
retaliation(aCharacter, target)
def retaliation(self ,aCharacter, target):
target.hp - (aCharacter.atk/10)
print(getHP(target))
def battleMenu(aNumber, self):
if aNumber == 1:
attack(aCharacter, target)
if aNumber == 2:
guard(aCharacter)
print(aCharacter + " was unharmed!")
if aNumber == 3:
useSpecial(aCharacter, target)
print(getHP(target))
if aNumber == 4:
heal = useItem(aCharacter)
print(heal)
def myTurn(self):
print("ATTACK")
print("GUARD")
print("SPECIAL")
print("ITEM")
aNumber = int(input("What would you like to do? "))
battleMenu(aNumber)
def oppTurn(self):
print("It is the opponent's turn")
randomNum = random.randint(1,4)
battleMenu(randomNum)
一些注意事項,顯然由於這種格式,你不能說我的所有方法實際上都是在課堂上,但它們是。 你可以忽略前幾個部分,我關注的部分是戰鬥菜單。 因此,我創造了一個小文本遊戲作爲我自己的項目,純粹的休閒沒有學術或工作。就我自己而言,只是一個小遊戲來幫助我慢慢地回憶起python的記憶。所以這就是我的比賽的直覺,字符類是另一個文件並沒有什麼重要的在那裏只是人物的屬性(HP,MP,攻擊力,DEF) 這裏的主要(更新)Python:爲什麼它說我的方法沒有定義,當它明確是什麼?
import random
import character
import battle
#create a battle menu
#and a character value check menu
#add buttons
def main():
char1 = character.Character("Charlie", 5000, 2000, 1500, 750)
char2 = character.Character("Mark", 2000, 4000, 2000, 900)
newGame = battle.GameMech(char1)
while char1.hp != 0 or char2.hp != 0:
newGame.myTurn()
newGame.oppTurn()
主( ) (不知道爲什麼它不包括我的主要定義在代碼片段中) 任何我得到這個錯誤說'name'battleMenu'未定義「。 名稱通常適用於變量是否正確?但戰鬥菜單是一種方法是否有我得到這個錯誤的原因?
Traceback (most recent call last):
File "E:\PythonGame\mainGame.py", line 22, in <module>
main()
File "E:\PythonGame\mainGame.py", line 20, in main
newGame.myTurn()
File "E:\PythonGame\battle.py", line 55, in myTurn
battleMenu(self,aNumber)
NameError: name 'battleMenu' is not defined
請編輯您的問題以顯示實際的錯誤追溯。 –