python时间间隔循环_python3中循环之间的时间间隔

这是我创建的一个类,用于处理我正在处理的项目中的“自动攻击”。它工作得很好(它接近于每秒1次攻击,而不是2次攻击那么公平,而且出于某些原因,3次或更大的攻击都是非常合适的)有没有更有效的方法来处理这个问题?在import time

import random

### your critical percentage

critStat = 20

### enemy block percentage

eBlockchance = 12

### your hit percentage

hitStat = 90

### Your attack speed is X/second. (ie. 2.4 would be 2.4 attacks per second)

atkSpeed = 1

### This works perfectly, probably a better way though.

def atkInterval(atkSpeed):

"""Sets the attack interval to 1 second divided by the attack speed"""

start = time.time()

end = 0

while end <= 1/atkSpeed :

end = time.time()- start

### Change parameters to the real algorithm

def atkDamage(strength, defense):

"""computes damage as strength - defense"""

base = strength - defense

damage = random.randint(base/2, base) ## Raised an arror when not divisible by 2

if hitChance(hitStat) == False:

print("Miss!")

return 0

else:

if enemyBlock(eBlockchance) == True:

print("Blocked!")

return 0

else:

if critChance(critStat) == True:

print(int(damage*1.5), "Crit!")

return int(damage * 1.5)

else:

return damage

### Critical Strike chance takes a whole number

def critChance(critStat):

"""If your crit chance is higher than random 1-100 returns true or false"""

chance = random.randint(1, 100)

if chance <= critStat:

return True

else:

return False

### Block chance is the same as crit

def enemyBlock(eBlockchance):

"""If enemy crit chance is higher than random 1-100 return true or false"""

chance = random.randint(1,100)

if chance <= eBlockchance:

return True

else:

return False

### Hit percentage

def hitChance(hitStat):

"""if hit chance is higher than random 1-100 return true/false"""

chance = random.randint(1,100)

if chance > hitStat:

return False

else:

return True

### The main function sets enemy health to 1000 and loops damage until health < 0.

def main():

health = 1000

numAttacks = 0

start = time.time()

while health > 0:

atkInterval(atkSpeed)

health -= atkDamage(100,0)

numAttacks+=1

print("Health remaining:", health)

end = time.time() - start

print("It took", numAttacks, "attacks and", end, "Seconds")

main()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值