View RSS Feed

infinity

Python text adventure game

Rate this Entry
Okay so I have been meaning to learn a programming language called python. Well one of the best ways to learn a new language is to make a game. So here is a game I worked on. Spent a couple days working on it and feel the source code is in a releasable state. Original source code can be found here https://balau82.wordpress.com/2010/0...ame-in-python/ for comparison.

I have the engine working good. The game has an objective and is winnable, Try to escape the room! Now I just gotta write a better story for the game, there isn't much of one in this example. But I had to get it running good before I can polish it. And besides coming up with a cool story is the fun part.

Feel free to use this source code in any of your projects it's released under gplv3. Hooray for free software. I just ask you try to keep the credits listed.

Major update:
Added a hunger/starve game play mechanic.
Added multiple endings.
Added an item inventory.
Added a ASCII intro.
Added preliminary code for a companion.
Huge code clean up.

Todo:
Make companions help during battle.
Expand the environment.
Work on the story.
Write better instructions.
Add unlock function.

Wishlist:
Add graphics and sound.

isolation.py version 0.1.9
Code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from random import randint


class Character:

    def __init__(self):
        self.name = ''
        self.health = 1
        self.health_max = 1

    def do_damage(self, enemy):
        damage = min(max(randint(0, self.health) - randint(0,
                     enemy.health), 0), enemy.health)
        enemy.health = enemy.health - damage
        if damage == 0:
            print "%s evades %s's attack.\nDamage: -%dhp" \
                % (enemy.name, self.name, damage)
        else:
            print '%s hurts %s!\nDamage: -%dhp' % (self.name,
                    enemy.name, damage)
        return enemy.health <= 0


class Enemy(Character):

    def __init__(self, player):
        Character.__init__(self)
        enemy_name1 = randint(0, 7)
        ex = randint(3, player.health_max)
        if enemy_name1 == 0:
            en = 'a demon'
            eh = ex + 3
        elif enemy_name1 == 1:
            en = 'a shadowy figure'
            eh = ex
        elif enemy_name1 == 2:
            en = 'some maggots'
            eh = ex - 1
        elif enemy_name1 == 3:
            en = 'a bloody nurse'
            eh = ex + 2
        elif enemy_name1 == 4:
            en = 'a masked killer'
            eh = ex + 3
        elif enemy_name1 == 5:
            en = 'an alien'
            eh = ex + 1
        elif enemy_name1 == 6:
            en = 'the cult leader'
            eh = ex + 4
        elif enemy_name1 == 7:
            en = 'a rabid dog'
            eh = ex + 1
        self.name = '%s' % en
        self.health = eh


