-
Notifications
You must be signed in to change notification settings - Fork 0
/
entangled.py
438 lines (359 loc) · 14.9 KB
/
entangled.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# developed by Jeremy Gangnier - http://jergagnier.wix.com/
# no licensing information provided
# code improvements by [email protected]
import os
import json
import time
import pygame as pg
from random import randint
from math import cos, sin, radians
high_scores_filename = os.path.expanduser("~/.config/entangled-scores.json")
pg.init()
class color:
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0,255,0)
gray1 = (180, 180, 180)
gray2 = (160, 160, 160)
bordeaux = (100, 0, 30)
greyish_violet = (220, 200, 240)
orange = (255, 160, 0)
background = gray2
blank_tile = gray1
start_tile = bordeaux
tile = greyish_violet
path_extreme = orange
class fonts:
name = 'fonts/BOOKOS.TTF'
if not os.path.exists(name):
name = pg.font.get_default_font()
p = pg.font.Font(name, 20)
t1 = pg.font.Font(name, 50)
t2 = pg.font.Font(name, 40)
def hex_point(angle, offset=None):
"""
calculates the position of the 12 possible connectors
"""
rang = radians(angle * 30 - 15)
if offset is None:
offset = [0, 0]
return (50 + int(cos(rang) * 45) + offset[0],
50 + int(sin(rang) * 45) + offset[1])
size = width,height = 600,660
screen = pg.display.set_mode(size)
trans_scr = pg.Surface(size)
trans_scr.set_alpha(150)
trans_scr.set_colorkey(color.black)
paths_img = pg.Surface(size)
paths_img.set_colorkey(color.black)
hex_points = [
(50 + 48 * cos(radians(i * 60)), 50 + 48 * sin(radians(i * 60)))
for i in range(6)
]
empty_hex = pg.Surface((101,100)) # Base image for a board piece
empty_hex.set_colorkey(color.black)
pg.draw.polygon(empty_hex, color.blank_tile, hex_points)
start_hex = pg.Surface((101,100))
start_hex.set_colorkey(color.black)
pg.draw.polygon(start_hex, color.start_tile, hex_points)
class Board:
def __init__(self):
"""This class handles the game board."""
self.pieces = []
self.line1 = [[3,3],10]
# This list helps significantly in drawing the board pieces
self.coords = ((250,0,[0,0]),(325,44,[1,0]),(400,88,[2,0]),(475,132,[3,0]),(175,44,[0,1]),(250,88,[1,1]),\
(325,132,[2,1]),(400,176,[3,1]),(475,220,[4,1]),(100,88,[0,2]),(175,132,[1,2]),(250,176,[2,2]),(325,220,[3,2]),\
(400,264,[4,2]),(475,308,[5,2]),(25,132,[0,3]),(100,176,[1,3]),(175,220,[2,3]),(250,264,[3,3]),(325,308,[4,3]),\
(400,352,[5,3]),(475,396,[6,3]),(25,220,[1,4]),(100,264,[2,4]),(175,308,[3,4]),(250,352,[4,4]),(325,396,[5,4]),\
(400,440,[6,4]),(25,308,[2,5]),(100,352,[3,5]),(175,396,[4,5]),(250,440,[5,5]),(325,484,[6,5]),(25,396,[3,6]),\
(100,440,[4,6]),(175,484,[5,6]),(250,528,[6,6]))
for i in self.coords:
if i[2] != [3,3]:
self.pieces.append([(empty_hex,[]),(i[0],i[1])])
else:
self.pieces.append([(start_hex,1),(i[0],i[1])])
def add_piece(self,piece,position):
"""Adds a piece to the board."""
self.pieces.append([piece,position])
def draw_board(self,surface):
"""Draws the board."""
for i in self.pieces: surface.blit(i[0][0],i[1])
def draw_extreme(self,surface):
"""
draws the extreme of the current path taken through the board
"""
for i in self.coords:
if i[2] == self.line1[0]:
pg.draw.circle(surface, color.path_extreme,
hex_point(self.line1[1], i), 6, 2)
def bezier(p, t):
return (
p[0] * (1 - t) ** 3 +
p[1] * (1 - t) ** 2 * t * 3 +
p[2] * (1 - t) * t ** 2 * 3 +
p[3] * t ** 3
)
def draw_path(surface, path, color, width, offset=None, mag=1):
cx = 50 + (0 if offset is None else offset[0])
cy = 50 + (0 if offset is None else offset[1])
p1 = hex_point(path[0], offset)
p2 = hex_point(path[1], offset)
cx = (cx + p1[0] + p2[0]) / 3 # reduce curvature
cy = (cy + p1[1] + p2[1]) / 3
points = []
for i in range(11):
x = bezier([p1[0], cx, cx, p2[0]], i / 10.)
y = bezier([p1[1], cy, cy, p2[1]], i / 10.)
points.append((mag * x, mag * y))
pg.draw.lines(surface, color, False, points, width)
def draw_tile(paths):
piece = pg.Surface((202, 200))
piece.set_colorkey(color.black)
path_img = pg.Surface((202, 200))
path_img.set_colorkey(color.white)
path_img.set_alpha(254)
path_img.fill(color.white)
# draw base
for b in range(10):
points = []
for i in range(6):
a = radians(i * 60)
t = lambda f: 100 + f(a) * (100 - b)
points.append((t(cos), t(sin)))
clr = (color.tile[0] - b * 10,
color.tile[1] - b * 10,
color.tile[2] - b * 10)
pg.draw.polygon(piece, clr, points)
# draw paths
for path in paths:
draw_path(path_img, path, color.black, 9, None, 2)
piece.blit(path_img,(0,0))
return pg.transform.smoothscale(piece, (101, 100))
def make_piece():
paths = []
for i in range(6):
while 1:
path_s = randint(1, 12)
path_e = randint(1, 12)
breaker = 1
for b in paths:
if b[0] == path_s or b[1] == path_s \
or b[0] == path_e or b[1] == path_e \
or path_s == path_e:
breaker = 0
break
if len(paths) == 0 and path_s == path_e:
breaker = 0
if breaker:
break
paths.append([path_s, path_e])
return [draw_tile(paths), paths]
def draw_text(x,y,font,text,colour,surface=screen):
"""Helper function for drawing text."""
x2,y2 = font.size(text)
surface.blit(font.render(text,1,colour),(x-x2/2,y-y2/2))
xy_incs = [(1, 0), (1, 1), (0, 1), (-1, 0), (-1, -1), (0, -1)]
xy_incs = [xy_incs[i // 2] for i in range(12)]
xy_px_incs = [(75, 44), (0, 88), (-75, 44), (-75, -44), (0, -88), (75, -44)]
xy_px_incs = [xy_px_incs[i // 2] for i in range(12)]
class Entangled:
st_interactive, st_chaining, st_gameover = range(3)
def __init__(self):
self.done = False
self.new_game()
self.high_scores = None
def new_game(self):
self.g_board = Board() # Initialise game board
self.state = self.st_interactive
self.score = 0
self.hs_updated = False
self.new_tile = make_piece() # Make the next tile
self.rep_tile = make_piece() # Make the reserved tile
paths_img.fill(color.black)
def check_events(self):
self.left_click = False
self.right_click = False
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True
elif event.type == pg.MOUSEBUTTONDOWN:
self.left_click = (event.button == 1)
self.right_click = (event.button == 3)
if pg.key.get_pressed()[pg.K_ESCAPE]:
self.done = True
self.mx, self.my = pg.mouse.get_pos()
def check_new_game_button(self):
"""
check if click was on new game button and act accordingly
"""
if self.left_click and 440 < self.mx < 560 and 15 < self.my < 45 \
or pg.key.get_pressed()[pg.K_RETURN]:
self.new_game()
return True
def check_reserve_button(self):
"""
check if click was on reserve button, swapping the current
tile with its reserve if affirmative
"""
if self.mx < 100 and 530 < self.my < 630:
self.new_tile, self.rep_tile = self.rep_tile, self.new_tile
return True
def rotate_tile(self, ccw=False):
"""
rotate the current tile
"""
for i in self.new_tile[1]:
i[0] = (i[0] + (-2 if ccw else 2)) % 12
i[1] = (i[1] + (-2 if ccw else 2)) % 12
if i[0] == 0: i[0] = 12
if i[1] == 0: i[1] = 12
self.new_tile[0] = draw_tile(self.new_tile[1])
def place_tile(self):
"""
place down new tile and update path
"""
# Change x,y coordinate
self.g_board.line1[0][0] += xy_incs[self.g_board.line1[1] - 1][0]
self.g_board.line1[0][1] += xy_incs[self.g_board.line1[1] - 1][1]
# Complex path changing
path_update = ((self.g_board.line1[1] - 1 & ~1) + 7) % 12
self.g_board.line1[1] = self.g_board.line1[1] % 2 + path_update
for i in range(len(self.g_board.coords)):
if self.g_board.coords[i][2] == self.g_board.line1[0]:
break
else:
self.state = self.st_gameover
self.g_board.pieces[i][0] = self.new_tile
del self.new_tile
def calc_chain(self, start=False):
for i in range(len(self.g_board.coords)):
if self.g_board.coords[i][2] == self.g_board.line1[0]: break
current_piece = self.g_board.pieces[i]
if current_piece[0][1] != [] and self.state != self.st_gameover:
self.state = self.st_chaining
if current_piece[0][1] == 1: self.state = self.st_gameover
if self.state == self.st_chaining: # Award points for chaining
self.chain = 1 + (0 if start else self.chain)
self.score += self.chain
for i in current_piece[0][1]:
draw_line = 0
if self.g_board.line1[1] == i[0]:
self.g_board.line1[1] = i[1]
draw_line = 1
elif self.g_board.line1[1] == i[1]:
self.g_board.line1[1] = i[0]
draw_line = 1
if draw_line:
draw_path(paths_img, i, color.red, 3, current_piece[1])
for i in self.g_board.coords:
if i[2] == self.g_board.line1[0]:
x = i[0] + xy_px_incs[self.g_board.line1[1] - 1][0]
y = i[1] + xy_px_incs[self.g_board.line1[1] - 1][1]
for i in range(len(self.g_board.coords)):
if (x,y) == self.g_board.coords[i][0:2]: break
next_piece = self.g_board.pieces[i]
if next_piece[0][1] == []: # Done chaining, make next tile
self.state = self.st_interactive
self.new_tile = make_piece()
def update_highscores(self):
if self.hs_updated:
return
try:
if not self.high_scores:
self.high_scores = json.loads(open(high_scores_filename).read())
except:
self.high_scores = []
self.high_scores.append([self.score, time.strftime("%Y-%m-%d %H:%M")])
self.high_scores = sorted(self.high_scores, reverse=True)[:50]
open(high_scores_filename, 'w').write(json.dumps(self.high_scores))
self.hs_updated = True
def main_loop(self):
"""
coordinate the game processing flow
"""
while True:
self.check_events()
if self.done:
return
if self.check_new_game_button():
continue
if self.state == self.st_interactive:
if self.left_click:
if not self.check_reserve_button():
self.place_tile()
elif self.right_click:
self.rotate_tile()
else:
if pg.key.get_pressed()[pg.K_LEFT]:
self.rotate_tile(ccw=True)
pg.time.wait(100)
elif pg.key.get_pressed()[pg.K_RIGHT]:
self.rotate_tile()
pg.time.wait(100)
elif pg.key.get_pressed()[pg.K_UP]:
self.place_tile()
pg.time.wait(200)
elif pg.key.get_pressed()[pg.K_DOWN]:
self.new_tile, self.rep_tile = self.rep_tile, self.new_tile
pg.time.wait(100)
# Update the screen image
screen.fill(color.background)
self.g_board.draw_board(screen)
for i in self.g_board.coords:
if i[2] == self.g_board.line1[0]:
x = i[0] + xy_px_incs[self.g_board.line1[1] - 1][0]
y = i[1] + xy_px_incs[self.g_board.line1[1] - 1][1]
in_map = False
for i in self.g_board.coords:
if i[0:2] == (x,y):
in_map = True
if not in_map:
self.state = self.st_gameover
if self.state == self.st_interactive:
try:
trans_scr.fill(color.black)
trans_scr.blit(self.new_tile[0],(x,y)) # Draw transparent tile
except:
self.calc_chain(start=True)
elif self.state == self.st_chaining:
# Change x,y coordinate
self.g_board.line1[0][0] += xy_incs[self.g_board.line1[1] - 1][0]
self.g_board.line1[0][1] += xy_incs[self.g_board.line1[1] - 1][1]
# Change path end
path_update = ((self.g_board.line1[1] - 1 & ~1) + 7) % 12
self.g_board.line1[1] = self.g_board.line1[1] % 2 + path_update
self.calc_chain()
# Finish up drawing
if self.state == self.st_gameover:
trans_scr.fill((1, 1, 1))
screen.blit(paths_img, (0, 0))
screen.blit(trans_scr, (0, 0))
if self.state != self.st_gameover:
screen.blit(self.rep_tile[0], (10, 530))
p_msg = fonts.p.render('Points: '+str(self.score),1,color.black)
screen.blit(p_msg,(10,10))
draw_text(500, 30, fonts.p, 'New Game',color.black)
if 440 < self.mx < 560 and 15 < self.my < 45:
pg.draw.rect(screen,color.black,(440,15,120,30),2)
self.g_board.draw_extreme(screen)
if self.state == self.st_gameover: # Game ending condition
screen.blit(fonts.t1.render('Game Over', 0, color.black), (150, 125))
screen.blit(fonts.t1.render('Game Over', 1, color.white), (150, 125))
if self.score == 1:
screen.blit(fonts.t2.render('You are a disgrace.', 1, color.white), (100, 185))
else:
screen.blit(fonts.t2.render('You scored %i points.' % self.score, 1, color.white), (70, 185))
self.update_highscores()
for i, s in enumerate(self.high_scores[:10]):
surf = fonts.p.render('%s - %i' % (s[1], s[0]), 1,
(0, 255, 255) if s[0] == self.score else color.green)
screen.blit(surf, (185, 255 + i * 30))
pg.display.flip()
pg.time.wait(30)
if __name__ == '__main__':
game = Entangled()
game.main_loop()