Skip to content

Instantly share code, notes, and snippets.

@kchang2
Created December 31, 2016 21:09
Show Gist options
  • Save kchang2/1c92745c5b3f27af6c0cb634e54059bb to your computer and use it in GitHub Desktop.
Save kchang2/1c92745c5b3f27af6c0cb634e54059bb to your computer and use it in GitHub Desktop.
Simple object creation in Python using a 2 player battle simulator as an example.
'''
A rather lengthy battle combat between two characters without rehealing properties.
Leveling up does not count as a recovery property.
'''
import numpy as np
class character(object):
'''
A character for a possible game structure.
Note the character class has certain "features" (ie. name, exp, health),
and each of these features MINUS the name are inherently set. This example shows
you CANNOT customize the health, att/def stats, etc, while showing that there is no
inherent name set if you do not set it when you initialize a character.
self. is used within the class because it knows it's own information, but outside of
the class, the variables inside (or features) are private (just like methods), so you wil
need a get and set function to do such outside of the class structure.
Each instance is an object dervied from the character class, and will contain all features and methods.
'''
def __init__(self, name):
self.name = name
self.exp = 0.00
self.ceilexp = 50
self.hp = 100
self.a = 10
self.d = 10
self.crit = 0.05
def getname(self):
return self.name
def gethp(self):
return self.hp
def sethp(self, newhp):
self.hp = newhp
def setad(self, newa, newd):
self.a = newa
self.d = newd
def getad(self):
return self.a, self.d
def getcrit(self):
return self.crit
def setcrit(self, newcrit):
self.crit = newcrit
def getexp(self):
return self.exp
def setexp(self, newexp):
self.exp = newexp
def isDed(self):
if self.hp < 0:
return True
return False
def gen_att(self):
'''
Generate poisson distribution centered around att,
we choose poisson because we believe the battle should be offense driven.
'''
if np.random.uniform() < self.crit:
crit = 1.85
else:
crit = 1.0
val = self.a
while val > 0.25 * self.a:
val = crit * np.random.poisson(self.a / 5.00)
return val
def gen_def(self):
'''
Generates gaussian distribution centered around def,
we choose normal distribution because the battle should be more offensive driven.
'''
coef = np.random.poisson(0.2)
if coef == 0:
coef = 0.2
def_val = np.random.normal(loc=float(self.d / 5.00), scale=coef * float(self.d / 5.00) )
return def_val
def LevelUp(self):
'''
Appropriate leveling. We do a simple scaled experience ceiling.
In a more complex system, we would have a more exponential ceiling scale.
'''
if self.exp > 0 and self.exp / self.ceilexp > 1:
self.hp += 10
self.a += 1
self.d += 1
self.crit += 0.001
self.ceilexp += 50
def printstat(self):
return self.a, self.d, self.exp, self.crit
def sim_battle(c1, c2):
c1max = 0
c2max = 0
while not c1.isDed() and not c2.isDed():
# generate c1, c2 attacks and def
c1a, c1d = c1.gen_att(), c1.gen_def()
c2a, c2d = c2.gen_att(), c2.gen_def()
# generates exp gained
c1exp = max(0, c1a - c2d)
c2exp = max(0, c2a - c1d)
if c1exp > c1max:
c1max = c1exp
if c2exp > c2max:
c2max = c2exp
# update results
c1.setexp(c1.getexp() + c1exp)
c2.setexp(c2.getexp() + c2exp)
c1.sethp(c1.gethp() - c2exp)
c2.sethp(c2.gethp() - c1exp)
c1.LevelUp()
c2.LevelUp()
# print c1.getname(), ' hp: ', c1.gethp(), ' exp: ', c1.getexp(), ' att: ', c1exp, (c1a, c1d)
# print c2.getname(), ' hp: ', c2.gethp(), ' exp: ', c2.getexp(), ' att: ', c2exp, (c2a, c2d)
# someone was defeated
if c1.isDed() and not c2.isDed():
print c2.getname(), ' is the victor'
elif c2.isDed() and not c1.isDed():
print c1.getname(), ' is the victor'
else:
print 'Both fighters are equally matched'
print 'Final stats'
print c1.getname(), c1.printstat(), c1max
print c2.getname(), c2.printstat(), c2max
# creates 2 characters
c1 = character('Bob')
c2 = character('Joe')
# simulates the battle
sim_battle(c1, c2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment