|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "collapsed": true |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "# Introduction to Simple-playgrounds\n", |
| 10 | + "\n", |
| 11 | + "Welcome to Simple-playgrounds, a library that allows you to easily build environments for AI.\n", |
| 12 | + "\n", |
| 13 | + "First of all, we are going to create an empty playground and add a Scene Element in it.\n", |
| 14 | + "In order to run the playground, and display its contents, we are using a game engine." |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 1, |
| 20 | + "metadata": { |
| 21 | + "collapsed": false |
| 22 | + }, |
| 23 | + "outputs": [ |
| 24 | + { |
| 25 | + "name": "stdout", |
| 26 | + "output_type": "stream", |
| 27 | + "text": [ |
| 28 | + "pygame 1.9.6\nHello from the pygame community. https://www.pygame.org/contribute.html\nLoading chipmunk for Linux (64bit) [/home/michael/.local/lib/python3.6/site-packages/pymunk/libchipmunk.so]\n" |
| 29 | + ] |
| 30 | + } |
| 31 | + ], |
| 32 | + "source": [ |
| 33 | + "from simple_playgrounds.playgrounds import SingleRoom\n", |
| 34 | + "from simple_playgrounds import Engine\n", |
| 35 | + "\n", |
| 36 | + "my_playground = SingleRoom(size=(150, 100))\n", |
| 37 | + "\n", |
| 38 | + "# we use the option screen=True to use a keyboard controlled agent later on.\n", |
| 39 | + "engine = Engine(time_limit=10000, playground= my_playground, screen=True)\n", |
| 40 | + "\n", |
| 41 | + "engine.display_full_scene()" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "markdown", |
| 46 | + "metadata": {}, |
| 47 | + "source": [ |
| 48 | + "## Adding a scene element to the playground\n", |
| 49 | + "\n", |
| 50 | + "Now that we have an empty playground, we can add Scene Elements to it.\n", |
| 51 | + "Let's start simple, with a fixed circular object at coordinate (30, 30). Note that you always have to specify the orientation of the object, and here it will be 0." |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": 2, |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [], |
| 59 | + "source": [ |
| 60 | + "from simple_playgrounds.entities.scene_elements import Basic\n", |
| 61 | + "\n", |
| 62 | + "circular_object = Basic((30, 30, 0), physical_shape='circle', radius=10, texture = [120, 230, 0])\n", |
| 63 | + "my_playground.add_scene_element(circular_object)\n", |
| 64 | + "engine.display_full_scene()" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "markdown", |
| 69 | + "metadata": {}, |
| 70 | + "source": [ |
| 71 | + "## Using already existing configurations\n", |
| 72 | + "\n", |
| 73 | + "We can use already existing configurations for basic objects.\n", |
| 74 | + "Every parameter that we add as a keyword argument will overwrite the default parameter.\n", |
| 75 | + "\n", |
| 76 | + "As an exampe, we will make a new element which is movable.\n", |
| 77 | + "Note that movable objets require that you set a mass (duh)." |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "code", |
| 82 | + "execution_count": 3, |
| 83 | + "metadata": {}, |
| 84 | + "outputs": [], |
| 85 | + "source": [ |
| 86 | + "other_circular_object = Basic((120, 70, 0), default_config_key='circle', radius=5, mass=10, movable=True)\n", |
| 87 | + "my_playground.add_scene_element(other_circular_object)\n", |
| 88 | + "engine.display_full_scene()" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "markdown", |
| 93 | + "metadata": {}, |
| 94 | + "source": [ |
| 95 | + "## Adding an agent and moving around\n", |
| 96 | + "\n", |
| 97 | + "Finally, we can add an agent controlled by a keyboard.\n", |
| 98 | + "This requires the creation of a pygame screen, and will work only on your local machine.\n", |
| 99 | + "If no initial position is set for the agent, it will appear in the center of the playground by default." |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": 4, |
| 105 | + "metadata": {}, |
| 106 | + "outputs": [], |
| 107 | + "source": [ |
| 108 | + "from simple_playgrounds.controllers import Keyboard\n", |
| 109 | + "from simple_playgrounds.entities.agents import BaseAgent\n", |
| 110 | + "\n", |
| 111 | + "my_agent = BaseAgent(controller=Keyboard())\n", |
| 112 | + "my_playground.add_agent(my_agent)\n", |
| 113 | + "\n", |
| 114 | + "engine.display_full_scene()" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "markdown", |
| 119 | + "metadata": {}, |
| 120 | + "source": [ |
| 121 | + "## Runing the simulation\n", |
| 122 | + "\n", |
| 123 | + "Now that our playground is complete, we can run the simulation until termination of the game.\n", |
| 124 | + "Alternatively, once you are bored, you can terminte the game by pressing q.\n" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "code", |
| 129 | + "execution_count": 6, |
| 130 | + "metadata": {}, |
| 131 | + "outputs": [], |
| 132 | + "source": [ |
| 133 | + "engine.run(with_screen=True)\n", |
| 134 | + "engine.terminate()" |
| 135 | + ] |
| 136 | + } |
| 137 | + ], |
| 138 | + "metadata": { |
| 139 | + "kernelspec": { |
| 140 | + "display_name": "Python 2", |
| 141 | + "language": "python", |
| 142 | + "name": "python2" |
| 143 | + }, |
| 144 | + "language_info": { |
| 145 | + "codemirror_mode": { |
| 146 | + "name": "ipython", |
| 147 | + "version": 2 |
| 148 | + }, |
| 149 | + "file_extension": ".py", |
| 150 | + "mimetype": "text/x-python", |
| 151 | + "name": "python", |
| 152 | + "nbconvert_exporter": "python", |
| 153 | + "pygments_lexer": "ipython2", |
| 154 | + "version": "2.7.6" |
| 155 | + } |
| 156 | + }, |
| 157 | + "nbformat": 4, |
| 158 | + "nbformat_minor": 0 |
| 159 | +} |
0 commit comments