0

これは、私が取り組んでいるプロジェクトで「自動攻撃」を処理するために作成したクラスです。それは十分に機能します (1 秒あたりの攻撃は 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()
4

2 に答える 2

0

ビジー待機ループを使用して遅延を作成しないでください。そのために使用time.sleep(seconds)します。次のように書き換えるatkIntervalことができます。

def atkInterval(atkSpeed):
    time.sleep(1/atkSpeed)
于 2013-04-26T18:00:06.243 に答える