Giter VIP home page Giter VIP logo

adventurelib's Introduction

adventurelib

Build Documentation Status

adventurelib provides basic functionality for writing text-based adventure games, with the aim of making it easy enough for young teenagers to do.

The foundation of adventurelib is the ability to define functions that are called in response to commands. For example, you could write a function to be called when the user types commands like "take hat":

@when('take THING')
def take(thing):
    print(f'You take the {thing}.')
    inventory.append(thing)

It also includes the foundations needed to write games involving rooms, items, characters and more... but users will have to implement these features for themselves as they explore Python programming concepts.

Installing

adventurelib.py is a single file that can be copied into your project. You can also install it with pip:

pip install adventurelib

Documentation

Comprehensive documentation is on Read The Docs.

adventurelib's People

Contributors

adrianogil avatar flosincapite avatar horstjens avatar keyweeusr avatar kmlawson avatar lordmauve avatar nephyx avatar tjguk avatar tomviner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

adventurelib's Issues

`cast MAGIC` command does not work in demo_game.py

> north
You go north.
You are in a enchanted forest where magic grows wildly.

> help
Here is a list of the commands you can give:
?
cast
drop THING
east
help
inventory
look
north
quit
south
take ITEM
west

> cast
Which magic you would like to spell?

> cast fireball
I don't understand 'cast fireball'.

For some reason, "cast" works but "cast fireball" is not recognized.

Windows Support

Currently the project doesn't seem to run correctly on Windows.
(Windows 10 version 1803 | Python 3.7)

H:\Git\adventurelib>python demo_game.py
Traceback (most recent call last):
File "demo_game.py", line 1, in <module>
    from adventurelib import *
File "H:\Git\adventurelib\adventurelib.py", line 4, in <module>
    import readline  # noqa: adds readline semantics to input()
ModuleNotFoundError: No module named 'readline'

The readline module is not supported for Windows and is depreciated.

H:\Git\adventurelib>pip install readline

Collecting readline
Using cached https://files.pythonhosted.org/packages/f4/01/2cf081af8d880b44939a5f1b446551a7f8d59eae414277fd0c303757ff1b/readline-6.2.4.1.tar.gz

Complete output from command python setup.py egg_info:
error: this module is not meant to work on Windows

On the PyPI website, it states: WARNING: THIS PACKAGE IS DEPRECATED! It has been renamed to GNUREADLINE to resolve a name clash with the standard library module.

When trying to install the new suggested replacement (GNUREADLINE), it says it is not supported for Windows and to try pyreadline instead.

Finally, I installed pyreadline which installed and then the demo game ran.

Can someone please provide screenshots of how the demo game runs on Linux as I think it may not be functioning correctly, thanks.

Allow adventurelib games to be built to run in the browser

In order to distribute adventurelib games, it would be convenient to be able to build them into a single HTML file that can be hosted on a static webserver or e-mailed to friends.

The idea would be to use one of the several browser-based Python implementations to run the code or transform it to Javascript. While the compatibility of these implementations may not be perfect, I would expect that many adventurelib games are simple enough to run without issue.

@flexo made a start on this at Pycon UK - work is at iguanaonmystack/adventurelib@cd0d0db

Rooms cannot have attributes that are Rooms

Currently a Room treats any attempt to assign a Room object as an attribute as an attempt to create a bi-directional relationship with another Room.

It should be possible to have relationships between Rooms that are not navigation directions, or at least, not bi-directional.

Add a system to limit commands to a specific context

Some commands should only be available in a certain context. This would avoid needing to write many if checks at the start of each handler.

An API might be

adventurelib.set_context('limbo')

plus

@when('give ITEM', context='limbo')

One could also imagine more sophisticated ways of matching contexts - eg. hierarchical:

adventurelib.set_context('conversation.jerry')

plus

@when('ask about COLOUR shell', context='conversation')
def ask_shell(colour):
    say("I don't know about that")

@when('ask about red shell', context='conversation.samantha')
def ask_shell(colour):
    say("I have one of those too!")

Translation engine (global variables in external module?)

I've been looking for a similar project for years. A fantastic job, simple, easy to extend, and written in python! I love this library.

I was thinking that maybe it's a good idea to create external modules with global variables for the translation of the output of the engine. simply recall and use only the module for the desired language and the output will be translated. Obviously it will be enough to include a template module for the translation of the various languages.

Although languages such as Russian, Chinese, Japanese ... at the moment can not be used, I think it is a good solution for all European languages such as German, French, Italian, Spanish etc.

What do you think about it? I'm experimenting with a simple and effective solution.

