From 4bde100b4c3ae01b59f8490111ef957a9779badc Mon Sep 17 00:00:00 2001 From: Amy Wooding Date: Sat, 4 Jun 2016 15:10:18 -0700 Subject: [PATCH 1/4] drawing hexagons! --- Making a Wargame.ipynb | 209 +++++++++++++++++++++++++++++++++++------ 1 file changed, 178 insertions(+), 31 deletions(-) diff --git a/Making a Wargame.ipynb b/Making a Wargame.ipynb index 22a7359..dd0793b 100644 --- a/Making a Wargame.ipynb +++ b/Making a Wargame.ipynb @@ -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", + ":\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", + ":\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", + " 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", + " 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", + " 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, From e3b32ca4fa8cb3f54b10aa9c309f3aa3d40c1067 Mon Sep 17 00:00:00 2001 From: Amy Wooding Date: Sat, 4 Jun 2016 21:01:21 -0700 Subject: [PATCH 2/4] up to adding an overlay --- Making a Wargame.ipynb | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/Making a Wargame.ipynb b/Making a Wargame.ipynb index 8d466e9..26a820a 100644 --- a/Making a Wargame.ipynb +++ b/Making a Wargame.ipynb @@ -704,13 +704,13 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": { "collapsed": false }, - "outputs": [], - "source": [] + "source": [ + "Replaced the debug labels and coloured the status and minimap two different ways. " + ] }, { "cell_type": "code", @@ -719,7 +719,23 @@ "collapsed": true }, "outputs": [], - "source": [] + "source": [ + "#%load strategygame.kv" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "We should probably have a viewer and a hexagon map underneath!!\n", + "\n", + "But not for now...let's make an overlay of boxes...and label them with an \"offset-r)\n", + "\n", + "Hmmmm....overlay...is...having...issues. \n", + "\n" + ] }, { "cell_type": "code", From f5aaa50433c4c26c3d9fd1252636cf662b6da79e Mon Sep 17 00:00:00 2001 From: Amy Wooding Date: Sat, 4 Jun 2016 22:20:00 -0700 Subject: [PATCH 3/4] hexagons work!! hurray! --- Making a Wargame.ipynb | 373 ++++++++++++++++++++++++++++++++++++++++- main.py | 23 ++- strategygame.kv | 39 ++--- 3 files changed, 401 insertions(+), 34 deletions(-) diff --git a/Making a Wargame.ipynb b/Making a Wargame.ipynb index 26a820a..614b7f2 100644 --- a/Making a Wargame.ipynb +++ b/Making a Wargame.ipynb @@ -720,7 +720,189 @@ }, "outputs": [], "source": [ - "#%load strategygame.kv" + "%%file strategygame.kv\n", + "#:import math math\n", + "\n", + ":\n", + " id: _game\n", + " main_map: _main_map\n", + " map_rows: 30\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", + " Label:\n", + " id: _stats\n", + " text: 'status'\n", + " size_hint: 1, .66\n", + " canvas.before:\n", + " Color:\n", + " rgba: .49, .49, .81, 1\n", + " Rectangle:\n", + " pos: _stats.pos\n", + " size: _stats.size\n", + " Button:\n", + " text: 'mini-map'\n", + " size_hint: 1, .33\n", + " background_color: .75, .71, .99, 1\n", + "\n", + "\n", + ":\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", + " 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", + " 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", + " 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", + " size_hint: 1, None\n", + " height: self.width / math.sqrt(3)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "%%file main.py\n", + "import collections\n", + "import random\n", + "\n", + "from kivy import app, properties\n", + "from kivy.uix import button, label\n", + "from kivy.uix.floatlayout import FloatLayout\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", + "\n", + " # Add hex cells to make up the map.\n", + " hex_cell = self.pick_hex_cell(row=row, col=col)\n", + " self.main_map.add_widget(hex_cell)\n", + "\n", + " # Add overlay conditionally.\n", + " if (row % 6 == 2 and col % 2 == 0) or (row % 6 == 5 and col % 2 == 1):\n", + " print('({}, {})'.format(row, col))\n", + " self.add_widget(HexMapControlCell(hex_bind=hex_cell))\n", + "\n", + " @staticmethod\n", + " def pick_hex_cell(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", + " super(HexMapCell, self).__init__(**kwargs)\n", + " self.coords = MapCoords(row, col)\n", + "\n", + "\n", + "class BU(HexMapCell):\n", + " pass\n", + "\n", + "\n", + "class TD(HexMapCell):\n", + " pass\n", + "\n", + "\n", + "class L(HexMapCell):\n", + " pass\n", + "\n", + "\n", + "class R(HexMapCell):\n", + " pass\n", + "\n", + "\n", + "class HexMapControlCell(button.Button):\n", + " def __init__(self, hex_bind=None, **kwargs):\n", + " super(HexMapControlCell, self).__init__(**kwargs)\n", + " self.hex_bind = hex_bind\n", + " self.background_color = random.random(), random.random(), random.random(), 1\n", + " self.bind(pos=self.reposition_control_cell, size=self.resize_control_cell)\n", + " self.text = '({}, {})'.format(self.hex_bind.coords.row, self.hex_bind.coords.col)\n", + "\n", + " def reposition_control_cell(self, obj, value):\n", + " self.pos = self.hex_bind.pos\n", + "\n", + " def resize_control_cell(self, obj, value):\n", + " self.height = self.hex_bind.height * 2\n", + " self.width = self.hex_bind.width * 2\n", + "\n", + "\n", + "class StrategyGameApp(app.App):\n", + " def build(self):\n", + " return StrategyGame()\n", + "\n", + "if __name__ == '__main__':\n", + " StrategyGameApp().run()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Warning** if you run this your will get an error!!" ] }, { @@ -744,7 +926,194 @@ "collapsed": true }, "outputs": [], - "source": [] + "source": [ + "%%file strategygame.kv\n", + "#:import math math\n", + "#:include debug.kv\n", + "\n", + ":\n", + " id: _game\n", + " main_map: _main_map\n", + " map_rows: 30\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", + " Label:\n", + " id: _stats\n", + " text: 'status'\n", + " size_hint: 1, .66\n", + " canvas.before:\n", + " Color:\n", + " rgba: .49, .49, .81, 1\n", + " Rectangle:\n", + " pos: _stats.pos\n", + " size: _stats.size\n", + " Button:\n", + " text: 'mini-map'\n", + " size_hint: 1, .33\n", + " background_color: .75, .71, .99, 1\n", + "\n", + ":\n", + " pos_hint: {'center_x':.5, 'center_y':.5}\n", + " canvas.after:\n", + " Color:\n", + " rgba: 1,1,1,1\n", + " Ellipse:\n", + " segments: 6\n", + " pos: self.pos\n", + " size: min(self.width, self.height), min(self.width, self.height)\n", + "\n", + ":\n", + " size_hint: 1, None\n", + " height: self.width / math.sqrt(3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "%%file main.py\n", + "import collections\n", + "import random\n", + "import math\n", + "from kivy import app, properties\n", + "from kivy.uix import button, label\n", + "from kivy.uix.floatlayout import FloatLayout\n", + "from kivy.graphics import Color, Ellipse, Line\n", + "from kivy.logger import Logger\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", + "\n", + " # Add hex cells to make up the map.\n", + " hex_cell = self.pick_hex_cell(row=row, col=col)\n", + " self.main_map.add_widget(hex_cell)\n", + "\n", + " # Add overlay conditionally.\n", + " if (row % 6 == 1 and col % 2 == 1) or (row % 6 == 4 and col % 2 == 0):\n", + " print('({}, {})'.format(row, col))\n", + " #radius = math.sqrt(hex_cell.width**2 + hex_cell.height**2)\n", + " radius = 2*hex_cell.height\n", + " with hex_cell.canvas.after:\n", + " Color(1,0,1,1)\n", + " hex_cell.ell = Line(circle=(hex_cell.x, hex_cell.y,radius, 0, 360, 6), width=2)\n", + " hex_cell.bind(pos=hex_cell.update_pos, size=hex_cell.update_pos)\n", + "\n", + "\n", + " @staticmethod\n", + " def pick_hex_cell(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", + "\n", + "class HexMapCell(label.Label):\n", + " def __init__(self, row=0, col=0, **kwargs):\n", + " super(HexMapCell, self).__init__(**kwargs)\n", + " self.coords = MapCoords(row, col)\n", + "\n", + " def update_pos(self, instance, value):\n", + " Logger.info(\"StratGame: {}\".format(instance))\n", + " #radius = math.sqrt(self.width**2 + self.height**2)\n", + " radius = 2*self.height\n", + " self.ell.circle = (self.x, self.y, radius, 0, 360, 6)\n", + "\n", + "\n", + "\n", + "class BU(HexMapCell):\n", + " pass\n", + "\n", + "\n", + "class TD(HexMapCell):\n", + " pass\n", + "\n", + "\n", + "class L(HexMapCell):\n", + " pass\n", + "\n", + "\n", + "class R(HexMapCell):\n", + " pass\n", + "\n", + "\n", + "\n", + "class HexMapControlCell(button.Button):\n", + " def __init__(self, hex_bind=None, **kwargs):\n", + " super(HexMapControlCell, self).__init__(**kwargs)\n", + " self.hex_bind = hex_bind\n", + " self.background_color = random.random(), random.random(), random.random(), 1\n", + " self.bind(pos=self.reposition_control_cell, size=self.resize_control_cell)\n", + " self.text = '({}, {})'.format(self.hex_bind.coords.row, self.hex_bind.coords.col)\n", + "\n", + " def reposition_control_cell(self, obj, value):\n", + " self.pos = self.hex_bind.pos\n", + "\n", + " def resize_control_cell(self, obj, value):\n", + " self.height = self.hex_bind.height * 2\n", + " self.width = self.hex_bind.width * 2\n", + "\n", + "\n", + "class StrategyGameApp(app.App):\n", + " def build(self):\n", + " return StrategyGame()\n", + "\n", + "if __name__ == '__main__':\n", + " StrategyGameApp().run()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "What did we learn? \n", + "\n", + "* angle 0 appears to be straight up for a circle or ellipse\n", + "* The radius of the hexagon is 2*height\n", + "* Make sure you fix the aspect ratio of the rectangles you're basing things now\n", + "* " + ] } ], "metadata": { diff --git a/main.py b/main.py index f29b13c..9cfe363 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,11 @@ import collections import random - +import math from kivy import app, properties from kivy.uix import button, label from kivy.uix.floatlayout import FloatLayout +from kivy.graphics import Color, Ellipse, Line +from kivy.logger import Logger MapCoords = collections.namedtuple('MapCoords', ['row', 'col']) @@ -26,9 +28,15 @@ class StrategyGame(FloatLayout): self.main_map.add_widget(hex_cell) # Add overlay conditionally. - if (row % 6 == 2 and col % 2 == 0) or (row % 6 == 5 and col % 2 == 1): + if (row % 6 == 1 and col % 2 == 1) or (row % 6 == 4 and col % 2 == 0): print('({}, {})'.format(row, col)) - self.add_widget(HexMapControlCell(hex_bind=hex_cell)) + #radius = math.sqrt(hex_cell.width**2 + hex_cell.height**2) + radius = 2*hex_cell.height + with hex_cell.canvas.after: + Color(1,0,1,1) + hex_cell.ell = Line(circle=(hex_cell.x, hex_cell.y,radius, 0, 360, 6), width=2) + hex_cell.bind(pos=hex_cell.update_pos, size=hex_cell.update_pos) + @staticmethod def pick_hex_cell(row, col): @@ -53,11 +61,19 @@ class StrategyGame(FloatLayout): return L() + class HexMapCell(label.Label): def __init__(self, row=0, col=0, **kwargs): super(HexMapCell, self).__init__(**kwargs) self.coords = MapCoords(row, col) + def update_pos(self, instance, value): + Logger.info("StratGame: {}".format(instance)) + #radius = math.sqrt(self.width**2 + self.height**2) + radius = 2*self.height + self.ell.circle = (self.x, self.y, radius, 0, 360, 6) + + class BU(HexMapCell): pass @@ -75,6 +91,7 @@ class R(HexMapCell): pass + class HexMapControlCell(button.Button): def __init__(self, hex_bind=None, **kwargs): super(HexMapControlCell, self).__init__(**kwargs) diff --git a/strategygame.kv b/strategygame.kv index 7efe1d1..37efb25 100644 --- a/strategygame.kv +++ b/strategygame.kv @@ -1,4 +1,5 @@ #:import math math +#:include debug.kv : id: _game @@ -30,36 +31,16 @@ size_hint: 1, .33 background_color: .75, .71, .99, 1 - -: - canvas: +: + pos_hint: {'center_x':.5, 'center_y':.5} + canvas.after: Color: - rgba: (1,1,1,1) - Line: - points: (self.x, self.y, self.right, self.top) - width: 2 -: - canvas: - Color: - rgba: (1,1,1,1) - Line: - points: (self.x, self.top, self.right, self.y) - width: 2 -: - canvas: - Color: - rgba: (1,1,1,1) - Line: - points: (self.x, self.y, self.x, self.top) - width: 2 -: - canvas: - Color: - rgba: (1,1,1,1) - Line: - points: (self.right, self.y, self.right, self.top) - width: 2 + rgba: 1,1,1,1 + Ellipse: + segments: 6 + pos: self.pos + size: min(self.width, self.height), min(self.width, self.height) : size_hint: 1, None - height: self.width / math.sqrt(3) + height: self.width / math.sqrt(3) \ No newline at end of file From 8b5b258be757a93a6601bc9ce493481653c4b23d Mon Sep 17 00:00:00 2001 From: Amy Wooding Date: Sat, 4 Jun 2016 23:04:53 -0700 Subject: [PATCH 4/4] added random colours to the hexagons --- Making a Wargame.ipynb | 18 +++++++++++-- main.py | 59 ++++++++++-------------------------------- strategygame.kv | 2 +- 3 files changed, 31 insertions(+), 48 deletions(-) diff --git a/Making a Wargame.ipynb b/Making a Wargame.ipynb index 614b7f2..4589815 100644 --- a/Making a Wargame.ipynb +++ b/Making a Wargame.ipynb @@ -1110,10 +1110,24 @@ "What did we learn? \n", "\n", "* angle 0 appears to be straight up for a circle or ellipse\n", - "* The radius of the hexagon is 2*height\n", + "* The radius of the hexagon is $2*height$ for a pointed top hexagon, and for flat top, the radius of the hexagon is $2*width$\n", "* Make sure you fix the aspect ratio of the rectangles you're basing things now\n", - "* " + "* segments are inscribed **inside** the circle\n", + "* tiling problems are hard!!! get a mathematician.\n", + "* there is a utility for hex codes for colours\n", + "* draw the hexagon using a Line with a circle to make an outline instead of an Ellipse\n", + "\n", + "Cleaned some stuff up and added colours to the hexagons. " ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/main.py b/main.py index 9cfe363..fb38a99 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,14 @@ import collections import random import math + from kivy import app, properties from kivy.uix import button, label from kivy.uix.floatlayout import FloatLayout from kivy.graphics import Color, Ellipse, Line from kivy.logger import Logger +import kivy.utils +from kivy.vector import Vector MapCoords = collections.namedtuple('MapCoords', ['row', 'col']) @@ -24,7 +27,7 @@ class StrategyGame(FloatLayout): col = region % self.map_cols # Add hex cells to make up the map. - hex_cell = self.pick_hex_cell(row=row, col=col) + hex_cell = HexMapCell() self.main_map.add_widget(hex_cell) # Add overlay conditionally. @@ -32,65 +35,31 @@ class StrategyGame(FloatLayout): print('({}, {})'.format(row, col)) #radius = math.sqrt(hex_cell.width**2 + hex_cell.height**2) radius = 2*hex_cell.height + solid_x = hex_cell.x - hex_cell.height*2 + solid_y = hex_cell.y - hex_cell.height*2 + solid_size = (4*hex_cell.height, 4*hex_cell.height) with hex_cell.canvas.after: Color(1,0,1,1) hex_cell.ell = Line(circle=(hex_cell.x, hex_cell.y,radius, 0, 360, 6), width=2) + Color(*kivy.utils.get_random_color(alpha = .5)) + hex_cell.solid = Ellipse(pos = (solid_x, solid_y), size = solid_size, segments = 6 ) hex_cell.bind(pos=hex_cell.update_pos, size=hex_cell.update_pos) - @staticmethod - def pick_hex_cell(row, col): - row_mod = row % 6 - if col % 2 == 0: - if row_mod == 0: - return BU() - elif row_mod in (1, 2): - return L() - elif row_mod == 3: - return TD() - elif row_mod in (4, 5): - return R() - else: - if row_mod == 0: - return TD() - elif row_mod in (1, 2): - return R() - elif row_mod == 3: - return BU() - elif row_mod in (4, 5): - return L() - - - class HexMapCell(label.Label): def __init__(self, row=0, col=0, **kwargs): super(HexMapCell, self).__init__(**kwargs) self.coords = MapCoords(row, col) def update_pos(self, instance, value): - Logger.info("StratGame: {}".format(instance)) #radius = math.sqrt(self.width**2 + self.height**2) radius = 2*self.height + solid_x = self.x - self.height*2 + solid_y = self.y - self.height*2 + solid_size = (4*self.height, 4*self.height) self.ell.circle = (self.x, self.y, radius, 0, 360, 6) - - - -class BU(HexMapCell): - pass - - -class TD(HexMapCell): - pass - - -class L(HexMapCell): - pass - - -class R(HexMapCell): - pass - - + self.solid.pos = (solid_x, solid_y) + self.solid.size = solid_size class HexMapControlCell(button.Button): def __init__(self, hex_bind=None, **kwargs): diff --git a/strategygame.kv b/strategygame.kv index 37efb25..886890a 100644 --- a/strategygame.kv +++ b/strategygame.kv @@ -1,5 +1,5 @@ #:import math math -#:include debug.kv +##:include debug.kv : id: _game