Skip to content

Commit 6d39eae

Browse files
authored
Update - from Jacques
1 parent a9f55e6 commit 6d39eae

File tree

1 file changed

+58
-51
lines changed

1 file changed

+58
-51
lines changed

memory/src/memory.py

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
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]
1316
JS.shuffle(allcolors)
1417

15-
1618
# Helper Functions:
1719
def all(iterable):
1820
for element in iterable:
1921
if not element:
2022
return False
2123
return True
2224

23-
2425
# Main Grid wich contents all the cells
2526
# Grille principale contenant les cellules
2627
class Grid:
@@ -40,7 +41,7 @@ def display(self):
4041
for num in cells:
4142
color = allcolors[num]
4243
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 )
4445

4546
# FrontFace : Sprite or Color
4647
sprite = self.game.rectangle(128, 128, color)
@@ -50,8 +51,8 @@ def display(self):
5051
sprite.content = color
5152
sprite.showed = False
5253

53-
# Backface
54-
rectb = self.game.rectangle(128, 128, "blue")
54+
# Backface
55+
rectb = self.game.rectangle(128, 128, "lightGray")
5556
rectb.x = posx
5657
rectb.y = posy
5758
rectb.num = num
@@ -60,11 +61,11 @@ def display(self):
6061

6162

6263
# Game Handler
63-
# Gestion du Jeu
64+
# Gestion du Jeu
6465
class Memory:
65-
def __init__(self, width=512, height=512):
66+
def __init__(self, width=524, height=524):
6667
self.game = hexi(width, height, self.setup)
67-
self.game.backgroundColor = "seagrean"
68+
self.game.backgroundColor = "seaGreen"
6869
self.mouse = self.game.pointer
6970
self.mouse.tap = self.tap
7071
self.grid = Grid(self.game)
@@ -74,67 +75,73 @@ def __init__(self, width=512, height=512):
7475
def tap(self):
7576
self.tapped = True
7677

77-
def setup(self):
78+
def setup(self):
7879
self.game.scaleToWindow("seaGreen")
79-
self.grid.display()
80+
self.grid.display()
8081
self.game.state = self.play
8182

8283
def get_curcell(self):
8384
for i in range(self.grid.rows):
8485
for j in range(self.grid.cols):
8586
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
8889

8990
def compare_cells(self):
9091

9192
if len(self.clickedcells) < 2:
9293
return
9394

9495
numrows = self.grid.rows
95-
numcols = self.grid.cols
96+
numcols = self.grid.cols
9697

9798
def resetcell(cells):
9899
def _reset():
99100
cells[0].alpha = 1
100101
cells[1].alpha = 1
101102
self.clickedcells = []
102-
return _reset
103+
return _reset
103104

104105
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+
122125
def check_endgame(self):
126+
def endgame ():
127+
for s in lst_spr:
128+
s.alpha = 0
129+
self.game.state = self.end
130+
123131
# flatten liste sprites
124132
# On 'applatit la liste des sprites'
125133
lst_spr = [sprite for liste_sprites in self.grid.spr for sprite in liste_sprites]
126134
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+
132138
def play(self):
133-
self.check_endgame()
139+
self.check_endgame()
134140
self.get_curcell()
135141

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)
138145
self.mouse.tapped = False
139146
self.compare_cells()
140147

0 commit comments

Comments
 (0)