From 8b5b258be757a93a6601bc9ce493481653c4b23d Mon Sep 17 00:00:00 2001 From: Amy Wooding Date: Sat, 4 Jun 2016 23:04:53 -0700 Subject: [PATCH] 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