-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
113 lines (94 loc) · 2.96 KB
/
memory.py
File metadata and controls
113 lines (94 loc) · 2.96 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Memory Game based on the FreeGames collection, with modifications made
# for a better gaming experience.
# Authors: Regina Luna, A01655821
# Diego Samperio, A01662935
# Abigail Curiel, A01655892
# Date: 23/03/2023
from random import shuffle
from turtle import *
from freegames import path
# Load the image of the memory card's back
car = path('car.gif')
# Initialize state variables
tiles = list(range(32)) * 2 # Create a list of 64 tiles (32 pairs)
state = {'mark': None} # Track the current marked tile
hide = [True] * 64 # Hide all tiles initially
taps = 0 # Count the number of taps made
# Set up the screen and add the car image
setup(420, 420, 370, 0)
addshape(car)
shape(car)
hideturtle()
tracer(False)
# Function to draw a square for each tile
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()
# Function to convert tile index to screen coordinates
def index(x, y):
"Convert (x, y) coordinates to tile index."
return int((x + 200) // 50 + ((y + 200) // 50) * 8)
# Function to convert tile index to screen coordinates
def xy(count):
"Convert tile count to (x, y) coordinates."
return (count % 8) * 50 - 200, (count // 8) * 50 - 200
# Function to handle clicks and reveal tiles
def tap(x, y):
"Update mark and hidden tiles based on tap."
global taps
spot = index(x, y) # Get tile index from click position
mark = state['mark']
taps += 1 # Increment tap counter
if mark is None or mark == spot or tiles[mark] != tiles[spot]:
# Mark a new tile if it's not already marked or doesn't match
state['mark'] = spot
else:
# If the two tiles match, reveal them
hide[spot] = False
hide[mark] = False
state['mark'] = None
# Function to draw the board and tiles
def draw():
"Draw image and tiles."
clear()
# Draw all tiles
for count in range(64):
x, y = xy(count)
if hide[count]:
square(x, y) # Draw hidden tile
else:
up()
goto(x + 2, y)
color('black')
write(tiles[count], font=('Arial', 30, 'normal')) # Display tile number
mark = state['mark']
if mark is not None and hide[mark]:
# Highlight the marked tile
x, y = xy(mark)
up()
goto(x, y)
down()
color('black')
write(tiles[mark], font=('Arial', 30, 'normal'))
# Display the number of taps
up()
goto(-180, 180)
down()
color('black')
write(f'Taps: {taps}', font=('Arial', 16, 'normal'))
update()
ontimer(draw, 100)
# Shuffle the tiles randomly at the start
shuffle(tiles)
# Start the game with the tap handler and drawing loop
onscreenclick(tap)
draw()
done()