コード例 #1
0
ファイル: textbasedgame.py プロジェクト: V1Soft/textbasedgame
def loadGame(name=None):
    global usr, usrFile, previousVendor
    users = []
    for file in os.listdir(utils.fileDir + '/saves'):
        if file.endswith('.save') or file.endswith('.db'): 
            users.append(file.split('.')[0])
    try:
        usr = utils.choose('List of users:', users, 'What is your username?')
        usrFile = usr + '.save'
    except KeyboardInterrupt:
        play()
    try:
        entities.worldEntities = utils.loadInfo(usr, 'worldEntities')
        entities.player = utils.loadInfo(usr, 'player.' + usr)
        previousVendor = utils.loadInfo(usr, 'previousVendor')
    except KeyError:
        print('Savefile is broken. Creating new savefile...')
        newGame(usr)
    print('Game save loaded.')
    try:
        if entities.player.location == entities.getLocation('Inventory'):
            locations.inventory()
        elif entities.player.location == entities.getLocation('Market'):
            utils.goToVendor(previousVendor)
        elif entities.player.location == entities.getLocation('Interact'):
            utils.fight(entities.player.location.entity, entities.player.location.entity.weapon)
            inventory()
        else:
            commandLine()
    except KeyboardInterrupt or EOFError:
        sys.exit(1)
コード例 #2
0
ファイル: locations.py プロジェクト: V1Soft/textbasedgame
def market():
    entities.player.location = entities.getLocation('Market')
    print('''
+-----------------------------------------------------+
| Welcome to the Market!                              |
| Type an item\'s name to purchase it.                 |
| Type "info <item>" for more information on an item. |
| Type "exit" to leave the store.                     |
+-----------------------------------------------------+''')
    isVendor = False
    while not isVendor:
        vendors = []
        for vendor in entities.vendors:
            vendors.append(vendor.name)
        command = utils.choose('\nPlease type the vendor you want to visit.', vendors)
        for vendor in entities.vendors:
            if vendor.name == command:
                vendorToVisit = vendor
                isVendor = True
                break

        if command == 'exit':
            print('You left the store.')
            return
        else:
            print('Vendor or command not found.')
            break
    utils.goToVendor(vendorToVisit)
コード例 #3
0
ファイル: utils.py プロジェクト: V1Soft/textbasedgame
def goToVendor(vendor):
    global previousVendor
    previousVendor = vendor
    entities.player.location = entities.getLocation('Market')
    entities.player.location.entity = vendor
    print('%s\nItems for sale:' % vendor.message)
    vendor.say(vendor.goods)
    while True:
        command = input('Market > %s : ' % vendor.name).split(' ')
        thingToBuy = None
        buying = False
        for good in vendor.goods:
            if good.name == command[0]:
                thingToBuy = good
                buying = True
                break
        if buying:
            entities.player.inventory.append(thingToBuy)
            entities.player.spend(thingToBuy.cost)
            print('%s purchased for %s money.' % (thingToBuy.name, thingToBuy.cost))
        elif command[0].upper() == 'INFO':
            thingToGetInfoOn = command[1]
            itemInShop = False
            for item in vendor.goods:
                if item.name == thingToGetInfoOn:
                    itemInShop = True
                    break
            if not itemInShop:
                print('Item not found.')
            else:
                if isinstance(item, obj.Weapon):
                    print('Power: %s' % item.power)
                elif isinstance(item, obj.Food):
                    print('Healing power: %s' % item.hp)
                print('Description: ' + item.description)
        elif command[0].upper() == 'EXIT':
            print('You left the store.')
            entities.player.location.entity = entities.getLocation('Main')
            return
        elif command[0].upper() == 'HELP':
            entities.getHelpMsg('Market').printMsg()
        elif command[0].upper() == 'MONEY':
            print(entities.player.money + ' coins')
        else:
            print('Command not found.')
