/
main.py
executable file
·1155 lines (1021 loc) · 38.6 KB
/
main.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 -*-
""" simple Color Games made with kivy """
__version__ = '0.5.39'
from kivy.app import App
from kivy.uix.progressbar import ProgressBar
from kivy.uix.button import Button
from kivy.properties import ObjectProperty, StringProperty, NumericProperty
from kivy.core.audio import SoundLoader
from kivy.core.window import Window
from plyer import vibrator
from kivy.utils import platform
from kivy.clock import Clock
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.label import Label
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.settings import SettingsWithNoMenu
from kivy.uix.image import Image
import random
import time
import os
import gettext
Window.size = (480, 800)
# Set up message catalog access
t = gettext.translation('main', 'locale', fallback=True)
_ = t.ugettext
class Buttonmy(ButtonBehavior, Label):
""" kv: GameScreen custon button """
pass
class ButtonHelp(ButtonBehavior, Image):
""" kv: MenuScreen, MenuScreenRepeat... custonm button """
pass
class ButtonSound(ButtonBehavior, Image):
""" kv: StartScreen: Display sound image """
def __init__(self, **kwargs):
""" Bind the button state """
super(ButtonSound, self).__init__(**kwargs)
self.bind(state=self.state_changed)
def state_changed(self, *args):
""" Change the img source when the button is pressed """
print GameScreen.active
if BrainColorGame.sound_game.volume == 0:
self.source = (os.getcwd() + "/data/sound_on_off1.png")
else:
self.source = (os.getcwd() + "/data/sound_off_on1.png")
class LooseColorRepeat(Screen):
""" name: loose """
score = 0
scores_ = str(1)
def __init__(self, **kwargs):
""" """
super(LooseColorRepeat, self).__init__(**kwargs)
def best_score_medium(self):
""" MenuScreenRepeat: called by ButtonSrart (kv) """
self.scores_ = str(BrainColorGame.best_scores_color_repeat_medium)
best_score = self.ids['best_score']
best_score.text = self.scores_
def best_score_hard(self):
""" MenuScreenRepeat: called by ButtonSrart (kv) """
self.scores_ = str(BrainColorGame.best_scores_color_repeat_hard)
best_score = self.ids['best_score']
best_score.text = self.scores_
class ScoresColorWord(Screen):
""" Screen Name = scores-color-word """
return_ = _("Return")
high_score_in_color_mode = _("High Score in Color mode: ")
high_score_in_text_mode = _("High Score in Text mode: ")
def __init__(self, **kwargs):
""" """
super(ScoresColorWord, self).__init__(**kwargs)
def update(self):
""" called by MenuScreen ButtonHelp """
word_color_ = self.ids['best_scores_color_word_color_label']
word_color_.text = str(BrainColorGame.best_scores_color_word_color)
word_text_ = self.ids['best_scores_color_word_text_label']
word_text_.text = str(BrainColorGame.best_scores_color_word_text)
class ScoresColorRepeat(Screen):
""" Screen Name = scores-color-repeat """
return_ = _("Return")
high_level_in_medium_mode = _("High Level in Medium mode: ")
high_level_in_hard_mode = _("High Level in Hard mode: ")
def __init__(self, **kwargs):
""" """
super(ScoresColorRepeat, self).__init__(**kwargs)
def update(self):
""" called by MenuScreen ButtonHelp """
ok = self.ids['best_scores_color_repeat_medium_label']
ok.text = str(BrainColorGame.best_scores_color_repeat_medium)
ok1 = self.ids['best_scores_color_repeat_hard_label']
ok1.text = str(BrainColorGame.best_scores_color_repeat_hard)
class InstructionsColorRepeat(Screen):
""" Screen Name = instruction-color-repeat """
return_ = _("Return")
color_repeat = _("Color Repeat")
text = _("There is two modes in this game:\n"
"Medium and Hard.\n"
"\n"
"In medium you must reproduice\n"
"the sequence of\nColor/Sound showned,\n"
"it's always the same sequence.\n"
"\n"
"In Hard:\n"
"The sequence is differente every time!\n"
"\n"
"Good Luck")
def __init__(self, **kwargs):
""" """
super(InstructionsColorRepeat, self).__init__(**kwargs)
class InstructionsColorWord(Screen):
""" Screen Name = instruction-color-word """
return_ = _("Return")
color_word = _("Color Word")
text = _("There is two modes in this game:\n"
"Color and Text.\n"
"\n"
"In Color Mode you must click\n"
"on the good color\n"
"You don't care about the text inside the button\n"
"Just click on the good color\n"
"\n"
"In Text Mode you must click\n"
"On the same Text showned\n"
"You don't care about the color\n"
"Just click on the same Text\n"
"\n"
"Good Luck")
def __init__(self, **kwargs):
""" """
super(InstructionsColorWord, self).__init__(**kwargs)
class ButtonOk(ButtonBehavior, Image):
""" Custon Ok Button """
def __init__(self, **kwargs):
""" """
super(ButtonOk, self).__init__(**kwargs)
self.bind(state=self.state_changed)
def state_changed(self, *args):
""" Change the img source when the button is pressed """
if self.source == (os.getcwd() + "/data/Button-Ok-push.png"):
self.source = (os.getcwd() + "/data/Button-Ok.png")
else:
self.source = (os.getcwd() + "/data/Button-Ok-push.png")
pass
class ButtonColours(ButtonBehavior, Image):
""" My custon Image Button """
def __init__(self, **kwargs):
super(ButtonColours, self).__init__(**kwargs)
self.bind(state=self.state_changed)
def state_changed(self, *args):
""" Change the img source when the button is pressed """
if self.source == (os.getcwd() + "/data/Button_yellow.png"):
self.source = (os.getcwd() + "/data/Button_purple.png")
else:
self.source = (os.getcwd() + "/data/Button_yellow.png")
class ButtonText(ButtonBehavior, Image):
""" My custon Image Button """
def __init__(self, **kwargs):
super(ButtonText, self).__init__(**kwargs)
self.bind(state=self.state_changed)
def state_changed(self, *args):
""" Change the img source when the button is pressed """
if self.source == (os.getcwd() + "/data/Button_yellow.png"):
self.source = (os.getcwd() + "/data/Button_purple.png")
else:
self.source = (os.getcwd() + "/data/Button_yellow.png")
class StartScreen(Screen):
""" The main start Screen """
exit = _("Exit")
color_word = _("Color Word")
color_repeat = _("Color Repeat")
def __init__(self, **kwargs):
""" """
super(StartScreen, self).__init__(**kwargs)
self.post_build_init()
def post_build_init(self):
""" Bind the android or the keyboard key """
if platform() == 'android':
import android
android.map_key(android.KEYCODE_BACK, 1001)
win = Window
win.bind(on_keyboard=self.key_handler)
def key_handler(self, window, keycode1, keycode2, text, modifiers):
""" Called by escape key, 'reboot' the game """
if keycode1 == 27 or keycode1 == 1001:
if self.manager.current == 'loose':
self.manager.current = 'menu-repeat'
return True
elif self.manager.current == 'win':
self.manager.current = 'menu'
return True
elif self.manager.current == 'menu':
self.manager.current = 'start'
return True
elif self.manager.current == 'menu-repeat':
self.manager.current = 'start'
return True
elif self.manager.current == 'game':
self.manager.current = 'menu'
return True
elif self.manager.current == 'game-repeat':
self.manager.current = 'menu-repeat'
return True
elif self.manager.current == 'scores-color-word':
self.manager.current = 'menu'
return True
elif self.manager.current == 'scores-color-repeat':
self.manager.current = 'menu-repeat'
return True
elif self.manager.current == 'instruction-color-repeat':
self.manager.current = 'menu-repeat'
return True
elif self.manager.current == 'instruction-color-word':
self.manager.current = 'menu'
return True
elif self.manager.current == 'start':
self.leave()
return True
return False
@staticmethod
def leave():
""" Leave the apps """
App.get_running_app().stop()
class MenuScreen(Screen):
""" BoxLayout called by kivy """
text_mode = _("Text Mode")
color_mode = _("Color Mode")
return_ = _("Return")
@staticmethod
def start_text_mode():
""" show_popup: start Text Mode """
GameScreen.mode_game = "Text Mode"
@staticmethod
def start_color_mode():
""" show_popup: start Colours Mode """
GameScreen.mode_game = "Colours Mode"
@staticmethod
def sound_validation():
""" Play a sound when call by MyButton: kv file """
sound_valid = SoundLoader.load("data/validation.ogg")
sound_valid.play()
class GameScreen(Screen):
""" BoxLayout called by kivy """
sound_pos = "unmute"
# Text when the game start
text = _('Push a button for start')
text_button = _("Push a button for start")
# Color: rouge, vert, bleu and jaune
colour1 = [1, 0, 0, 1]
# colour2 = [0, 1, 0.2, 1]
colour2 = [0.02, 0.898, 0.2, 1]
colour3 = [0, 0, 1, 1]
colour4 = [1, 1, 0, 1]
text1 = _("red")
text2 = _("green")
text3 = _("blue")
text4 = _("yellow")
colors = [colour1, colour2, colour3, colour4]
texts = [text1, text2, text3, text4]
colors_dict = {_("red"): [1, 0, 0, 1],
_("green"): [0.02, 0.898, 0.2, 1],
_("blue"): [0, 0, 1, 1],
_("yellow"): [1, 1, 0, 1]}
texts_test = ""
# Used for mix colors and texts
number_random = 0
# Used for count points
points = NumericProperty(0)
no_points = NumericProperty(0)
# start the game
points_str = ""
# Choose the game mode, by default: Colours Mode
mode_game = StringProperty(None)
old_mode_game = ""
# Time
time_1 = ""
time_2 = 0
time_penality = 0
# time_2 - time1
t_final = ""
t_final_no_penality = 0
# best time final
score_file = os.getcwd() + "/scores_bcg"
score_file_color = os.getcwd() + "/scores_color_bcg"
score_file_text = os.getcwd() + "/scores_text_bcg"
t_best = 999.
t_best_color = 999.
t_best_text = 999.
records = "New Record = "
t_best_mode = ObjectProperty(None)
# 0/1 time mode active/disable
time_mode = 0
# Switch active: active/disable time mode
active = False
# ProgressBar for time mode
progress_bar_1 = ObjectProperty(None)
value_progress_bar = 0
time_mode_ = _("Time Mode")
def __init__(self, **kwargs):
""" """
super(GameScreen, self).__init__(**kwargs)
self.post_build_init()
def get_time_1(self):
""" Used for start chronometer """
self.time_1 = time.time()
def get_time_2(self):
""" Used for end chronometer """
self.time_2 = time.time()
def reset_points_str(self):
""" Called by kv: MenuScreen Button """
self.points_str = ""
def get_time_final(self, mode):
""" chronometer time """
self.t_final = (self.time_2 + float(self.time_penality)) - self.time_1
self.t_final_no_penality = self.time_2 - self.time_1
self.t_final_no_penality = "%.2F" % self.t_final_no_penality
self.t_final = "%.2f" % self.t_final
self.compare_time_final(mode)
def update_records(self):
""" Called by kv """
BrainColorGame.best_scores_color_word_color = self.t_best_color
BrainColorGame.best_scores_color_word_text = self.t_best_text
def compare_time_final(self, mode):
""" Compare saved time and t_final """
if mode == "Colours Mode":
try:
self.open_file(self.score_file_color, mode)
except IOError:
pass
# t_best depend of the mode
self.t_best = self.t_best_color
if float(self.t_final) < float(self.t_best_color):
self.t_best_color = self.t_final
self.t_best = self.t_best_color
self.save_new_record(self.score_file_color, self.t_best_color)
else:
self.no_new_record()
elif mode == "Text Mode":
try:
self.open_file(self.score_file_text, mode)
except IOError:
pass
# t_best depend of the mode
self.t_best = self.t_best_text
if float(self.t_final) < float(self.t_best_text):
self.t_best_text = self.t_final
self.t_best = self.t_best_text
self.save_new_record(self.score_file_text, self.t_best_text)
else:
self.no_new_record()
def open_init(self):
""" Called by kv """
self.open_file(self.score_file_color, "Colour")
self.open_file(self.score_file_text, "Text")
def open_file(self, score_file_mode, mode):
""" Open scores file """
try:
file_saved = open(score_file_mode, 'r')
scr = file_saved.read()
scr = float(scr)
if "Colour" in mode:
self.t_best_color = scr
else:
self.t_best_text = scr
file_saved.close()
except IOError:
pass
def no_new_record(self):
""" if t_final >= t_best_mode """
self.records = "Best Record = "
self.sound_validation()
def save_new_record(self, score_file_mode, t_best_mode):
""" save scores in file: mode = text or color """
self.records = "New Record !!! "
file_saved = open(score_file_mode, 'w')
file_saved.write(str(t_best_mode))
file_saved.close()
def replay(self):
""" Button replay """
self.text = ""
self.value_progress_bar = 0
try:
self.progress_bar_1.value = self.value_progress_bar
except AttributeError:
pass
try:
points_kv = GameScreen.ids['points']
except TypeError:
pass
self.points = 0
self.no_points = 0
self.time_penality = 0
try:
points_kv.text = ""
except UnboundLocalError:
pass
self.ask()
self.time_1 = ""
pts_kv = self.ids['points']
pts_kv.text = ""
welcome1 = self.ids['welcome_text']
welcome1.text = _("Push a button for start")
def ask(self):
""" update question text """
try:
welcome = self.ids['welcome_text']
bt_q = self.ids['bt_q']
except KeyError:
pass
# Better than: self.number_random = random.randint(0, 3)
self.number_random = int(random.choice('0123'))
if GameScreen.mode_game == "Colours Mode":
self.text = _('Push on the good color')
elif GameScreen.mode_game == "Text Mode":
self.text = _('Push on the Good Text')
else:
pass
self.text_button = str(self.texts[self.number_random])
try:
welcome.text = self.text
bt_q.text = self.text_button
# bt_q.source = ("/home/user/Programmes/brain-color-game3/
# brain-color-game/" +
# str(self.text_button))
except UnboundLocalError:
pass
def start(self):
""" on_click start this method """
# Change the game mode (Colors Mode or Text Mode)
# Mix colors and texts
random.shuffle(self.colors)
random.shuffle(self.texts)
# Kivy buttons text
bt1b = self.ids['bt1b']
bt1b.color = self.colors[0]
bt1b.text = self.texts[0]
bt1 = self.ids['bt1']
# bt1.color = self.colors[0]
bt1.text = self.texts[0]
bt2b = self.ids['bt2b']
bt2b.color = self.colors[1]
bt2b.text = self.texts[1]
bt2 = self.ids['bt2']
# bt2.color = self.colors[1]
bt2.text = self.texts[1]
bt3b = self.ids['bt3b']
bt3b.color = self.colors[2]
bt3b.text = self.texts[2]
bt3 = self.ids['bt3']
# bt3.color = self.colors[2]
bt3.text = self.texts[2]
bt4b = self.ids['bt4b']
bt4b.color = self.colors[3]
bt4b.text = self.texts[3]
bt4 = self.ids['bt4']
# bt4.color = self.colors[3]
bt4.text = self.texts[3]
def count_points_good(self):
""" Called by count_points """
self.points += 50
if self.active is True:
self.value_progress_bar += 3
else:
pass
self.sound_points_play()
try:
self.progress_bar_1.value = self.value_progress_bar
except AttributeError:
pass
def count_points_false(self):
""" Called by count_points """
self.no_points += 1
self.time_penality += 3
self.sound_miss_play()
# On Android
try:
vibrator.vibrate(0.4)
except NotImplementedError:
pass
def count_points(self, nbr):
""" Count the points """
points_kv = self.ids['points']
# progress_bar = self.ids['progress']
# Start the Game now
if not self.points_str or nbr == 5:
self.points_str = " "
else:
# Game already started: Count points (depends on the Game mode)
if not self.time_1:
self.get_time_1()
else:
pass
if self.mode_game == "Colours Mode":
# Colours Mode
self.color_name_to_rgb(self.texts[self.number_random])
if self.colors[nbr] == self.texts_test:
# Win
self.count_points_good()
else:
# Miss
self.count_points_false()
elif self.mode_game == "Text Mode":
# Text Mode
if nbr == self.number_random:
# Win
self.count_points_good()
else:
# Miss
self.count_points_false()
else:
pass
points_kv.text = (_("Points ") + str(self.points) +
_(" Miss ") + str(self.no_points))
if self.value_progress_bar >= 99:
self.count_points_win()
def count_points_win(self):
""" Called by count_point if value_progress_bar > 99 """
# progress_bar.value = self.value_progress_bar
self.get_time_2()
if self.mode_game == "Colours Mode":
self.get_time_final("Colours Mode")
elif self.mode_game == "Text Mode":
self.get_time_final("Text Mode")
self.value_progress_bar = 0
self.manager.current = 'win'
BrainColorGame.sound_game.stop()
BrainColorGame.sound_win.play()
self.replay()
@staticmethod
def sound_miss_play():
""" If no points: play miss sound """
sound1 = SoundLoader.load('data/miss.ogg')
sound1.play()
@staticmethod
def sound_points_play():
""" If points: play point sound """
sound1 = SoundLoader.load('data/points.ogg')
sound1.play()
@staticmethod
def sound_play():
""" When change mode: play point sound """
sound1 = SoundLoader.load('data/change.ogg')
sound1.play()
@staticmethod
def sound_validation():
""" Play a sound when call by MyButton: kv file """
sound_valid = SoundLoader.load("data/validation.ogg")
sound_valid.play()
def color_name_to_rgb(self, name):
""" Change a name color to a rgb color """
name = self.colors_dict[name]
self.texts_test = name
def post_build_init(self):
""" import BACK_KEY from Android """
if platform() == 'android':
import android
android.map_key(android.KEYCODE_BACK, 1001)
win = Window
win.bind(on_keyboard=self.key_handler)
def key_handler(self, window, keycode1, keycode2, text, modifiers):
""" On push Back_key: run go_start (popup) """
if keycode1 == 27 or keycode1 == 1001:
self.replay()
def on_pause(self):
""" Enable pause on mobile """
return True
def change_time_mode(self):
""" Enable or Disable time_mode """
if self.time_mode == 0:
self.progress_bar_1 = ProgressBar(id='progress',
value=self.value_progress_bar,
size_hint_y=0.1,
pos_hint={'center_x': 0.5,
'center_y': 0.9})
self.time_mode = 1
self.active = True
self.add_widget(self.progress_bar_1)
else:
self.time_mode = 0
self.active = False
self.remove_widget(self.progress_bar_1)
if not self.points_str:
pass
else:
self.replay()
@staticmethod
def self_active():
""" Mute/Unmute """
if GameScreen.sound_pos == "unmute":
GameScreen.sound_pos = "mute"
BrainColorGame.sound_game.volume = 0
elif GameScreen.sound_pos == "mute":
GameScreen.sound_pos = "unmute"
BrainColorGame.sound_game.volume = 1
class WinScreen(Screen):
""" On Win: Progress_bar """
text1 = str(GameScreen.points)
text2 = StringProperty("")
text3 = StringProperty("")
text4 = StringProperty("")
text5 = StringProperty("")
text6 = StringProperty("")
you_win_ = _("You Win !")
time_ = _("time: ")
seconds_ = _("seconds")
time_final_ = _("Time final: ")
miss_ = _("Miss: ")
penality_ = _("Penality: 3x")
@staticmethod
def sound_stop():
""" Game finish: change sound """
BrainColorGame.sound_game.play()
BrainColorGame.sound_win.stop()
class ButtonStart(ButtonBehavior, Image):
""" Custom Button """
def __init__(self, **kwargs):
super(ButtonStart, self).__init__(**kwargs)
# self.bind(state=self.state_changed)
class GameScreenRepeat(Screen):
""" What do you want I describe here? ^^ """
mode = 'forgot' # forgot, remember
question_index = 0
bt1 = Button()
bt2 = Button()
bt3 = Button()
bt4 = Button()
colours = {'red': [1, 0, 0, 1],
'blue': [0, 0, 1, 1],
'yellow': [1, 1, 0, 1],
'green': [0, 1, 0, 1]}
colors = []
question = []
lenquestion = _("Level: ") + str(1)
level = 2
started = 0
life = 3
path = os.getcwd()
bt1_sound = SoundLoader.load(path + '/data/bt1_sound.ogg')
bt2_sound = SoundLoader.load(path + '/data/bt2_sound.ogg')
bt3_sound = SoundLoader.load(path + '/data/bt3_sound.ogg')
bt4_sound = SoundLoader.load(path + '/data/bt4_sound.ogg')
sound_false = SoundLoader.load(path + '/data/sound_false.ogg')
sound_win = SoundLoader.load(path + '/data/sound_win.ogg')
level_best_medium = 1
level_best_hard = 1
score_file_repeat_medium = os.getcwd() + "/scores_repeat_medium_bcg"
score_file_repeat_hard = os.getcwd() + "/scores_repeat_hard_bcg"
color_repeat_life_3 = os.getcwd() + "/data/ColorRepeat_Life3.png"
color_repeat_life_2 = os.getcwd() + "/data/ColorRepeat_Life2.png"
color_repeat_life_1 = os.getcwd() + "/data/ColorRepeat_Life1.png"
new_record = ""
old_level = _("Best Level")
def __init__(self, **kwargs):
""" """
super(GameScreenRepeat, self).__init__(**kwargs)
self.ini()
self.post_build_init()
def post_build_init(self):
""" Bind the android or the keyboard key """
if platform() == 'android':
import android
android.map_key(android.KEYCODE_BACK, 1001)
win = Window
win.bind(on_keyboard=self.key_handler)
def key_handler(self, window, keycode1, keycode2, text, modifiers):
""" Called by escape key, 'reboot' the game """
if keycode1 == 27 or keycode1 == 1001:
self.level = 2
self.started = 0
self.question = []
self.colors = []
self.question_index = 0
def open_init(self):
""" Called by kv """
self.open_file(self.score_file_repeat_medium, "medium")
self.open_file(self.score_file_repeat_hard, "hard")
def open_file(self, score_file_mode, level):
""" Open scores file """
try:
file_saved = open(score_file_mode, 'r')
scr = file_saved.read()
scr = int(scr)
if level == "medium":
self.level_best_medium = scr
else:
self.level_best_hard = scr
file_saved.close()
except IOError:
pass
def update_records(self):
""" Called by kv """
BrainColorGame.best_scores_color_repeat_medium = self.level_best_medium
BrainColorGame.best_scores_color_repeat_hard = self.level_best_hard
def save_new_record(self, score_file_mode):
""" save scores in file: mode = text or color """
file_saved = open(score_file_mode, 'w')
if self.mode == 'remember':
file_saved.write(str(self.level_best_medium))
else:
file_saved.write(str(self.level_best_hard))
file_saved.close()
def ini(self):
""" build """
self.lenquestion = _("Level: ") + str(1)
lenquestionlabel = self.ids['lenquestionlabel']
lenquestionlabel.text = self.lenquestion
self.life = 3
lifelabel = self.ids['lifelabel']
lifelabel.text = _("Life: ") # + str(self.life)
self.level = 2
self.question = []
self.question_index = 0
for num in range(self.level):
self.question.append(random.choice(self.colours.values()))
print len(self.question)
def forgot(self):
""" See MenuScreen Button in kv """
self.mode = 'forgot'
def remember(self):
""" See MenuScreen Button in kv """
self.mode = 'remember'
def start(self, delta_time):
""" Let's go """
if self.manager.current == 'game-repeat':
self.start_started(1)
else:
pass
def button_start_pushed(self):
""" Called by kv """
self.new_record = "Level"
self.old_level = _("Best Level")
lifeimage = self.ids['lifeimage']
lifeimage.source = self.color_repeat_life_3
self.ini()
Clock.schedule_once(lambda dt: self.button_start_pushed_go(), 0.7)
def button_start_pushed_go(self):
""" see above stupid fat hobbit ! """
# self.ini()
self.start_started(1)
def start_started(self, delta_time):
""" see above too ! """
BrainColorGame.sound_game.stop()
bt1 = self.ids['bt1']
bt2 = self.ids['bt2']
bt3 = self.ids['bt3']
bt4 = self.ids['bt4']
print "here"
print self.question_index
print len(self.question)
if bt1.color == self.colours['yellow'] and \
bt2.color == self.colours['green'] and \
bt3.color == self.colours['red'] and \
bt4.color == self.colours['blue'] and \
self.question_index < len(self.question):
print "ok"
if bt1.color == \
self.question[self.question_index]:
bt1.color = [0.3, 1, 3, 1]
self.bt1_sound.play()
elif bt2.color == \
self.question[self.question_index]:
bt2.color = [0.3, 1, 3, 1]
self.bt2_sound.play()
elif bt3.color == \
self.question[self.question_index]:
bt3.color = [0.3, 1, 3, 1]
self.bt3_sound.play()
elif bt4.color == \
self.question[self.question_index]:
bt4.color = [0.3, 1, 3, 1]
self.bt4_sound.play()
else:
pass
Clock.schedule_once(self.reboot_button, 0.5)
else:
pass
def reboot_button(self, dt):
""" Button have now the original color """
bt1 = self.ids['bt1']
bt2 = self.ids['bt2']
bt3 = self.ids['bt3']
bt4 = self.ids['bt4']
print "reboot_button"
self.question_index += 1
bt1.color = self.colours['yellow']
bt2.color = self.colours['green']
bt3.color = self.colours['red']
bt4.color = self.colours['blue']
Clock.schedule_once(self.start, 0.3)
def button_initial_color(self):
""" Called by bt pressed """
bt1 = self.ids['bt1']
bt2 = self.ids['bt2']
bt3 = self.ids['bt3']
bt4 = self.ids['bt4']
bt1.color = self.colours['yellow']
bt2.color = self.colours['green']
bt3.color = self.colours['red']
bt4.color = self.colours['blue']
def bt1_pressed(self):
""" Check if the answer red is good """
if self.question_index < len(self.question):
self.question_index = len(self.question)
else:
bt1 = self.ids['bt1']
self.colors.append(bt1.color)
print self.colors
self.bt1_sound.stop()
self.bt1_sound.play()
bt1.color = [0.3, 1, 3, 1]
Clock.schedule_once(lambda dt: self.button_initial_color(), 0.2)
self.check_answer()
def bt2_pressed(self):
""" Check if the answer blue is good """
if self.question_index < len(self.question):
self.question_index = len(self.question)
else:
bt2 = self.ids['bt2']
self.colors.append(bt2.color)
print self.colors
self.bt2_sound.stop()
self.bt2_sound.play()
bt2.color = [0.3, 1, 3, 1]
Clock.schedule_once(lambda dt: self.button_initial_color(), 0.2)
self.check_answer()
def bt3_pressed(self):
""" Check if the answer yellow is good """
if self.question_index < len(self.question):
self.question_index = len(self.question)
else:
bt3 = self.ids['bt3']
self.colors.append(bt3.color)
print self.colors
self.bt3_sound.stop()
self.bt3_sound.play()
bt3.color = [0.3, 1, 3, 1]
Clock.schedule_once(lambda dt: self.button_initial_color(), 0.2)
self.check_answer()
def bt4_pressed(self):
""" Check if the answer green is good """
if self.question_index < len(self.question):
self.question_index = len(self.question)
else:
bt4 = self.ids['bt4']
self.colors.append(bt4.color)
print self.colors
self.bt4_sound.stop()
self.bt4_sound.play()
bt4.color = [0.3, 1, 3, 1]
Clock.schedule_once(lambda dt: self.button_initial_color(), 0.2)
self.check_answer()
def win(self):
""" Called by check_answer """
self.sound_win.play()
self.level += 1
self.colors = []
# Forgot the question or not
if self.mode == 'forgot':
self.question = []
for x in range(self.level):
self.question.append(random.choice(self.colours.values()))
else:
self.question.append(random.choice(self.colours.values()))
print self.question
self.question_index = 0
Clock.schedule_once(self.start, 1)
def new_record_check(self):
print "here new_record"
if self.mode == 'remember':
if self.level_best_medium < (len(self.question)-1):
self.new_record = _("New Record!")
self.old_level = _("Old Best Level")
print "ok NEW REcord!"
else:
if self.level_best_hard < (len(self.question)-1):
self.new_record = _("New Record!")
self.old_level = _("Old Best Level")
def check_answer(self):
""" Called by Button's push """
lifeimage = self.ids['lifeimage']
if self.colors == self.question:
print "win"
self.win()
self.lenquestion = _("Level: ") + str(len(self.question)-1)