class Player(Character):

    def __init__(self):
        Character.__init__(self)
        self.state = 'normal'
        self.health = 10
        self.health_max = 10
        self.hunger = 5
        self.hunger_max = 5
        self.area = 'room'
        self.food = 1
        self.items = []

    def starve(self):
        if self.hunger <= 0:
            print '%s starved to death!!!\nR.I.P.' % self.name
            self.health = 0
        elif self.hunger <= 2:
            print '%s feels hungry.' % self.name

    def look(self):
        self.starve()
        if self.state != 'normal':
            print '%s is too busy right now!' % self.name
            self.enemy_attacks()
        elif self.hunger > 0:
            if randint(0, 1):
                self.enemy = Enemy(self)
                print '%s tries to look but is attacked by %s!\nEnemy hp: %d.' \
                    % (self.name, self.enemy.name, self.enemy.health)
                self.state = 'fight'
            else:
                if self.area == 'room':
                    print '%s looks around and sees a bed, desk, window, and a door.\nWhere should %s go?' \
                        % (self.name, self.name)
                elif self.area == 'bed' and 'desk key' in self.items:
                    print '%s already found the desk key here.' \
                        % self.name
                elif self.area == 'bed':
                    print '%s ckecks the sheets and under the mattress. And finds a desk key!' \
                        % self.name
                    self.items.append('desk key')
                elif self.area == 'desk' and 'desk key' in self.items:
                    print '%s unlocks the desk drawer and finds a room key.' \
                        % self.name
                    self.items.remove('desk key')
                    self.items.append('room key')
                elif self.area == 'desk':
                    print '%s tries to unlock the drawer but does not have a desk key.' \
                        % self.name
                elif self.area == 'door' and 'room key' in self.items \
                    and 'puppy' in self.items:
                    print '%s unlocks the door with a room key!!!\n%s and the puppy survive!' \
                        % (self.name, self.name)
                    self.health = 0
                elif self.area == 'door' and 'room key' in self.items:
                    print '%s unlocks the door with a room key!\n%s survives!' \
                        % (self.name, self.name)
                    self.health = 0
                elif self.area == 'door':
                    print '%s tries to unlock the door but does not have a room key.' \
                        % self.name
                elif self.area == 'window' and 'puppy' in self.items:
                    print '%s already saved the puppy that was here.' \
                        % self.name
                elif self.area == 'window' and self.food <= 0:
                    print '%s looks under the window and sees a starving puppy but %s has no food to give it.' \
                        % (self.name, self.name)
                elif self.area == 'window' and self.food >= 1:
                    print '%s sees a starving puppy and gives it some food.\nThe puppy starts following %s.' \
                        % (self.name, self.name)
                    self.items.append('puppy')
                    self.food = self.food - 1
                else:
                    print 'Something went horribly wrong'

    def bed(self):
        self.starve()
        if self.state != 'normal':
            print '%s is too busy right now!' % self.name
            self.enemy_attacks()
        elif self.hunger > 0:
            if self.area == 'bed':
                print '%s is already at the bed. Try looking, or going somewhere else.' \
                    % self.name
            else:
                print '%s approaches the bed and sees a queen size bed with blood soaked sheets.' \
                    % self.name
                self.area = 'bed'
                self.hunger = self.hunger - 1
                if randint(0, 1):
                    self.enemy = Enemy(self)
                    print '%s encounters %s!\nEnemy hp: %d.' \
                        % (self.name, self.enemy.name,
                           self.enemy.health)
                    self.state = 'fight'
                else:
                    if randint(0, 1):
                        self.tired()

    def room(self):
        self.starve()
        if self.state != 'normal':
            print '%s is too busy right now!' % self.name
            self.enemy_attacks()
        elif self.hunger > 0:
            if self.area == 'room':
                print '%s is already in the center of the room. Try looking, or going somewhere else.' \
                    % self.name
            else:
                print '%s looks around and sees a bed, desk, window, and a door.' \
                    % self.name
                self.area = 'room'
                self.hunger = self.hunger - 1
                if randint(0, 1):
                    self.enemy = Enemy(self)
                    print '%s encounters %s!\nEnemy hp: %d.' \
                        % (self.name, self.enemy.name,
                           self.enemy.health)
                    self.state = 'fight'
                else:
                    if randint(0, 1):
                        self.tired()

    def desk(self):
        self.starve()
        if self.state != 'normal':
            print '%s is too busy right now!' % self.name
            self.enemy_attacks()
        elif self.hunger > 0:
            if self.area == 'desk':
                print '%s is already at the desk. Try looking, or going somewhere else.' \
                    % self.name
            else:
                print '%s approaches the desk and sees a drawer on the desk.' \
                    % self.name
                self.area = 'desk'
                self.hunger = self.hunger - 1
                if randint(0, 1):
                    self.enemy = Enemy(self)
                    print '%s encounters %s!\nEnemy hp: %d.' \
                        % (self.name, self.enemy.name,
                           self.enemy.health)
                    self.state = 'fight'
                else:
                    if randint(0, 1):
                        self.tired()

    def window(self):
        self.starve()
        if self.state != 'normal':
            print '%s is too busy right now!' % self.name
            self.enemy_attacks()
        elif self.hunger > 0:
            if self.area == 'window':
                print '%s is already at the window. Try looking, or going somewhere else.' \
                    % self.name
            else:
                print "%s approaches the window and noticed it's locked with thick steel bars.\nThere is something moving under the window." \
                    % self.name
                self.area = 'window'
                self.hunger = self.hunger - 1
                if randint(0, 1):
                    self.enemy = Enemy(self)
                    print '%s encounters %s!\nEnemy hp: %d.' \
                        % (self.name, self.enemy.name,
                           self.enemy.health)
                    self.state = 'fight'
                else:
                    if randint(0, 1):
                        self.tired()

    def door(self):
        self.starve()
        if self.state != 'normal':
            print '%s is too busy right now!' % self.name
            self.enemy_attacks()
        elif self.hunger > 0:
            if self.area == 'door':
                print '%s is already at the door. Try looking, or going somewhere else.' \
                    % self.name
            else:
                print '%s approachesthe door and tries to open the door but its locked.' \
                    % self.name
                self.area = 'door'
                self.hunger = self.hunger - 1
                if randint(0, 1):
                    self.enemy = Enemy(self)
                    print '%s encounters %s!\nEnemy hp: %d.' \
                        % (self.name, self.enemy.name,
                           self.enemy.health)
                    self.state = 'fight'
                else:
                    if randint(0, 1):
                        self.tired()

    def eat(self):
        if self.state != 'normal':
            print "%s can't eat now!" % self.name
            self.enemy_attacks()
        else:
            if self.food > 0:
                if self.hunger < self.hunger_max:
                    self.hunger = self.hunger + 1
                    self.food = self.food - 1
                    print "%s eat's some food." % self.name
                    if self.hunger > self.hunger_max:
                        self.hunger = self.hunger_max
                else:
                    print '%s is not hungry.' % self.name
            else:
                print '%s has no food.' % self.name

    def quit(self):
        print "%s can't find the way out of this room, and dies of starvation.\nR.I.P." \
            % self.name
        self.health = 0

    def help(self):
        print Commands.keys()

    def status(self):
        print "%s's status ['health: %d/%d', 'hunger: %d/%d', 'location: %s', 'food: %d']" \
            % (
            self.name,
            self.health,
            self.health_max,
            self.hunger,
            self.hunger_max,
            self.area,
            self.food,
            )

    def items(self):
        if not self.items:
            print '%s has no items right now.' % self.name
        else:
            print "%s's items: %s" % (self.name, self.items)

    def tired(self):
        print '%s feels tired.' % self.name
        self.health = max(1, self.health - 1)

    def rest(self):
        if self.state != 'normal':
            print "%s can't rest now!" % self.name
            self.enemy_attacks()
        else:
            print '%s rests.' % self.name
            if randint(0, 1):
                self.enemy = Enemy(self)
                print '%s is rudely awakened by %s!\nEnemy hp: %d.' \
                    % (self.name, self.enemy.name, self.enemy.health)
                self.state = 'fight'
                self.enemy_attacks()
            else:
                if self.health < self.health_max:
                    self.health = self.health + 1
                else:
                    print '%s slept too much.' % self.name
                    self.health = self.health - 1
                    self.hunger = self.hunger - 1

    def flee(self):
        if self.state != 'fight':
            print '%s runs in circles for a while.' % self.name
            self.hunger = self.hunger - 1
            self.tired()
            self.starve()
        else:
            if randint(1, self.health + 5) > randint(1,
                    self.enemy.health):
                print '%s flees from %s.' % (self.name, self.enemy.name)
                self.enemy = None
                self.state = 'normal'
                self.hunger = self.hunger - 1
            else:
                print "%s couldn't escape from %s!" % (self.name,
                        self.enemy.name)
                self.enemy_attacks()

    def attack(self):
        if self.state != 'fight':
            print '%s swats the air, without notable results.' \
                % self.name
            self.hunger = self.hunger - 1
            self.tired()
            self.starve()
        else:
            if self.do_damage(self.enemy):
                print '%s executes %s!' % (self.name, self.enemy.name)
                self.enemy = None
                self.state = 'normal'
                efood = randint(0, 1)
                self.food = efood + self.food
                print '%s recovered %d food from the corpse.' \
                    % (self.name, efood)
                if randint(0, self.health) < 10:
                    self.health = self.health + 1
                    self.health_max = self.health_max + 1
                    print '%s feels stronger!' % self.name
            else:
                self.enemy_attacks()

    def enemy_attacks(self):
        if self.enemy.do_damage(self):
            print '%s was slaughtered by %s!!!\nR.I.P.' % (self.name,
                    self.enemy.name)


