当用户选择动作三时,我试图将ctr
编码为计数器+ 1,以计算限制数40个中的1个。我无法弄清楚,因为我在5次不同的时间内一直收到相同的错误。我今年刚接触Python。有什么想法吗?
这就是我想在大学为我的项目的一部分打造的Monster游戏。我还没有弄清楚。
class Player:
def __init__(self, name):
self.name = name
self.hp = 40
self.atk = 5
self.sp = 10
self.snk = 3 #FOR SNACKS ONLY
self.lvl = 1
self.exp = 0
def attack(self, other):
print("%s attacks %s for %d damage\n"%(self.name, other.name, self.a>
other.takeDamage(self.atk, self)
def takeDamage(self, dmg):
print("%s takes %d damage\n" % (self.name, dmg))
self.hp -= dmg
#if(self.hp <= 0):
# self.die()
#def die(self):
#print("%s died" %(self.name))
#print("-------GAMEOVER-------")
#print("You earned %d exp this game" %(self.exp))
def takeTurn(self, monsterArray):
print("%s HP:%d"%(self.name, self.hp))
print("Atk:%d"%(self.atk))
print("SP:%d"%(self.sp))
print()
#For every monster i, print information about it
for i in monsterArray:
#print information about Monster i
i.printInfo()
action = self.getAction()
#Player selected "Attack"
if(action == 1):
print("\nChoose target: ")
counter = 0
for i in monsterArray:
print("\n%d: " % (counter), end="")
i.printInfo()
counter += 1
userInput = int(input(">"))
self.attack(monsterArray[userInput])
if(monsterArray[userInput].hp <= 0):
monsterArray.pop(userInput)
if(action == 2):
print("\nYou are in Passive mode... - 1 dmg!\n")
self.hp -= 1
if(action == 3):
print("\nYou are heal up to 3+ SP")
self.hp += 8
ctr = ctr + 1 #count how many time used
def getAction(self):
while(1):
print("\nAvailable actions:")
print("1: Attack")
print("2: Passive Mode [-1 dmg]")
print("3: Snacks [%d out of 40]" % (ctr), end="")
try:
userInput = int(input(">"))
except ValueError:
print("Please enter a number")
continue
if(userInput < 1 or userInput > 3):
print("invalid action number")
continue
elif(userInput == 2 and self.sp < 5):
print("Loser! \n You are skipping yourself. Continue to figh>
continue
elif(userInput == 3 and self.hp< 40):
print("NO MORE.... Use your health leftover!")
continue
return userInput
预期:
cs15125@cs:/u1/class/cs15125/project> python3 battle.py
Enter your name adventurer:
> EXAMPLE
EXAMPLE HP:40
Atk:5
SP:10
Diggle - HP:10
Tegmul - HP:5
Erthran - HP:7
Ozal - HP:14
Xag'thazok - HP:10
Available actions:
1: Attack
2: Passive Mode [-1 dmg]
3: Snack [5 out of 40]
> 3
实际:
Enter your name adventurer:
> EXAMPLE
EXAMPLE HP:40
Atk:5
SP:10
Diggle - HP:10
Tegmul - HP:5
Erthran - HP:7
Ozal - HP:14
Xag'thazok - HP:10
Available actions:
1: Attack
2: Passive Mode [-1 dmg]
Traceback (most recent call last):
File "battle.py", line 19, in <module>
p.takeTurn(monArray) #done
File "/u1/class/cs15125/project/player.py", line 31, in takeTurn
action = self.getAction()
File "/u1/class/cs15125/project/player.py", line 60, in getAction
print("3: Snacks [%d out of 40]" % (ctr), end="")
NameError: name 'ctr' is not defined
cs15125@cs:/u1/class/cs15125/project>
这是我目录中第二个运行Battle.py的文件
battle.py:
from monster import *
from player import Player
red = "\u001b[31m"
green = "\u001b[32m"
yellow = "\u001b[33m"
blue = "\u001b[34m"
cyan = "\u001b[36m"
#white = ""
reset = "\u001b[0m"
u = input(blue + "Enter your name adventurer:\n> " + reset)
p = Player(u)
monArray = createMonsters()
#print(monArray[0].name)
while(1):
p.takeTurn(monArray) #done
#for every monster
# monster take turn
for i in monArray:
i.takeTurn(p)
if(p.hp <= 0):
print("YOU'RE DEAD! Best of luck next time!")
exit()
if(len(monArray) == 0):
#game ends, you win
print("You WON!")
exit()
monster.py
class Monster:
def __init__(self, name, hp, atk):
self.name = name
self.hp = hp
self.atk = atk
self.exp = (hp/10)+atk
def takeDamage(self, dmg, other):
print("The %s takes %d damage" % (self.name, dmg))
self.hp -= dmg
if(self.hp <= 0):
self.die(other)
def die(self, other):
print("The %s dies in a pool of disappointment" %(self.name))
other.exp += self.exp
print("%s gains %d exp" %(other.name,self.exp))
def printInfo(self):
print("%s - HP:%d" % (self.name, self.hp))
def takeTurn(self, player):
self.attack(player)
def attack(self, other):
print("%s attacks %s for %d damage"%(self.name, other.name, self.atk))
other.takeDamage(self.atk)
#Loads data.txt, reading monster information.
#Parsing the text into monster objects.
#Appended obj to array, returning the array.
def createMonsters():
monArray = []
try:
f = open("data.txt","r")
except:
print("Can't open data.txt")
exit()
fileData = f.read().split("\n")
f.close()
i = 0
while(i < len(fileData)):
dataLine = fileData[i].split(":")
if(dataLine[0] == "Name"):
name = dataLine[1]
dataLine = fileData[i+1].split(":")
hp = int(dataLine[1])
dataLine = fileData[i+2].split(":")
atk = int(dataLine[1])
m = Monster(name, hp, atk)
monArray.append(m)
i += 1
return monArray
答案 0 :(得分:0)
检查ctr
您可以做的是:
全局定义点击率:
ctr = 0
#或您想要的任何值
然后在要重用的函数中声明:
global ctr
然后您可以在任何函数中使用它而不会出现任何错误。
将您的代码用于例如:
ctr = 0
class Player:
def __init__(self, name):
self.name = name
self.hp = 40
self.atk = 5
self.sp = 10
self.snk = 3 #FOR SNACKS ONLY
self.lvl = 1
self.exp = 0
def attack(self, other):
print("%s attacks %s for %d damage\n"%(self.name, other.name, self.a>
other.takeDamage(self.atk, self)
def takeDamage(self, dmg):
print("%s takes %d damage\n" % (self.name, dmg))
self.hp -= dmg
#if(self.hp <= 0):
# self.die()
#def die(self):
#print("%s died" %(self.name))
#print("-------GAMEOVER-------")
#print("You earned %d exp this game" %(self.exp))
def takeTurn(self, monsterArray):
global ctr # <---- here
print("%s HP:%d"%(self.name, self.hp))
print("Atk:%d"%(self.atk))
print("SP:%d"%(self.sp))
print()
#For every monster i, print information about it
for i in monsterArray:
#print information about Monster i
i.printInfo()
action = self.getAction()
#Player selected "Attack"
if(action == 1):
print("\nChoose target: ")
counter = 0
for i in monsterArray:
print("\n%d: " % (counter), end="")
i.printInfo()
counter += 1
userInput = int(input(">"))
self.attack(monsterArray[userInput])
if(monsterArray[userInput].hp <= 0):
monsterArray.pop(userInput)
if(action == 2):
print("\nYou are in Passive mode... - 1 dmg!\n")
self.hp -= 1
if(action == 3):
print("\nYou are heal up to 3+ SP")
self.hp += 8
ctr = ctr + 1 #count how many time used
def getAction(self):
global ctr # <---- and here
while(1):
print("\nAvailable actions:")
print("1: Attack")
print("2: Passive Mode [-1 dmg]")
print("3: Snacks [%d out of 40]" % (ctr), end="")
try:
userInput = int(input(">"))
except ValueError:
print("Please enter a number")
continue
if(userInput < 1 or userInput > 3):
print("invalid action number")
continue
elif(userInput == 2 and self.sp < 5):
print("Loser! \n You are skipping yourself. Continue to figh>
continue
elif(userInput == 3 and self.hp< 40):
print("NO MORE.... Use your health leftover!")
continue
return userInput
没有global
:
class Player:
def __init__(self, name):
self.name = name
self.hp = 40
self.atk = 5
self.sp = 10
self.snk = 3 #FOR SNACKS ONLY
self.lvl = 1
self.exp = 0
self.ctr = 0
def attack(self, other):
print("%s attacks %s for %d damage\n"%(self.name, other.name, self.a))
other.takeDamage(self.atk, self)
def takeDamage(self, dmg):
print("%s takes %d damage\n" % (self.name, dmg))
self.hp -= dmg
#if(self.hp <= 0):
# self.die()
#def die(self):
#print("%s died" %(self.name))
#print("-------GAMEOVER-------")
#print("You earned %d exp this game" %(self.exp))
def takeTurn(self, monsterArray):
print("%s HP:%d"%(self.name, self.hp))
print("Atk:%d"%(self.atk))
print("SP:%d"%(self.sp))
print()
#For every monster i, print information about it
for i in monsterArray:
#print information about Monster i
i.printInfo()
action = self.getAction()
#Player selected "Attack"
if(action == 1):
print("\nChoose target: ")
counter = 0
for i in monsterArray:
print("\n%d: " % (counter), end="")
i.printInfo()
counter += 1
userInput = int(input(">"))
self.attack(monsterArray[userInput])
if(monsterArray[userInput].hp <= 0):
monsterArray.pop(userInput)
if(action == 2):
print("\nYou are in Passive mode... - 1 dmg!\n")
self.hp -= 1
if(action == 3):
print("\nYou are heal up to 3+ SP")
self.hp += 8
self.ctr = self.ctr + 1 #count how many time used
def getAction(self):
while(1):
print("\nAvailable actions:")
print("1: Attack")
print("2: Passive Mode [-1 dmg]")
print("3: Snacks [%d out of 40]" % (self.ctr), end="")
try:
userInput = int(input(">"))
except ValueError:
print("Please enter a number")
continue
if(userInput < 1 or userInput > 3):
print("invalid action number")
continue
elif(userInput == 2 and self.sp < 5):
print("Loser! \n You are skipping yourself. Continue to figh")
continue
elif(userInput == 3 and self.hp< 40):
print("NO MORE.... Use your health leftover!")
continue
return userInput