コード例 #4
0
ファイル: textbasedgame.py プロジェクト: V1Soft/textbasedgame
def newGame(name=None):
    global usr, usrFile
    entities.worldEntities = []
    if name != None:
        usr = name
    else:
        try:
            usr = input('What is your desired username? : ')
            usrFile = usr + '.save' # Add extension
        except KeyboardInterrupt:
            play()
    entities.player = obj.Player(usr, 100, 100, float(5))
    entities.player.inventory = [entities.getWeapon('stick'), entities.getFood('potato')]
    entities.player.location = entities.getLocation('Main')
    print('New game set up. Welcome.')
    commandLine()
コード例 #5
0
ファイル: locations.py プロジェクト: V1Soft/textbasedgame
def personInteraction():
    entities.player.location = entities.getLocation('Interact')
    personType = random.randint(1, 3)
    if personType == 1:
        person = [random.choice(entities.enemies), random.choice(entities.weapons)]
        if utils.confirm('You see a mean-looking person in the distance. Do you choose to approach?'):
            utils.fight(person[0], person[1])
        else:
            print('You run away in fear.')
    elif personType == 2:
        if entities.worldEntities:
            person = random.choice(entities.worldEntities)
            person.inventory.append(random.choice(entities.weapons))
            if utils.confirm('You see a familiar, mean-looking person in the distance. Do you choose to approach?'):
                utils.fight(person, person.inventory[0])
            else:
                print('You run away in fear.')
        else:
            person = random.choice(entities.enemies)
            person.inventory.append(random.choice(entities.weapons))
            if utils.confirm('You see a mean-looking person in the distance. Do you choose to approach?'):
                utils.fight(person, person.inventory[0])
            else:
                print('You run away in fear.')
    else:
        person = [random.choice(entities.helpers), random.choice(entities.helperItems)]
        if utils.confirm('You see a kind-looking person in the distance. Do you choose to approach?'):
            print('The person is a(n) ' + person[0].name + '!')
            if person[0] == entities.getHelper('old lady'):
                if random.randint(0,1) == 0:
                    utils.fight(entities.getEnemy('old lady'), entities.getWeapon('cane'))
                else:
                    time.sleep(0.5)
                    print('The %s smiles and holds a(n) %s out in her hand.' % (person[0].name, person[1].name))
                    entities.player.inventory.append(person[1])
                    time.sleep(0.2)
                    print(person[1].name + ' added to your inventory!')
            else:
                time.sleep(0.5)
                print('The %s smiles and holds a(n) %s out in her hand.' % (person[0].name, person[1].name))
                entities.player.inventory.append(person[1])
                time.sleep(0.2)
                print(person[1].name + ' added to your inventory!')
        else:
            print('You walk away')
            time.sleep(2)
コード例 #6
0
ファイル: locations.py プロジェクト: V1Soft/textbasedgame
def inventory():
    entities.player.location = entities.getLocation('Inventory')
    while True:
        command = input('Inventory : ').split(' ')
        if command[0] == '?' or command[0].upper() == 'HELP':
            entities.getHelpMsg('Inventory').printMsg()
        elif command[0].upper() == 'LIST':
            if len(command) > 1:
                if command[1].upper() == 'WEAPONS':
                    utils.listItems(listedItems=entities.player.inventory, objType=obj.Weapon)
                elif command[1].upper() == 'FOOD':
                    utils.listItems(listedItems=entities.player.inventory, objType=obj.Food)
                elif command[1].upper() == 'HEALTH':
                    print(entities.player.health)
                elif command[1].upper() == 'MONEY':
                    print(entities.player.money)
                else:
                    print('Usage: list\nlist <type>')
            else:
                utils.listItems(listedItems=entities.player.inventory)
        elif command[0].upper() == 'EAT':
            failed = False
            for item in entities.player.inventory:
                if item.name.upper() == command[1].upper():
                    if isinstance(item, obj.Food):
                        entities.player.inventory.remove(item)
                        entities.player.health += item.hp
                        print('%s points added to health!' % item.hp)
                        failed = False
                        break
            if failed:
                print('Food not in Inventory.')

        elif command[0].upper() == 'EXIT':
            print('You left your Inventory.')
            break

        else:
            print('Inventory command "' + command[0] + '" not found. Type "help" for help.')