forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memorygame.py
79 lines (61 loc) · 1.47 KB
/
memorygame.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
from random import *
from turtle import *
from freegames import path
car = path("car.gif")
tiles = list(range(32)) * 2
state = {"mark": None}
hide = [True] * 64
def square(x, y):
"Draw white square with black outline at (x, y)."
up()
goto(x, y)
down()
color("black", "white")
begin_fill()
for count in range(4):
forward(50)
left(90)
end_fill()
def index(x, y):
"Convert (x, y) coordinates to tiles index."
return int((x + 200) // 50 + ((y + 200) // 50) * 8)
def xy(count):
"Convert tiles count to (x, y) coordinates."
return (count % 8) * 50 - 200, (count // 8) * 50 - 200
def tap(x, y):
"Update mark and hidden tiles based on tap."
spot = index(x, y)
mark = state["mark"]
if mark is None or mark == spot or tiles[mark] != tiles[spot]:
state["mark"] = spot
else:
hide[spot] = False
hide[mark] = False
state["mark"] = None
def draw():
"Draw image and tiles."
clear()
goto(0, 0)
shape(car)
stamp()
for count in range(64):
if hide[count]:
x, y = xy(count)
square(x, y)
mark = state["mark"]
if mark is not None and hide[mark]:
x, y = xy(mark)
up()
goto(x + 2, y)
color("black")
write(tiles[mark], font=("Arial", 30, "normal"))
update()
ontimer(draw, 100)
shuffle(tiles)
setup(420, 420, 370, 0)
addshape(car)
hideturtle()
tracer(False)
onscreenclick(tap)
draw()
done()