Commands = {
    'quit': Player.quit,
    'help': Player.help,
    'status': Player.status,
    'rest': Player.rest,
    'flee': Player.flee,
    'attack': Player.attack,
    'bed': Player.bed,
    'look': Player.look,
    'room': Player.room,
    'desk': Player.desk,
    'door': Player.door,
    'window': Player.window,
    'eat': Player.eat,
    'items': Player.items,
    }

p = Player()
print ' _           _       _   _             '
print '(_)___  ___ | | __ _| |_(_) ___  _ __  '
print "| / __|/ _ \| |/ _` | __| |/ _ \| '_ \ "
print "| \__ \ (_) | | (_| | |_| | (_) | | | |"
print "|_|___/\___/|_|\__,_|\__|_|\___/|_| |_|\n"
p.name = raw_input("What is your character's name? ")
print '(type help to get a list of actions)\n'
print '%s wakes up on the floor of a strange room. Starving to death.' \
    % p.name
print "%s has to get out of here before it's to late." % p.name
print '%s looks around and sees a bed, desk, window, and a door.' \
    % p.name
print 'Where should %s go?' % p.name

while p.health > 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])]:
                Commands[c](p)
                commandFound = True
                break
        if not commandFound:
            print "%s doesn't understand the suggestion." % p.name

"""
Kevin Lindsay 2016
Maxwell Phelps 2013
Francesco Balducci 2010 
 
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
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
 
See <http://www.gnu.org/licenses/> for a copy of the GNU General Public License.
"""

Submit "Python text adventure game" to Digg Submit "Python text adventure game" to del.icio.us Submit "Python text adventure game" to StumbleUpon Submit "Python text adventure game" to Google Submit "Python text adventure game" to Facebook

Categories
Linux

Comments