forked from noobien/pytk-hexagon-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexagon.py
153 lines (134 loc) · 5.12 KB
/
hexagon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/python
# -*- coding: utf-8 -*-
from tkinter import *
from math import cos, sin, sqrt, radians
#------------------------------------------------------------------------------
class Field:
types = {
"grass": "#a1e2a1",
"water": "#60ace6",
"moutain": "#a1603a"
}
def __init__(self, parent, x, y, kind, size):
self.parent = parent
self.x = x
self.y = y
self.kind = kind
self.color = Field.types[self.kind]
self.selected = False
def draw(self):
FillHexagon(self.parent, self.x, self.y, self.size, self.color)
def enlight(self):
pass
#------------------------------------------------------------------------------
class StrokeHexagon:
def __init__(self, parent, x, y, length, color):
self.parent = parent # canvas
self.x = x # top left x
self.y = y # top left y
self.length = length # length of a side
self.color = color # outline color
self.draw()
def draw(self):
start_x = self.x
start_y = self.y
angle = 60
for i in range(6):
end_x = start_x + self.length * cos(radians(angle * i))
end_y = start_y + self.length * sin(radians(angle * i))
self.parent.create_line(start_x,
start_y,
end_x,
end_y,
fill=self.color)
start_x = end_x
start_y = end_y
#------------------------------------------------------------------------------
class FillHexagon:
def __init__(self, parent, x, y, length, color, tags):
self.parent = parent # canvas
self.x = x # top left x
self.y = y # top left y
self.length = length # length of a side
self.color = color # fill color
self.selected = False
self.tags = tags
self.draw()
def draw(self):
start_x = self.x
start_y = self.y
angle = 60
coords = []
for i in range(6):
end_x = start_x + self.length * cos(radians(angle * i))
end_y = start_y + self.length * sin(radians(angle * i))
coords.append([start_x, start_y])
start_x = end_x
start_y = end_y
self.parent.create_polygon(coords[0][0],
coords[0][1],
coords[1][0],
coords[1][1],
coords[2][0],
coords[2][1],
coords[3][0],
coords[3][1],
coords[4][0],
coords[4][1],
coords[5][0],
coords[5][1],
fill=self.color,
outline="gray",
tags=self.tags)
#---------------------------------------------------------
class App(Tk):
def __init__(self):
Tk.__init__(self)
self.title("Hexagon Grid")
self.can = Canvas(self, width=400, height=300, bg="#a1e2a1")
self.can.pack()
self.hexagons = []
self.initGrid(10, 7, 30, debug=False)
self.can.bind("<Button-1>", self.click)
def initGrid(self, cols, rows, size, debug):
"""
2d grid of hexagons
"""
for c in range(cols):
if c % 2 == 0:
offset = size * sqrt(3) / 2
else:
offset = 0
for r in range(rows):
h = FillHexagon(self.can,
c * (size * 1.5),
(r * (size * sqrt(3))) + offset ,
size,
"#a1e2a1",
"{}.{}".format(r, c))
self.hexagons.append(h)
if debug :
coords = "{}, {}".format(r, c)
self.can.create_text(c * (size * 1.5) + (size/2),
(r * (size * sqrt(3))) + offset + (size/2),
text=coords)
def click(self, evt):
"""
hexagon detection on mouse click
"""
x , y = evt.x, evt.y
for i in self.hexagons:
i.selected = False
i.isNeighbour = False
self.can.itemconfigure(i.tags, fill=i.color)
clicked = self.can.find_closest(x, y)[0] # find closest
self.hexagons[int(clicked)-1].selected = True
for i in self.hexagons: # re-configure selected only
if i.selected:
self.can.itemconfigure(i.tags, fill="#53ca53")
if i.isNeighbour:
self.can.itemconfigure(i.tags, fill="#76d576")
#----------------------------------------------------------
if __name__ =='__main__':
app =App()
app.mainloop()