Audio mode

A modern approach to a text adventure game might be to use voice recognition and speech synthesis to converse with the game.

This could be via Python TTS APIs or putting adventurelib behind an Alexa skill/Google Home Action.

Example code for a similar project is at
https://github.com/lordmauve/brexit-alexa

[Feature Request] TAB Completion

Greetings!!

The following project is awesome, but there is a feature missing here. It's TAB completion for the game. I would really love to see it's existence. Thanks!

--

with love
naryal2580

gui version

A while ago I started work on a GUI fork of adventurelib. It works but it's not suitable for merging because it replaces adventurelib code. Obviously the correct thing to do would be to put the text version code in one module and the GUI code in another module and the shared code in a third module. But I never had time to do that so it's just sat on my hard drive. Anyway I found it again and thought I'd put it online in case it is useful to anyone. https://github.com/electronstudio/adventurelib_gui

Error

We noticed an error on your adventurelib foundation page. Under bags of items, there is this take command:
@when('take ITEM')
def take(item):
obj = current_room.take(item)
if not obj:
print('There is no %s here.' % item)
else:
inventory.add(item)
print('You take the %s.' % obj)
Instead of obj = current_room.take(item), it should be obj = current_room.items.take(item).
We hope you take this into consideration.
Thank you.

Output colors with say

Games cry out for color / format options. Perhaps use the following API with say:

say(something, fg="red", bg="white")

There is colorama that would help implement this, but if you wanted to keep it so that users could just use adventurelib.py in their own projects, maybe have to implement ourselves?

Room lets you use exit as a direction even though it is a method

Whenever I do 'north', 'east', 'west' or 'south', the game crashes with an error:

Traceback (most recent call last):
  File "C:\Users\84367\Desktop\main.py", line 81, in <module>
    start()
  File "G:\Python\lib\site-packages\adventurelib.py", line 536, in start
    _handle_command(cmd)
  File "G:\Python\lib\site-packages\adventurelib.py", line 509, in _handle_command
    func(**args)
  File "C:\Users\84367\Desktop\main.py", line 53, in go
    room = current_room.exit(direction)
TypeError: 'NoneType' object is not callable

@when decorator fails to catch duplicate identifiers

This kind of usage will not cause an exception in adventurelib; it will match and simply throw away a term:

@when('use ITEM on ITEM') 
def f(item): 
    pass

Instead an exception should be raised when defining a function with a name collision like this.

@when does not play nicely with other decorators.

I would like to be able to do something like this:

@adventurelib.when("command")
@other_decorator
def command():
    pass

If I want other_decorator to capture the args passed to command for some reason, I have to implement other_decorator like this:

def other_decorator():
    def dec(func):
        def wrapped(*args, **kwargs):
            # do something with args, kwargs
            return func(*args, **kwargs)
        return wrapped
    return dec

However, adventurelib.when prevents this, since the signature of wrapped(*args, **kwargs) differs from the signature of command().

Ideally, the signature validation logic in _register would be able to detect that signatures are compatible, even if they're not identical. Failing that, can you add a mechanism to disable signature enforcement?

Also note that changing the order of decorators won't work, e.g.

@other_decorator
@when(...)
def command():
    ...

will not incorporate other_decorator into the registered function.

Inventory.find problem

testadventurelib.py I edited the cast(magic) function by adding

    obj = inventory.find('mallet')
    drop(obj)

as shown below.

def cast(magic):
if magic == None:
say("Which magic you would like to spell?")
elif magic == 'fireball':
say("A flaming Fireball shoots form your hands!")
obj = inventory.find('mallet')
drop(obj)

This produces the error:

cast fireball
A flaming Fireball shoots form your hands!
Traceback (most recent call last):
File "main.py", line 85, in
start()
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/adventurelib.py", line 536, in start
_handle_command(cmd)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/adventurelib.py", line 509, in _handle_command
func(**args)
File "main.py", line 82, in cast
drop(obj)
File "main.py", line 52, in drop
obj = inventory.take(thing)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/adventurelib.py", line 260, in take
obj = self.find(name)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/adventurelib.py", line 236, in find
if name.lower() in item.aliases:
AttributeError: 'Item' object has no attribute 'lower'

if I change obj in drop(obj) to drop('mallet') it functions as expected.

Backtracking parser

Adventurelib's command parser is inefficient when there are 2+ wildcards in a command. It tries all possible variations of assignment to each wildcard until one matches.

A more efficient parser would greedily match words until a constraint is violated and backtrack to see if any word in the input could fix it. This means it can fail fast if no such word exists.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.