snakeng


Namesnakeng JSON
Version 2.0.0 PyPI version JSON
download
home_pagehttp://github.com/eriknyquist/snakeng
SummaryEasy-to-use snake game engine. Quickly implement snake for anything!
upload_time2023-04-08 19:14:31
maintainer
docs_urlNone
authorErik Nyquist
requires_python>=3.7
licenseApache 2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            snakeng
-------

``snakeng`` is an implementation of the back-end of the classic "snake" game. It provides
an interface to inject directional inputs (up/down/right/left), and produces a data structure
representing the game state, for each frame of gameplay. This allows snake to be quickly
implemented and played on various platforms.

Features:

* Configurable game area width/height
* Configurable wall behaviour (teleport/wrap or death)
* Serializable game state object (if you want to e.g. save/load game states to .json files)
* Configurable snake speed options (fixed speed, or automatically increase speed as snake grows)
* Configurable apple behaviour (set time before apples disappear, or make apples permanent until collected)

For more information, see the `API documentation <https://eriknyquist.github.io/snakeng/snakeng.html>`_.

Install
-------

Install via pip:

::

    pip install snakeng

Minimal snake game implementation
---------------------------------

This is the simplest possible implementation of snake, using ``snakeng``:

.. code:: python

    import sys
    import time
    import keyboard
    from snakeng.snake import SnakeGame, Direction

    dirmap = {'up': Direction.UP, 'down': Direction.DOWN, 'left': Direction.LEFT, 'right': Direction.RIGHT}

    game = SnakeGame()                 # Create game instance

    def keypress_event(e):
        new_direction = dirmap.get(e.name, None)
        if new_direction is not None:
            game.direction_input(new_direction)

    keyboard.on_press(keypress_event)  # Register callback function to save last keypress

    while True:
        new_state = game.process()                             # Produce new frame
        sys.stdout.write("\033[2J\n" + new_state.to_string())  # Clear terminal screen and print new game state
        sys.stdout.flush()                                     # Flush output
        time.sleep(0.05)


Sample command-line (ASCII) implementation
-------------------------------------------

Additionally, a sample terminal-based implementation of a snake game is provided,
which can be accessed by running ``snakeng`` as a module:

::

    python -m snakeng

.. image:: https://github.com/eriknyquist/snakeng/raw/master/images/terminal_example.gif

The terminal-based implementation accepts several arguments, detailed here:

::

    usage: snakeng [-h] [-x WIDTH] [-y HEIGHT] [-f FPS]
                   [-s {slow,medium,fast,faster}] [-w] [-o OUTPUT_FILE]
                   [-i INPUT_FILE] [-p] [-a APPLE_TICKS]

    Simple terminal-based snake game showing how to use snakeng. Use arrow keys to
    change snake direction, use 'p' to pause, and use 'Ctrl-C' to quit.

    options:
      -h, --help            show this help message and exit
      -x WIDTH, --width WIDTH
                            Game area width in characters. (default: 40)
      -y HEIGHT, --height HEIGHT
                            Game area height in characters. (default: 30)
      -f FPS, --fps FPS     Framerate in frames per second. (default: 24)
      -s {slow,medium,fast,faster}, --fixed-speed {slow,medium,fast,faster}
                            Sets the snake speed for the whole game. If unset, the
                            snake speed will automatically increase as the snake
                            size increases. (default: None)
      -w, --wall-death      If True, snake will die if it hits a wall. Default
                            behaviour is to "wrap around" and come out of the
                            opposite wall. (default: False)
      -o OUTPUT_FILE, --output-file OUTPUT_FILE
                            If set, the game state will be saved to the specified
                            filename when you quit with Ctrl-C. (default: None)
      -i INPUT_FILE, --input-file INPUT_FILE
                            If set, the game state will be loaded from the
                            specified filename. (default: None)
      -p, --permanent-apples
                            If True, apples will stay forever until collected.
                            Default is to disappear after a fixed number of
                            frames. (default: False)
      -a APPLE_TICKS, --apple-ticks APPLE_TICKS
                            Specifies the number of frames before an uncollected
                            apple disappears. Can only be used if -p is not set.
                            Default is to use the width or height of the game
                            area, whichever is larger. (default: None)
    NOTE: the sample implementation uses an ANSI escape sequence to clear the terminal screen,
    so it won't work in terminals that don't support ANSI escape sequences.

Contributions
-------------

Contributions are welcome, please open a pull request at `<https://github.com/eriknyquist/snakeng>`_.

If you have any questions about / need help with contributions, please contact Erik at eknyquist@gmail.com.

            

