Merge branch 'llfkj' of https://github.com/llf-amy/gamecamp into llfkj

This commit is contained in:
Amy Wooding 2016-07-16 15:43:58 -04:00
commit 6a41aaaa01
1 changed files with 34 additions and 30 deletions

62
main.py
View File

@ -1,14 +1,10 @@
import collections import collections
import random
import math
from kivy import app, properties from kivy import app, properties
from kivy.uix import button, label from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Color, Ellipse, Line from kivy.graphics import Color, Ellipse, Line
from kivy.logger import Logger
import kivy.utils import kivy.utils
from kivy.vector import Vector
MapCoords = collections.namedtuple('MapCoords', ['row', 'col']) MapCoords = collections.namedtuple('MapCoords', ['row', 'col'])
@ -27,54 +23,62 @@ class StrategyGame(FloatLayout):
col = region % self.map_cols col = region % self.map_cols
# Add hex cells to make up the map. # Add hex cells to make up the map.
hex_cell = HexMapCell() hex_cell = HexMapCell(row, col)
self.main_map.add_widget(hex_cell) self.main_map.add_widget(hex_cell)
# Add overlay conditionally. # Add overlay conditionally.
if (row % 6 == 1 and col % 2 == 1) or (row % 6 == 4 and col % 2 == 0): if (row % 6 == 1 and col % 2 == 1) or (row % 6 == 4 and col % 2 == 0):
print('({}, {})'.format(row, col)) print('({}, {})'.format(row, col))
#radius = math.sqrt(hex_cell.width**2 + hex_cell.height**2)
radius = 2*hex_cell.height # Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex.
radius = 2 * hex_cell.height
solid_x = hex_cell.x - hex_cell.height*2 solid_x = hex_cell.x - hex_cell.height*2
solid_y = hex_cell.y - hex_cell.height*2 solid_y = hex_cell.y - hex_cell.height*2
solid_size = (4*hex_cell.height, 4*hex_cell.height) solid_size = (4*hex_cell.height, 4*hex_cell.height)
with hex_cell.canvas.after: 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) # Create the outline of hexagon, based off the centre of the hex.
Color(*kivy.utils.get_random_color(alpha = .5)) Color(1, 0, 1, 1)
hex_cell.solid = Ellipse(pos = (solid_x, solid_y), size = solid_size, segments = 6 ) hex_cell.ell = Line(circle=(hex_cell.x, hex_cell.y, radius, 0, 360, 6), width=2)
# Create the solid background of the hexagon, from the bottom left coordinate of the hex.
Color(*kivy.utils.get_random_color(alpha=.5))
hex_cell.solid = Ellipse(pos=(solid_x, solid_y), size=solid_size, segments=6)
Color(1, 1, 1, 1)
hex_cell.coord_label = Label(
text=hex_cell.coordinate_text(), center_x=hex_cell.x, center_y=hex_cell.y)
# 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) hex_cell.bind(pos=hex_cell.update_pos, size=hex_cell.update_pos)
class HexMapCell(label.Label): class HexMapCell(Label):
def __init__(self, row=0, col=0, **kwargs): def __init__(self, row=0, col=0, **kwargs):
super(HexMapCell, self).__init__(**kwargs) super(HexMapCell, self).__init__(**kwargs)
self.coords = MapCoords(row, col) self.coords = MapCoords(row, col)
def coordinate_text(self):
return '({}, {})'.format(self.coords.row, self.coords.col)
def update_pos(self, instance, value): def update_pos(self, instance, value):
#radius = math.sqrt(self.width**2 + self.height**2) # Determine the location of the solid hexagon cell. Needs to be offset from the centre of the hex.
radius = 2*self.height radius = 2 * self.height
solid_x = self.x - self.height*2 solid_x = self.x - self.height*2
solid_y = self.y - self.height*2 solid_y = self.y - self.height*2
solid_size = (4*self.height, 4*self.height) 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) self.ell.circle = (self.x, self.y, radius, 0, 360, 6)
# Resize the actual cell.
self.solid.pos = (solid_x, solid_y) self.solid.pos = (solid_x, solid_y)
self.solid.size = solid_size self.solid.size = solid_size
class HexMapControlCell(button.Button): self.coord_label.center_x = self.x
def __init__(self, hex_bind=None, **kwargs): self.coord_label.center_y = self.y
super(HexMapControlCell, self).__init__(**kwargs)
self.hex_bind = hex_bind
self.background_color = random.random(), random.random(), random.random(), 1
self.bind(pos=self.reposition_control_cell, size=self.resize_control_cell)
self.text = '({}, {})'.format(self.hex_bind.coords.row, self.hex_bind.coords.col)
def reposition_control_cell(self, obj, value):
self.pos = self.hex_bind.pos
def resize_control_cell(self, obj, value):
self.height = self.hex_bind.height * 2
self.width = self.hex_bind.width * 2
class StrategyGameApp(app.App): class StrategyGameApp(app.App):