-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathgame.py
More file actions
2173 lines (1916 loc) · 81.3 KB
/
game.py
File metadata and controls
2173 lines (1916 loc) · 81.3 KB
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 -*-
# Copyright (c) 2009-14 Walter Bender
# Copyright (c) 2009 Michele Pratusevich
# Copyright (c) 2009 Vincent Le
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# You should have received a copy of the GNU General Public License
# along with this library; if not, write to the Free Software
# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
from cairoplot import cairoplot
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GdkPixbuf
from gi.repository import GLib
gi.require_version('PangoCairo', '1.0')
from gi.repository import Pango
import os
import glob
from gettext import gettext as _
from math import sqrt
import logging
_logger = logging.getLogger('dimensions-activity')
try:
from sugar3.graphics.style import GRID_CELL_SIZE, DEFAULT_SPACING
from sugar3.graphics.alert import NotifyAlert
NOTIFY = True
except:
GRID_CELL_SIZE = 55
DEFAULT_SPACING = 16
NOTIFY = False
from constants import (HIGH, ROW, COL, CARD_WIDTH, WORD_CARD_INDICIES, LABELH,
WORD_CARD_MAP, CARD_HEIGHT, DEAL, DIFFICULTY_LEVEL,
DECKSIZE, CUSTOM_CARD_INDICIES, CARDS_IN_A_MATCH,
NUMBER_STYLES_C, NUMBER_STYLES_O, CARD_STYLES)
from grid import Grid
from deck import Deck
from card import Card
from sprites import Sprites, Sprite
from gencards import (generate_match_card, generate_frowny_shape,
generate_smiley, generate_frowny_texture,
generate_frowny_color, generate_frowny_number,
generate_label, generate_background,
generate_new_smiley_card, generate_new_game_card)
CURSOR = '█'
BACKGROUND_LAYER = 0
SELECT_LAYER = 2000
DRAG_LAYER = 5000
SMILE_LAYER = 10000
ANIMATION_LAYER = 20000
HELP_LAYER = 30000
def _distance(pos1, pos2):
''' simple distance function '''
return sqrt((pos1[0] - pos2[0]) * (pos1[0] - pos2[0]) +
(pos1[1] - pos2[1]) * (pos1[1] - pos2[1]))
def _find_the_number_in_the_name(name):
''' Find which element in an array (journal entry title) is a number '''
parts = name.split('.')
before = ''
after = ''
for i in range(len(parts)):
ii = len(parts) - i - 1
try:
int(parts[ii])
for j in range(ii):
before += (parts[j] + '.')
for j in range(ii + 1, len(parts)):
after += ('.' + parts[j])
return before, after, ii
except ValueError:
pass
return '', '', -1
def _construct_a_name(before, i, after):
''' Make a numbered filename from parts '''
return '%s%s%s' % (before, str(i), after)
class Click():
''' A simple class to hold a clicked card '''
def __init__(self):
self.spr = None
self.pos = [0, 0]
def reset(self):
self.spr = None
self.pos = [0, 0]
def hide(self):
if self.spr is not None:
self.spr.hide()
self.reset()
class Game():
''' The game play -- called from within Sugar or GNOME '''
def __init__(self, canvas, parent=None, card_type='pattern'):
''' Initialize the playing surface '''
self.activity = parent
self._first_time = True
self._animation_id = None
self._counter_id = None
self._match_id = None
if parent is None: # Starting from command line
self._sugar = False
self._canvas = canvas
else: # Starting from Sugar
self._sugar = True
self._canvas = canvas
parent.show_all()
self._canvas.set_can_focus(True)
self._canvas.add_events(Gdk.EventMask.TOUCH_MASK)
self._canvas.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
self._canvas.add_events(Gdk.EventMask.BUTTON_RELEASE_MASK)
self._canvas.add_events(Gdk.EventMask.BUTTON_MOTION_MASK)
self._canvas.connect('event', self.__event_cb)
self._canvas.connect('draw', self.__draw_cb)
self._width = Gdk.Screen.width()
self._height = Gdk.Screen.height()
if self._width < self._height:
self.portrait = True
self._scale = 0.67 * self._width / (CARD_HEIGHT * 5.5)
else:
self.portrait = False
self._scale = 0.67 * self._height / (CARD_HEIGHT * 5.5)
self._card_width = CARD_WIDTH * self._scale
self._card_height = CARD_HEIGHT * self._scale
self.custom_paths = [None, None, None, None, None, None, None, None,
None]
self._sprites = Sprites(self._canvas)
self._sprites.set_delay(True)
self._press = None
self.matches = 0
self.robot_matches = 0
self.match_list = []
self._match_area = []
self._matches_on_display = False
self._smiley = []
self._smiley_sprs = []
self._frowny = []
self._robot_card = None
self._help = []
self._chart_sprite = []
self._help_id = None
self._stop_help_on_click = False
self._failure = None
self.clicked = []
self.last_click = None
self._drag_pos = [0, 0]
self._start_pos = [0, 0]
self.low_score = [-1, -1, -1]
self.all_scores = {
'pattern': [], 'number': [], 'word': [], 'custom': []}
self.robot = False
self.robot_time = 0
self.total_time = 0
self.numberC = 0
self.numberO = 0
self.word_lists = None
self.editing_word_list = False
self.editing_custom_cards = False
self._edit_card = None
self._dead_key = None
self._found_a_match = False
self.level = 0
self.card_type = card_type
self.buddies = []
self._dealing = False
self._the_game_is_over = False
# self._played_animation = False
self._choosing_card_type = True
self._choosing_number_type = False
self._showing_robot_match = False
self.grid = Grid(self._width, self._height, self._card_width,
self._card_height)
self.backgrounds = []
if self.portrait:
width = Gdk.Screen.height()
height = Gdk.Screen.width()
else:
width = Gdk.Screen.width()
height = Gdk.Screen.height()
# generate landscape background
string = generate_background(width, height)
self.backgrounds.append(Sprite(
self._sprites, 0, 0, svg_str_to_pixbuf(string, width, height)))
if self.portrait:
width = Gdk.Screen.width()
height = Gdk.Screen.height()
else:
width = Gdk.Screen.height()
height = Gdk.Screen.width()
self.backgrounds[-1].type = 'background'
# generate portrait background
string = generate_background(width, height)
self.backgrounds.append(Sprite(
self._sprites, 0, 0, svg_str_to_pixbuf(string, width, height)))
if self.portrait:
self.backgrounds[0].hide()
else:
self.backgrounds[1].hide()
self.backgrounds[-1].type = 'background'
self._cards = []
for i in range(DECKSIZE):
self._cards.append(Card(scale=self._scale))
self.deck = Deck(self._cards, scale=self._scale)
for i in range(CARDS_IN_A_MATCH):
self.clicked.append(Click())
self._match_area.append(Card(scale=self._scale))
self._match_area[-1].create(
generate_match_card(self._scale), sprites=self._sprites)
self._match_area[-1].spr.move(self.grid.match_to_xy(i))
self._make_smiley_cards()
self._make_frowny_cards()
self._make_new_game_card()
if self._sugar:
self._generate_robot_card(self._scale * 2)
self._robot_card.spr.hide()
self._make_card_type_buttons()
self._make_help_buttons()
self._make_number_type_buttons()
size = min(self._width, self._height)
self._label = Card()
self._label.create(generate_label(size, LABELH * 4),
sprites=self._sprites)
self._label.spr.move((LABELH, LABELH))
self._label.spr.set_label_attributes(24, horiz_align="left")
self._label.spr.type = 'label'
self._label_time = Card()
self._label_time.create(generate_label(size, LABELH * 4),
sprites=self._sprites)
self._label_time.spr.move((Gdk.Screen.width() - size - LABELH, LABELH))
self._label_time.spr.set_label_attributes(24, horiz_align="right")
self._label_time.spr.type = 'label'
self._label_custom = Card()
self._label_custom.create(generate_label(self._width, LABELH * 4),
sprites=self._sprites)
self._label_custom.spr.set_label_attributes(24, horiz_align='center')
self._label_custom.spr.move((0, self.grid.grid_to_xy(9)[1]))
self._label_custom.spr.set_layer(ANIMATION_LAYER)
self._label_custom.spr.type = 'label'
self._label_custom.spr.hide()
self._labels = {'deck': '', 'match': '', 'clock': '', 'status': ''}
Gdk.Screen.get_default().connect('size-changed', self._configure_cb)
def _smiley_xy(self):
x = int(Gdk.Screen.width() / 2) - self._card_width + DEFAULT_SPACING
y = int(Gdk.Screen.height() / 2) - self._card_height - \
DEFAULT_SPACING * 2
return ((x, y))
def _configure_cb(self, event):
self.grid.stop_animation = True
self._width = Gdk.Screen.width()
self._height = Gdk.Screen.height()
if self._width < self._height:
self.portrait = True
self.backgrounds[0].hide()
self.backgrounds[1].set_layer(BACKGROUND_LAYER)
else:
self.portrait = False
self.backgrounds[1].hide()
self.backgrounds[0].set_layer(BACKGROUND_LAYER)
size = min(self._width, self._height)
self._label_time.spr.move((Gdk.Screen.width() - size - LABELH, LABELH))
self.grid.rotate(self._width, self._height)
for i in range(CARDS_IN_A_MATCH):
self._match_area[i].spr.move(self.grid.match_to_xy(i))
for i in range(1):
x = self._smiley_xy()[0] - i * int(self._card_width / 2)
y = self._smiley_xy()[1] - i * int(self._card_height / 2)
self._smiley[i].spr.move((x, y))
for c in self._frowny:
c.spr.move(self._smiley_xy())
if self._sugar:
self._robot_card.spr.move(self._smiley_xy())
for i, spr in self._card_type_buttons:
spr.move(
(int(((self._width - size) / 2) - ((i + 2) % 3) * size + size),
int((self._height - size) / 4)))
for i, c in enumerate(self.clicked):
if c.spr is not None:
c.spr.move(self.grid.match_to_xy(i))
def _hide_card_type_selector(self):
self._choosing_card_type = False
for spr in self._card_type_buttons:
spr.hide()
for spr in self._help_buttons:
spr.hide()
if self.portrait:
self.backgrounds[0].hide()
self.backgrounds[1].set_layer(BACKGROUND_LAYER)
else:
self.backgrounds[1].hide()
self.backgrounds[0].set_layer(BACKGROUND_LAYER)
def _hide_number_type_selector(self):
self._choosing_number_type = False
for spr in self._number_type_c_buttons:
spr.hide()
for spr in self._number_type_o_buttons:
spr.hide()
if self.portrait:
self.backgrounds[0].hide()
self.backgrounds[1].set_layer(BACKGROUND_LAYER)
else:
self.backgrounds[1].hide()
self.backgrounds[0].set_layer(BACKGROUND_LAYER)
def choose_card_type(self):
self._the_game_is_over = False
self._hide_smiley()
self._new_game_spr.hide()
if self._choosing_number_type:
self._hide_number_type_selector()
self._stop_help_on_click = True
self._choosing_card_type = True
self._help_buttons[0].set_layer(ANIMATION_LAYER)
self._help_buttons[2].set_layer(ANIMATION_LAYER)
n = len(CARD_STYLES)
if not self._first_time and self.card_type is not None:
i = CARD_STYLES.index(self.card_type)
else:
i = None
self._first_time = False
for j in range(n):
if j == i:
self._card_type_buttons[i + n].set_layer(ANIMATION_LAYER)
self._card_type_buttons[i].hide()
else:
self._card_type_buttons[j].set_layer(ANIMATION_LAYER)
self._card_type_buttons[j + n].hide()
if self.portrait:
self.backgrounds[0].hide()
self.backgrounds[1].set_layer(SMILE_LAYER)
else:
self.backgrounds[1].hide()
self.backgrounds[0].set_layer(SMILE_LAYER)
def choose_number_type(self):
self._choosing_card_type = False
self._choosing_number_type = True
n = len(NUMBER_STYLES_C)
i = self.numberC
for j in range(n):
if j == i:
self._number_type_c_buttons[i + n].set_layer(ANIMATION_LAYER)
self._number_type_c_buttons[i].hide()
else:
self._number_type_c_buttons[j].set_layer(ANIMATION_LAYER)
self._number_type_c_buttons[j + n].hide()
n = len(NUMBER_STYLES_O)
i = self.numberO
for j in range(n):
if j == i:
self._number_type_o_buttons[i + n].set_layer(ANIMATION_LAYER)
self._number_type_o_buttons[i].hide()
else:
self._number_type_o_buttons[j].set_layer(ANIMATION_LAYER)
self._number_type_o_buttons[j + n].hide()
if self.portrait:
self.backgrounds[0].hide()
self.backgrounds[1].set_layer(SMILE_LAYER)
else:
self.backgrounds[1].hide()
self.backgrounds[0].set_layer(SMILE_LAYER)
self._sprites.draw_all()
def new_game(self, saved_state=None, deck_index=0, show_selector=False):
''' Start a new game '''
# If we were editing the word list, time to stop
self.grid.stop_animation = True
self.editing_word_list = False
self.editing_custom_cards = False
self._edit_card = None
self._label_custom.spr.set_label('')
self._label_custom.spr.hide()
self._saved_state = saved_state
self._deck_index = deck_index
self._stop_help_on_click = True
if self._sugar:
if show_selector:
self.choose_card_type()
if self._sharing():
self.activity._collab.post(dict(action='choose_c_type'))
return
elif self._choosing_number_type:
return
else: # if self._saved_state is not None:
self._hide_card_type_selector()
self._hide_number_type_selector()
self.activity.busy()
GLib.timeout_add(200, self._prepare_new_game)
def _prepare_new_game(self):
# If there is already a deck, hide it.
if hasattr(self, 'deck'):
self.deck.hide()
self._dealing = False
self._hide_clicked()
self._matches_on_display = False
self._failure = None
self._hide_smiley()
self._hide_frowny()
self._new_game_spr.hide()
if self._sugar:
self._robot_card.spr.hide()
if self._saved_state is not None:
_logger.debug('Restoring state: %s' % (str(self._saved_state)))
self._first_time = False
if self.card_type == 'custom':
if self._sharing():
self.activity._collab.post(dict(action='card_type',
card_type=self.card_type))
self.activity._collab.post(dict(action='numberO',
numberO=self.numberO))
self.activity._collab.post(dict(action='numberC',
numberC=self.numberC))
self.deck.create(self._sprites, self.card_type,
[self.numberO, self.numberC],
self.custom_paths,
DIFFICULTY_LEVEL[self.level])
else:
if self._sharing():
self.activity._collab.post(dict(action='card_type',
card_type=self.card_type))
self.activity._collab.post(dict(action='numberO',
numberO=self.numberO))
self.activity._collab.post(dict(action='numberC',
numberC=self.numberC))
self.deck.create(self._sprites, self.card_type,
[self.numberO, self.numberC],
self.word_lists,
DIFFICULTY_LEVEL[self.level])
self.deck.hide()
self.deck.index = self._deck_index
deck_start = ROW * COL + 3
deck_stop = deck_start + self.deck.count()
self._restore_word_list(self._saved_state[deck_stop +
3 * self.matches:])
if self._saved_state[deck_start] is not None:
self.deck.restore(self._saved_state[deck_start: deck_stop])
self.grid.restore(self.deck, self._saved_state[0: ROW * COL])
self._restore_matches(
self._saved_state[deck_stop: deck_stop + 3 * self.matches])
self._restore_clicked(
self._saved_state[ROW * COL: ROW * COL + 3])
else:
self.deck.hide()
self.deck.shuffle()
self.grid.deal(self.deck)
if not self._find_a_match():
self.grid.deal_extra_cards(self.deck)
self.matches = 0
self.robot_matches = 0
self.match_list = []
self.total_time = 0
elif not self.joiner():
_logger.debug('Starting new game.')
if self.card_type == 'custom':
if self._sharing():
self.activity._collab.post(dict(action='card_type',
card_type=self.card_type))
self.activity._collab.post(dict(action='numberO',
numberO=self.numberO))
self.activity._collab.post(dict(action='numberC',
numberC=self.numberC))
self.deck.create(self._sprites, self.card_type,
[self.numberO, self.numberC],
self.custom_paths,
DIFFICULTY_LEVEL[self.level])
else:
if self._sharing():
self.activity._collab.post(dict(action='card_type',
card_type=self.card_type))
self.activity._collab.post(dict(action='numberO',
numberO=self.numberO))
self.activity._collab.post(dict(action='numberC',
numberC=self.numberC))
self.deck.create(self._sprites, self.card_type,
[self.numberO, self.numberC], self.word_lists,
DIFFICULTY_LEVEL[self.level])
self.deck.hide()
self.deck.shuffle()
self.grid.deal(self.deck)
if not self._find_a_match():
self.grid.deal_extra_cards(self.deck)
self.matches = 0
self.robot_matches = 0
self.match_list = []
self.total_time = 0
# When sharer starts a new game, joiners should be notified.
if self.sharer():
self.activity._collab.post(dict(action='req_state'))
self._update_labels()
self._the_game_is_over = False
if self._game_over():
if self._counter_id:
GLib.source_remove(self._counter_id)
self._counter_id = None
else:
if self._match_id:
GLib.source_remove(self._match_id)
self._match_id = None
if self._animation_id:
GLib.source_remove(self._animation_id)
self._animation_id = None
self._timer_reset()
self._hide_smiley()
self._hide_frowny()
if self._sugar:
self._robot_card.spr.hide()
self._new_game_spr.hide()
self._sprites.draw_all()
if self._sugar:
self.activity.unbusy()
'''
if self._saved_state == None and not self._played_animation:
# Launch animated help
if self._sugar:
self.help_animation()
self._played_animation = True
'''
def _sharing(self):
''' Are we sharing? '''
return self._sugar and self.activity.get_shared()
def joiner(self):
''' Are you the one joining? '''
if self._sharing() and not self.activity.initiating:
return True
return False
def sharer(self):
''' Are you the one sharing? '''
if self._sharing() and self.activity.initiating:
return True
return False
def edit_custom_card(self):
''' Update the custom cards from the Journal '''
if not self.editing_custom_cards:
return
if self._sugar:
self._hide_number_type_selector()
self.activity.busy()
GLib.idle_add(self._edit_custom_card_action)
def _edit_custom_card_action(self):
# Set the card type to custom, and generate a new deck.
self._hide_clicked()
self.deck.hide()
self.card_type = 'custom'
if len(self.custom_paths) < 3:
for i in range(len(self.custom_paths), 81):
self.custom_paths.append(None)
self.deck.create(self._sprites, self.card_type,
[self.numberO, self.numberC], self.custom_paths,
DIFFICULTY_LEVEL.index(HIGH))
self.deck.hide()
self.matches = 0
self.robot_matches = 0
self.match_list = []
self.total_time = 0
self._edit_card = None
self._dead_key = None
if self._counter_id:
GLib.source_remove(self._counter_id)
self._counter_id = None
# Fill the grid with custom cards.
self.grid.restore(self.deck, CUSTOM_CARD_INDICIES)
self.set_label('deck', '')
self.set_label('match', '')
self.set_label('clock', '')
self.set_label('status', '')
self._label_custom.spr.set_label(_('Edit the custom cards.'))
self._label_custom.spr.set_layer(ANIMATION_LAYER)
self._sprites.draw_all()
if self._sugar:
self.activity.unbusy()
def edit_word_list(self):
''' Update the word cards '''
if not self.editing_word_list:
if hasattr(self, 'text_entry'):
self.text_entry.hide()
self.text_entry.disconnect(self.text_event_id)
return
# Set the card type to words, and generate a new deck.
self._hide_clicked()
self.deck.hide()
self.card_type = 'word'
self.deck.create(self._sprites, self.card_type,
[self.numberO, self.numberC], self.word_lists,
DIFFICULTY_LEVEL.index(HIGH))
self.deck.hide()
self.matches = 0
self.robot_matches = 0
self.match_list = []
self.total_time = 0
self._edit_card = None
self._dead_key = None
if self._counter_id:
GLib.source_remove(self._counter_id)
self._counter_id = None
# Fill the grid with word cards.
self.grid.restore(self.deck, WORD_CARD_INDICIES)
self.set_label('deck', '')
self.set_label('match', '')
self.set_label('clock', '')
self.set_label('status', '')
self._label_custom.spr.set_label(_('Edit the word cards.'))
if not hasattr(self, 'text_entry'):
self.text_entry = Gtk.TextView()
self.text_entry.set_wrap_mode(Gtk.WrapMode.WORD)
self.text_entry.set_pixels_above_lines(0)
self.text_entry.set_size_request(self._card_width,
self._card_height)
'''
rgba = Gdk.RGBA()
rgba.red, rgba.green, rgba.blue = rgb(self._colors[1])
rgba.alpha = 1.
self.text_entry.override_background_color(
Gtk.StateFlags.NORMAL, rgba)
'''
font_text = Pango.font_description_from_string('24')
self.text_entry.modify_font(font_text)
self.activity.fixed.put(self.text_entry, 0, 0)
def _text_focus_out_cb(self, widget=None, event=None):
if self._edit_card is None:
self.text_entry.hide()
self.text_entry.disconnect(self.text_event_id)
self._update_word_card()
self.text_entry.hide()
def _update_word_card(self):
bounds = self.text_buffer.get_bounds()
text = self.text_buffer.get_text(bounds[0], bounds[1], True)
self._edit_card.spr.set_label(text)
(i, j) = WORD_CARD_MAP[self._edit_card.index]
self.word_lists[i][j] = text
self._edit_card = None
def __event_cb(self, widget, event):
''' Handle touch events '''
if event.type in (Gdk.EventType.TOUCH_BEGIN,
Gdk.EventType.TOUCH_END,
Gdk.EventType.TOUCH_UPDATE,
Gdk.EventType.BUTTON_PRESS,
Gdk.EventType.BUTTON_RELEASE,
Gdk.EventType.MOTION_NOTIFY):
x = event.get_coords()[1]
y = event.get_coords()[2]
if event.type == Gdk.EventType.TOUCH_BEGIN or \
event.type == Gdk.EventType.BUTTON_PRESS:
self._button_press(x, y)
elif event.type == Gdk.EventType.TOUCH_UPDATE or \
event.type == Gdk.EventType.MOTION_NOTIFY:
self._drag_event(x, y)
elif event.type == Gdk.EventType.TOUCH_END or \
event.type == Gdk.EventType.BUTTON_RELEASE:
self._button_release(x, y)
def _button_press_cb(self, win, event):
''' Look for a card under the button press and save its position. '''
win.grab_focus()
x, y = list(map(int, event.get_coords()))
self._button_press(x, y)
def _button_press(self, x, y):
# Find the sprite under the mouse.
spr = self._sprites.find_sprite((x, y))
if self._showing_robot_match:
return True
# New game card
if spr is not None and spr == self._new_game_spr:
GLib.timeout_add(100, self.new_game)
# Turn off help animation
# not self._stop_help_on_click:
if spr in self._help or spr in self._chart_sprite:
self._stop_help_on_click = True
self._timer_reset()
self._update_labels()
self.choose_card_type()
return True
# Don't do anything if the game is over
if self._the_game_is_over:
return True
# Don't do anything during a deal
if self._dealing:
return True
# Show help?
if spr.type in ['help-button', 'help-button-selected']:
if spr.type == 'help-button':
self._help_buttons[0].hide()
self._help_buttons[1].set_layer(ANIMATION_LAYER)
GLib.timeout_add(100, self.help_animation)
return True
if spr.type in ['chart-button', 'chart-button-selected']:
if spr.type == 'chart-button':
self._help_buttons[2].hide()
self._help_buttons[3].set_layer(ANIMATION_LAYER)
GLib.timeout_add(100, self.score_chart)
return True
# Change card type
if spr.type in ['card-type-button', 'card-type-button-selected']:
n = len(CARD_STYLES)
i = CARD_STYLES.index(spr.name)
for j in range(n):
if j == i:
self._card_type_buttons[i + n].set_layer(ANIMATION_LAYER)
self._card_type_buttons[i].hide()
else:
self._card_type_buttons[j].set_layer(ANIMATION_LAYER)
self._card_type_buttons[j + n].hide()
self.card_type = spr.name
if spr.name == 'number':
self._hide_card_type_selector()
self._choosing_card_type = False
self._choosing_number_type = True
self.choose_number_type()
elif spr.name == 'custom' and None in self.custom_paths:
# Not all the custom cards are loaded.
self._hide_card_type_selector()
self.editing_custom_cards = True
self.editing_word_list = False
self._choosing_card_type = False
self.edit_custom_card()
else:
self._choosing_card_type = False
GLib.timeout_add(100, self.new_game)
return True
# Change number c type
if spr.type in ['number-type-c-button',
'number-type-c-button-selected']:
n = len(NUMBER_STYLES_C)
i = NUMBER_STYLES_C.index(spr.name)
for j in range(n):
if j == i:
self._number_type_c_buttons[i + n].set_layer(
ANIMATION_LAYER)
self._number_type_c_buttons[i].hide()
else:
self._number_type_c_buttons[j].set_layer(ANIMATION_LAYER)
self._number_type_c_buttons[j + n].hide()
self.numberC = i
self._choosing_number_type = False
self._choosing_card_type = False
GLib.timeout_add(100, self.new_game)
return True
# Change number o type
if spr.type in ['number-type-o-button',
'number-type-o-button-selected']:
n = len(NUMBER_STYLES_O)
i = NUMBER_STYLES_O.index(spr.name)
for j in range(n):
if j == i:
self._number_type_o_buttons[i + n].set_layer(
ANIMATION_LAYER)
self._number_type_o_buttons[i].hide()
else:
self._number_type_o_buttons[j].set_layer(ANIMATION_LAYER)
self._number_type_o_buttons[j + n].hide()
self.numberO = i
self._choosing_number_type = False
self._choosing_card_type = False
GLib.timeout_add(100, self.new_game)
return True
# Hide a frowny
for card in self._frowny:
if spr == card.spr:
spr.hide()
return True
# Hide a smiley
if spr == self._smiley[0].spr:
spr.hide()
return True
# Hide a robot card
if self._sugar and spr == self._robot_card.spr:
spr.hide()
return True
# If there is a match showing, hide it.
if self._matches_on_display:
self.clean_up_match(share=True)
# Nothing else to do.
if spr is None:
return True
# Don't grab cards in the match pile.
if spr in self.match_list:
return True
# Don't grab a card being animated.
if True in self.grid.animation_lock:
return True
# Don't do anything if a card is already in motion
if self._in_motion(spr, x=x, y=y):
return True
# Keep track of starting drag position.
self._drag_pos = [x, y]
self._start_pos = [x, y]
# If the match area is full, we need to move a card back to the grid
if self._failure is not None:
if not self.grid.xy_in_match(spr.get_xy()):
return True
# We are only interested in cards in the deck.
if self.deck.spr_to_card(spr) is not None:
self._press = spr
# Save its starting position so we can restore it if necessary
if self._where_in_clicked(spr) is None:
i = self._none_in_clicked()
if i is None:
self._press = None
else:
self.clicked[i].spr = spr
self.clicked[i].pos = spr.get_xy()
self.last_click = i
else:
self._press = None
return True
def clean_up_match(self, share=False):
''' Unselect clicked cards that are now in the match pile '''
self._matches_on_display = False
self._hide_clicked()
self._smiley[0].spr.hide()
if self._sugar:
self._robot_card.spr.hide()
if share and self._sharing():
self.activity._collab.post(dict(action='unselect_cards'))
def clean_up_no_match(self, spr, share=False):
''' Return last card played to grid '''
if self.clicked[2].spr is not None and self.clicked[2].spr != spr:
self.return_card_to_grid(2)
self.last_click = 2
if share and self._sharing():
self.activity._collab.post(dict(action='return_card'))
self._hide_frowny()
self._failure = None
def _mouse_move_cb(self, win, event):
''' Drag the card with the mouse. '''
win.grab_focus()
x, y = list(map(int, event.get_coords()))
self._drag_event(x, y)
def _drag_event(self, x, y):
if self._press is None or self.editing_word_list or \
self.editing_custom_cards:
self._drag_pos = [0, 0]
return True
dx = x - self._drag_pos[0]
dy = y - self._drag_pos[1]
self._press.set_layer(DRAG_LAYER)
self._press.move_relative((dx, dy))
self._drag_pos = [x, y]
def _button_release_cb(self, win, event):
''' Lots of possibilities here between clicks and drags '''
win.grab_focus()
x, y = list(map(int, event.get_coords()))
self._button_release(x, y)
def _button_release(self, x, y):
# Maybe there is nothing to do.
if self._press is None:
if self.editing_word_list:
self._text_focus_out_cb()
self._drag_pos = [0, 0]
return True
self._press.set_layer(SELECT_LAYER)
# Determine if it was a click, a drag, or an aborted drag
d = _distance((x, y), (self._start_pos[0], self._start_pos[1]))
if self.editing_custom_cards or d < self._card_width / 10: # click
move = 'click'
elif d < self._card_width / 2: # aborted drag
move = 'abort'
else:
move = 'drag'
if move == 'click':
if self.editing_word_list:
# Only edit one card at a time, so unselect other cards
for i, c in enumerate(self.clicked):
if c.spr is not None and c.spr != self._press:
c.spr.set_label(
c.spr.labels[0].replace(CURSOR, ''))
c.spr = None # Unselect
elif self.editing_custom_cards:
pass
else:
self.process_click(self._press)
elif move == 'abort':
i = self._where_in_clicked(self._press)
self._press.move(self.clicked[i].pos)
else: # move == 'drag'