-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapped trip of life.pyw
539 lines (397 loc) · 13.3 KB
/
wrapped trip of life.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import pyglet
from pyglet.window import key
from pyglet.gl import *
from app import App
from Tkinter import *
from tkFileDialog import askopenfilename, asksaveasfilename
size = 64
half_size = size/2
double_size = size*2
big_size = size*32
extension = {"defaultextension":"life", "filetypes":[('life','.life')]}
class CreateDialog:
def __init__(self, parent, game):
top = self.top = Toplevel(parent)
self.game = game
Label(top, text="Columns:").pack()
self.c = Entry(top)
self.c.pack(padx=5)
Label(top, text="Rows:").pack()
self.r = Entry(top)
self.r.pack(padx=5)
b = Button(top, text="Create", command=self.ok)
b.pack(pady=5)
top.bind('<Return>', self.enter)
def enter(self, event):
self.ok()
def ok(self):
try:
self.game.create(int(self.c.get()),int(self.r.get()))
except ValueError:
self.game.activate_caption("Creating failed, invalid input!")
self.top.destroy()
class Cell(object):
def __init__(self, game, x, y):
self.game = game
self.world = game.world
self.batch = game.app.batch
self.group = game.app.sprite_group
self.x = x
self.y = y
self.state = False
self.neighbours = 0
self.vertex_list = self.dead_vertex_list()
def set_vertex_list(self):
self.vertex_list.delete()
if self.state:
self.vertex_list = self.alive_vertex_list()
else:
self.vertex_list = self.dead_vertex_list()
def dead_vertex_list(self):
x = self.x * size
y = self.y * size
vertices = (x+half_size, y,
x, y+half_size,
x, y+half_size,
x+half_size, y+size,
x+half_size, y+size,
x+size, y+half_size,
x+size, y+half_size,
x+half_size, y)
return self.batch.add(8, GL_LINES, self.group, ('v2i', vertices), ('c3B', self.color(8)))
def alive_vertex_list(self):
x = self.x * size
y = self.y * size
vertices = (x, y,
x, y+size,
x+size, y+size,
x+size, y)
return self.batch.add(4, GL_QUADS, self.group, ('v2i', vertices), ('c3B', self.color(4)))
def color(self, count):
red = 0
green = 0
blue = 0
if self.state:
if self.neighbours > 3:
red = 255
elif self.neighbours < 2:
blue = 255
else:
green = 255
else:
if self.neighbours > 3:
red = 60
elif self.neighbours < 3:
blue = 60
else:
green = 100
return (red, green, blue)*count
def set_color(self):
self.vertex_list.colors = self.color(len(self.vertex_list.vertices)/2)
def toggle(self):
self.set_state(not self.state)
def set_state(self, state):
if state:
self.spawn()
else:
self.die()
def die(self):
if self.state:
self.state = False
self.set_vertex_list()
self.mod_neighbours(-1)
def spawn(self):
if not self.state:
self.state = True
self.set_vertex_list()
self.mod_neighbours(1)
def reset(self):
self.state = False
self.neighbours = 0
self.set_vertex_list()
def mod_neighbours(self, mod):
for x in range(-1,2):
for y in range(-1,2):
if x == 0 and y == 0:
pass
else:
xx = self.x+x
yy = self.y+y
if self.game.wrapping:
xx, yy = self.game.wrapped(xx, yy)
try:
cell = self.world[(xx, yy)]
cell.neighbours += mod
cell.set_color()
except KeyError:
pass
class CursorHandlers(object):
def __init__(self, game):
self.game = game
game.app.window.push_handlers(self)
def on_mouse_drag(self, x, y, dx, dy, button, modifiers):
self.game.cursor.move(x, y)
def on_mouse_motion(self, x, y, dx, dy):
self.game.cursor.move(x, y)
class Cursor(object):
def __init__(self, game):
self.handlers = CursorHandlers(game)
self.app = game.app
self.app.window.set_mouse_visible(False)
x = self.app.mouse.x - self.app.mouse.x % size
y = self.app.mouse.y - self.app.mouse.y % size
self.last = (x, y)
self.vertex_list = self.app.batch.add(24, GL_LINES, self.app.text_group, ('v2i', self.vertices(x, y)), ('c4B', self.color()))
def color(self):
shadow = (0,0,0,200)*8
inside = (255,255,255,200)*8
outside = (255,255,255,100,
255,255,255,0)*4
return shadow+inside+outside
def vertices(self, x, y):
shadow = (x, y,
x+size, y,
x+size, y,
x+size, y+size,
x+size, y+size,
x, y+size,
x, y+size,
x, y)
inside = (x-1, y-1,
x+size+1, y-1,
x+size+1, y-1,
x+size+1, y+size+1,
x+size+1, y+size+1,
x-1, y+size+1,
x-1, y+size+1,
x-1, y-1)
outside = (x, y+half_size,
x-big_size, y+half_size,
x+size, y+half_size,
x+size+big_size, y+half_size,
x+half_size, y+size,
x+half_size, y+size+big_size,
x+half_size, y,
x+half_size, y-big_size)
return shadow+inside+outside
def move(self, x, y):
x -= x % size
y -= y % size
if self.last != (x,y):
self.vertex_list.vertices = self.vertices(x, y)
def delete(self):
self.vertex_list.delete()
self.app.window.set_mouse_visible(True)
self.app.window.pop_handlers()
class GameHandlers(object):
def __init__(self, game):
self.game = game
game.app.window.push_handlers(self)
def on_key_press(self, symbol, modifiers):
if not modifiers & key.MOD_CTRL:
if symbol == key.SPACE:
self.game.toggle()
elif symbol == key.DELETE:
self.game.reset()
elif symbol == key.ENTER:
self.game.create_dialog()
elif symbol == key.S:
self.game.save_dialog()
elif symbol == key.L:
self.game.load_dialog()
elif symbol == key.N:
self.game.next_step()
elif symbol == key.C:
self.game.toggle_cursor()
elif symbol == key.W:
self.game.wrapping = not self.game.wrapping
def on_mouse_scroll(self, x, y, scroll_x, scroll_y):
self.game.next_step()
def on_mouse_press(self, x, y, button, modifiers):
self.game.draw_press(x,y)
def on_mouse_drag(self, x, y, dx, dy, button, modifiers):
self.game.draw_drag(x,y)
class Game(object):
def __init__(self, app, columns, rows):
self.app = app
self.running = False
self.tk = None
self.draw_last = (-1,-1)
self.draw_state = True
self.wrapping = True
self.handlers = GameHandlers(self)
self.cursor = Cursor(self)
self.create(columns, rows)
def toggle_cursor(self):
if self.cursor is None:
self.cursor = Cursor(self)
else:
self.cursor.delete()
self.cursor = None
def next_step(self):
if not self.running:
self.update()
def draw_press(self, x, y):
if self.tk is None:
try:
cell = self.world[x/size,y/size]
cell.toggle()
self.draw_state = cell.state
except KeyError:
pass
def wrapped(self, x, y):
if x >= self.columns:
x -= self.columns
elif x < 0:
x += self.columns
if y >= self.rows:
y -= self.rows
elif y < 0:
y += self.rows
return (x, y)
def draw_drag(self, x, y):
if self.tk is None:
try:
x = x/size
y = y/size
if self.wrapping:
x, y = self.wrapped(x, y)
now = (x, y)
cell = self.world[now]
if self.draw_last != now and cell.state != self.draw_state:
self.world[now].set_state(self.draw_state)
self.draw_last = now
except KeyError:
pass
def save_dialog(self):
self.stop()
Tk().withdraw()
filename = asksaveasfilename(**extension)
self.save(filename)
def load_dialog(self):
self.stop()
Tk().withdraw()
filename = askopenfilename(**extension)
self.load(filename)
def activate_caption(self, text):
self.app.window.activate()
self.caption(text)
def caption(self, text):
self.app.window.set_caption(text)
def check_filename(self, name, caption):
if name == "":
self.activate_caption(caption)
return False
return True
def open_filename(self, name, caption):
try:
f = open(name, 'r')
except IOError:
self.activate_caption(caption)
return None
return f
def save(self, name):
if not self.check_filename(name, "Saving failed, no filename specified!"):
return
self.caption("Saving: " + name)
f = open(name, 'w')
string = ""
for x in range(self.columns):
for y in range(self.rows):
cell = self.world[(x,y)].state
if cell:
string += "a"
else:
string += "d"
string += "\n"
f.write(string)
f.close()
self.activate_caption("Saving done!")
def load(self, name):
if not self.check_filename(name, "Loading failed, no filename specified!"):
return
f = self.open_filename(name, "Loading failed, specified file not found!")
if not f:
return
self.caption("Loading: " + name)
lines = []
for line in f:
lines.append(line)
columns = len(lines)
rows = len(lines[0])-1
self.create(columns, rows)
for x in range(columns):
for y in range(rows):
char = lines[x][y]
if char == "a":
state = True
else:
state = False
cell = self.world[(x,y)]
cell.set_state(state)
self.activate_caption("Loading done!")
def reset(self):
self.stop()
for key in self.world:
self.world[key].reset()
def create_dialog(self):
self.stop()
if self.tk is None:
self.tk = Tk()
self.tk.title("Create...")
self.tk.withdraw()
self.tk.update()
dialog = CreateDialog(self.tk, self)
self.tk.wait_window(dialog.top)
self.tk = None
def clear_batch(self):
try:
for key in self.world:
self.world[key].vertex_list.delete()
except AttributeError:
pass
def create(self, columns, rows):
self.clear_batch()
self.columns = columns
self.rows = rows
self.world = dict()
for x in range(columns):
for y in range(rows):
self.world[(x,y)] = Cell(self, x, y)
if not self.app.window.fullscreen:
self.app.window.set_size(columns*size,rows*size)
def toggle(self):
if self.running:
self.stop()
else:
self.start()
def start(self):
self.app.window.set_caption("Running...")
self.running = True
pyglet.clock.schedule(self._update)
def stop(self):
self.app.window.set_caption("Paused...")
self.running = False
pyglet.clock.unschedule(self._update)
def _update(self, dt):
self.update()
def update(self):
die = []
spawn = []
for key in self.world:
cell = self.world[key]
if cell.state:
if cell.neighbours > 3:
die.append(cell)
elif cell.neighbours < 2:
die.append(cell)
else:
if cell.neighbours == 3:
spawn.append(cell)
for cell in die:
cell.die()
for cell in spawn:
cell.spawn()
app = App()
game = Game(app, app.window.width/size, app.window.height/size)
app.run()