-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathblack_white.py
1491 lines (1298 loc) · 57 KB
/
black_white.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
# coding: utf-8
"""Main game module.
Scene subclass (Game) handles all game logic and events.
"""
# Import Scene and UI modules
from scene import *
import ui
# Import standard modules
from math import pi
from random import choice, randint, randrange, random, uniform
from time import sleep, time
from datetime import date
import dialogs
import pickle
import sound
import os
import webbrowser
# Import custom modules
from database import check_final_score, get_all_scores
from square import Square
from start_finish import StartFinish
from powerup import Powerup
from configuration import save_settings
from common import *
# Class: Game
class Game (Scene):
"""Main scene subclass.
Manages all aspects of the game mechanics.
"""
# Score property (get).
@property
def score(self):
"""Score property (get)."""
return self._score
# Score property (set).
@score.setter
def score(self, value):
"""Score property (set).
Automatically updates score label.
"""
self._score = value
self.score_label.text = str(self._score)
# Game setup method.
def setup(self):
"""Game setup method."""
# Background color
self.background_color = background_color
self.alpha = 0
# Score (see score properties at top of class)
self._score = 0
self.run_action(A.fade_to(1, 0.2))
# Background of random squares
bg = ui.Path().rounded_rect(0, 0, square_size, square_size, 4)
bg.fill()
bg.close()
self.bg_list = []
for x in range(randrange(20, 100)):
self.random_bg = ShapeNode(bg, color=choice(all_colors), position=(
randint(0, screen_w), randint(0, screen_h)), size=(square_size, square_size))
self.random_bg.alpha = 0.05 * randrange(1, 9)
self.random_bg.speed = randrange(1, 6)
self.random_bg.z_position = 0.2
self.add_child(self.random_bg)
self.bg_list.append(self.random_bg)
# Title (words)
self.title = LabelNode("black white", font=(
'Helvetica Neue', title_font_size), color=text_color, position=title_position)
self.title.z_position = 1.0
self.add_child(self.title)
# Title (&)
self.title2 = LabelNode("&", font=(
'Helvetica-bold', title_font_size), color=text_color, position=title_position)
self.title2.z_position = 1.0
self.add_child(self.title2)
# Settings Button
self.settings = SpriteNode(make_texture('Cog', text_color), position=(
top_button_side_gap, screen_h - top_button_top_gap), scale=top_button_scale)
self.settings.z_position = 0.9
self.add_child(self.settings)
# Highscore Button
self.highscore_button = SpriteNode(make_texture('Group', text_color), position=(
screen_w - top_button_side_gap, screen_h - top_button_top_gap), scale=top_button_scale)
self.highscore_button.z_position = 0.9
self.add_child(self.highscore_button)
# Set starting level
self.level = 0
# Message background
self.level_bg = SpriteNode(color=background_color, position=(
screen_w / 2, label_height), size=(screen_w, 26), alpha=0.7)
self.level_bg.z_position = 0.8
self.add_child(self.level_bg)
# Message label
self.level_label = LabelNode("Level 1", font=(
'Helvetica-bold', 20), color=text_color, position=(screen_w / 2, label_height))
self.level_label.z_position = 0.9
self.add_child(self.level_label)
# Power-Up 1
self.powerup_1 = Powerup(1, starting_powerups, (
screen_w / 2 - square_size * 2, powerup_vertical))
self.add_child(self.powerup_1)
# Power-Up 2
self.powerup_2 = Powerup(2, starting_powerups,
(screen_w / 2, powerup_vertical))
self.add_child(self.powerup_2)
# Power-Up 3
self.powerup_3 = Powerup(3, starting_powerups, (
screen_w / 2 + square_size * 2, powerup_vertical))
self.add_child(self.powerup_3)
# Square counters (black and white)
self.black_count = LabelNode("0", font=(
'Helvetica', counter_font_size), color=color2, position=(-100, -100), z_position=0.55)
self.add_child(self.black_count)
self.white_count = LabelNode("0", font=(
'Helvetica', counter_font_size), color=color1, position=(-100, -100), z_position=0.55)
self.add_child(self.white_count)
# Empty list of squares for the grid
self.squares = []
# Start and End Points
self.start = StartFinish(row=randint(1, rows), type="start")
self.add_child(self.start)
self.finish = StartFinish(row=randint(1, rows), type="finish")
self.add_child(self.finish)
self.backdrop3a = SpriteNode(position=self.start.position, size=(
8 * square_size, square_size + 10), color=text_color)
self.backdrop3a.anchor_point = (0.965, 0.5)
self.backdrop3a.z_position = 0.4
self.add_child(self.backdrop3a)
self.backdrop3b = SpriteNode(position=self.finish.position, size=(
8 * square_size, square_size + 10), color=text_color)
self.backdrop3b.anchor_point = (0.035, 0.5)
self.backdrop3b.z_position = 0.4
self.add_child(self.backdrop3b)
# Commit Button
bd4 = ui.Path().rounded_rect(0, 0, square_size * 2, 50, 8)
bd4.fill()
bd4.close()
self.backdrop4 = ShapeNode(bd4, position=(
screen_w / 2, bottom_vertical), color=color1, size=(square_size * 2, 50))
self.backdrop4.z_position = 0.8
self.add_child(self.backdrop4)
bd5 = ui.Path().rounded_rect(0, 0, square_size * 2 - 10, 40, 4)
bd5.fill()
bd5.close()
self.backdrop5 = ShapeNode(bd5, stroke_color=color2, position=(
screen_w / 2, bottom_vertical), size=(2 * square_size - 10, 40))
self.backdrop5.line_width = 2
self.backdrop5.z_position = 0.85
self.add_child(self.backdrop5)
self.commit_button = SpriteNode(texture=make_texture('Check', text_color), color=text_color, position=(
screen_w / 2, bottom_vertical), size=(square_size, square_size), z_position=0.9)
self.add_child(self.commit_button)
# Restart Button and Timer
circle1 = ui.Path()
circle1.add_arc(0, 0, 24, 0, pi * 2)
circle1.line_width = 4
circle1.stroke()
circle1.close()
circle2 = ui.Path()
circle2.add_arc(0, 0, 15, 0, pi * 2)
circle2.close()
circle3 = ui.Path()
circle3.add_arc(0, 0, 26, 0, 2 * pi)
circle3.line_width = 2
circle3.stroke()
circle3.close()
tex_res = Texture('iob:ios7_refresh_32')
self.restart_button = SpriteNode(texture=tex_res, position=(
screen_w / 2 + square_size * 2.5, bottom_vertical), scale=1)
self.restart_button.z_position = 0.4
self.add_child(self.restart_button)
# Timer backgrounds
self.green_timer_background = ShapeNode(
circle1, color4, color1, position=self.restart_button.position)
self.green_timer_background.rotation = pi / 2
self.green_timer_background.z_position = 0.18
self.add_child(self.green_timer_background)
self.white_timer_background = ShapeNode(
circle2, fill_color=color4, position=self.restart_button.position)
self.white_timer_background.z_position = 0.35
self.add_child(self.white_timer_background)
# Timer
p = ui.Path()
p.add_arc(0, 0, 26, 0, 0)
p.stroke()
p.close()
self.timer = ShapeNode(p, background_color, background_color)
self.timer.z_position = 0.19
self.timer.rotation = pi / 2
self.timer.position = self.restart_button.position
self.timer.alpha = 1
self.add_child(self.timer)
# Timer stright line markings
line = ui.Path().rect(0, 0, 4, 25)
line.fill()
line.close()
self.timer_mark = ShapeNode(
line, color1, color1, position=self.restart_button.position, size=(4, 25))
self.timer_mark.anchor_point = (0.5, 0)
self.timer_mark.z_position = 0.3
self.add_child(self.timer_mark)
self.timer_mark_2 = ShapeNode(
line, color1, color1, position=self.restart_button.position, size=(4, 25))
self.timer_mark_2.anchor_point = (0.5, 0)
self.timer_mark_2.z_position = 0.3
self.add_child(self.timer_mark_2)
self.timer_ring = ShapeNode(circle3)
self.timer_ring.alpha = 0
# Low time warning flag
self.warning_flag = False
# Score label
self.score_label = LabelNode("0", font=('Helvetica-bold', 40), color=text_color, position=(
screen_w / 2 - square_size * 2.5, bottom_vertical), size=(square_size, square_size), z_position=0.9)
self.add_child(self.score_label)
# Initialise variables needed for running of game
self.can_play = True
self.can_settings = True
self.can_highscore = True
self.unlock = False
self.can_restart = False
self.win = False
self.no_whites = False
self.punishment = False
self.reward = False
self.can_flip = False
self.low_time = False
self.green_list = []
# Start a new game!
self.new_game(False)
if not username or username == "Player":
self.get_name()
# Prompt user to enter name on first startup.
@ui.in_background
def get_name(self):
"""Prompt user to enter name on first startup."""
global username
sleep(1)
try:
username = dialogs.input_alert(
"Enter your name", "This will be used on the high score leaderboard!")
if username == '':
username = 'Player'
except KeyboardInterrupt:
username = "Player"
first_time = False
new_data = (background_color, color3, color4,
text_color, difficulty, username, first_time)
save_settings(new_data)
# Handle tap on main commit button (bottom centre).
def commit(self):
"""Handle tap on main commit button (bottom centre)."""
self.stop_squares_moving()
self.destroy_crosses()
self.can_play = False
self.low_time = False
self.can_settings = False
self.highscore_button.alpha = 1
sound.play_effect(button_sound)
for square in self.squares:
# Start the process with the square beside the start point
if square.row == self.start.row and square.col == 1:
if square.state == 2:
square.state = 3
square.color = color4
self.go(square)
else: # Lose if starting square is not white!
self.losing()
# Check for valid path of white squares through grid.
def go(self, start_square):
"""Check for valid path of white squares through grid.
Uses a double-ended queue data structure.
"""
def cascade(node, progress):
if progress == 1 and node.state == 3 and self.win:
node.state = 4
self.sparkle(color4, node.position, image='shp:RoundRect')
node.color = color4
elif progress == 1 and node.state == 4 and self.win:
self.sparkle(color4, node.position, image='shp:RoundRect')
node.color = color4
self.finish.color = color4
elif progress == 1 and node.state == 0 and not self.win:
self.sparkle(color3, node.position, image='shp:RoundRect')
node.color = color3
self.green_list.append(start_square)
index = 0.01
while self.green_list:
square = self.green_list.pop(randint(0, len(self.green_list) - 1))
square.state = 3
square.run_action(A.call(cascade, index))
index += 0.01
for n in square.white_neighbours(self.squares):
if n not in self.green_list:
self.green_list.append(n)
# Once list is empty, check win status
self.check_win()
# Check state of square beside end point to determine win status.
def check_win(self):
"""Check state of square beside end point to determine win status."""
self.can_play = False
self.can_flip = False
self.unlock = False
for square in self.squares:
square.rotation = 0.0
if square.row == self.finish.row and square.col == cols:
if square.state == 3:
square.state = 4
self.win = True
self.can_play = False
self.winning()
return
elif square.state == 4:
return
self.losing()
# Win confirmed!
def winning(self):
"""Win confirmed!"""
square_states = [square.state for square in self.squares]
self.black_count.text = str(square_states.count(1))
self.white_count.text = str(square_states.count(2))
add_score = 0
for square in self.squares:
if square.state >= 3:
add_score += 1
self.commit_button.texture = make_texture('Right', text_color)
self.restart_button.texture = Texture('iob:checkmark_circled_32')
if self.star_square:
if self.star_square.state >= 3:
self.sparkle(text_color, self.star_square.position,
image='shp:Star', spread=40, z_position=1.1)
p_list = []
for item in (self.powerup_1, self.powerup_2, self.powerup_3):
if item.count < 9:
p_list.append(item)
if not p_list:
p_list.append(self.score_label)
target = choice(p_list)
pos = target.position
self.powerup_indicator = target
if text_color == 1:
self.star_square.star_icon.scale = 0.8
self.star_square.star_icon.texture = Texture('typw:Star')
self.star_square.star_icon.z_position = 0.9
self.star_square.star_icon.run_action(A.sequence(A.scale_by(0.2, 0.1), A.scale_by(-0.2, 0.1), A.group(A.move_to(
pos[0], pos[1], 1.8, TIMING_SINODIAL), A.fade_to(0, 1.8, TIMING_EASE_IN), A.rotate_by(2 * pi, 2)), A.remove()))
if target != self.score_label:
target.run_action(A.sequence(A.wait(1.8), A.scale_to(
1.2, 0.2, TIMING_BOUNCE_IN_OUT), A.scale_to(1, 0.2)))
if target == self.score_label:
self.score += self.level
self.ten = LabelNode("+" + str(self.level), font=('Helvetica-bold', 30),
position=self.star_square.position + (0, 30), color=text_color, z_position=0.81)
self.add_child(self.ten)
self.ten.run_action(A.sequence(A.wait(0.2), A.group(A.move_to(
pos[0], pos[1] + 50, 1.8, TIMING_SINODIAL), A.fade_to(0, 1.9, TIMING_EASE_IN)), A.remove()))
for item in (self.powerup_1, self.powerup_2, self.powerup_3):
self.sparkle(color4, item.position, image='shp:Star')
item.run_action(A.sequence(
A.scale_to(1.2, 0.4), A.scale_to(1, 0.4)))
else:
def powerup_increment(pu, progress):
if progress == 1 and pu.count < 9:
sound.play_effect(powerup_sound)
pu.count += 1
target.run_action(A.call(powerup_increment, 1.8))
for bg in self.bg_list:
bg.color = color4
self.backdrop5.color = color4
self.move_counters()
self.score_change(add_score, self.win)
self.end_sound()
# Loss confirmed.
def losing(self):
"""Lose confirmed."""
score_value = self.score
self.green_timer_background.fill_color = color3
for square in self.squares:
if square.star:
square.star_icon.z_position = 1
square.star_icon.run_action(
A.sequence(A.scale_to(0, 1), A.remove()))
if square.state == 3:
square.state = 0
if square.state == 0:
square.z_position = 0.25
bg_target = choice(self.bg_list).position
square.run_action(A.sequence(A.wait(random() + 1), A.group(A.scale_to(0, 3), A.fade_to(
0, 2), A.move_to(bg_target[0], bg_target[1], 2, TIMING_SINODIAL)), A.remove()))
self.start.color = color3
self.score_label.color = color3
self.backdrop5.color = color3
self.commit_button.texture = make_texture('Cross', text_color)
if self.level == 1:
self.level_label.text = choice(quick_fail_text)
elif score_value == 0 and self.level > 1:
self.level_label.text = choice(zero_fail_text)
elif score_value < 0:
self.level_label.text = choice(neg_fail_text)
else:
self.level_label.text = choice(fail_text)
self.move_counters()
self.save(score_value)
for bg in self.bg_list:
bg.color = color3
self.can_settings = True
self.end_sound()
# Handle score change animation.
def score_change(self, num, win):
"""Handle score change animation."""
if num > 0:
text = "+" + str(num)
elif num < 0:
text = str(num)
else:
text = ""
red_count = len(
[square for square in self.squares if square.state == 0])
if win:
self.score_label1 = LabelNode(text, font=('Helvetica', score_label_font_size), color=color4, position=(
screen_w / 2, 100 + score_label_gap), size=(square_size, square_size), z_position=0.8, alpha=0)
self.add_child(self.score_label1)
self.score_label1.run_action(score_action_1)
self.score_label2 = LabelNode("-" + str(self.white_count.text), font=('Helvetica', score_label_font_size),
color=color2, position=(screen_w / 2, 100), size=(square_size, square_size), z_position=0.8, alpha=0)
self.add_child(self.score_label2)
self.score_label2.run_action(score_action_1)
self.score_label3 = LabelNode("-" + str(red_count), font=('Helvetica', score_label_font_size), color=color3, position=(
screen_w / 2, 100 - score_label_gap), size=(square_size, square_size), z_position=0.8, alpha=0)
self.add_child(self.score_label3)
self.score_label3.run_action(score_action_1)
sq = ui.Path().rounded_rect(0, 0, square_size * 4 - 12, square_size * 4 - 12, 4)
sq.fill()
sq.close()
self.score_label_back = ShapeNode(
sq, color=color1, position=(screen_w / 2, 100), alpha=0)
self.score_label_back.z_position = 0.7
self.score_label_back.size = (
4 * square_size - 14, 4 * square_size - 14)
self.add_child(self.score_label_back)
self.score_label_back.run_action(score_action_2)
if self.no_whites:
self.score_label2.text = "+" + str(rows + cols)
num += rows + cols
total_score_change = num - int(self.white_count.text) - red_count
self.total_score_change_label = LabelNode("+" + str(total_score_change), font=(
'Helvetica-bold', 40), color=text_color, position=self.score_label.position)
self.total_score_change_label.z_position = 0.6
if self.no_whites:
if total_score_change < rows * cols:
self.level_label.text = choice(no_white_text)
else:
self.reward = True
if self.powerup_1.count + self.powerup_2.count + self.powerup_3.count < 18:
self.level_label.text = choice(reward_text)
else:
self.level_label.text = choice(no_white_text)
for item in (self.powerup_1, self.powerup_2, self.powerup_3):
if item.count < 9:
item.count += 1
item.run_action(A.sequence(A.scale_to(
1.2, 0.2, TIMING_BOUNCE_IN_OUT), A.scale_to(1, 0.2)))
self.sparkle(
color4, item.position - (square_size / 4.0, 0), image='shp:RoundRect')
else:
if total_score_change > 0:
self.level_label.text = choice(win_text)
self.total_score_change_label.text = "+" + \
str(total_score_change)
elif total_score_change == 0:
self.level_label.text = choice(zero_text)
self.total_score_change_label.text = "+" + \
str(total_score_change)
self.punishment = True
elif total_score_change < 0:
self.level_label.text = choice(neg_text)
self.total_score_change_label.text = str(
total_score_change)
self.total_score_change_label.color = color3
self.punishment = True
else:
total_score_change = -1 * self.score
self.total_score_change_label = LabelNode(str(total_score_change), font=(
'Helvetica-bold', 40), color=color3, position=self.score_label.position)
self.add_child(self.total_score_change_label)
self.score += total_score_change
self.total_score_change_label.run_action(A.sequence(A.fade_to(1, 0.1), A.wait(
1), A.move_to(screen_w / 2, bottom_vertical, 2, TIMING_EASE_IN_OUT), A.remove()))
self.score_label.run_action(A.sequence(
A.fade_to(0, 0), A.wait(1.8), A.fade_to(1, 0.5)))
# Start a new game.
def new_game(self, win):
"""Start a new game."""
self.can_restart = False
for item in (self.timer_mark, self.timer_mark_2):
item.alpha = 1
item.run_action(A.sequence(
A.scale_y_to(0.6, 0), A.scale_y_to(1, 0.3)))
for item in (self.green_timer_background, self.white_timer_background):
item.run_action(A.sequence(A.scale_to(0.6, 0), A.scale_to(1, 0.3)))
self.restart_button.texture = Texture('iob:ios7_refresh_32')
# Alert if score would be reset
if self.can_play and self.score != 0:
for square in self.squares:
square.rotation = 0
try:
dialogs.alert("Do you want to start again?",
"Your score will be reset to zero!", "Restart")
self.level = 0
self.level_label.text = 'level ' + str(self.level)
self.losing()
except KeyboardInterrupt:
self.can_restart = True
return
# Set variables
self.can_play = False
self.no_whites = False
self.win = False
self.level += 1
self.level_label.text = 'Level ' + str(self.level)
# Reset timer elements
self.white_timer_background.fill_color = color2
self.green_timer_background.fill_color = color4
p = ui.Path()
p.add_arc(0, 0, 28.0, 0, 0)
p.stroke()
p.close()
self.timer.path = p
self.timer.position = self.restart_button.position
self.timer_mark_2.rotation = 0
self.warning_flag = False
# Move background squares
for bg in self.bg_list:
bg.run_action(A.move_to(randint(0, screen_w),
randint(0, screen_h), random()))
bg.color = choice(all_colors)
# Make a new grid
self.make_grid()
# Remove score popup if still present
try:
for item in (self.score_label1, self.score_label2, self.score_label3, self.score_label_back):
item.run_action(A.remove())
except:
pass
# Reset score, level and powerups if previous round not won
if not win:
self.score = 0
self.level = 0
self.level_label.text = 'Level ' + str(self.level)
self.can_settings = True
for pu in (self.powerup_1, self.powerup_2, self.powerup_3):
pu.count = starting_powerups
# Move black & white square counters
self.move_counters()
self.score_label.color = text_color
self.backdrop5.color = color4
self.commit_button.texture = make_texture('Check', text_color)
self.restart_button.remove_all_actions()
# New game sound
sound.play_effect(new_game_sound)
# MCreate a new grid of squares.
def make_grid(self):
"""Create a new grid of squares."""
global rows, cols, top_left
if self.punishment:
rows, cols = 4, 4
self.level_label.text = choice(punishment_text)
else:
rows = randrange(4, 11, 2)
if rows == 4:
cols = 6
else:
cols = randrange(4, 7, 2)
top_left = (centre[0] - square_size * (cols / 2.0 - 0.5),
(centre[1] + square_size * (rows / 2.0 - 0.5)))
for square in self.squares:
bg_target = choice(self.bg_list).position
square.alpha = 0.6
square.scale = 0.6
square.run_action(A.sequence(A.group(A.scale_to(0, 2), A.fade_to(
0, 2), A.move_to(bg_target[0], bg_target[1], 2, TIMING_SINODIAL)), A.remove()))
self.start.row = randint(1, rows)
self.start.color = color4
self.finish.row = randint(1, rows)
self.finish.color = color2
border = ui.Path().rounded_rect(
0, 0, cols * square_size + 20, rows * square_size + 20, 4)
border.line_width = 6
border.stroke()
border.close()
fine_border = ui.Path().rect(0, 0, cols * square_size, rows * square_size)
fine_border.line_width = 1
fine_border.stroke()
fine_border.close()
try:
self.backdrop.run_action(A.remove())
self.backdrop2.run_action(A.remove())
except:
pass
self.backdrop = ShapeNode(border, (0, 0, 0, 0), text_color, position=centre, size=(
square_size * cols + 20, square_size * rows + 20))
self.backdrop.blend_mode = BLEND_NORMAL
self.backdrop.z_position = 0.3
self.add_child(self.backdrop)
self.backdrop2 = ShapeNode(fine_border, (0, 0, 0, 0), text_color, position=centre, size=(
square_size * cols, square_size * rows))
self.backdrop2.z_position = 0.2
self.add_child(self.backdrop2)
self.start.run_action(A.move_to(
top_left[0] - square_size, top_left[1] - square_size * (self.start.row - 1), 0.3))
self.finish.run_action(A.move_to(
top_left[0] + square_size * cols, top_left[1] - square_size * (self.finish.row - 1), 0.3))
self.backdrop3a.run_action(A.move_to(
top_left[0] - square_size, top_left[1] - square_size * (self.start.row - 1), 0.3))
self.backdrop3b.run_action(A.move_to(
top_left[0] + square_size * cols, top_left[1] - square_size * (self.finish.row - 1), 0.3))
horizontal = top_left[0]
vertical = top_left[1]
self.squares = []
for x in range(cols):
for y in range(rows):
self.square = Square(col=x + 1, row=y + 1, position=(horizontal, vertical), size=(
square_size, square_size), state=choice((1, 2)), color=None)
self.square.z_position = 0.5
self.add_child(self.square)
self.squares.append(self.square)
vertical -= square_size
vertical = top_left[1]
horizontal += square_size - 0.05 # Prevents vertical lone artefact between squares
longest_square = choice(self.squares)
for square in self.squares:
bg_target = choice(self.bg_list).position
pos = square.position
if square != longest_square:
square.run_action(A.sequence(A.group(A.scale_to(0, 0), A.fade_to(0, 0), A.rotate_to(randrange(0, 6), 0), A.move_to(bg_target[0], bg_target[1], 0)), A.group(A.scale_to(
1, randrange(5, 10) * 0.1), A.fade_to(0.8, randrange(1, 10) * 0.1), A.rotate_to(0, randrange(1, 10) * 0.1), A.move_to(pos[0], pos[1], randrange(1, 10) * 0.1, TIMING_SINODIAL))))
elif square == longest_square:
def now_can_play():
self.can_play = True
self.can_restart = True
self.timestamp = self.t
square.run_action(A.sequence(A.group(A.scale_to(0, 0), A.fade_to(0, 0), A.rotate_to(randrange(0, 6), 0), A.move_to(bg_target[0], bg_target[1], 0)), A.group(
A.scale_to(1, 1), A.fade_to(0.8, 1), A.rotate_to(0, 1), A.move_to(pos[0], pos[1], 1, TIMING_SINODIAL)), A.wait(0.2), A.call(now_can_play)))
try:
self.star_square.star_icon.run_action(A.sequence(
A.group(A.scale_to(0.3, 1), A.fade_to(0, 1)), A.remove()))
except:
pass
if randrange(1, 3) == 1:
self.star_square = choice(self.squares)
self.star_square.go_star()
else:
self.star_square = None
self.punishment = False
self.reward = False
self.can_flip = False
self.unlock = False
# Move the number labels that count black and white squares.
def move_counters(self):
"""Move the number labels that count black and white squares."""
black_list = [square for square in self.squares if square.state == 1]
white_list = [square for square in self.squares if square.state == 2]
self.black_count.text = str(len(black_list))
self.white_count.text = str(len(white_list))
wc = len(white_list)
# Prevents the square counters appearing on top of the star, if it exists.
try:
if self.star_square:
if self.star_square in black_list:
black_list.remove(self.star_square)
elif self.star_square in white_list:
white_list.remove(self.star_square)
except:
pass
try:
b = choice(black_list)
self.black_count.position = b.position
except:
# Off screen if no black squares
self.black_count.position = (-100, -100)
try:
w = choice(white_list)
self.white_count.position = w.position
except:
# Off screen if no white squares
self.white_count.position = (-100, -100)
if not wc:
# Set no_whites flag for bonus
self.no_whites = True
# Save score and check if new high score.
@ui.in_background
def save(self, number):
"""Save score and check if new high score.
Get result flag (nil, "g" or "p") from check_final_score method in database module.
"""
result = check_final_score(username, number, difficulty)
if result == "g":
sleep(1)
try:
sound.play_effect(star_bonus_sound)
dialogs.alert("New High Score!", "Well done " + username +
"!\n\nYou are the new champion with a score of " + str(number), "OK", hide_cancel_button=True)
except KeyboardInterrupt:
pass
elif result == "p":
sleep(1)
try:
sound.play_effect(star_bonus_sound)
dialogs.alert("New Personal Best!", "Well done " + username +
"!\n\nYour top score is " + str(number), "OK", hide_cancel_button=True)
except KeyboardInterrupt:
pass
# Show high score leaderboard.
@ui.in_background
def display_scores(self):
"""Show high score leaderboard."""
all_scores = get_all_scores(difficulty)
if not all_scores:
sound.play_effect(fail_sound)
dialogs.hud_alert("Cannot get high scores", icon="error")
self.highscore_button.alpha = 1
self.can_highscore = True
return
if self.can_play:
can_play_marker = True
self.can_play = False
else:
can_play_marker = False
if not self.win:
for square in self.squares:
if square.state == 0:
square.run_action(A.fade_to(0, 0.2))
paused_time = self.t
sheet = ui.load_view("ui/highscores.pyui")
score_table = sheet["score_table"]
title_label = sheet["title_label"]
title_label.text = "High Scores - " + \
["Easy", "Regular", "Hard"][difficulty - 1]
champion = sheet["champion"]
champion_label = sheet["champion_label"]
high_score_strings = ["{}{}{}".format(str(index + 1).ljust(4, ' '), str(item[0])[:20].ljust(
22, ' '), str(item[1]).rjust(5, ' ').ljust(8, ' ')) for index, item in enumerate(all_scores)]
data_source = ui.ListDataSource(high_score_strings)
# Monospace font (matches .pyui)
data_source.font = ("Inconsolata-Regular", 18)
score_table.data_source = data_source
score_table.reload_data()
score_table.separator_color = color4
champion.text = all_scores[0][0][:20] + " : " + str(all_scores[0][1])
score_table.bounces = False
sheet.present(hide_title_bar=True)
# sound.play_effect(button_sound)
if can_play_marker:
self.can_play = True
self.can_restart = True
self.can_highscore = True
self.highscore_button.alpha = 1
self.timestamp += self.t - paused_time
# Use timestamp for countdown.
def timing(self):
"""Use timestamp for countdown.
This is called with each update.
"""
if self.can_play:
time_allowed = 61 - (difficulty * 10) - \
(self.level * 0.5 * difficulty)
time_allowed = max(time_allowed, 5)
time_elapsed = self.t - self.timestamp
angle = 2 * pi * time_elapsed / time_allowed
if time_elapsed >= time_allowed:
self.can_play = False
self.timer_mark.run_action(A.fade_to(0, 0))
self.timer_mark_2.run_action(A.fade_to(0, 0))
self.sparkle(color3, self.restart_button.position,
image='shp:Circle', spread=50, z_position=0.2, n=20)
self.commit()
return
elif time_elapsed > time_allowed - 8 and not self.low_time and not self.warning_flag:
self.warning_flag = True
self.level_label.text = choice(hurry_text)
elif time_elapsed > time_allowed - 5 and not self.low_time:
self.low_time = True
self.green_timer_background.fill_color = color3
self.timer_sound()
# Draw arc of timer (this is a background-colored (invisible) arc that grows from 12 o'clock position clockwise to cover the coloured timer background)
radius = 28.0
p = ui.Path()
p.add_arc(0, 0, radius, 0, angle)
p.stroke()
p.close()
self.timer.path = p
mid_frame = (self.timer.frame[2] / 2.0, self.timer.frame[3] / 2.0)
rp = self.restart_button.position
# Timer position needs to change as size changes throughout animation as anchor point would cause it to move erratically
if angle < pi:
self.timer.position = (
rp[0] + mid_frame[0], rp[1] + radius - mid_frame[1])
else:
self.timer.position = (
rp[0] + radius - mid_frame[0], rp[1] + radius - mid_frame[1])
# The motion is smoothed by rotating the second black mark over the leading edge of the timer
self.timer_mark_2.rotation = 2 * pi - \
(time_elapsed / time_allowed * 2 * pi)
# Low time warning sound.
@ui.in_background
def timer_sound(self):
"""Low time warning sound."""
self.countdown_flag = True
self.highscore_button.alpha = 0.2
marker = 5
for x in range(5):
if self.can_play and self.low_time:
if self.countdown_flag:
self.level_label.text = str(marker)
sound.play_effect(timer_tick)
marker -= 1
sleep(0.98)
else:
return
# Destroy locked square crosses.
def destroy_crosses(self):
"""Destroy locked square crosses.
These are created when power-up 2 is active.
"""
for square in self.squares:
try:
square.cross.run_action(A.sequence(
A.scale_to(0, 0.2), A.remove()))
except:
pass
# Stop square animations.
def stop_squares_moving(self):
"""Stop square animations."""
for square in self.squares:
square.remove_all_actions()
square.scale = 1
try:
if self.star_square and self.can_play:
self.star_square.star_icon.remove_all_actions()
except:
pass
self.black_count.remove_all_actions()
self.white_count.remove_all_actions()
# Particle effects.
def sparkle(self, color, position, image='shp:sparkle', spread=40, z_position=0.6, n=6):
"""Particle effects."""
for i in range(n):
p = SpriteNode(image, position=position, color=color,
z_position=z_position, alpha=0.5)
r = spread
dx, dy = uniform(-r, r), uniform(-r, r)
p.run_action(A.sequence(A.group(A.scale_to(0, 0.8), A.move_by(
dx, dy, 0.8, TIMING_EASE_OUT_2)), A.remove()))
self.add_child(p)
# Determine which sound to play at end of game.
def end_sound(self):
"""Determine which sound to play at end of game."""
if self.win:
if self.star_square and self.star_square.state >= 3:
if self.powerup_indicator == self.score_label:
sound.play_effect(star_bonus_sound)
return
else:
sound.play_effect(star_sound)
if self.no_whites:
sound.play_effect(no_white_sound)
return
if int(self.total_score_change_label.text) <= 0:
sound.play_effect(neg_sound)
return
sound.play_effect(win_sound)
else:
sound.play_effect(fail_sound)