-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathMemory.py
126 lines (115 loc) · 3.89 KB
/
Memory.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
# Implementation of card game - Memory
import simplegui
import random
global counter, m, index1, index2, choice1, choice2, state, val, position, exposed, numbers, CARD_WIDTH, CARD_LENGTH, HALF_CARD_WIDTH, HALF_CARD_LENGTH
numbers = []
exposed = []
position = [0]
val = range(16)
state = 0
choice1 = []
choice2 = []
index1 = []
index2 = []
m = 1
counter = 0
win = 0
def dimensions():
global CARD_WIDTH, CARD_LENGTH, HALF_CARD_WIDTH, HALF_CARD_LENGTH
CARD_WIDTH = 50
HALF_CARD_WIDTH = CARD_WIDTH // 2
CARD_LENGTH = 100
HALF_CARD_LENGTH = CARD_LENGTH // 2
# helper function to initialize globals
def init():
global val, choice1, choice2, index1, index2, m, counter, state, numbers, exposed, CARD_WIDTH, CARD_LENGTH, HALF_CARD_WIDTH, HALF_CARD_LENGTH
dimensions()
numbers = []
exposed = []
state = 0
choice1 = []
choice2 = []
index1 = []
index2 = []
m = 1
counter = 0
l.set_text("Moves = 0")
val = range(16)
for i in range(16):
if (i < 8):
numbers.append(i)
exposed.append(False)
position.append(HALF_CARD_WIDTH + ((CARD_WIDTH//2)+1))
HALF_CARD_WIDTH += CARD_WIDTH + 1
else:
numbers.append(i - 8)
exposed.append(False)
position.append(HALF_CARD_WIDTH + ((CARD_WIDTH//2)+1))
HALF_CARD_WIDTH += CARD_WIDTH + 1
random.shuffle(numbers)
# define event handlers
def mouseclick(pos):
global counter, m, index1, index2, choice1, choice2, state, val, exposed
if state == 0:
for i in range(16):
if (pos[0] > position[i]) and (pos[0] < position[i + 1]):
val[i] = numbers[i]
if exposed[i] == True:
state = 0
elif exposed[i] == False:
compare()
choice1.append(val[i])
index1.append(i)
exposed[i] = True
state = 1
elif state == 1:
for i in range(16):
if (pos[0] > position[i]) and (pos[0] < position[i + 1]):
val[i] = numbers[i]
if exposed[i] == True:
state = 1
elif exposed[i] == False:
choice2.append(val[i])
index2.append(i)
exposed[i] = True
state = 0
m += 1
counter += 1
l.set_text("Moves = " + str(counter))
def compare():
global win, index1, index2, choice1, choice2
if m > 1:
for i in range(len(choice1)):
if (choice1[i] == choice2[i]):
exposed[index1[i]] = True
exposed[index2[i]] = True
else:
exposed[index1[i]] = False
exposed[index2[i]] = False
# cards are logically 50x100 pixels in size
def draw(canvas):
global val, exposed, position, numbers, CARD_WIDTH, HALF_CARD_WIDTH, HALF_CARD_LENGTH
for i in range(16):
if (exposed[i] == True):
display = str(val[i])
canvas.draw_text(display, [position[i] + HALF_CARD_WIDTH/1.5, HALF_CARD_LENGTH], 15, "White")
else:
canvas.draw_line(((position[i] + HALF_CARD_WIDTH),0), ((position[i] + HALF_CARD_WIDTH),CARD_LENGTH), CARD_WIDTH, "White")
win = 0
for i in range(16):
if (exposed[i] == True):
win += 1
if (win == 16):
l.set_text("You Won!!!!!")
dimensions()
# create frame and add a button and labels
frame = simplegui.create_frame("Memory", 815, 100)
frame.add_button("Restart", init)
l=frame.add_label("Moves = 0")
# initialize global variables
init()
# register event handlers
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)
# get things rolling
frame.start()