1
- rgb2hex = JS .rgb2hex
2
- color1 = rgb2hex ("rgba(255,0,0,0)" )
3
- color2 = rgb2hex ("rgba(255,255,0,0)" )
4
- color3 = rgb2hex ("rgba(255,255,255,0)" )
5
- color4 = rgb2hex ("rgba(255,0,255,0)" )
6
- color5 = rgb2hex ("rgba(255,80,80,0)" )
7
- color6 = rgb2hex ("rgba(255,128,0,0)" )
8
- color7 = rgb2hex ("rgba(255,128,255,0)" )
9
- color8 = rgb2hex ("rgba(255,0,128,0)" )
10
-
11
- colors = [color1 ,color2 , color3 , color4 , color5 , color6 , color7 , color8 ]
12
- allcolors = [color for tuplecolor in zip (colors , colors ) for color in tuplecolor ]
1
+ # memory.py
2
+ # This file is translated to Javascript using Transcrypt
3
+
4
+ colors = [JS .rgb2hex ("rgba({}, 0)" .format (color )) for color in [
5
+ (0 , 0 , 0 ),
6
+ (0 , 0 , 255 ),
7
+ (0 , 255 , 0 ),
8
+ (0 , 255 , 255 ),
9
+ (255 , 0 , 0 ),
10
+ (255 , 0 , 255 ),
11
+ (255 , 255 , 0 ),
12
+ (255 , 255 , 255 )
13
+ ]]
14
+
15
+ allcolors = [color for tuplecolor in zip (colors , colors ) for color in tuplecolor ]
13
16
JS .shuffle (allcolors )
14
17
15
-
16
18
# Helper Functions:
17
19
def all (iterable ):
18
20
for element in iterable :
19
21
if not element :
20
22
return False
21
23
return True
22
24
23
-
24
25
# Main Grid wich contents all the cells
25
26
# Grille principale contenant les cellules
26
27
class Grid :
@@ -40,7 +41,7 @@ def display(self):
40
41
for num in cells :
41
42
color = allcolors [num ]
42
43
i , j = num % self .cols , num // self .rows
43
- posx , posy = i * (128 + self .offset ), j * (128 + self .offset )
44
+ posx , posy = i * (128 + self .offset ), j * (128 + self .offset )
44
45
45
46
# FrontFace : Sprite or Color
46
47
sprite = self .game .rectangle (128 , 128 , color )
@@ -50,8 +51,8 @@ def display(self):
50
51
sprite .content = color
51
52
sprite .showed = False
52
53
53
- # Backface
54
- rectb = self .game .rectangle (128 , 128 , "blue " )
54
+ # Backface
55
+ rectb = self .game .rectangle (128 , 128 , "lightGray " )
55
56
rectb .x = posx
56
57
rectb .y = posy
57
58
rectb .num = num
@@ -60,11 +61,11 @@ def display(self):
60
61
61
62
62
63
# Game Handler
63
- # Gestion du Jeu
64
+ # Gestion du Jeu
64
65
class Memory :
65
- def __init__ (self , width = 512 , height = 512 ):
66
+ def __init__ (self , width = 524 , height = 524 ):
66
67
self .game = hexi (width , height , self .setup )
67
- self .game .backgroundColor = "seagrean "
68
+ self .game .backgroundColor = "seaGreen "
68
69
self .mouse = self .game .pointer
69
70
self .mouse .tap = self .tap
70
71
self .grid = Grid (self .game )
@@ -74,67 +75,73 @@ def __init__(self, width=512, height=512):
74
75
def tap (self ):
75
76
self .tapped = True
76
77
77
- def setup (self ):
78
+ def setup (self ):
78
79
self .game .scaleToWindow ("seaGreen" )
79
- self .grid .display ()
80
+ self .grid .display ()
80
81
self .game .state = self .play
81
82
82
83
def get_curcell (self ):
83
84
for i in range (self .grid .rows ):
84
85
for j in range (self .grid .cols ):
85
86
curcell = self .grid .ligs [i ][j ]
86
- if ( self .game .hit (self .game .pointer , curcell ) ):
87
- self .curcell = curcell
87
+ if self .game .hit (self .game .pointer , curcell ):
88
+ self .curcell = curcell
88
89
89
90
def compare_cells (self ):
90
91
91
92
if len (self .clickedcells ) < 2 :
92
93
return
93
94
94
95
numrows = self .grid .rows
95
- numcols = self .grid .cols
96
+ numcols = self .grid .cols
96
97
97
98
def resetcell (cells ):
98
99
def _reset ():
99
100
cells [0 ].alpha = 1
100
101
cells [1 ].alpha = 1
101
102
self .clickedcells = []
102
- return _reset
103
+ return _reset
103
104
104
105
cella , cellb = self .clickedcells [:2 ]
105
- if (cella .num != cellb .num ):
106
- icella , jcella = cella .num % numcols , cella .num // numrows
107
- icellb , jcellb = cellb .num % numcols , cellb .num // numrows
108
- spritea = self .grid .spr [icella ][jcella ]
109
- spriteb = self .grid .spr [icellb ][jcellb ]
110
- contenta = spritea .content
111
- contentb = spriteb .content
112
- cellb .alpha = 0
113
- cella .alpha = 0
114
- if (contenta != contentb ):
115
- setTimeout (resetcell ([cella , cellb ]), 1000 )
116
- else :
117
- spritea .showed = True
118
- spriteb .showed = True
119
- self .clickedcells = []
120
-
121
-
106
+ if cella .num == cellb .num :
107
+ self .clickedcells = self .clickedcells [:1 ]
108
+ return
109
+ else :
110
+ icella , jcella = cella .num % numcols , cella .num // numrows
111
+ icellb , jcellb = cellb .num % numcols , cellb .num // numrows
112
+ spritea = self .grid .spr [icella ][jcella ]
113
+ spriteb = self .grid .spr [icellb ][jcellb ]
114
+ contenta = spritea .content
115
+ contentb = spriteb .content
116
+ cellb .alpha = 0
117
+ cella .alpha = 0
118
+ if contenta != contentb :
119
+ setTimeout (resetcell ([cella , cellb ]), 500 )
120
+ else :
121
+ spritea .showed = True
122
+ spriteb .showed = True
123
+ self .clickedcells = []
124
+
122
125
def check_endgame (self ):
126
+ def endgame ():
127
+ for s in lst_spr :
128
+ s .alpha = 0
129
+ self .game .state = self .end
130
+
123
131
# flatten liste sprites
124
132
# On 'applatit la liste des sprites'
125
133
lst_spr = [sprite for liste_sprites in self .grid .spr for sprite in liste_sprites ]
126
134
showed_values = [s .showed for s in lst_spr ]
127
- if (all (showed_values )):
128
- for s in lst_spr :
129
- s .alpha = 0
130
- self .game .state = self .end
131
-
135
+ if all (showed_values ):
136
+ setTimeout (endgame , 2000 )
137
+
132
138
def play (self ):
133
- self .check_endgame ()
139
+ self .check_endgame ()
134
140
self .get_curcell ()
135
141
136
- if (self .mouse .tapped ):
137
- self .clickedcells .append (self .curcell ) if self .curcell not in self .clickedcells else None
142
+ if self .mouse .tapped :
143
+ lc = len (self .clickedcells )
144
+ self .clickedcells .append (self .curcell )
138
145
self .mouse .tapped = False
139
146
self .compare_cells ()
140
147
0 commit comments