latest notes
This commit is contained in:
parent
7536075103
commit
248c6ece0c
|
@ -39,25 +39,530 @@
|
||||||
},
|
},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"ename": "ValueError",
|
"name": "stdout",
|
||||||
"evalue": "'strategygame.kv' was not found in history, as a file, url, nor in the user namespace.",
|
"output_type": "stream",
|
||||||
"output_type": "error",
|
"text": [
|
||||||
"traceback": [
|
"Overwriting strategygame.kv\n"
|
||||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
|
||||||
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
|
|
||||||
"\u001b[0;32m<ipython-input-3-20c9424f0bfd>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmagic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mu'load strategygame.kv'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
|
||||||
"\u001b[0;32m/Users/kjell/anaconda/envs/py27/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc\u001b[0m in \u001b[0;36mmagic\u001b[0;34m(self, arg_s)\u001b[0m\n\u001b[1;32m 2161\u001b[0m \u001b[0mmagic_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmagic_arg_s\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marg_s\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpartition\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m' '\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2162\u001b[0m \u001b[0mmagic_name\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmagic_name\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlstrip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprefilter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mESC_MAGIC\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2163\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun_line_magic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmagic_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmagic_arg_s\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2164\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2165\u001b[0m \u001b[0;31m#-------------------------------------------------------------------------\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
||||||
"\u001b[0;32m/Users/kjell/anaconda/envs/py27/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc\u001b[0m in \u001b[0;36mrun_line_magic\u001b[0;34m(self, magic_name, line)\u001b[0m\n\u001b[1;32m 2082\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'local_ns'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getframe\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstack_depth\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf_locals\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2083\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbuiltin_trap\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2084\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2085\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2086\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
||||||
"\u001b[0;32m<decorator-gen-45>\u001b[0m in \u001b[0;36mload\u001b[0;34m(self, arg_s)\u001b[0m\n",
|
|
||||||
"\u001b[0;32m/Users/kjell/anaconda/envs/py27/lib/python2.7/site-packages/IPython/core/magic.pyc\u001b[0m in \u001b[0;36m<lambda>\u001b[0;34m(f, *a, **k)\u001b[0m\n\u001b[1;32m 191\u001b[0m \u001b[0;31m# but it's overkill for just that one bit of state.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 192\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmagic_deco\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 193\u001b[0;31m \u001b[0mcall\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mlambda\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 194\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 195\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcallable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
||||||
"\u001b[0;32m/Users/kjell/anaconda/envs/py27/lib/python2.7/site-packages/IPython/core/magics/code.pyc\u001b[0m in \u001b[0;36mload\u001b[0;34m(self, arg_s)\u001b[0m\n\u001b[1;32m 316\u001b[0m \u001b[0msearch_ns\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'n'\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mopts\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 317\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 318\u001b[0;31m \u001b[0mcontents\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshell\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfind_user_code\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msearch_ns\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msearch_ns\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 319\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 320\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;34m's'\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mopts\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
||||||
"\u001b[0;32m/Users/kjell/anaconda/envs/py27/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc\u001b[0m in \u001b[0;36mfind_user_code\u001b[0;34m(self, target, raw, py_only, skip_encoding_cookie, search_ns)\u001b[0m\n\u001b[1;32m 3184\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3185\u001b[0m raise ValueError((\"'%s' was not found in history, as a file, url, \"\n\u001b[0;32m-> 3186\u001b[0;31m \"nor in the user namespace.\") % target)\n\u001b[0m\u001b[1;32m 3187\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3188\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcodeobj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstring_types\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
|
||||||
"\u001b[0;31mValueError\u001b[0m: 'strategygame.kv' was not found in history, as a file, url, nor in the user namespace."
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%load strategygame.kv"
|
"%%file strategygame.kv\n",
|
||||||
|
"\n",
|
||||||
|
"#:include debug.kv\n",
|
||||||
|
"\n",
|
||||||
|
"<StrategyGame>:\n",
|
||||||
|
" BoxLayout:\n",
|
||||||
|
" orientation: 'horizontal'\n",
|
||||||
|
" DebugLabel:\n",
|
||||||
|
" text: 'Main map'\n",
|
||||||
|
" size_hint: .75, 1\n",
|
||||||
|
" BoxLayout:\n",
|
||||||
|
" orientation: 'vertical'\n",
|
||||||
|
" size_hint: .25, 1\n",
|
||||||
|
" DebugLabel:\n",
|
||||||
|
" text: 'status'\n",
|
||||||
|
" size_hint: 1, .66\n",
|
||||||
|
" DebugLabel:\n",
|
||||||
|
" text: 'mini-map'\n",
|
||||||
|
" size_hint: 1, .33\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Overwriting main.py\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"%%file main.py\n",
|
||||||
|
"from kivy.app import App\n",
|
||||||
|
"from kivy.uix.floatlayout import FloatLayout\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"class StrategyGame(FloatLayout):\n",
|
||||||
|
" pass\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"class StrategyGameApp(App):\n",
|
||||||
|
" def build(self):\n",
|
||||||
|
" return StrategyGame()\n",
|
||||||
|
"\n",
|
||||||
|
"if __name__ == '__main__':\n",
|
||||||
|
" StrategyGameApp().run()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"#!python main.py\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Let's make a gridlayout in the game area, and give it a label so we can refer to it from our code. We are going to build a rectangular grid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%%file strategygame.kv\n",
|
||||||
|
"\n",
|
||||||
|
"#:include debug.kv\n",
|
||||||
|
"\n",
|
||||||
|
"<StrategyGame>:\n",
|
||||||
|
" main_map: _main_map\n",
|
||||||
|
" BoxLayout:\n",
|
||||||
|
" orientation: 'horizontal'\n",
|
||||||
|
" GridLayout:\n",
|
||||||
|
" id: _main_map\n",
|
||||||
|
" cols: 10\n",
|
||||||
|
" size_hint: .75, 1\n",
|
||||||
|
" BoxLayout:\n",
|
||||||
|
" orientation: 'vertical'\n",
|
||||||
|
" size_hint: .25, 1\n",
|
||||||
|
" DebugLabel:\n",
|
||||||
|
" text: 'status'\n",
|
||||||
|
" size_hint: 1, .66\n",
|
||||||
|
" DebugLabel:\n",
|
||||||
|
" text: 'mini-map'\n",
|
||||||
|
" size_hint: 1, .33\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Now let's hook to that from code. We will do this in `__init__` in our main StrategyGame class. Let's make a loop to add buttons to the root widget. We will use properties in the kv file to determine how many rows/cols to add. We will also need to refer to various objects in the widget hierarchy in the code."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"You can refer to the top-level object (non-indented) as `root` in the kv file, but it we want to refer to conveniently elsewhere in the code, we need to use an id.\n",
|
||||||
|
"\n",
|
||||||
|
"`id: _game`\n",
|
||||||
|
"\n",
|
||||||
|
"and then from any widget we want to refer to it, we do a \n",
|
||||||
|
"\n",
|
||||||
|
"`game: _game`\n",
|
||||||
|
"\n",
|
||||||
|
"We'll use this to access the number of regions to layout in the grid via `map_cols` and `map_rows` from the main game widget. \n",
|
||||||
|
"\n",
|
||||||
|
"Fill in grids: it fills it in by default, left to right, top to bottom, and give them coordinate references. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%%file strategygame.kv\n",
|
||||||
|
"\n",
|
||||||
|
"#:include debug.kv\n",
|
||||||
|
"\n",
|
||||||
|
"<StrategyGame>:\n",
|
||||||
|
" id: _game\n",
|
||||||
|
" main_map: _main_map\n",
|
||||||
|
" map_rows: 10\n",
|
||||||
|
" map_cols: 10\n",
|
||||||
|
" BoxLayout:\n",
|
||||||
|
" orientation: 'horizontal'\n",
|
||||||
|
" GridLayout:\n",
|
||||||
|
" id: _main_map\n",
|
||||||
|
" game: _game\n",
|
||||||
|
" cols: root.map_cols\n",
|
||||||
|
" size_hint: .75, 1\n",
|
||||||
|
" BoxLayout:\n",
|
||||||
|
" orientation: 'vertical'\n",
|
||||||
|
" size_hint: .25, 1\n",
|
||||||
|
" DebugLabel:\n",
|
||||||
|
" text: 'status'\n",
|
||||||
|
" size_hint: 1, .66\n",
|
||||||
|
" DebugLabel:\n",
|
||||||
|
" text: 'mini-map'\n",
|
||||||
|
" size_hint: 1, .33\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# %load main.py\n",
|
||||||
|
"from kivy.app import App\n",
|
||||||
|
"from kivy import properties\n",
|
||||||
|
"from kivy.uix import button\n",
|
||||||
|
"from kivy.uix.floatlayout import FloatLayout\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"class StrategyGame(FloatLayout):\n",
|
||||||
|
" main_map = properties.ObjectProperty(None)\n",
|
||||||
|
" map_rows = properties.NumericProperty(0)\n",
|
||||||
|
" map_cols = properties.NumericProperty(0)\n",
|
||||||
|
"\n",
|
||||||
|
" def __init__(self, **kwargs):\n",
|
||||||
|
" super(StrategyGame, self).__init__(**kwargs)\n",
|
||||||
|
"\n",
|
||||||
|
" number_of_regions = self.map_rows * self.map_cols\n",
|
||||||
|
" for region in xrange(0, number_of_regions):\n",
|
||||||
|
" row = region / self.map_cols\n",
|
||||||
|
" col = region % self.map_cols\n",
|
||||||
|
" self.main_map.add_widget(button.Button(text='({}, {})'.format(row, col)))\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"class StrategyGameApp(App):\n",
|
||||||
|
" def build(self):\n",
|
||||||
|
" return StrategyGame()\n",
|
||||||
|
"\n",
|
||||||
|
"if __name__ == '__main__':\n",
|
||||||
|
" StrategyGameApp().run()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"We draw alternating rows with the patterns:\n",
|
||||||
|
"```\n",
|
||||||
|
"/ \\\n",
|
||||||
|
"| |\n",
|
||||||
|
"| |\n",
|
||||||
|
"```\n",
|
||||||
|
"and\n",
|
||||||
|
"```\n",
|
||||||
|
"\\ /\n",
|
||||||
|
" |\n",
|
||||||
|
" |\n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%%file strategygame.kv\n",
|
||||||
|
"\n",
|
||||||
|
"#:include debug.kv\n",
|
||||||
|
"\n",
|
||||||
|
"<StrategyGame>:\n",
|
||||||
|
" id: _game\n",
|
||||||
|
" main_map: _main_map\n",
|
||||||
|
" map_rows: 10\n",
|
||||||
|
" map_cols: 10\n",
|
||||||
|
" BoxLayout:\n",
|
||||||
|
" orientation: 'horizontal'\n",
|
||||||
|
" GridLayout:\n",
|
||||||
|
" id: _main_map\n",
|
||||||
|
" game: _game\n",
|
||||||
|
" cols: root.map_cols\n",
|
||||||
|
" size_hint: .75, 1\n",
|
||||||
|
" BoxLayout:\n",
|
||||||
|
" orientation: 'vertical'\n",
|
||||||
|
" size_hint: .25, 1\n",
|
||||||
|
" DebugLabel:\n",
|
||||||
|
" text: 'status'\n",
|
||||||
|
" size_hint: 1, .66\n",
|
||||||
|
" DebugLabel:\n",
|
||||||
|
" text: 'mini-map'\n",
|
||||||
|
" size_hint: 1, .33\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%%file main.py\n",
|
||||||
|
"from kivy.app import App\n",
|
||||||
|
"from kivy import properties\n",
|
||||||
|
"from kivy.uix import button\n",
|
||||||
|
"from kivy.uix.floatlayout import FloatLayout\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"class StrategyGame(FloatLayout):\n",
|
||||||
|
" main_map = properties.ObjectProperty(None)\n",
|
||||||
|
" map_rows = properties.NumericProperty(0)\n",
|
||||||
|
" map_cols = properties.NumericProperty(0)\n",
|
||||||
|
"\n",
|
||||||
|
" def __init__(self, **kwargs):\n",
|
||||||
|
" super(StrategyGame, self).__init__(**kwargs)\n",
|
||||||
|
"\n",
|
||||||
|
" number_of_regions = self.map_rows * self.map_cols\n",
|
||||||
|
" for region in xrange(0, number_of_regions):\n",
|
||||||
|
" row = region / self.map_cols\n",
|
||||||
|
" col = region % self.map_cols\n",
|
||||||
|
" self.main_map.add_widget(button.Button(text='({}, {})'.format(row, col)))\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"class StrategyGameApp(App):\n",
|
||||||
|
" def build(self):\n",
|
||||||
|
" return StrategyGame()\n",
|
||||||
|
"\n",
|
||||||
|
"if __name__ == '__main__':\n",
|
||||||
|
" StrategyGameApp().run()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Wait, that doesn't draw where we think it should.'"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%%file main.py\n",
|
||||||
|
"import collections\n",
|
||||||
|
"\n",
|
||||||
|
"from kivy.app import App\n",
|
||||||
|
"from kivy import properties\n",
|
||||||
|
"from kivy import graphics\n",
|
||||||
|
"from kivy.uix import label\n",
|
||||||
|
"from kivy.uix.floatlayout import FloatLayout\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"MapCoords = collections.namedtuple('MapCoords', ['row', 'col'])\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"class StrategyGame(FloatLayout):\n",
|
||||||
|
" main_map = properties.ObjectProperty(None)\n",
|
||||||
|
" map_rows = properties.NumericProperty(0)\n",
|
||||||
|
" map_cols = properties.NumericProperty(0)\n",
|
||||||
|
"\n",
|
||||||
|
" def __init__(self, **kwargs):\n",
|
||||||
|
" super(StrategyGame, self).__init__(**kwargs)\n",
|
||||||
|
"\n",
|
||||||
|
" number_of_regions = self.map_rows * self.map_cols\n",
|
||||||
|
" for region in xrange(0, number_of_regions):\n",
|
||||||
|
" row = region / self.map_cols\n",
|
||||||
|
" col = region % self.map_cols\n",
|
||||||
|
" self.main_map.add_widget(HexMapCell(row=row, col=col))\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"class HexMapCell(label.Label):\n",
|
||||||
|
" def __init__(self, row=0, col=0, **kwargs):\n",
|
||||||
|
" self.region_in_map = MapCoords(row, col)\n",
|
||||||
|
" super(HexMapCell, self).__init__(**kwargs)\n",
|
||||||
|
" self.draw_hex_edge()\n",
|
||||||
|
"\n",
|
||||||
|
" def draw_hex_edge(self):\n",
|
||||||
|
" edge = ''\n",
|
||||||
|
" if self.region_in_map.col % 2 == 0:\n",
|
||||||
|
" row_mod = self.region_in_map.row % 6\n",
|
||||||
|
" if row_mod == 0:\n",
|
||||||
|
" edge = 'BU'\n",
|
||||||
|
" with self.canvas:\n",
|
||||||
|
" graphics.Color(1, 1, 1, 1)\n",
|
||||||
|
" graphics.Line(points=[self.x, self.y, self.width + self.x, self.height + self.y])\n",
|
||||||
|
" elif row_mod in (1, 2):\n",
|
||||||
|
" edge = 'L '\n",
|
||||||
|
" elif row_mod == 3:\n",
|
||||||
|
" edge = 'TD'\n",
|
||||||
|
" elif row_mod in (4, 5):\n",
|
||||||
|
" edge = ' R'\n",
|
||||||
|
" else:\n",
|
||||||
|
" row_mod = self.region_in_map.row % 6\n",
|
||||||
|
" if row_mod == 0:\n",
|
||||||
|
" edge = 'TD'\n",
|
||||||
|
" elif row_mod in (1, 2):\n",
|
||||||
|
" edge = ' R'\n",
|
||||||
|
" elif row_mod == 3:\n",
|
||||||
|
" edge = 'BU'\n",
|
||||||
|
" elif row_mod in (4, 5):\n",
|
||||||
|
" edge = 'L '\n",
|
||||||
|
"\n",
|
||||||
|
" self.text = edge\n",
|
||||||
|
"\n",
|
||||||
|
"class StrategyGameApp(App):\n",
|
||||||
|
" def build(self):\n",
|
||||||
|
" return StrategyGame()\n",
|
||||||
|
"\n",
|
||||||
|
"if __name__ == '__main__':\n",
|
||||||
|
" StrategyGameApp().run()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 14,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Overwriting main.py\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"%%file main.py\n",
|
||||||
|
"from kivy.app import App\n",
|
||||||
|
"from kivy.uix.gridlayout import GridLayout\n",
|
||||||
|
"from kivy.lang import Builder\n",
|
||||||
|
"from kivy.properties import NumericProperty\n",
|
||||||
|
"\n",
|
||||||
|
"class HelloWorld(GridLayout):\n",
|
||||||
|
" cols = NumericProperty(4)\n",
|
||||||
|
" def __init__(self, **kw):\n",
|
||||||
|
" super(HelloWorld, self).__init__(**kw)\n",
|
||||||
|
" self.add_widget(self.BU())\n",
|
||||||
|
" self.add_widget(self.TD())\n",
|
||||||
|
" self.add_widget(self.BU())\n",
|
||||||
|
" self.add_widget(self.TD())\n",
|
||||||
|
"\n",
|
||||||
|
" def BU(self):\n",
|
||||||
|
" return Builder.load_string('''\n",
|
||||||
|
"Label:\n",
|
||||||
|
" canvas:\n",
|
||||||
|
" Color:\n",
|
||||||
|
" rgba: (1,1,1,1)\n",
|
||||||
|
" Line:\n",
|
||||||
|
" points: (self.x, self.y, self.right, self.top)\n",
|
||||||
|
" width: 2''')\n",
|
||||||
|
"\n",
|
||||||
|
" def TD(self):\n",
|
||||||
|
" return Builder.load_string('''\n",
|
||||||
|
"Label:\n",
|
||||||
|
" canvas:\n",
|
||||||
|
" Color:\n",
|
||||||
|
" rgba: (1,1,1,1)\n",
|
||||||
|
" Line:\n",
|
||||||
|
" points: (self.x, self.top, self.right, self.y)\n",
|
||||||
|
" width: 2''')\n",
|
||||||
|
"\n",
|
||||||
|
" def L(self):\n",
|
||||||
|
" return Builder.load_string('''\n",
|
||||||
|
"Label:\n",
|
||||||
|
" canvas:\n",
|
||||||
|
" Color:\n",
|
||||||
|
" rgba: (1,1,1,1)\n",
|
||||||
|
" Line:\n",
|
||||||
|
" points: (self.x, self.y, self.x, self.top)\n",
|
||||||
|
" width: 2''')\n",
|
||||||
|
"\n",
|
||||||
|
" def R(self):\n",
|
||||||
|
" return Builder.load_string('''\n",
|
||||||
|
"Label:\n",
|
||||||
|
" canvas:\n",
|
||||||
|
" Color:\n",
|
||||||
|
" rgba: (1,1,1,1)\n",
|
||||||
|
" Line:\n",
|
||||||
|
" points: (self.right, self.y, self.right, self.top)\n",
|
||||||
|
" width: 2''')\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"class HelloWorldApp(App):\n",
|
||||||
|
" def build(self):\n",
|
||||||
|
" return HelloWorld()\n",
|
||||||
|
"\n",
|
||||||
|
"if __name__ == '__main__':\n",
|
||||||
|
" HelloWorldApp().run()\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# %load strategygame.kv\n",
|
||||||
|
"\n",
|
||||||
|
"#:include debug.kv\n",
|
||||||
|
"\n",
|
||||||
|
"<StrategyGame>:\n",
|
||||||
|
" id: _game\n",
|
||||||
|
" main_map: _main_map\n",
|
||||||
|
" map_rows: 10\n",
|
||||||
|
" map_cols: 10\n",
|
||||||
|
" BoxLayout:\n",
|
||||||
|
" orientation: 'horizontal'\n",
|
||||||
|
" GridLayout:\n",
|
||||||
|
" id: _main_map\n",
|
||||||
|
" game: _game\n",
|
||||||
|
" cols: root.map_cols\n",
|
||||||
|
" size_hint: .75, 1\n",
|
||||||
|
" BoxLayout:\n",
|
||||||
|
" orientation: 'vertical'\n",
|
||||||
|
" size_hint: .25, 1\n",
|
||||||
|
" DebugLabel:\n",
|
||||||
|
" text: 'status'\n",
|
||||||
|
" size_hint: 1, .66\n",
|
||||||
|
" DebugLabel:\n",
|
||||||
|
" text: 'mini-map'\n",
|
||||||
|
" size_hint: 1, .33\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue