-
Notifications
You must be signed in to change notification settings - Fork 6
/
example_hex.py
79 lines (63 loc) · 2.58 KB
/
example_hex.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
import numpy as np
import pygame as pg
import hexy as hx
def make_hex_surface(color, radius, border_color=(100, 100, 100), border=True, hollow=False):
"""
Draws a hexagon with gray borders on a pygame surface.
:param color: The fill color of the hexagon.
:param radius: The radius (from center to any corner) of the hexagon.
:param border_color: Color of the border.
:param border: Draws border if True.
:param hollow: Does not fill hex with color if True.
:return: A pygame surface with a hexagon drawn on it
"""
angles_in_radians = np.deg2rad([60 * i + 30 for i in range(6)])
x = radius * np.cos(angles_in_radians)
y = radius * np.sin(angles_in_radians)
points = np.round(np.vstack([x, y]).T)
sorted_x = sorted(points[:, 0])
sorted_y = sorted(points[:, 1])
minx = sorted_x[0]
maxx = sorted_x[-1]
miny = sorted_y[0]
maxy = sorted_y[-1]
sorted_idxs = np.lexsort((points[:, 0], points[:, 1]))
surf_size = np.array((maxx - minx, maxy - miny)) * 2 + 1
center = surf_size / 2
surface = pg.Surface(surf_size)
surface.set_colorkey((0, 0, 0))
# Set alpha if color has 4th coordinate.
if len(color) >= 4:
surface.set_alpha(color[-1])
# fill if not hollow.
if not hollow:
pg.draw.polygon(surface, color, points + center, 0)
points[sorted_idxs[-1:-4:-1]] += [0, 1]
# if border is true or hollow is true draw border.
if border or hollow:
pg.draw.lines(surface, border_color, True, points + center, 1)
return surface
class ExampleHex(hx.HexTile):
def __init__(self, axial_coordinates, color, radius):
self.axial_coordinates = np.array([axial_coordinates])
self.cube_coordinates = hx.axial_to_cube(self.axial_coordinates)
self.position = hx.axial_to_pixel(self.axial_coordinates, radius)
self.color = color
self.radius = radius
self.image = make_hex_surface(color, radius)
self.value = None
def set_value(self, value):
self.value = value
def get_draw_position(self):
"""
Get the location to draw this hex so that the center of the hex is at `self.position`.
:return: The location to draw this hex so that the center of the hex is at `self.position`.
"""
draw_position = self.position[0] - [self.image.get_width() / 2, self.image.get_height() / 2]
return draw_position
def get_position(self):
"""
Retrieves the location of the center of the hex.
:return: The location of the center of the hex.
"""
return self.position[0]