drawing hexagons!

This commit is contained in:
Amy Wooding 2016-06-04 15:10:18 -07:00
parent 248c6ece0c
commit 4bde100b4c
1 changed files with 178 additions and 31 deletions

View File

@ -33,19 +33,11 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting strategygame.kv\n"
]
}
],
"outputs": [],
"source": [
"%%file strategygame.kv\n",
"\n",
@ -70,19 +62,11 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting main.py\n"
]
}
],
"outputs": [],
"source": [
"%%file main.py\n",
"from kivy.app import App\n",
@ -112,7 +96,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"metadata": {
"collapsed": false
},
@ -452,19 +436,11 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting main.py\n"
]
}
],
"outputs": [],
"source": [
"%%file main.py\n",
"from kivy.app import App\n",
@ -565,6 +541,177 @@
" size_hint: 1, .33\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Trying to draw the hexmap\n",
"\n",
"We tried adding the ability to style our widgets for to draw the Hexmap using lines like this\n",
"\n",
" BU: /\n",
" TD: \\\n",
" L: |\n",
" R: |\n",
"We get hexs now, but we need to figure out how to do the aspect ratio properly now...for a spiky top, we need each grid widget to have aspec ratio $$\\sqrt{3}\\cdot height = width$$"
]
},
{
"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",
"\n",
"<BU>:\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",
"<TD>:\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",
"<L>:\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",
"<R>:\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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"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",
"import math\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(self.pick_hex_cell(row=row, col=col))\n",
"\n",
"\n",
" def pick_hex_cell(self, row, col):\n",
" row_mod = row % 6\n",
" if col % 2 == 0:\n",
" if row_mod == 0:\n",
" return BU()\n",
" elif row_mod in (1, 2):\n",
" return L()\n",
" elif row_mod == 3:\n",
" return TD()\n",
" elif row_mod in (4, 5):\n",
" return R()\n",
" else:\n",
" if row_mod == 0:\n",
" return TD()\n",
" elif row_mod in (1, 2):\n",
" return R()\n",
" elif row_mod == 3:\n",
" return BU()\n",
" elif row_mod in (4, 5):\n",
" return L()\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",
"\n",
"class BU(HexMapCell):\n",
" pass\n",
"class TD(HexMapCell):\n",
" pass\n",
"class L(HexMapCell):\n",
" pass\n",
"class R(HexMapCell):\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": false
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,