-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgameoflife.py
executable file
·1385 lines (1100 loc) · 48.3 KB
/
gameoflife.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
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from kivy.config import Config # For settings
from kivy.core.window import Window # To determine window size
## The app
from kivy.app import App # To run the app
from kivy.uix.screenmanager import ScreenManager, Screen # Manage screens
from kivy.uix.screenmanager import FadeTransition, FallOutTransition, RiseInTransition # Transitions
from kivy.uix.boxlayout import BoxLayout # Options pane and Scatter container
from kivy.uix.floatlayout import FloatLayout # Root widget
from kivy.uix.button import Button # For options
from kivy.uix.scatter import Scatter # Movable and zoomable space
from kivy.uix.gridlayout import GridLayout # Grid for the tiles
from kivy.uix.image import Image # Image to act as tiles
from kivy.properties import StringProperty # Test for image source
from random import random # For shuffling the tiles
from kivy.clock import Clock # For scheduling functions
from kivy.uix.listview import ListView, ListItemButton # For selecting stamps
from kivy.adapters.dictadapter import DictAdapter # For selecting stamps
from kivy.uix.textinput import TextInput # For saving stamps
## Settings panel
from kivy.config import ConfigParser
from kivy.uix.settings import SettingsWithSidebar
from gameoflifesettings import logic
from gameoflifesettings import aesthetics
from gameoflifesettings import about_me
## Tools during development
import os
# TODO:
# Dynamic word sizes - physical word sizes change depending on screen resolution
# Package for OS X and Windows
# upgrade android sdk from VM and try to build again!
# Package for Android - upgrade android sdk from VM and try to build again!
# Package for iOS
## Future TODOs - difficult right now
## Compile for Android
## Used to work on my Ubuntu machine, but I broke the computer.
## Getting it to work on Ubuntu still took a lot of Googling - Buildozer never worked right out of the box
## Rename to main.py and main.kv, of course
# Change to Android toasts:
# print "The whole board is empty - there is nothing to save."
# print "No initial state to go back to."
# print "Invalid input given for new tile size."
# print "There is no stamp to paste."
# print "That's ridiculous - let's do size = 5."
# Given up:
# Use Config.set() to set the living and birth requirements in the settings panel to reflect
# the new set of rules that the user chose
# E.g. living requirement was "1, 2, 3, 4, 5". User chooses "Conway" as the rule set to use
# The game should automatically change living requirement to say "2, 3" to reflect that
# After about two hours to trying things and reading documentation, I just can't get it to work
# I don't know what I'm doing wrong
# It seems that Config.set only works from within the build_config() method. Even if I save
# that Config reference and use it to set a field by calling Config.set from another method,
# it will not work (but works fine if I use Config.set() from within build_config)
# Similarly, this is also difficult due to Config being unusable outside of build_config()
# To prevent confusion (the app's old settings are loaded in the settings panel but the default
# rules are actually the ones in effect), gameoflife.ini is being removed every time the app is run
# so the app is forced to display default selections on the settings panel (conforming to the
# default rules in effect)
# Want to changes persistent
# Note: external file to toggle between custom and built-in so I know which one to load
# Note: Split by " = " for gameoflife.ini, and remove the os.remove() line
# Tile live image
# Tile dead image
# Tile size
# Background image
# Update speed
# Life requirement * apply to both game screen and preview screen
# Birth requirement * apply to both game screen and preview screen
# Save user-defined rules. Not hard to implement but since the Kivy settings panel is provided, there's
# no way for me to add a button that's just used for saving. The alternative is to make yet another
# button to say "save current rules" but the game screen is already cluttered enough, and it would
# be a useless button most of the time
# In numeric fields, "0" will be gone if at the beginning with numeric, but using string input is awkward
# And int field that limits the user's keyboard to numbers will also return an int with the leading
# zeros stripped off
# Can't figure out kivy's touch propagation
# Root passes the touch to its children and the touch is propagated recursively
# Make the images behave more like buttons
# i.e. When the user touches down on a button (like pause), but drags it to the tile grid (as if changing their mind)
# then the game should know the touch started from a button and should not react
## Settings the screen to a big size
PY_WIDTH = 1600
PY_HEIGHT = 1200
TOP_BAR_SCALE = 0.1
GRID_SCALE = 0.8
BOTTOM_BAR_SCALE = 0.1
Config.set("graphics", "width", str(int(0.5*PY_WIDTH)))
Config.set("graphics", "height", str(int(0.5*PY_HEIGHT)))
Window.size = (int(0.5*PY_WIDTH), int(0.5*PY_HEIGHT))
## Updates the bounds of a widget
def get_bounds(bounded):
bounded.left = bounded.pos[0]
bounded.right = bounded.pos[0] + bounded.size[0]
bounded.bottom = bounded.pos[1]
bounded.top = bounded.pos[1] + bounded.size[1]
## Master widget, it juggles between the different screens
class Juggler(ScreenManager):
def __init__(self, **kwargs):
super(Juggler, self).__init__(**kwargs)
self.fade = FadeTransition()
self.fall = FallOutTransition()
self.rise = RiseInTransition()
self.transition = self.fade
## Primary screen holding the menu options and grid
class GameScreen(Screen):
def __init__(self, **kwargs):
super(GameScreen, self).__init__(**kwargs)
self.name = "game_screen"
## Holds the top and bottom menu bars plus the game screen
class MasterBox(FloatLayout):
def __init__(self, **kwargs):
super(MasterBox, self).__init__(**kwargs)
## Holds all the individual tiles
class TileGrid(GridLayout):
def __init__(self, **kwargs):
super(TileGrid, self).__init__(**kwargs)
self.side_len = 20
self.rows = int((GRID_SCALE)*PY_HEIGHT/self.side_len) ## GRID_SCALE is proportion of screen height used for grid
self.cols = PY_WIDTH/self.side_len
self.tiles = self.rows*self.cols
self.req_to_live = [2, 3]
self.req_to_birth = [3]
self.updates_per_second = 10.0
self.playing = False
self.running = None
self.initial_state = None
self.stamp = None
## Adds appropriate number of tiles to itself
def build_self(self):
for i in range(self.tiles):
self.add_widget(Tile(self.tiles - i - 1))
## Need to update rows, cols, tiles when self.side_len changes
def update_rct(self):
self.rows = int(GRID_SCALE*Window.height/self.side_len)
self.cols = Window.width/self.side_len
self.tiles = self.rows*self.cols
## Kills all the tiles
def clear_board(self):
for child in self.children:
child.die()
## Scrambles the tiles
def randomize(self):
self.stop()
for child in self.children:
if random() < 0.5:
child.live()
else:
child.die()
## Determines the state of the cell at the specified coordinates
## True for alive, False for dead, None for out of bounds
def determine_state(self, row, col):
if row < 0 or row >= self.rows or col < 0 or col >= self.cols:
return None
else:
tile_num = row*self.cols + col
return self.children[tile_num].alive
## Return list of neighbour states
def find_neighbour_states(self, tile_num):
# Coordinates are in row, col
tile_coords = [tile_num/self.cols, tile_num%self.cols]
r = tile_coords[0]
c = tile_coords[1]
## I thought it was:
## [----->n]
## [------>]
## [0 1 -->]
## Actually:
## [n<-----]
## [<------]
## [<-- 1 0]
## Not going to change the code since I am only counting neighbours,
## and left/right doesn't matter here
## Shouldn't matter anywhere - orientation is irrelevant - just need to be consistent.
## Be abstract - go to "next" row/col is still next row/col no matter the orientation.
top_left = [r+1, c-1]
top_centre = [r+1, c]
top_right = [r+1, c+1]
left = [r, c-1]
right = [r, c+1]
bottom_left = [r-1, c-1]
bottom_centre = [r-1, c]
bottom_right = [r-1, c+1]
neighbours = [top_left, top_centre, top_right, left, right, bottom_left, bottom_centre, bottom_right]
state_list = []
for n in neighbours:
state_list.append(self.determine_state(n[0], n[1]))
return state_list
## Counts the number of living neighbours a tile has
def number_alive(self, tile_num):
neighbour_states = self.find_neighbour_states(tile_num)
living_neighbours = 0
for s in neighbour_states:
if s:
living_neighbours += 1
return living_neighbours
## Determines the next state of the board (who lives, who dies)
def get_life_list(self):
next_frame = []
for child in self.children:
living_neighbours = self.number_alive(child.index)
if child.alive:
if living_neighbours in self.req_to_live:
next_frame.append(True)
else:
next_frame.append(False)
else:
if living_neighbours in self.req_to_birth:
next_frame.append(True)
else:
next_frame.append(False)
return next_frame
## Updates the board using the list returned by get_life_list()
## *args needed - Clock will pass additional arguments (interval?)
def update_board(self, *args):
next_frame = self.get_life_list()
for i, j in zip(next_frame, self.children):
if i:
j.live()
else:
j.die()
## Toggles the state of the board - switches between constantly updating the board and stopping
def toggle(self):
if not self.playing:
self.running = Clock.schedule_interval(self.update_board, 1.0/self.updates_per_second)
self.playing = True
else:
Clock.unschedule(self.running)
self.running = None
self.playing = False
## Stops updating the board
def stop(self):
if self.running != None:
Clock.unschedule(self.running)
self.running = None
self.playing = False
## Saves the state of the board
## Called before the board starts animating in case the user wants to restart
def save_state(self):
self.initial_state = []
for child in self.children:
self.initial_state.append(child.alive)
## Stops the animation and restores the board to its initial state
def load_initial_state(self):
if self.initial_state == None:
print "No initial state to go back to."
else:
self.stop()
root.ids["play_pause_button"].stop()
for i, j in zip(self.initial_state, self.children):
if i == True:
j.live()
else:
j.die()
## Keeps only the outer consecutive patterns: e.g.
## [0, 1, 2, 3, 6, 7, 12, 13] -> [0, 1, 2, 3, 12, 13]
## Assumes non-empty list
## Assumes no duplicates in the list
## Assumes the consecutive patterns are ascending
## A consecutive list is returned as-is
def keep_outer_consecutives(self, seq):
## Counting consecutives from the front
beginning = []
end = []
head = seq[0]
for n in seq:
if n == head:
beginning.append(n)
head += 1
else:
break
## Flip the seq so I can iterate forward
seq_rev = seq[::-1]
tail = seq[-1]
for n in seq_rev:
if n == tail:
end.append(n)
tail -= 1
else:
break
## Flip the ending portion back to it is ascending order
end = end[::-1]
if beginning == end:
return beginning
else:
return beginning + end
## Takes the current state, trims away outer dead rows/columns to save
## the smallest possible grid that contains the current pattern
def record_stamp(self):
## Save everything in a grid
state_matrix = []
row_state = []
counter = 0
empty = True
for child in self.children:
counter += 1
row_state.append(child.alive)
if child.alive:
empty = False
if counter%self.cols == 0:
state_matrix.append(row_state)
row_state = []
## Out-of-bounds error if continuing with empty board
if empty:
print "The whole board is empty - there is nothing to save."
self.stamp = None
return
## Trim rows with all dead tiles
dead_rows = [] # Holds the indices of the dead rows in state_matrix
dead_row = [] # Definition of a dead row
## Defining a row of all dead tiles
for i in range(self.cols):
dead_row.append(False)
for i in range(self.rows):
if state_matrix[i] == dead_row:
dead_rows.append(i)
## Keep inner empty rows (they are intentional)
dead_rows = self.keep_outer_consecutives(dead_rows)
## Reverse order so they can be removed safely (not removing elements as I iterate forward)
dead_rows = dead_rows[::-1]
## print "Dead rows to remove, in this order:", dead_rows
for row in dead_rows:
state_matrix.pop(row)
## print "Empty rows removed, up-down, left-right flipped:"
## self.print_binary_matrix(state_matrix)
## Trim columns with all dead tiles
dead_cols = []
for col in range(self.cols):
col_is_dead = True
for row in state_matrix:
if row[col] == True:
col_is_dead = False
break
if col_is_dead:
dead_cols.append(col)
## Keep inner empty columns (they are intentional)
dead_cols = self.keep_outer_consecutives(dead_cols)
## Reverse order so they can be removed
dead_cols = dead_cols[::-1]
## print "Columns to remove, in this order: ", dead_cols
for col in dead_cols:
for row in state_matrix:
row.pop(col)
## print "Empty rows and columns removed, up-down, left-right flipped:"
## self.print_binary_matrix(state_matrix)
self.stamp = state_matrix
## If the given state is True, make the tile at the given coordinates live
## Used to help with paste_stamp()
def enforce_life(self, row, col, state):
## If out of bounds, do nothing
if row < 0 or row >= self.rows or col < 0 or col >= self.cols:
return
## If trying to enforce death, skip
if state == False:
return
child_number = row*self.cols + col
self.children[child_number].live()
## Takes a midpoint (child #) and draws stamp (list of lists) around the midpoint
def paste_stamp(self, midpoint, stamp):
if stamp == None:
print "There is no stamp to paste."
return
## Find the middle coordinate
stamp_rows = len(stamp)
stamp_cols = len(stamp[0])
mid_row = midpoint/self.cols
mid_col = midpoint%self.cols
least_row = mid_row - stamp_rows/2
least_col = mid_col - stamp_cols/2
if least_row < 0:
least_row = 0
if least_col < 0:
least_col = 0
## Paste each of the stamp's tiles onto the board
for r in range(stamp_rows):
for c in range(stamp_cols):
self.enforce_life(least_row + r, least_col + c, stamp[r][c])
## Letters are possible inputs, so non-numeric characters are filtered out
## If the remaining string is a valid number, the tile size is updated to that size
def update_tile_size(self, new_tile_size):
## Filter out non-numeric characters
new_tile_size = str(new_tile_size)
clean_number = ""
str_numbers = ['.', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
for char in new_tile_size:
if char in str_numbers:
clean_number += char
try:
## Try statement in case no user enters no numeric character
new_tile_size = int(clean_number)
if new_tile_size < 5:
print "That's ridiculous - let's do size = 5."
new_tile_size = 5
except:
## Does nothing in the app.
print "Invalid input given for new tile size."
return
self.clear_widgets()
self.side_len = new_tile_size
self.update_rct()
Tile.side_len = new_tile_size
self.build_self()
# Any saved state would be invalid now
self.initial_state = None
## Base class for the picture buttons
class ImageButton(Image):
def __init__(self, **kwargs):
super(ImageButton, self).__init__(**kwargs)
## Hack fix. Need to get bounds for the image at construction/on first touch down
## or game will crash, saying there are no left/right/top/bottom fields in this class
## I created the left/right/top/bottom fields and they are used when comparing touch coordinates
## It appears that a touch is applied right when the app starts up, but before get_bounds is first called
## This hack fix can be avoided if I just used self.pos[1] + self.size[1] instead of self.top
## but self.top just feels so much cleaner
get_bounds(self)
## If the touch is within this widget's bounds, the touch_down image is displayed, instead
def on_touch_down(self, touch):
get_bounds(self)
if (self.left <= touch.x <= self.right) and (self.bottom <= touch.y <= self.top):
self.source = self.down_source
## If the touch movement is within this widget's bounds, the down_source is applied
## Otherwise the up_source is applied
def on_touch_move(self, touch):
if (self.left <= touch.x <= self.right) and (self.bottom <= touch.y <= self.top):
self.source = self.down_source
else:
self.source = self.up_source
## Switches the game to draw mode
class PenButton(ImageButton):
def __init__(self, **kwargs):
super(PenButton, self).__init__(**kwargs)
self.up_source = "./Images/PenUp.png"
self.down_source = "./Images/PenDown.png"
self.source = self.up_source
def on_touch_up(self, touch):
if (self.left <= touch.x <= self.right) and (self.bottom <= touch.y <= self.top):
self.source = self.up_source
Tile.to_draw_mode()
## Switches the game to stamp mode
class StampButton(ImageButton):
def __init__(self, **kwargs):
super(StampButton, self).__init__(**kwargs)
self.up_source = "./Images/StampUp.png"
self.down_source = "./Images/StampDown.png"
self.source = self.up_source
def on_touch_up(self, touch):
if (self.left <= touch.x <= self.right) and (self.bottom <= touch.y <= self.top):
self.source = self.up_source
Tile.to_stamp_mode()
## Swaps the game to the choose stamp screen
class ChooseStampButton(ImageButton):
def __init__(self, **kwargs):
super(ChooseStampButton, self).__init__(**kwargs)
self.up_source = "./Images/ChooseStampUp.png"
self.down_source = "./Images/ChooseStampDown.png"
self.source = self.up_source
def on_touch_up(self, touch):
if (self.left <= touch.x <= self.right) and (self.bottom <= touch.y <= self.top):
self.source = self.up_source
root.ids["grid"].stop()
root.ids["play_pause_button"].stop()
root.current = "stamp_screen"
## Swaps the game to the settings screen
class SettingsButton(ImageButton):
def __init__(self, **kwargs):
super(SettingsButton, self).__init__(**kwargs)
get_bounds(self)
self.up_source = "./Images/SettingsUp.png"
self.down_source = "./Images/SettingsDown.png"
self.source = self.up_source
def on_touch_up(self, touch):
if (self.left <= touch.x <= self.right) and (self.bottom <= touch.y <= self.top):
self.source = self.up_source
root.ids["play_pause_button"].stop()
root.ids["grid"].stop()
app.open_settings()
## Swaps the game between playing and stopping the animation
## on_touch_down and on_touch_move are overridden because this button has two images
class PlayPauseButton(ImageButton):
def __init__(self, **kwargs):
super(PlayPauseButton, self).__init__(**kwargs)
self.play_down_source = "./Images/PlayDown.png"
self.play_up_source = "./Images/PlayUp.png"
self.pause_down_source = "./Images/PauseDown.png"
self.pause_up_source = "./Images/PauseUp.png"
self.source = self.play_up_source
self.playing = False
def stop(self):
self.source = self.play_up_source
self.playing = False
def on_touch_down(self, touch):
if (self.left <= touch.x <= self.right) and (self.bottom <= touch.y <= self.top):
if self.source == self.play_up_source:
self.source = self.play_down_source
elif self.source == self.pause_up_source:
self.source = self.pause_down_source
def on_touch_move(self, touch):
if (self.left <= touch.x <= self.right) and (self.bottom <= touch.y <= self.top):
if self.playing:
self.source = self.pause_down_source
else:
self.source = self.play_down_source
else:
if self.playing:
self.source = self.pause_up_source
else:
self.source = self.play_up_source
def on_touch_up(self, touch):
if (self.left <= touch.x <= self.right) and (self.bottom <= touch.y <= self.top):
if not self.playing:
root.ids["grid"].save_state()
self.playing = True
self.source = self.pause_up_source
else:
self.playing = False
self.source = self.play_up_source
root.ids["grid"].toggle()
## Stops the animation, if any, and goes to the next frame
class NextButton(ImageButton):
def __init__(self, **kwargs):
super(NextButton, self).__init__(**kwargs)
get_bounds(self)
self.up_source = "./Images/NextUp.png"
self.down_source = "./Images/NextDown.png"
self.source = self.up_source
def on_touch_up(self, touch):
if (self.left <= touch.x <= self.right) and (self.bottom <= touch.y <= self.top):
self.source = self.up_source
root.ids["grid"].stop()
root.ids["play_pause_button"].stop()
root.ids["grid"].update_board()
## Stops the animation, if any, and restores the grid to its original state
class RestoreButton(ImageButton):
def __init__(self, **kwargs):
super(RestoreButton, self).__init__(**kwargs)
get_bounds(self)
self.up_source = "./Images/RestoreUp.png"
self.down_source = "./Images/RestoreDown.png"
self.source = self.up_source
def on_touch_up(self, touch):
if (self.left <= touch.x <= self.right) and (self.bottom <= touch.y <= self.top):
self.source = self.up_source
root.ids["grid"].load_initial_state()
## Saves the board's current state and takes the user to the save stamp screen
## Does nothing if the board is empty
class SaveButton(ImageButton):
def __init__(self, **kwargs):
super(SaveButton, self).__init__(**kwargs)
get_bounds(self)
self.up_source = "./Images/SaveUp.png"
self.down_source = "./Images/SaveDown.png"
self.source = self.up_source
def on_touch_up(self, touch):
if (self.left <= touch.x <= self.right) and (self.bottom <= touch.y <= self.top):
self.source = self.up_source
root.ids["grid"].stop()
root.ids["play_pause_button"].stop()
root.ids["grid"].record_stamp()
## grid.stamp is set to None if user tried to save blank screen
if root.ids["grid"].stamp == None:
return
root.current = "save_stamp_screen"
## Scrambles the tiles
class RandomButton(ImageButton):
def __init__(self, **kwargs):
super(RandomButton, self).__init__(**kwargs)
get_bounds(self)
self.up_source = "./Images/RandomUp.png"
self.down_source = "./Images/RandomDown.png"
self.source = self.up_source
def on_touch_up(self, touch):
if (self.left <= touch.x <= self.right) and (self.bottom <= touch.y <= self.top):
self.source = self.up_source
root.ids["play_pause_button"].stop()
root.ids["grid"].randomize()
## Clears the tiles
class ClearButton(ImageButton):
def __init__(self, **kwargs):
super(ClearButton, self).__init__(**kwargs)
get_bounds(self)
self.up_source = "./Images/ClearUp.png"
self.down_source = "./Images/ClearDown.png"
self.source = self.up_source
def on_touch_up(self, touch):
if (self.left <= touch.x <= self.right) and (self.bottom <= touch.y <= self.top):
self.source = self.up_source
root.ids["grid"].clear_board()
root.ids["grid"].stop()
root.ids["play_pause_button"].stop()
# Touch: parent receives signal. If return False (default), then pass to
# most recently added child, then second most recent, etc.
# Root widget receives touch first
## Class representing tiles in the game
class Tile(Image):
live_source = "./Images/GreenFade.png"
dead_source = "./Images/Transparent.png"
side_len = 20
draw_mode = True
def __init__(self, index, **kwargs):
super(Tile, self).__init__(**kwargs)
self.source = Tile.dead_source
self.allow_stretch = True
self.keep_ratio = False
self.index = index
self.alive = False
self.departed = True
## Changes tile behaviour to draw mode
@staticmethod
def to_draw_mode():
Tile.draw_mode = True
## Changes tile behaviour to stamp mode
@staticmethod
def to_stamp_mode():
Tile.draw_mode = False
## Enforces that this tile uses the live source
## If already alive, don't bother reassigning the source
def live(self):
if self.alive and self.source == Tile.live_source:
return
self.alive = True
self.source = Tile.live_source
## Same as live. Enforces that the tile uses the dead source
## If already dead, don't bother reassigning the source
def die(self):
if not self.alive and self.source == Tile.dead_source:
return
self.alive = False
self.source = Tile.dead_source
# self.departed: true if the touch has left this tile.
def on_touch_down(self, touch):
get_bounds(self)
## If touching this tile:
if (self.left <= touch.x <= self.right) and (self.bottom <= touch.y <= self.top):
## If draw mode: then draw a dot
if Tile.draw_mode == True:
if self.alive:
self.die()
else:
self.live()
## If stamp mode: draw the stamp
else:
self.parent.paste_stamp(self.index, self.parent.stamp)
## Either way, departed is now False
self.departed = False
# Status should only be updated if the user is returning (i.e. departed, then came back)
# Should not continuously shift back and forth because user's finger stayed too long
def on_touch_move(self, touch):
get_bounds(self)
## If coming back to this tile and in draw mode:
if self.departed and Tile.draw_mode and (self.left <= touch.x <= self.right) and (self.bottom <= touch.y <= self.top):
if self.alive == True:
self.die()
else:
self.live()
self.departed = False
## If touch wasn't on this tile, then departed is True
if (not (self.left <= touch.x <= self.right)) or (not (self.bottom <= touch.y <= self.top)):
self.departed = True
## No consideration for stamps because user shouldn't be able to accidentally
## paste the same stamp mutiple times next to each other (ruins the stamp)
def on_touch_up(self, touch):
get_bounds(self)
## If touch up was on this dot, the departed is True
if (self.left <= touch.x <= self.right) and (self.bottom <= touch.y <= self.top):
self.departed = True
## The app itself
class GameOfLifeApp(App):
def __init__(self, **kwargs):
super(GameOfLifeApp, self).__init__(**kwargs)
self.settings_functions = {
u'updates_per_second' : self.update_updates_per_second,
u'req_to_live' : self.update_req_to_live,
u'req_to_birth' : self.update_req_to_birth,
u'rule_to_use' : self.update_rule_to_use,
u'live_tile' : self.update_live_tile,
u'dead_tile' : self.update_dead_tile,
u'tile_size' : self.update_tile_size,
u'background' : self.update_background,
u'custom_live_tile' : self.update_custom_live_tile,
u'custom_dead_tile' : self.update_custom_dead_tile,
u'custom_background' : self.update_custom_background }
self.rules = self.read_rules_from_file("gameofliferules.txt")
self.tiles = self.read_tiles_from_file("gameoflifetiles.txt")
global app
app = self
## Takes a file name and updates the rules dictionary with definitions from the file
def read_rules_from_file(self, filename):
rules = {}
rule_file = open(filename, "r")
for line in rule_file:
rule = line.split(":")
living_req_str = rule[1]
living_req_list = []
living_req_len = len(living_req_str)
for i in range(living_req_len):
living_req_list.append(int(living_req_str[i]))
birth_req_str = rule[2][:-1] ## Get rid of newline character at the end
birth_req_list = []
birth_req_len = len(birth_req_str)
for i in range(birth_req_len):
birth_req_list.append(int(birth_req_str[i]))
rules[rule[0]] = (living_req_list, birth_req_list)
return rules
## Takes a file name and updates the tiles dictionary with paths from the file
def read_tiles_from_file(self, filename):
tiles = {}
tile_file = open(filename, "r")
for line in tile_file:
tile = line.split(":")
tiles[tile[0]] = tile[1][:-1] ## Get rid of the newline character at the end
return tiles
## Calls the grid's build_self function
def build_grid(self):
self.grid.build_self()
## Sets everything up
def build(self):
self.root = Juggler()
self.grid = self.root.children[0].children[0].children[2].children[0].children[0]
self.build_grid()
## Settings panel
self.settings_cls = SettingsWithSidebar
self.use_kivy_settings = False
## Make root accessible to all
global root
root = self.root
return self.root
## Sets up default settings in the settings panel
def build_config(self, config):
config.setdefaults("logic", {
"updates_per_second" : 10,
"req_to_live" : "2, 3",
"req_to_birth" : "3",
"rule_to_use" : "Conway",})
config.setdefaults("aesthetics", {
"live_tile" : "Green Fade",
"dead_tile" : "Transparent",
"tile_size" : 20,
"background" : "Black",
"custom_live_tile" : "/",
"custom_dead_tile" : "/"})
## Adds the two settings panels
def build_settings(self, settings):
settings.add_json_panel("Gameplay", self.config, data=logic)
settings.add_json_panel("Aesthetics", self.config, data=aesthetics)
## Calls corresponding function to the setting that is changed
def on_config_change(self, config, section, key, value):
self.settings_functions.get(key, self.setting_not_found)(config, value)
## Should never be called, but here in case
def setting_not_found(self, value, *args):
print "Can't do anything about %s, setting not found!" % str(value)
## Changes the update frequency
def update_updates_per_second(self, config, new_updates_per_second):
new_updates_per_second = float(new_updates_per_second)
self.grid.updates_per_second = new_updates_per_second
## Converts string input to individual digits
def to_single_digits(self, single_digits):
string = str(single_digits)
str_numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '0']
new_numbers = []
for char in string:
if char in str_numbers:
new_numbers.append(int(char))
return new_numbers
## Updates the requirement to living
def update_req_to_live(self, config, new_req_to_live):
new_numbers = self.to_single_digits(new_req_to_live)
self.grid.req_to_live = new_numbers
new_live_str = ""
for char in new_numbers:
new_live_str += str(char)
new_live_str += ", "
new_live_str = new_live_str[:-2]
## Updates the requirement to come alive
def update_req_to_birth(self, config, new_req_to_birth):
new_numbers = self.to_single_digits(new_req_to_birth)
print "New list of neighbours needed to come to live: ", new_numbers
self.grid.req_to_birth = new_numbers
## Updates the set of rules to use, e.g. Conway is 2, 3 to live, 3 to come alive
def update_rule_to_use(self, config, new_rule_to_use):
new_rule_set = self.rules[new_rule_to_use]
self.update_req_to_live(config, new_rule_set[0])
self.update_req_to_birth(config, new_rule_set[1])
## Updates live tile with new picture
def update_live_tile(self, config, new_live_tile):
Tile.live_source = self.tiles[str(new_live_tile)]
print "Tile.live_source is updated to: ", Tile.live_source
for child in self.root.ids["grid"].children:
if child.alive:
child.source = Tile.live_source
## Updates dead tile with new picture
def update_dead_tile(self, config, new_dead_tile):
Tile.dead_source = self.tiles[str(new_dead_tile)]
print "Tile.dead_source is updated to: ", Tile.dead_source
for child in self.root.ids["grid"].children:
if not child.alive:
child.source = Tile.dead_source
## Delete all tiles, update tile size, add new number of tiles back
## This takes a while...