Simplest RPG, in Python

Posted on 2010/06/28


This is a small text-based Role-Playing Game that I wrote while learning Python. The game is the simplest RPG adventure I could think of, that maintains the essential traits of the classics.

The more I dive into Python the more I understand the work done to create a language that is easy by design. I tried to apply some non-trivial functionalities such as:

  • Class inheritance (Enemy and Player inherit from Character)
  • Function pointers (Commands contains a dictionary of functions)
  • String manipulation (creating output and parsing input)

I think that this game can be a clean example to understand how to apply these aspects in a Python program.

To play the game

  1. Install Python (installed by default in many Linux distributions, see for other platforms)
  2. Copy the following code into a file called (for example)
  3. In Linux, run “python” from the command line in the directory of the file. In Windows, open the file with Python.
  4. Enjoy!
  5. Quit and get a life.
from random import randint

class Character:
  def __init__(self): = "" = 1
    self.health_max = 1
  def do_damage(self, enemy):
    damage = min(
        max(randint(0, - randint(0,, 0), = - damage
    if damage == 0: print "%s evades %s's attack." % (,
    else: print "%s hurts %s!" % (,
    return <= 0

class Enemy(Character):
  def __init__(self, player):
    Character.__init__(self) = 'a goblin' = randint(1,

class Player(Character):
  def __init__(self):
    self.state = 'normal' = 10
    self.health_max = 10
  def quit(self):
    print "%s can't find the way back home, and dies of starvation.\nR.I.P." % = 0
  def help(self): print Commands.keys()
  def status(self): print "%s's health: %d/%d" % (,, self.health_max)
  def tired(self):
    print "%s feels tired." % = max(1, - 1)
  def rest(self):
    if self.state != 'normal': print "%s can't rest now!" %; self.enemy_attacks()
      print "%s rests." %
      if randint(0, 1):
        self.enemy = Enemy(self)
        print "%s is rudely awakened by %s!" % (,
        self.state = 'fight'
        if < self.health_max:
 = + 1
        else: print "%s slept too much." %; = - 1
  def explore(self):
    if self.state != 'normal':
      print "%s is too busy right now!" %
      print "%s explores a twisty passage." %
      if randint(0, 1):
        self.enemy = Enemy(self)
        print "%s encounters %s!" % (,
        self.state = 'fight'
        if randint(0, 1): self.tired()
  def flee(self):
    if self.state != 'fight': print "%s runs in circles for a while." %; self.tired()
      if randint(1, + 5) > randint(1,
        print "%s flees from %s." % (,
        self.enemy = None
        self.state = 'normal'
      else: print "%s couldn't escape from %s!" % (,; self.enemy_attacks()
  def attack(self):
    if self.state != 'fight': print "%s swats the air, without notable results." %; self.tired()
      if self.do_damage(self.enemy):
        print "%s executes %s!" % (,
        self.enemy = None
        self.state = 'normal'
        if randint(0, < 10:
 = + 1
          self.health_max = self.health_max + 1
          print "%s feels stronger!" %
      else: self.enemy_attacks()
  def enemy_attacks(self):
    if self.enemy.do_damage(self): print "%s was slaughtered by %s!!!\nR.I.P." %(,

Commands = {
  'quit': Player.quit,
  'status': Player.status,
  'explore': Player.explore,
  'flee': Player.flee,
  'attack': Player.attack,

p = Player() = raw_input("What is your character's name? ")
print "(type help to get a list of actions)\n"
print "%s enters a dark cave, searching for adventure." %

while( > 0):
  line = raw_input("> ")
  args = line.split()
  if len(args) > 0:
    commandFound = False
    for c in Commands.keys():
      if args[0] == c[:len(args[0])]:
        commandFound = True
    if not commandFound:
      print "%s doesn't understand the suggestion." %

Copyright 2010 Francesco Balducci

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.

See <> for a copy of the GNU General Public License.
Tagged: , , ,
Posted in: Software