Skip to content

Instantly share code, notes, and snippets.

@darkf
Created May 18, 2012 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save darkf/2727462 to your computer and use it in GitHub Desktop.
Save darkf/2727462 to your computer and use it in GitHub Desktop.
Terrible text-based RPG
import random
def say(msg):
print ":", msg
def rnd(min, max):
return random.randint(min, max)
class Player:
def __init__(self):
self.stats = {"hp": 10, "mp": 10, "ap": 5, "agi": 2, "atk": 1}
self.hp = self.stats["hp"]
self.where = "Gomura"
self.inFight = False
self.inventory = []
self.weapon = Weapon("Rusty Dagger", {"atk":1})
self.enemy = None
def attack(self):
if not self.inFight:
say("You swing aimlessly")
return
atk = self.stats["atk"] + self.weapon.stats["atk"]
if rnd(1, self.enemy.stats["agi"] - atk):
say("You hit for %d dmg" % atk)
else:
say("You miss.")
def walk(self):
if self.inFight:
say("You can't walk away from a fight!")
return
self.enemy = Bee()
self.inFight = True
say("A bee shows up for a fight.")
class Enemy:
def __init__(self):
self.stats = {}
self.dead = False
class Bee(Enemy):
def __init__(self):
self.stats = {"hp": rnd(1, 10), "ap": 10, "agi": 10, "atk": 1}
self.hp = self.stats["hp"]
self.dead = False
def damage(self, amount=1):
self.hp -= amount
if self.hp <= 0:
self.dead = True
class Place:
def enter(self): pass
def walk(self): pass
def handleCommand(self, cmd): return False
class Weapon:
def __init__(self, name, stats):
self.stats = stats
def doShop():
inventory = {"Greatsword": Weapon("Greatsword", {"atk": 10})}
while True:
say("What would you like to purchase?")
for k,v in inventory.iteritems():
say("%s: %r" % (k,v))
x = raw_input(">> ")
if x in inventory:
player.inventory.append(inventory[x])
say("Thank you for shopping!")
return
class Gomura(Place):
def enter(self):
say("Welcome to Gomura. There's an inn here, and a shop.")
def handleCommand(self, cmd):
if cmd[0] == "shop":
say("You walk into the shop.")
doShop()
return True
return False
class Freever(Place):
def enter(self):
say("You're in Freever Forest. It looks creepy.")
def handleCommand(self, cmd):
if cmd[0] == "walk":
player.walk()
return True
elif cmd[0] == "attack":
if player.inFight:
player.attack()
else:
say("You're not in a fight!")
return True
return False
player = Player()
PLACES = {"Gomura": Gomura(), "Freever": Freever(), "Wallhold": Place()}
def travelTo(place):
if place not in PLACES:
say("That isn't a place!")
player.where = PLACES[place]
player.where.enter()
def parseCommand(line):
return line.split(" ")
def main():
global player
player.where = PLACES["Gomura"]
player.where.enter()
while True:
ln = parseCommand(raw_input("> "))
if ln[0] == "go" or ln[0] == "goto":
if player.inFight:
say("You can't leave in a fight!")
else:
travelTo(ln[1])
elif ln[0] == "exit" or ln[0] == "quit":
say("Goodbye, traveler.")
return
else:
if not player.where.handleCommand(ln):
say("Unknown command")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment