{ "info": { "author": "Juan J. Martinez", "author_email": "jjm@usebox.net", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Topic :: Games/Entertainment", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "Harness for pysdl2\n==================\n\nThese are a set of classes to make easier to use `pysdl2 `_.\n\nThis is a work in progress so use it at your own risk!\n\n\nRequired\n--------\n\n- Python 3 (Python 2.7.x may work, but is not a priority!)\n- pysdl2\n- SDL2 and SDL2_Mixer installed in your system\n- Optionally SDL2_Image (otherwise only uncompressed BMP images are supported)\n\n\nInstallation\n------------\n\nThe easiest way to install Harness is using `pip`:\n\n.. code-block:: bash\n\n $ pip install pysdl2-harness\n\n\nHow does it looks like?\n-----------------------\n\nIt is inspired by `pyglet `_ and specially focused\non 2D games.\n\nExample:\n\n.. code-block:: python\n\n #!/usr/bin/env python\n\n from harness import Harness\n\n game = Harness(width=320, height=240, zoom=3)\n\n title = game.load_resource(\"title.bmp\")\n\n @game.draw\n def draw(renderer):\n renderer.draw(title, 10, 10)\n\n @game.update\n def update(dt):\n if game.keys[game.KEY_ESCAPE]:\n game.quit()\n\n game.loop()\n\nSee **harness** module docstrings for a complete list of classes and methods.\n\nThere's an example game in ``example.py`` and remember that you can still use\n**pysdl2** directly if you need to!\n\n\nComponents\n----------\n\nHarness tries to provide a clean and simple interface to the following\ncomponents:\n\n1. The game loop\n2. Resource management\n3. Controls\n4. Audio\n\n1. The game loop\n^^^^^^^^^^^^^^^^\n\nHarness implements a game loop with a fixed frame rate determined by the vsync\nof the screen (usually 60 FPS), with support for fixed updates for the game\nlogic (by default at 80 times per second).\n\nThe usual workflow is:\n\n1. Create a Harness object (we'll call it *game* in the examples).\n2. Load resources.\n3. Declare the draw and update functions.\n4. Run the ``loop()`` method in your Harness instance.\n\nThe game loop should be called once and it will run until the game is quitted\n(eg, using ``quit()`` method).\n\nDraw functions can be defined with the ``draw`` decorator, and update\nfunctions with the ``update`` decorator.\n\nThe draw functions should expect a \"renderer\" parameter that allows to draw\ntextures, bitmap fonts, etc.\n\nExample:\n\n.. code-block:: python\n\n game = Harness()\n\n tex = game.load_resource(\"bitmap.bmp\")\n\n @game.draw\n def draw(renderer):\n renderer.draw(tex)\n\n game.loop()\n\nThe update function should expect a \"dt\" parameter that provides the delta\ntime (time elapsed between updates); in this case fixed at ``Harness.UFPS_DT``\n(1 / UFPS).\n\nExample:\n\n.. code-block:: python\n\n game = Harness()\n\n @game.update\n def update(dt):\n print(\"%s elapsed since last update\" % dt)\n\n game.loop()\n\nSeveral draw and update functions can be defined and they will be run in the\nsame order they were defined.\n\nThe game instance can be accessed from the update function to test for key\nstates, quit the game, etc.\n\nThe method ``quit()`` can be used to exit the game loop.\n\nExample:\n\n.. code-block:: python\n\n game = Harness()\n\n @game.update\n def update(dt):\n\n if game.keys[game.KEY_ESCAPE]:\n game.quit()\n # in case we don't want to complete the update\n return\n\n game.loop()\n\nA draw or update function can be removed from the game loop with ``remove_handler()``\nmethod, passing the function to be removed as parameter.\n\nExample:\n\n.. code-block:: python\n\n game = Harness()\n debug = False\n\n def update_debug(dt):\n print(dt)\n\n @game.update\n def update(dt):\n global debug\n\n if game.keys[game.KEY_D]:\n print(\"D was pressed!\")\n if debug:\n # remove the update_debug update function\n game.remove_handler(update_debug)\n else:\n # add a new update function\n game.update(update_debug)\n debug = not debug\n # remove the key press once processed\n game.keys[game.KEY_D] = False\n\n if game.keys[game.KEY_ESCAPE]:\n game.quit()\n\n game.loop()\n\n2. Loading resources\n^^^^^^^^^^^^^^^^^^^^\n\nResources can loaded with ``load_resource()`` method. This method allows loading\nresources searching for them in the paths specified in the ``resource_path`` list.\n\nBy default the files will be searched for in the \"data\" subdirectory at the same\nlevel as the script running the game.\n\nDepending on the resource some extra libraries may be required in the system\n(eg, **SDL_Image**).\n\nResources not in use can be freed using ``free_resources()`` method, but\nbe careful to not use any reference to the resource once it has been released.\n\nHarness will free all resources after exiting the game loop.\n\n2.1 Bitmap fonts\n****************\n\nThe method ``load_bitmap_font()`` can be used to load a image that will be used to draw\ntext with ``renderer.draw_text()``. Harness will map a text string into a fixed\nwidth and height part of the font image.\n\nExample:\n\n.. code-block:: python\n\n game = Harness()\n\n font = game.load_bitmap_font(\"font.png\", width=6, height=10)\n\n @game.draw\n def draw(renderer):\n renderer.draw_text(font, 10, 10, \"This is a text!\")\n\n game.loop()\n\nFonts can be freed with ``free_resources()``.\n\n3. Controls\n^^^^^^^^^^^\n\nThe state of the keys is exposed in ``keys`` dictionary and it\ngets updated in each game loop iteration.\n\nIn ``Harness.KEY_*`` there are constants to test in the ``keys`` dictionary. If a key\nis being pressed, the value in the dictionary will be ``True``.\n\nExample:\n\n.. code-block:: python\n\n game = Harness()\n\n @game.update\n def update(dt):\n\n if game.keys[game.KEY_ESCAPE]:\n game.quit()\n\n game.loop()\n\n3.1 Game controllers\n********************\n\nGame controllers can be mapped into key states so the game can access to the\ncontroller like the player was using the keyboard.\n\nThe default mapping is:\n\n- DPad up: up arrow key\n- DPad down: down arrow key\n- DPad left: left arrow key\n- DPad right: right arrow key\n- Button A: key c\n- Button B: key v\n- Start button: key s\n- Back button: escape key\n\nHarness will manage the controller automatically in the game loop updating the\n``keys`` dictionary as needed.\n\n``has_controllers`` property can be checked to see if any game controller was\ndetected. Harness includes a game controller database with definitions for most\ncommon devices, and SDL2 functions can be used to add more. If there's no information\nabout a given controller, it will be silently ignored.\n\nIn order to use a controller, the ``controllers`` property can be accessed to\nactivate any detected controller.\n\nExample:\n\n.. code-block:: python\n\n game = Harness()\n\n # enumerate all detected controllers\n for controller in game.controllers:\n print(controller.name)\n\n\nOnce the controller has been activated, it can be deactivated using ``close()``\ncontroller method.\n\nThe key mapping can be changed using the ``set_mapping()`` method on the controller.\n\nExample:\n\n.. code-block:: python\n\n game = Harness()\n\n # first controller\n controller = game.controllers[0]\n\n # remap button a to key a\n controller.set_mapping(a=\"KEY_A\")\n\nThe valid parameters are: up, down, left, right, a, b, start and back. Use a\nstring defining the key (see ``Harness.KEY_*``).\n\nThe use of a controller won't disable the keyboard. If that is required, the\ngame controllers can be accessed using SDL2 functions directly.\n\n4. Audio\n^^^^^^^^\n\nThe method ``play()`` can be used to play a sample loaded with ``load_resource()``.\nOptionally a ``loops`` parameter can be provided stating how many times the sample\nwill be repeated (use -1 for an infinite loop).\n\nBy default .ogg and .wav files are supported (in theory it could load any\nformat supported by **SDL_Mixer** but Harness will only identify files with the\naforementioned extensions).\n\n``play()`` returns the channel number used to play the sample and that\nnumber can be used to muted the channel with ``stop_playback()`` (if a channel\nnumber s not provided, it will stop all channels).\n\nBy default ``Harness.AUDIO_CHANNELS`` channels are allocated (6 channels).\n\nUsing OOP\n---------\n\nHarness can be used in a class to take advantage of object oriented programming\nand avoid the use of global variables. Just use composition and register the\nupdate and draw methods with ``update()`` and ``draw()`` instead of using the\ndecorators:\n\nExample:\n\n.. code-block:: python\n\n from harness import Harness\n\n class MyGame(object):\n\n def __init__(self):\n self.harness = Harness()\n\n # register update and draw methods\n self.harness.update(self.update)\n self.harness.draw(self.draw)\n\n # load some resources\n self.image = self.harness.load_resource(\"image.png\")\n\n def run(self):\n self.harness.loop()\n\n def update(self, dt):\n if self.harness.keys[self.harness.KEY_ESCAPE]:\n self.harness.quit()\n\n def draw(self, renderer):\n renderer.draw(self.image)\n\n\n if __name__ == \"__main__\":\n game = MyGame()\n game.run()\n\nSee ``example-oop.py``.\n\nAuthor and Contributors\n-----------------------\n\nJuan J. Martinez \n\nThis is free software under MIT license terms.\n\nContributors:\n\n- Your name here?\n\n", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/reidrac/pysdl2-harness", "keywords": "pysdl2 game sdl", "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "pysdl2-harness", "package_url": "https://pypi.org/project/pysdl2-harness/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/pysdl2-harness/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/reidrac/pysdl2-harness" }, "release_url": "https://pypi.org/project/pysdl2-harness/0.2/", "requires_dist": null, "requires_python": null, "summary": "Some simple classes to make working with pysdl2 easier", "version": "0.2" }, "last_serial": 1569783, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "9264ce0c3f20679d3ce6a60ebaf04dd3", "sha256": "9621f76530b9f63299d7bdce57979cc9670f5e6fc6d7015ec8d50fdf780f0f10" }, "downloads": -1, "filename": "pysdl2_harness-0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "9264ce0c3f20679d3ce6a60ebaf04dd3", "packagetype": "bdist_wheel", "python_version": "3.4", "requires_python": null, "size": 17613, "upload_time": "2015-05-31T08:29:33", "url": "https://files.pythonhosted.org/packages/f4/54/ed531f31d7b8f94f5999e908efc9206be28a4f8150f3e260a83f27981ba6/pysdl2_harness-0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "b29e7afb002f50e3dd3be302c1eb90da", "sha256": "a7fa501e555e97da137c85fb6fde7c04c661d5fd4c29a6f5e437570b51085e3e" }, "downloads": -1, "filename": "pysdl2-harness-0.1.tar.gz", "has_sig": false, "md5_digest": "b29e7afb002f50e3dd3be302c1eb90da", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4424485, "upload_time": "2015-05-31T08:29:28", "url": "https://files.pythonhosted.org/packages/cb/f8/d4d3aef13cde37adf9c72a314fdbc229675d9f7b8442b719c7f2acc4caf1/pysdl2-harness-0.1.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "5b62c6d4f48b917544bef4468a346108", "sha256": "e23ca01449a8572214d0aaa6baddd91addcd7dc70e7eb0d4264fa294e12a0545" }, "downloads": -1, "filename": "pysdl2_harness-0.1.1-py3-none-any.whl", "has_sig": false, "md5_digest": "5b62c6d4f48b917544bef4468a346108", "packagetype": "bdist_wheel", "python_version": "3.4", "requires_python": null, "size": 11386, "upload_time": "2015-05-31T08:45:03", "url": "https://files.pythonhosted.org/packages/6e/47/83d8f2f34bf6142cff3eedf3e75b0b50a5ae6ac324d8b4e4ce2951a5eeea/pysdl2_harness-0.1.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "bde66c6b7ddd0fe20a0741f145429d1b", "sha256": "e43d0363c9bfce14b7cd07faa97812276d7e9e51c7b342a5b505fb781a148fdc" }, "downloads": -1, "filename": "pysdl2-harness-0.1.1.tar.gz", "has_sig": false, "md5_digest": "bde66c6b7ddd0fe20a0741f145429d1b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4420140, "upload_time": "2015-05-31T08:44:58", "url": "https://files.pythonhosted.org/packages/5e/e0/9a4712365f8bf28e3bb78e6a9c59a79ebac6d64a82a1b7bb42b0c3801ca9/pysdl2-harness-0.1.1.tar.gz" } ], "0.2": [ { "comment_text": "", "digests": { "md5": "f4ae6d5cb5b1230b5b6288bfb9cf9ec0", "sha256": "097614235c017d29a25173c9c28bde6ac2c8ca6e9fddaf1c81cde3dfed2e3c99" }, "downloads": -1, "filename": "pysdl2_harness-0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "f4ae6d5cb5b1230b5b6288bfb9cf9ec0", "packagetype": "bdist_wheel", "python_version": "3.4", "requires_python": null, "size": 11372, "upload_time": "2015-05-31T09:19:54", "url": "https://files.pythonhosted.org/packages/1a/e6/fff9c4d4bb76dad9dd01d2cdc7ae27c026811c5ccabc555506278a6a060e/pysdl2_harness-0.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "4be3b1c0e678d4c0d0d1deae4f3ec244", "sha256": "101f853b56a5db87ab5f0f4f181867dfc449e22498a646faf83a95eca5705da7" }, "downloads": -1, "filename": "pysdl2-harness-0.2.tar.gz", "has_sig": false, "md5_digest": "4be3b1c0e678d4c0d0d1deae4f3ec244", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4419547, "upload_time": "2015-05-31T09:19:49", "url": "https://files.pythonhosted.org/packages/b0/4a/79ccf99853f0199ec6ef15f030d00fc33d885a5f7cc0e33c467f325a1b97/pysdl2-harness-0.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "f4ae6d5cb5b1230b5b6288bfb9cf9ec0", "sha256": "097614235c017d29a25173c9c28bde6ac2c8ca6e9fddaf1c81cde3dfed2e3c99" }, "downloads": -1, "filename": "pysdl2_harness-0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "f4ae6d5cb5b1230b5b6288bfb9cf9ec0", "packagetype": "bdist_wheel", "python_version": "3.4", "requires_python": null, "size": 11372, "upload_time": "2015-05-31T09:19:54", "url": "https://files.pythonhosted.org/packages/1a/e6/fff9c4d4bb76dad9dd01d2cdc7ae27c026811c5ccabc555506278a6a060e/pysdl2_harness-0.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "4be3b1c0e678d4c0d0d1deae4f3ec244", "sha256": "101f853b56a5db87ab5f0f4f181867dfc449e22498a646faf83a95eca5705da7" }, "downloads": -1, "filename": "pysdl2-harness-0.2.tar.gz", "has_sig": false, "md5_digest": "4be3b1c0e678d4c0d0d1deae4f3ec244", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4419547, "upload_time": "2015-05-31T09:19:49", "url": "https://files.pythonhosted.org/packages/b0/4a/79ccf99853f0199ec6ef15f030d00fc33d885a5f7cc0e33c467f325a1b97/pysdl2-harness-0.2.tar.gz" } ] }