doric-engine/main.py

117 lines
3.4 KiB
Python
Raw Normal View History

import collections
import random
2016-06-05 01:20:00 -04:00
import math
from kivy import app, properties
from kivy.uix import button, label
2016-06-04 14:31:07 -04:00
from kivy.uix.floatlayout import FloatLayout
2016-06-05 01:20:00 -04:00
from kivy.graphics import Color, Ellipse, Line
from kivy.logger import Logger
2016-06-04 14:59:40 -04:00
MapCoords = collections.namedtuple('MapCoords', ['row', 'col'])
2016-06-04 14:59:40 -04:00
class StrategyGame(FloatLayout):
main_map = properties.ObjectProperty(None)
map_rows = properties.NumericProperty(0)
map_cols = properties.NumericProperty(0)
def __init__(self, **kwargs):
super(StrategyGame, self).__init__(**kwargs)
number_of_regions = self.map_rows * self.map_cols
for region in xrange(0, number_of_regions):
row = region / self.map_cols
col = region % self.map_cols
# Add hex cells to make up the map.
hex_cell = self.pick_hex_cell(row=row, col=col)
self.main_map.add_widget(hex_cell)
# Add overlay conditionally.
2016-06-05 01:20:00 -04:00
if (row % 6 == 1 and col % 2 == 1) or (row % 6 == 4 and col % 2 == 0):
print('({}, {})'.format(row, col))
2016-06-05 01:20:00 -04:00
#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):
2016-06-04 17:41:13 -04:00
row_mod = row % 6
if col % 2 == 0:
2016-06-04 15:51:50 -04:00
if row_mod == 0:
2016-06-04 17:41:13 -04:00
return BU()
2016-06-04 15:51:50 -04:00
elif row_mod in (1, 2):
2016-06-04 17:41:13 -04:00
return L()
2016-06-04 15:51:50 -04:00
elif row_mod == 3:
2016-06-04 17:41:13 -04:00
return TD()
2016-06-04 15:51:50 -04:00
elif row_mod in (4, 5):
2016-06-04 17:41:13 -04:00
return R()
2016-06-04 15:51:50 -04:00
else:
if row_mod == 0:
2016-06-04 17:41:13 -04:00
return TD()
2016-06-04 15:51:50 -04:00
elif row_mod in (1, 2):
2016-06-04 17:41:13 -04:00
return R()
2016-06-04 15:51:50 -04:00
elif row_mod == 3:
2016-06-04 17:41:13 -04:00
return BU()
2016-06-04 15:51:50 -04:00
elif row_mod in (4, 5):
2016-06-04 17:41:13 -04:00
return L()
2016-06-05 01:20:00 -04:00
2016-06-04 17:41:13 -04:00
class HexMapCell(label.Label):
def __init__(self, row=0, col=0, **kwargs):
super(HexMapCell, self).__init__(**kwargs)
self.coords = MapCoords(row, col)
2016-06-04 17:41:13 -04:00
2016-06-05 01:20:00 -04:00
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)
2016-06-04 17:41:13 -04:00
class BU(HexMapCell):
pass
2016-06-04 17:41:13 -04:00
class TD(HexMapCell):
pass
2016-06-04 17:41:13 -04:00
class L(HexMapCell):
pass
2016-06-04 17:41:13 -04:00
class R(HexMapCell):
pass
2016-06-04 15:51:50 -04:00
2016-06-04 14:59:40 -04:00
2016-06-05 01:20:00 -04:00
class HexMapControlCell(button.Button):
def __init__(self, hex_bind=None, **kwargs):
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):
2016-06-04 14:31:07 -04:00
def build(self):
2016-06-04 14:59:40 -04:00
return StrategyGame()
2016-06-04 14:31:07 -04:00
if __name__ == '__main__':
2016-06-04 14:59:40 -04:00
StrategyGameApp().run()