Game unit ideas
Back to Pastebinimport time
class BaseUnit (object):
BarFillTime = 10
BarRefillModifier = 1
AttackCount = 0
LastAttackTime = Null
def _attack(self):
# master API call
# dont subclass :)
self.LastAttackTime = int(time.time())
# call the subclasses custom attack
self.attack()
def attack(self):
# do custom attack here
print "KILL THEM ALL"
def checkBarProgress(self)
# i.e. when the bar will next be full
# subclass in each unit for customisation
if self.LastAttackTime == NULL: self._attack()
# calculate seconds since last attack and compare to our FillTime + Modifier
if (int(time.time()) - self.LastAttackTime) >= (self.BarFillTime + (self.AttackAcount * self.BarRefillModifier)):
# if "full" then call attack
self._attack()
# incremement the count
self.AttackAcount += 1
print "Next attack in %is" % (self.AttackAcount * self.BarRefillModifier))
unit = BaseUnit()
# loop indefinitely
while 1:
# check for bar progress
unit.checkBarProgress()
# only do this 5 times to show it working
if unit.AttackAcount == 5:
break
print "Battle over"
'''
OUTPUTS
KILL THEM ALL
Next attack is in 11s
KILL THEM ALL
Next attack is in 12s
KILL THEM ALL
Next attack is in 13s
KILL THEM ALL
Next attack is in 14s
KILL THEM ALL
Next attack is in 15s
Battle over
'''