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/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 diff --git a/hexmap.py b/hexmap.py index 77880df..88ec1ec 100644 --- a/hexmap.py +++ b/hexmap.py @@ -1,11 +1,10 @@ import collections - -from kivy.graphics.context_instructions import Color -from kivy.graphics.vertex_instructions import Line +from math import ceil import kivy.utils from kivy.logger import Logger from kivy.uix.label import Label +from kivy.graphics import Color, Ellipse, Line from kivy.vector import Vector MapCoords = collections.namedtuple('MapCoords', ['row', 'col']) @@ -15,17 +14,46 @@ 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) self.selected = False self.visible_on_map = False self.terrain_colour = Color(0, 0, 0, 1) self.terrain = '' - def map_coordinates(self): - return 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): - map_x, map_y = self.map_coordinates() - return "({}, {}) \n {}".format(map_x, map_y, self.terrain) + return "{}\n{} \n {}".format(self.even_r_coordinate_text(), self.cube_coordinate_text(), terrain) def update_pos(self, instance, value): # Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex. @@ -37,7 +65,7 @@ class HexMapCell(Label): # Resize the outline of the cell. self.ell.circle = (self.x, self.y, radius, 0, 360, 6) - # Resize the actual cell.f + # Resize the actual cell. self.solid.pos = (solid_x, solid_y) self.solid.size = solid_size diff --git a/main.py b/main.py index 5b347ee..d7ec556 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,10 @@ + +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 hexmap import HexMapCell -from kivy import app, properties -from kivy.graphics import Color, Ellipse, Line, Rectangle -from kivy.uix.floatlayout import FloatLayout -from kivy.uix.label import Label -from terrain import Terrains, choose_random_terrain class StrategyGame(FloatLayout): @@ -59,6 +59,7 @@ class StrategyGame(FloatLayout): # Bind the cell code so as to update its position and size when the parent widget resizes. hex_cell.bind(pos=hex_cell.update_pos, size=hex_cell.update_pos) + def update_selected_cell(self, coords, terrain_colour, *args): self.status.text = 'Coords: ({}, {})'.format(coords[0], coords[1]) with self.status.canvas.before: