Merge pull request #1 from llfkj/dorian

add hex grid
This commit is contained in:
Dorian Pula 2016-06-04 14:47:52 -07:00
commit f8d816e069
3 changed files with 155 additions and 29 deletions

94
Making a Wargame.ipynb Normal file
View File

@ -0,0 +1,94 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We want to make a turn-based strategy game, based on a **hexagonal grid**.\n",
"\n",
"Wait, how do we even lay out a hexagonal grid in kivy?\n",
"\n",
" http://playtechs.blogspot.com/2007/04/hex-grids.html\n",
" \n",
" Also\n",
" \n",
" http://gamedev.stackexchange.com/questions/15881/hexagonal-grid-tiles-tutorials\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ignore device orientation."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's lay out a grid. First we need to draw hexagons. Clone gamecamp. change the object names.\n",
"Can we lay out the basic screen?"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "ValueError",
"evalue": "'strategygame.kv' was not found in history, as a file, url, nor in the user namespace.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-3-20c9424f0bfd>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmagic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mu'load strategygame.kv'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/Users/kjell/anaconda/envs/py27/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc\u001b[0m in \u001b[0;36mmagic\u001b[0;34m(self, arg_s)\u001b[0m\n\u001b[1;32m 2161\u001b[0m \u001b[0mmagic_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmagic_arg_s\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marg_s\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpartition\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m' '\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2162\u001b[0m \u001b[0mmagic_name\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmagic_name\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlstrip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprefilter\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mESC_MAGIC\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2163\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun_line_magic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmagic_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmagic_arg_s\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2164\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2165\u001b[0m \u001b[0;31m#-------------------------------------------------------------------------\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/Users/kjell/anaconda/envs/py27/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc\u001b[0m in \u001b[0;36mrun_line_magic\u001b[0;34m(self, magic_name, line)\u001b[0m\n\u001b[1;32m 2082\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'local_ns'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getframe\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstack_depth\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf_locals\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2083\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbuiltin_trap\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2084\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\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[0m\n\u001b[0m\u001b[1;32m 2085\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2086\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<decorator-gen-45>\u001b[0m in \u001b[0;36mload\u001b[0;34m(self, arg_s)\u001b[0m\n",
"\u001b[0;32m/Users/kjell/anaconda/envs/py27/lib/python2.7/site-packages/IPython/core/magic.pyc\u001b[0m in \u001b[0;36m<lambda>\u001b[0;34m(f, *a, **k)\u001b[0m\n\u001b[1;32m 191\u001b[0m \u001b[0;31m# but it's overkill for just that one bit of state.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 192\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmagic_deco\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 193\u001b[0;31m \u001b[0mcall\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mlambda\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 194\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 195\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcallable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/Users/kjell/anaconda/envs/py27/lib/python2.7/site-packages/IPython/core/magics/code.pyc\u001b[0m in \u001b[0;36mload\u001b[0;34m(self, arg_s)\u001b[0m\n\u001b[1;32m 316\u001b[0m \u001b[0msearch_ns\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'n'\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mopts\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 317\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 318\u001b[0;31m \u001b[0mcontents\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshell\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfind_user_code\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msearch_ns\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msearch_ns\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 319\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 320\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;34m's'\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mopts\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/Users/kjell/anaconda/envs/py27/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc\u001b[0m in \u001b[0;36mfind_user_code\u001b[0;34m(self, target, raw, py_only, skip_encoding_cookie, search_ns)\u001b[0m\n\u001b[1;32m 3184\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3185\u001b[0m raise ValueError((\"'%s' was not found in history, as a file, url, \"\n\u001b[0;32m-> 3186\u001b[0;31m \"nor in the user namespace.\") % target)\n\u001b[0m\u001b[1;32m 3187\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3188\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcodeobj\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstring_types\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mValueError\u001b[0m: 'strategygame.kv' was not found in history, as a file, url, nor in the user namespace."
]
}
],
"source": [
"%load strategygame.kv"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

61
main.py
View File

@ -5,7 +5,7 @@ from kivy import properties
from kivy import graphics from kivy import graphics
from kivy.uix import label from kivy.uix import label
from kivy.uix.floatlayout import FloatLayout from kivy.uix.floatlayout import FloatLayout
import math
MapCoords = collections.namedtuple('MapCoords', ['row', 'col']) MapCoords = collections.namedtuple('MapCoords', ['row', 'col'])
@ -22,42 +22,45 @@ class StrategyGame(FloatLayout):
for region in xrange(0, number_of_regions): for region in xrange(0, number_of_regions):
row = region / self.map_cols row = region / self.map_cols
col = region % self.map_cols col = region % self.map_cols
self.main_map.add_widget(HexMapCell(row=row, col=col)) self.main_map.add_widget(self.pick_hex_cell(row=row, col=col))
def pick_hex_cell(self, 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): class HexMapCell(label.Label):
def __init__(self, row=0, col=0, **kwargs): def __init__(self, row=0, col=0, **kwargs):
self.region_in_map = MapCoords(row, col) self.region_in_map = MapCoords(row, col)
super(HexMapCell, self).__init__(**kwargs) super(HexMapCell, self).__init__(**kwargs)
self.draw_hex_edge()
def draw_hex_edge(self): class BU(HexMapCell):
edge = '' pass
if self.region_in_map.col % 2 == 0: class TD(HexMapCell):
row_mod = self.region_in_map.row % 6 pass
if row_mod == 0: class L(HexMapCell):
edge = 'BU' pass
with self.canvas: class R(HexMapCell):
graphics.Color(1, 1, 1, 1) pass
graphics.Line(points=[self.x, self.y, self.width + self.x, self.height + self.y])
elif row_mod in (1, 2):
edge = 'L '
elif row_mod == 3:
edge = 'TD'
elif row_mod in (4, 5):
edge = ' R'
else:
row_mod = self.region_in_map.row % 6
if row_mod == 0:
edge = 'TD'
elif row_mod in (1, 2):
edge = ' R'
elif row_mod == 3:
edge = 'BU'
elif row_mod in (4, 5):
edge = 'L '
self.text = edge
class StrategyGameApp(App): class StrategyGameApp(App):
def build(self): def build(self):

View File

@ -22,3 +22,32 @@
DebugLabel: DebugLabel:
text: 'mini-map' text: 'mini-map'
size_hint: 1, .33 size_hint: 1, .33
<BU>:
canvas:
Color:
rgba: (1,1,1,1)
Line:
points: (self.x, self.y, self.right, self.top)
width: 2
<TD>:
canvas:
Color:
rgba: (1,1,1,1)
Line:
points: (self.x, self.top, self.right, self.y)
width: 2
<L>:
canvas:
Color:
rgba: (1,1,1,1)
Line:
points: (self.x, self.y, self.x, self.top)
width: 2
<R>:
canvas:
Color:
rgba: (1,1,1,1)
Line:
points: (self.right, self.y, self.right, self.top)
width: 2