-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
93 lines (66 loc) · 2.08 KB
/
main.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
import sys
from time import time
from nbinput import *
from common import *
import game as g
class NamedHex(Hex):
def __init__(self, q, r, name=''):
super().__init__(q, r)
self._name = name
@property
def name(self):
return '{:2}'.format(self._name[:2])
@name.setter
def name(self, name):
self._name = name
def __str__(self):
return self.name
def print_hex(cells, player):
cart_cells = [cell.to_cart() for cell in cells]
player = player.to_cart()
min_x = min(cart_cells, key=lambda c: c.x).x
min_y = min(cart_cells, key=lambda c: c.y).y
max_x = max(cart_cells, key=lambda c: c.x).x
max_y = max(cart_cells, key=lambda c: c.y).y
x_len = 3 * (1+max_x-min_x)
y_len = 3 + max_y-min_y
out = [[' ' for x in range(x_len)] for y in range(y_len)]
for c_cell, cell in zip(cart_cells, cells):
x = (c_cell.x - min_x) * 3
y = c_cell.y - min_y
out[y ][x+1:x+3] = '__'
out[y+1][x :x+4] = '/{}\\'.format('##' if player == c_cell else cell)
out[y+2][x :x+4] = '\\__/'
print('\n'.join(''.join(row) for row in out))
def gen_board(radius):
return [
NamedHex(q, r)
for q in range(-radius, 1+radius)
for r in range(-radius, 1+radius)
if abs(q+r) <= radius
]
def main():
FPS = 40
IPS = 10
last_frame = 0
last_input = 0
change = True
board = gen_board(5 if len(sys.argv) < 2 else int(sys.argv[1]))
player = Cube(0, 0, 0)
with NonBlockingInput() as nbi:
while True:
c = escape_code(nbi)
if c and time() > last_input + 1/IPS:
c = c.lower()
if c == chr(27):
sys.exit('Quitting')
d = g.get_dir(c)
player += d
if d != Cube(0,0,0):
change = True
if change and time() > last_frame + 1/FPS:
last_frame = time()
change = False
print_hex(board, player)
if __name__ == '__main__':
main()