python中的游戏,Python中游戏的Counter+1动作

检查的范围

ctr

你可以做的是:

全局定义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 #

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 #

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值