Skip to content

Commit 74786a1

Browse files
authored
Memory Game
Memory the puzzle game of number pairs. How to play: 1. Count and print how many taps occur. 2. Decrease the number of tiles to a 4x4 grid. 3. Detect when all tiles are revealed. 4. Center single-digit tile. 5. Use letters instead of tiles.
1 parent 2d34fc1 commit 74786a1

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

memorygame.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from random import *
2+
from turtle import *
3+
from freegames import path
4+
5+
car = path('car.gif')
6+
tiles = list(range(32)) * 2
7+
state = {'mark': None}
8+
hide = [True] * 64
9+
10+
def square(x, y):
11+
"Draw white square with black outline at (x, y)."
12+
up()
13+
goto(x, y)
14+
down()
15+
color('black', 'white')
16+
begin_fill()
17+
for count in range(4):
18+
forward(50)
19+
left(90)
20+
end_fill()
21+
22+
def index(x, y):
23+
"Convert (x, y) coordinates to tiles index."
24+
return int((x + 200) // 50 + ((y + 200) // 50) * 8)
25+
26+
def xy(count):
27+
"Convert tiles count to (x, y) coordinates."
28+
return (count % 8) * 50 - 200, (count // 8) * 50 - 200
29+
30+
def tap(x, y):
31+
"Update mark and hidden tiles based on tap."
32+
spot = index(x, y)
33+
mark = state['mark']
34+
35+
if mark is None or mark == spot or tiles[mark] != tiles[spot]:
36+
state['mark'] = spot
37+
else:
38+
hide[spot] = False
39+
hide[mark] = False
40+
state['mark'] = None
41+
42+
def draw():
43+
"Draw image and tiles."
44+
clear()
45+
goto(0, 0)
46+
shape(car)
47+
stamp()
48+
49+
for count in range(64):
50+
if hide[count]:
51+
x, y = xy(count)
52+
square(x, y)
53+
54+
mark = state['mark']
55+
56+
if mark is not None and hide[mark]:
57+
x, y = xy(mark)
58+
up()
59+
goto(x + 2, y)
60+
color('black')
61+
write(tiles[mark], font=('Arial', 30, 'normal'))
62+
63+
update()
64+
ontimer(draw, 100)
65+
66+
shuffle(tiles)
67+
setup(420, 420, 370, 0)
68+
addshape(car)
69+
hideturtle()
70+
tracer(False)
71+
onscreenclick(tap)
72+
draw()
73+
done()

0 commit comments

Comments
 (0)