#!/usr/bin/python """ Untitled RPG system by Kris Schnee based on rules by D. Hazelton and others. Draft. """ ##___________________________________ ## ~Header~ ##___________________________________ __author__ = "Kris Schnee" __version__ = "2008.5.31" __license__ = "Public Domain" ##___________________________________ ## ~Imported Modules~ ##___________________________________ ## Standard import random ## Third-party ## Mine ##___________________________________ ## ~Constants~ ##___________________________________ ##___________________________________ ## ~Classes~ ##___________________________________ class Entity: """An entity such as a person or object. Properties: -Name (no game function) -Description (no game function) -Desc (no game function): Short description. Truncated from description if blank. -World: A World object in which I exist. Stats: Physical: Strength: Physical power Dexterity: Physical agility Constitution: Physical endurance Mental: Intelligence: Mental power Wistom: Mental agility Will: Mental endurance """ def __init__(self,**options): self.name = options.get("name","Entity") self.description = options.get("description","") self.desc = options.get("desc",self.description[:30]+"...") self.world = options.get("world",None) ## Core Stats ## Physical self.strength = options.get("strength",9) self.dexterity = options.get("dexterity",9) self.constitution = options.get("constitution",9) ## Mental self.intelligence = options.get("intelligence",9) self.wisdom = options.get("wisdom",9) self.will = options.get("will",9) class World: """A game setting. Properties: -Name (no game function) -Entities: A set of Entity objects or subclasses thereof. Functions: -Add -Remove -List """ def __init__(self,**options): self.name = options.get("name","World") self.entities = [] def Add(self,entity): """Place entity in world.""" if not entity in self.entities: self.entities.append(entity) entity.world = self else: raise "Entity is already in world." def Remove(self,entity): """Remove entity from world.""" if entity in self.entities: self.entities.remove(entity) entity.world = None else: raise "Entity isn't in the world." def List(self): """Get a formatted list of all entities in the world.""" text = " --- "+self.name+" ---" for entity in self.entities: text += "\n"+entity.name+": "+entity.desc return text ##___________________________________ ## ~Functions~ ##___________________________________ def Roll(dice=3,size=6): """Roll this many dice of this size, eg. 3d6; return the total.""" total = 0 for n in range(dice): total += random.randint(1,size) return total def Test(entity,stat,dice=3,size=6): """Test an entity's stat versus this die roll.""" try: stat = getattr(entity,stat) except: raise "Entity doesn't have that stat." n = Roll(dice,size) if n <= stat: return True else: return False ##__________________________________ ## ~Autorun~ ##__________________________________ if __name__ == "__main__": w = World(name="Meta: The Subclassing") grignr = Entity(name="Grignr", description="A mighty barbarian from the land of Ecordia, in search of grog, wenches, and booty.", desc="A mighty barbarian.", strength=15, intelligence=5, ) w.Add(grignr) print w.List() """ [Demo result:] --- Meta: The Subclassing --- Grignr: A mighty barbarian. """