数对拼图游戏
"""Memory, puzzle game of number pairs.
Exercises:
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.
"""
from random import *
from turtle import *
from freegames import path
car = path('car.gif')
tiles = list(range(8)) * 2
# print(tiles)
state = {'mark': None}
hide = [True] * 16
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(100)
left(90)
end_fill()
def index(x, y) -> int:
"""Convert (x, y) coordinates to tiles index."""
# 二维变一维
return int((x + 200) // 100 + ((y + 200) // 100) * 4)
def xy(count):
"""Convert tiles count to (x, y) coordinates."""
# 一维变二维 与index 互逆
return (count % 4) * 100 - 200, (count // 4) * 100 - 200
def tap(x, y):
"""Update mark and hidden tiles based on tap."""
spot = index(x, y)
mark = state['mark']
try:
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
print(mark, spot)
except Exception as e:
print(e)
def draw():
"""Draw image and tiles."""
clear()
goto(0, 0)
shape(car)
stamp() # 将当前位置上的形状复制到画布上
# 将海龟形状放到当前位置
for count in range(16):
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()