Raw data

            {
    "_id": null,
    "home_page": "http://github.com/eriknyquist/snakeng",
    "name": "snakeng",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "",
    "author": "Erik Nyquist",
    "author_email": "eknyquist@gmail.com",
    "download_url": "",
    "platform": null,
    "description": "snakeng\r\n-------\r\n\r\n``snakeng`` is an implementation of the back-end of the classic \"snake\" game. It provides\r\nan interface to inject directional inputs (up/down/right/left), and produces a data structure\r\nrepresenting the game state, for each frame of gameplay. This allows snake to be quickly\r\nimplemented and played on various platforms.\r\n\r\nFeatures:\r\n\r\n* Configurable game area width/height\r\n* Configurable wall behaviour (teleport/wrap or death)\r\n* Serializable game state object (if you want to e.g. save/load game states to .json files)\r\n* Configurable snake speed options (fixed speed, or automatically increase speed as snake grows)\r\n* Configurable apple behaviour (set time before apples disappear, or make apples permanent until collected)\r\n\r\nFor more information, see the `API documentation <https://eriknyquist.github.io/snakeng/snakeng.html>`_.\r\n\r\nInstall\r\n-------\r\n\r\nInstall via pip:\r\n\r\n::\r\n\r\n    pip install snakeng\r\n\r\nMinimal snake game implementation\r\n---------------------------------\r\n\r\nThis is the simplest possible implementation of snake, using ``snakeng``:\r\n\r\n.. code:: python\r\n\r\n    import sys\r\n    import time\r\n    import keyboard\r\n    from snakeng.snake import SnakeGame, Direction\r\n\r\n    dirmap = {'up': Direction.UP, 'down': Direction.DOWN, 'left': Direction.LEFT, 'right': Direction.RIGHT}\r\n\r\n    game = SnakeGame()                 # Create game instance\r\n\r\n    def keypress_event(e):\r\n        new_direction = dirmap.get(e.name, None)\r\n        if new_direction is not None:\r\n            game.direction_input(new_direction)\r\n\r\n    keyboard.on_press(keypress_event)  # Register callback function to save last keypress\r\n\r\n    while True:\r\n        new_state = game.process()                             # Produce new frame\r\n        sys.stdout.write(\"\\033[2J\\n\" + new_state.to_string())  # Clear terminal screen and print new game state\r\n        sys.stdout.flush()                                     # Flush output\r\n        time.sleep(0.05)\r\n\r\n\r\nSample command-line (ASCII) implementation\r\n-------------------------------------------\r\n\r\nAdditionally, a sample terminal-based implementation of a snake game is provided,\r\nwhich can be accessed by running ``snakeng`` as a module:\r\n\r\n::\r\n\r\n    python -m snakeng\r\n\r\n.. image:: https://github.com/eriknyquist/snakeng/raw/master/images/terminal_example.gif\r\n\r\nThe terminal-based implementation accepts several arguments, detailed here:\r\n\r\n::\r\n\r\n    usage: snakeng [-h] [-x WIDTH] [-y HEIGHT] [-f FPS]\r\n                   [-s {slow,medium,fast,faster}] [-w] [-o OUTPUT_FILE]\r\n                   [-i INPUT_FILE] [-p] [-a APPLE_TICKS]\r\n\r\n    Simple terminal-based snake game showing how to use snakeng. Use arrow keys to\r\n    change snake direction, use 'p' to pause, and use 'Ctrl-C' to quit.\r\n\r\n    options:\r\n      -h, --help            show this help message and exit\r\n      -x WIDTH, --width WIDTH\r\n                            Game area width in characters. (default: 40)\r\n      -y HEIGHT, --height HEIGHT\r\n                            Game area height in characters. (default: 30)\r\n      -f FPS, --fps FPS     Framerate in frames per second. (default: 24)\r\n      -s {slow,medium,fast,faster}, --fixed-speed {slow,medium,fast,faster}\r\n                            Sets the snake speed for the whole game. If unset, the\r\n                            snake speed will automatically increase as the snake\r\n                            size increases. (default: None)\r\n      -w, --wall-death      If True, snake will die if it hits a wall. Default\r\n                            behaviour is to \"wrap around\" and come out of the\r\n                            opposite wall. (default: False)\r\n      -o OUTPUT_FILE, --output-file OUTPUT_FILE\r\n                            If set, the game state will be saved to the specified\r\n                            filename when you quit with Ctrl-C. (default: None)\r\n      -i INPUT_FILE, --input-file INPUT_FILE\r\n                            If set, the game state will be loaded from the\r\n                            specified filename. (default: None)\r\n      -p, --permanent-apples\r\n                            If True, apples will stay forever until collected.\r\n                            Default is to disappear after a fixed number of\r\n                            frames. (default: False)\r\n      -a APPLE_TICKS, --apple-ticks APPLE_TICKS\r\n                            Specifies the number of frames before an uncollected\r\n                            apple disappears. Can only be used if -p is not set.\r\n                            Default is to use the width or height of the game\r\n                            area, whichever is larger. (default: None)\r\n    NOTE: the sample implementation uses an ANSI escape sequence to clear the terminal screen,\r\n    so it won't work in terminals that don't support ANSI escape sequences.\r\n\r\nContributions\r\n-------------\r\n\r\nContributions are welcome, please open a pull request at `<https://github.com/eriknyquist/snakeng>`_.\r\n\r\nIf you have any questions about / need help with contributions, please contact Erik at eknyquist@gmail.com.\r\n",
    "bugtrack_url": null,
    "license": "Apache 2.0",
    "summary": "Easy-to-use snake game engine. Quickly implement snake for anything!",
    "version": "2.0.0",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5fcf5c7dffc119b3803ba3b48d5ae5e469166cf40172e914313e2c5adf72ae10",
                "md5": "f370d94a1c0e151643f8e675e2d38f1b",
                "sha256": "4f15d405cc68b8142c58a383ebd539b007bb5d9e31417f3967ced9ecd2cdf013"
            },
            "downloads": -1,
            "filename": "snakeng-2.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f370d94a1c0e151643f8e675e2d38f1b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 13550,
            "upload_time": "2023-04-08T19:14:31",
            "upload_time_iso_8601": "2023-04-08T19:14:31.290752Z",
            "url": "https://files.pythonhosted.org/packages/5f/cf/5c7dffc119b3803ba3b48d5ae5e469166cf40172e914313e2c5adf72ae10/snakeng-2.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-08 19:14:31",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "eriknyquist",
    "github_project": "snakeng",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "snakeng"
}
        
Elapsed time: 0.10341s