-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgravitate3d.pyw
464 lines (391 loc) · 16.1 KB
/
gravitate3d.pyw
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/usr/bin/env python3
# Copyright © 2012-13 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version. It is provided for
# educational purposes and is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
import collections
import heapq
import itertools
import math
import os
import random
import pyglet
from pyglet.gl import *
SIZE = 475
BOARD_SIZE = 4 # Must be > 1.
ANGLE_INCREMENT = 5
RADIUS_FACTOR = 10
DELAY = 0.5 # seconds
COLORS = [
(255, 0, 0), # red
( 0, 255, 0), # green
( 0, 255, 255), # blue
( 0, 255, 255), # cyan
(255, 0, 255), # magenta
(255, 255, 0), # yellow
(160, 160, 164), # gray
(165, 42, 42), # brown
]
MIN_COLORS = 4
MAX_COLORS = min(len(COLORS), MIN_COLORS)
SELECTING_ENUMS = (GL_ALPHA_TEST, GL_DEPTH_TEST, GL_DITHER,
GL_LIGHT0, GL_LIGHTING, GL_MULTISAMPLE, GL_TEXTURE_1D,
GL_TEXTURE_2D, GL_TEXTURE_3D)
# Can't enable/disable GL_BLEND because it interacts badly with pyglet
# text labels
def main():
caption = "Gravitate 3D"
width = height = SIZE
resizable = True
try:
config = Config(sample_buffers=1, samples=4, depth_size=16,
double_buffer=True)
window = Window(width, height, caption=caption, config=config,
resizable=resizable)
except pyglet.window.NoSuchConfigException:
window = Window(width, height, caption=caption,
resizable=resizable)
path = os.path.realpath(os.path.dirname(__file__))
icon16 = pyglet.image.load(os.path.join(path, "gravitate_16x16.png"))
icon32 = pyglet.image.load(os.path.join(path, "gravitate_32x32.png"))
window.set_icon(icon16, icon32)
pyglet.app.run()
def vector(*args):
return (GLfloat * len(args))(*args)
class SceneObject:
__SelectColor = 0
def __init__(self, color):
self.color = color
SceneObject.__SelectColor += 1
self.selectColor = SceneObject.__SelectColor
@property
def selectColor(self):
return self.__selectColor
@selectColor.setter
def selectColor(self, value):
if value is None or isinstance(value, tuple):
self.__selectColor = value
else:
parts = []
for _ in range(3):
value, y = divmod(value, 256)
parts.append(y)
self.__selectColor = tuple(parts)
def __str__(self):
return "{}:{}".format(self.color, self.selectColor)
class Selecting:
def __init__(self, selecting):
self.selecting = selecting
def __enter__(self):
if self.selecting:
for enum in SELECTING_ENUMS:
glDisable(enum)
glShadeModel(GL_FLAT)
def __exit__(self, exc_type, exc_value, traceback):
if self.selecting:
for enum in SELECTING_ENUMS:
glEnable(enum)
glShadeModel(GL_SMOOTH)
class Window(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_minimum_size(200, 200)
self.xAngle = 10
self.yAngle = -15
self.minColors = MIN_COLORS
self.maxColors = MAX_COLORS
self.delay = DELAY
self.board_size = BOARD_SIZE
self._initialize_gl()
self.label = pyglet.text.Label("", bold=True, font_size=11,
anchor_x="center")
self._new_game()
def _initialize_gl(self):
glClearColor(0.9, 0.9, 0.9, 1) # lightgray
glEnable(GL_DEPTH_TEST)
glEnable(GL_MULTISAMPLE)
glEnable(GL_CULL_FACE)
glEnable(GL_COLOR_MATERIAL) # 1
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glLightfv(GL_LIGHT0, GL_POSITION, vector(0.5, 0.5, 1, 0))
glLightfv(GL_LIGHT0, GL_SPECULAR, vector(0.5, 0.5, 1, 1))
glLightfv(GL_LIGHT0, GL_DIFFUSE, vector(1, 1, 1, 1))
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50)
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, vector(1, 1, 1, 1))
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE) # 2
# 1 & 2 mean that we can use glColor*() to color materials
def _new_game(self):
self.score = 0
self.gameOver = False
self.selected = None
self.selecting = False
self.label.text = ("Click to Select • Click again to Delete • "
"Arrows to Rotate")
random.shuffle(COLORS)
colors = COLORS[:self.maxColors]
self.board = []
for x in range(self.board_size):
self.board.append([])
for y in range(self.board_size):
self.board[x].append([])
for z in range(self.board_size):
color = random.choice(colors)
self.board[x][y].append(SceneObject(color))
def on_draw(self):
diameter = min(self.width, self.height) / (self.board_size * 1.5)
radius = diameter / 2
offset = radius - ((diameter * self.board_size) / 2)
radius = max(RADIUS_FACTOR, radius - RADIUS_FACTOR)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glRotatef(self.xAngle, 1, 0, 0)
glRotatef(self.yAngle, 0, 1, 0)
with Selecting(self.selecting):
self._draw_spheres(offset, radius, diameter)
glPopMatrix()
if self.label.text:
self.label.y = (-self.height // 2) + 10
self.label.draw()
def _draw_spheres(self, offset, radius, diameter):
try:
sphere = gluNewQuadric()
gluQuadricNormals(sphere, GLU_SMOOTH)
for x, y, z in itertools.product(range(self.board_size),
repeat=3):
sceneObject = self.board[x][y][z]
if self.selecting:
color = sceneObject.selectColor
else:
color = sceneObject.color
if color is not None:
self._draw_sphere(sphere, x, y, z, offset, radius,
diameter, color)
finally:
gluDeleteQuadric(sphere)
def _draw_sphere(self, sphere, x, y, z, offset, radius, diameter,
color):
if self.selected == (x, y, z):
radius += RADIUS_FACTOR
glPushMatrix()
x = offset + (x * diameter)
y = offset + (y * diameter)
z = offset + (z * diameter)
glTranslatef(x, y, z)
glColor3ub(*color)
gluSphere(sphere, radius, 24, 24)
glPopMatrix()
def on_resize(self, width, height):
size = min(self.width, self.height) / 2
height = height if height else 1
width = width if width else 1
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
if width <= height:
glOrtho(-size, size, -size * height / width,
size * height / width, -size, size)
else:
glOrtho(-size * width / height, size * width / height,
-size, size, -size, size)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def on_key_press(self, symbol, modifiers):
if (symbol == pyglet.window.key.ESCAPE or
((modifiers & pyglet.window.key.MOD_CTRL or
modifiers & pyglet.window.key.MOD_COMMAND) and
symbol == pyglet.window.key.Q)):
pyglet.app.exit()
elif ((modifiers & pyglet.window.key.MOD_CTRL or
modifiers & pyglet.window.key.MOD_COMMAND) and
symbol == pyglet.window.key.N):
self._new_game()
elif (symbol in {pyglet.window.key.DELETE, pyglet.window.key.SPACE,
pyglet.window.key.BACKSPACE} and
self.selected is not None):
self._delete()
def _delete(self):
x, y, z = self.selected
self.selected = None
color = self.board[x][y][z].color
if not self._is_legal(x, y, z, color):
return
self._delete_adjoining(x, y, z, color)
self.label.text = "{:,}".format(self.score)
pyglet.clock.schedule_once(self._close_up, self.delay)
def _is_legal(self, x, y, z, color):
"""A legal click is on a colored sphere that is adjacent to
another sphere of the same color."""
if z > 0 and self.board[x][y][z - 1].color == color:
return True
if (z + 1 < self.board_size and
self.board[x][y][z + 1].color == color):
return True
if y > 0 and self.board[x][y - 1][z].color == color:
return True
if (y + 1 < self.board_size and
self.board[x][y + 1][z].color == color):
return True
if x > 0 and self.board[x - 1][y][z].color == color:
return True
if (x + 1 < self.board_size and
self.board[x + 1][y][z].color == color):
return True
return False
def _delete_adjoining(self, x, y, z, color):
adjoining = set()
self._populate_adjoining(x, y, z, color, adjoining)
self.score += len(adjoining) ** (self.maxColors - 2)
for x, y, z in adjoining:
sceneObject = self.board[x][y][z]
sceneObject.color = sceneObject.selectColor = None
def _populate_adjoining(self, x, y, z, color, adjoining):
if not ((0 <= x < self.board_size) and
(0 <= y < self.board_size) and
(0 <= z < self.board_size)):
return # Fallen off an edge
if ((x, y, z) in adjoining or
self.board[x][y][z].color != color):
return # Color doesn't match or already done
adjoining.add((x, y, z))
self._populate_adjoining(x - 1, y, z, color, adjoining)
self._populate_adjoining(x + 1, y, z, color, adjoining)
self._populate_adjoining(x, y - 1, z, color, adjoining)
self._populate_adjoining(x, y + 1, z, color, adjoining)
self._populate_adjoining(x, y, z - 1, color, adjoining)
self._populate_adjoining(x, y, z + 1, color, adjoining)
def _close_up(self, seconds):
self._move()
self._check_game_over()
def _move(self):
moved = True
while moved:
moved = False
for x, y, z in itertools.product(range(self.board_size),
repeat=3):
if self.board[x][y][z].color is not None:
if self._move_if_possible(x, y, z):
moved = True
break
def _move_if_possible(self, x, y, z):
empty_neighbours = self._empty_neighbours(x, y, z)
if empty_neighbours:
move, nx, ny, nz = self._nearest_to_middle(x, y, z,
empty_neighbours)
if move:
new = self.board[nx][ny][nz]
old = self.board[x][y][z]
new.color = old.color
new.selectColor = old.selectColor
old.color = old.selectColor = None
return True
return False
def _empty_neighbours(self, x, y, z):
neighbours = set()
for nx, ny, nz in ((x - 1, y, z), (x + 1, y, z), (x, y - 1, z),
(x, y + 1, z), (x, y, z - 1), (x, y, z + 1)):
if (0 <= nx < self.board_size and
0 <= ny < self.board_size and
0 <= nz < self.board_size and
self.board[nx][ny][nz].color is None):
neighbours.add((nx, ny, nz))
return neighbours
def _nearest_to_middle(self, x, y, z, empty_neighbours):
mid = self.board_size // 2
Δold = math.sqrt(((x - mid) ** 2) + ((y - mid) ** 2) +
((z - mid) ** 2))
heap = []
for nx, ny, nz in empty_neighbours:
if self._is_square(nx, ny, nz):
Δnew = math.sqrt(((nx - mid) ** 2) + ((ny - mid) ** 2) +
((nz - mid) ** 2))
heapq.heappush(heap, (Δnew, nx, ny, nz))
Δnew, nx, ny, nz = heap[0]
return (True, nx, ny, nz) if Δold > Δnew else (False, x, y, z)
def _is_square(self, x, y, z):
if z > 0 and self.board[x][y][z - 1].color is not None:
return True
if (z + 1 < self.board_size and
self.board[x][y][z + 1].color is not None):
return True
if y > 0 and self.board[x][y - 1][z].color is not None:
return True
if (y + 1 < self.board_size and
self.board[x][y + 1][z].color is not None):
return True
if x > 0 and self.board[x - 1][y][z].color is not None:
return True
if (x + 1 < self.board_size and
self.board[x + 1][y][z].color is not None):
return True
return False
def _check_game_over(self):
text = "{} \u2014 Click for New Game • Esc to Quit"
userWon, canMove = self._check_tiles()
if userWon:
self.label.text = text.format("You Won! \u2014 {:,}".format(
self.score))
self.gameOver = True
elif not canMove:
self.label.text = text.format("Game Over")
self.gameOver = True
def _check_tiles(self):
countForColor = collections.defaultdict(int)
userWon = True
canMove = False
for x, y, z in itertools.product(range(self.board_size),
repeat=3):
color = self.board[x][y][z].color
if color is not None:
countForColor[color] += 1
userWon = False
if self._is_legal(x, y, z, color): # We _can_ move
canMove = True
if 1 in countForColor.values():
canMove = False
return userWon, canMove
def on_text_motion(self, motion): # Rotate about the x or y axis
if motion == pyglet.window.key.MOTION_UP:
self.xAngle -= ANGLE_INCREMENT
elif motion == pyglet.window.key.MOTION_DOWN:
self.xAngle += ANGLE_INCREMENT
elif motion == pyglet.window.key.MOTION_LEFT:
self.yAngle -= ANGLE_INCREMENT
elif motion == pyglet.window.key.MOTION_RIGHT:
self.yAngle += ANGLE_INCREMENT
def on_mouse_press(self, x, y, button, modifiers):
if self.gameOver:
self._new_game()
return
self.selecting = True
self.on_draw()
self.selecting = False
selectColor = (GLubyte * 3)()
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, selectColor)
selectColor = tuple([component for component in selectColor])
self._clicked(selectColor)
# for systems whose coords have y top-left do:
# viewport = (GLint * 4)()
# glGetIntegerv(GL_VIEWPORT, viewport)
# and in the glReadPixels() call, replace y with
# viewport[3] - y
def _clicked(self, selectColor):
for x, y, z in itertools.product(range(self.board_size), repeat=3):
if selectColor == self.board[x][y][z].selectColor:
if (x, y, z) == self.selected:
self._delete() # Second click deletes
else:
self.selected = (x, y, z)
return
if __name__ == "__main__":
import sys
if len(sys.argv) > 1 and sys.argv[1] == "--regression":
print("Loaded OK")
else:
main()