From 54dbdfad9481836d7aa910215517219ef6d83e1d Mon Sep 17 00:00:00 2001 From: Amy Wooding Date: Fri, 15 Jul 2016 11:48:18 -0400 Subject: [PATCH 1/6] Add missing hexagon.kv file from repo. --- hexagon.kv | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 hexagon.kv diff --git a/hexagon.kv b/hexagon.kv new file mode 100644 index 0000000..6669fa4 --- /dev/null +++ b/hexagon.kv @@ -0,0 +1,20 @@ +#:include debug.kv +BoxLayout: + orientation: 'horizontal' + Hex: + pos: self.pos + DebugLabel: + text: '1' + Hex: + pos: self.pos + DebugLabel: + text: '1' +: + pos_hint: {'center_x':.5, 'center_y':.5} + canvas.after: + Color: + rgba: 1,1,1,1 + Ellipse: + segments: 6 + pos: self.pos + size: min(self.width, self.height), min(self.width, self.height) \ No newline at end of file From 2b9f4dba36ac8c855c183f1d1db2a5a5a641cbae Mon Sep 17 00:00:00 2001 From: Amy Wooding Date: Sat, 16 Jul 2016 17:39:03 -0400 Subject: [PATCH 2/6] Added even_r_coords and cube_coords to HexMapCell. That is, hexagons now know they're coordinates. --- main.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 4f66fca..d7f27f5 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,7 @@ from kivy.uix.label import Label from kivy.uix.floatlayout import FloatLayout from kivy.graphics import Color, Ellipse, Line import kivy.utils +from math import ceil MapCoords = collections.namedtuple('MapCoords', ['row', 'col']) @@ -60,15 +61,40 @@ class HexMapCell(Label): def __init__(self, row=0, col=0, **kwargs): super(HexMapCell, self).__init__(**kwargs) self.coords = MapCoords(row, col) + ## set the cube coordinates of the hexagon + ## as [x, y, z] + self.cube_coords = self.even_r_to_cube(self.coords.row / 3, self.coords.col / 2) + + + def even_r_to_cube(self, row, col): + '''compute cube coordinates from even-r hex coordinates''' + x = int(col - ceil(float(row)/2)) + z = row + y = - x - z + return([x, y, z]) + + def cube_to_even_r(self, x, y, z): + row = int(x + ceil(z / 2)) + col = z + return ([row, col]) + + @property + def even_r_coords(self): + '''return even-r coordinates of the hexagon.''' + return(self.cube_to_even_r(*self.cube_coords)) def coordinate_text(self): return '({}, {})'.format(self.coords.row, self.coords.col) - def map_coordinate_text(self): - return '[{}, {}]'.format(self.coords.row / 3, self.coords.col / 2) + def even_r_coordinate_text(self): + return '{}'.format(self.even_r_coords) + + def cube_coordinate_text(self): + x, y, z = self.cube_coords + return '{}\n{}\n{}'.format(x, y, z) def map_display_text(self): - return "{}\n{}".format(self.coordinate_text(), self.map_coordinate_text()) + return "{}\n{}".format(self.even_r_coordinate_text(), self.cube_coordinate_text()) def update_pos(self, instance, value): # Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex. From cf6ef569a594224d2113e999018370acade3dc2e Mon Sep 17 00:00:00 2001 From: Amy Wooding Date: Sat, 16 Jul 2016 17:45:02 -0400 Subject: [PATCH 3/6] Added setter to make sure that the even-r and cube coordinates never get out of sync. --- main.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index d7f27f5..4e47e31 100644 --- a/main.py +++ b/main.py @@ -83,6 +83,10 @@ class HexMapCell(Label): '''return even-r coordinates of the hexagon.''' return(self.cube_to_even_r(*self.cube_coords)) + @even_r_coords.setter + def even_r_coords(self, value): + self.cube_coords = self.even_r_to_cube(*value) + def coordinate_text(self): return '({}, {})'.format(self.coords.row, self.coords.col) @@ -90,8 +94,7 @@ class HexMapCell(Label): return '{}'.format(self.even_r_coords) def cube_coordinate_text(self): - x, y, z = self.cube_coords - return '{}\n{}\n{}'.format(x, y, z) + return '{}\n{}\n{}'.format(*self.cube_coords) def map_display_text(self): return "{}\n{}".format(self.even_r_coordinate_text(), self.cube_coordinate_text()) From acf52537a2ca19111d5956261213ba5a73d64a4d Mon Sep 17 00:00:00 2001 From: Amy Wooding Date: Sat, 16 Jul 2016 17:49:58 -0400 Subject: [PATCH 4/6] Moved HexMapCell out of main.py into its own module. --- hexmap.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 64 +---------------------------------------------------- 2 files changed, 67 insertions(+), 63 deletions(-) create mode 100644 hexmap.py diff --git a/hexmap.py b/hexmap.py new file mode 100644 index 0000000..401f9c1 --- /dev/null +++ b/hexmap.py @@ -0,0 +1,66 @@ +import collections + +from math import ceil +from kivy.uix.label import Label + +MapCoords = collections.namedtuple('MapCoords', ['row', 'col']) + +class HexMapCell(Label): + def __init__(self, row=0, col=0, **kwargs): + super(HexMapCell, self).__init__(**kwargs) + self.coords = MapCoords(row, col) + ## set the cube coordinates of the hexagon + ## as [x, y, z] + self.cube_coords = self.even_r_to_cube(self.coords.row / 3, self.coords.col / 2) + + + def even_r_to_cube(self, row, col): + '''compute cube coordinates from even-r hex coordinates''' + x = int(col - ceil(float(row)/2)) + z = row + y = - x - z + return([x, y, z]) + + def cube_to_even_r(self, x, y, z): + row = int(x + ceil(z / 2)) + col = z + return ([row, col]) + + @property + def even_r_coords(self): + '''return even-r coordinates of the hexagon.''' + return(self.cube_to_even_r(*self.cube_coords)) + + @even_r_coords.setter + def even_r_coords(self, value): + self.cube_coords = self.even_r_to_cube(*value) + + def coordinate_text(self): + return '({}, {})'.format(self.coords.row, self.coords.col) + + def even_r_coordinate_text(self): + return '{}'.format(self.even_r_coords) + + def cube_coordinate_text(self): + return '{}\n{}\n{}'.format(*self.cube_coords) + + def map_display_text(self): + return "{}\n{}".format(self.even_r_coordinate_text(), self.cube_coordinate_text()) + + def update_pos(self, instance, value): + # Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex. + 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) + + # Resize the outline of the cell. + self.ell.circle = (self.x, self.y, radius, 0, 360, 6) + + # Resize the actual cell. + self.solid.pos = (solid_x, solid_y) + self.solid.size = solid_size + + self.coord_label.center_x = self.x + self.coord_label.center_y = self.y + diff --git a/main.py b/main.py index 4e47e31..63a96c4 100644 --- a/main.py +++ b/main.py @@ -1,13 +1,11 @@ -import collections from kivy import app, properties from kivy.uix.label import Label from kivy.uix.floatlayout import FloatLayout from kivy.graphics import Color, Ellipse, Line import kivy.utils -from math import ceil +from hexmap import HexMapCell -MapCoords = collections.namedtuple('MapCoords', ['row', 'col']) class StrategyGame(FloatLayout): @@ -57,66 +55,6 @@ class StrategyGame(FloatLayout): hex_cell.bind(pos=hex_cell.update_pos, size=hex_cell.update_pos) -class HexMapCell(Label): - def __init__(self, row=0, col=0, **kwargs): - super(HexMapCell, self).__init__(**kwargs) - self.coords = MapCoords(row, col) - ## set the cube coordinates of the hexagon - ## as [x, y, z] - self.cube_coords = self.even_r_to_cube(self.coords.row / 3, self.coords.col / 2) - - - def even_r_to_cube(self, row, col): - '''compute cube coordinates from even-r hex coordinates''' - x = int(col - ceil(float(row)/2)) - z = row - y = - x - z - return([x, y, z]) - - def cube_to_even_r(self, x, y, z): - row = int(x + ceil(z / 2)) - col = z - return ([row, col]) - - @property - def even_r_coords(self): - '''return even-r coordinates of the hexagon.''' - return(self.cube_to_even_r(*self.cube_coords)) - - @even_r_coords.setter - def even_r_coords(self, value): - self.cube_coords = self.even_r_to_cube(*value) - - def coordinate_text(self): - return '({}, {})'.format(self.coords.row, self.coords.col) - - def even_r_coordinate_text(self): - return '{}'.format(self.even_r_coords) - - def cube_coordinate_text(self): - return '{}\n{}\n{}'.format(*self.cube_coords) - - def map_display_text(self): - return "{}\n{}".format(self.even_r_coordinate_text(), self.cube_coordinate_text()) - - def update_pos(self, instance, value): - # Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex. - 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) - - # Resize the outline of the cell. - self.ell.circle = (self.x, self.y, radius, 0, 360, 6) - - # Resize the actual cell. - self.solid.pos = (solid_x, solid_y) - self.solid.size = solid_size - - self.coord_label.center_x = self.x - self.coord_label.center_y = self.y - - class StrategyGameApp(app.App): def build(self): return StrategyGame() From 032f6db266dc3813a553bd9f8440fc22c98d5b43 Mon Sep 17 00:00:00 2001 From: Amy Wooding Date: Sat, 16 Jul 2016 19:16:41 -0400 Subject: [PATCH 5/6] Started setting things up to test the hexmap coordinate logic separately from the game. --- Making a Wargame.ipynb | 66 ++++++++++++++++++++++++++++++++---------- hexmap.py | 1 - main.py | 1 - 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/Making a Wargame.ipynb b/Making a Wargame.ipynb index 4589815..f7eadd4 100644 --- a/Making a Wargame.ipynb +++ b/Making a Wargame.ipynb @@ -57,7 +57,7 @@ " size_hint: 1, .66\n", " DebugLabel:\n", " text: 'mini-map'\n", - " size_hint: 1, .33\n" + " size_hint: 1, .33" ] }, { @@ -82,7 +82,7 @@ " return StrategyGame()\n", "\n", "if __name__ == '__main__':\n", - " StrategyGameApp().run()\n" + " StrategyGameApp().run()" ] }, { @@ -102,7 +102,7 @@ }, "outputs": [], "source": [ - "#!python main.py\n" + "#!python main.py" ] }, { @@ -140,7 +140,7 @@ " size_hint: 1, .66\n", " DebugLabel:\n", " text: 'mini-map'\n", - " size_hint: 1, .33\n" + " size_hint: 1, .33" ] }, { @@ -199,7 +199,7 @@ " size_hint: 1, .66\n", " DebugLabel:\n", " text: 'mini-map'\n", - " size_hint: 1, .33\n" + " size_hint: 1, .33" ] }, { @@ -237,7 +237,7 @@ " return StrategyGame()\n", "\n", "if __name__ == '__main__':\n", - " StrategyGameApp().run()\n" + " StrategyGameApp().run()" ] }, { @@ -290,7 +290,7 @@ " size_hint: 1, .66\n", " DebugLabel:\n", " text: 'mini-map'\n", - " size_hint: 1, .33\n" + " size_hint: 1, .33" ] }, { @@ -328,7 +328,7 @@ " return StrategyGame()\n", "\n", "if __name__ == '__main__':\n", - " StrategyGameApp().run()\n" + " StrategyGameApp().run()" ] }, { @@ -431,7 +431,7 @@ " return StrategyGame()\n", "\n", "if __name__ == '__main__':\n", - " StrategyGameApp().run()\n" + " StrategyGameApp().run()" ] }, { @@ -503,7 +503,7 @@ " return HelloWorld()\n", "\n", "if __name__ == '__main__':\n", - " HelloWorldApp().run()\n" + " HelloWorldApp().run()" ] }, { @@ -538,7 +538,7 @@ " size_hint: 1, .66\n", " DebugLabel:\n", " text: 'mini-map'\n", - " size_hint: 1, .33\n" + " size_hint: 1, .33" ] }, { @@ -619,7 +619,7 @@ " rgba: (1,1,1,1)\n", " Line:\n", " points: (self.right, self.y, self.right, self.top)\n", - " width: 2\n" + " width: 2" ] }, { @@ -700,7 +700,7 @@ " return StrategyGame()\n", "\n", "if __name__ == '__main__':\n", - " StrategyGameApp().run()\n" + " StrategyGameApp().run()" ] }, { @@ -785,7 +785,7 @@ "\n", ":\n", " size_hint: 1, None\n", - " height: self.width / math.sqrt(3)\n" + " height: self.width / math.sqrt(3)" ] }, { @@ -895,7 +895,7 @@ " return StrategyGame()\n", "\n", "if __name__ == '__main__':\n", - " StrategyGameApp().run()\n" + " StrategyGameApp().run()" ] }, { @@ -1100,7 +1100,7 @@ " return StrategyGame()\n", "\n", "if __name__ == '__main__':\n", - " StrategyGameApp().run()\n" + " StrategyGameApp().run()" ] }, { @@ -1120,6 +1120,40 @@ "Cleaned some stuff up and added colours to the hexagons. " ] }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "## Test out the hexmap conversion code" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "ename": "TypeError", + "evalue": "must be type, not classobj", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mfoo\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mHexMapCell\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mhex_cell\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mHexMapCell\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m9\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/Users/amywooding/python-code/gamecamp/foo.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, row, col, **kwargs)\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;32mclass\u001b[0m \u001b[0mHexMapCell\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mObject\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrow\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcol\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\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[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0;31m#super(HexMapCell, self).__init__(**kwargs)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcoords\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mMapCoords\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrow\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;31m## set the cube coordinates of the hexagon\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: must be type, not classobj" + ] + } + ], + "source": [ + "from foo import HexMapCell\n", + "hex_cell = HexMapCell(9, 4)" + ] + }, { "cell_type": "code", "execution_count": null, diff --git a/hexmap.py b/hexmap.py index 401f9c1..605c3fb 100644 --- a/hexmap.py +++ b/hexmap.py @@ -13,7 +13,6 @@ class HexMapCell(Label): ## as [x, y, z] self.cube_coords = self.even_r_to_cube(self.coords.row / 3, self.coords.col / 2) - def even_r_to_cube(self, row, col): '''compute cube coordinates from even-r hex coordinates''' x = int(col - ceil(float(row)/2)) diff --git a/main.py b/main.py index 63a96c4..66502d2 100644 --- a/main.py +++ b/main.py @@ -7,7 +7,6 @@ import kivy.utils from hexmap import HexMapCell - class StrategyGame(FloatLayout): main_map = properties.ObjectProperty(None) map_rows = properties.NumericProperty(0) From 21b993de085736b52c6a22b08a9644cf24c3694a Mon Sep 17 00:00:00 2001 From: Amy Wooding Date: Sat, 16 Jul 2016 19:32:57 -0400 Subject: [PATCH 6/6] Added missing import statements. --- hexmap.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hexmap.py b/hexmap.py index 9a8bff0..75eac3c 100644 --- a/hexmap.py +++ b/hexmap.py @@ -2,6 +2,8 @@ import collections from math import ceil from kivy.uix.label import Label +from kivy.graphics import Color, Ellipse, Line +import kivy.utils MapCoords = collections.namedtuple('MapCoords', ['row', 'col'])