-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_5.py
74 lines (62 loc) · 2.3 KB
/
1_5.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
import simplegui
import random
# helper function to initialize globals
def new_game():
global fulldeck, exposed, state, cardindex1, cardindex2, turns
state, turns, cardindex1, cardindex2 = 0, 0, None, None
lst1 = [i for i in range(8)]
lst2 = [i for i in range(8)]
fulldeck = lst1 + lst2
random.shuffle(fulldeck)
exposed = [False for i in range(16)]
indicator.set_text("Turns: " + str(turns))
# define event handlers
def mouseclick(pos):
global fulldeck, exposed, state, cardindex1, cardindex2, turns
position = pos[0]/50
if not exposed[position]:
if state == 0:
cardindex1 = position
exposed[cardindex1] = True
state = 1
elif state == 1:
cardindex2 = position
exposed[cardindex2] = True
state = 2
turns += 1
indicator.set_text("Turns: " + str(turns))
elif state == 2:
if fulldeck[cardindex1] != fulldeck[cardindex2]:
exposed[cardindex1], exposed[cardindex2] = False, False
cardindex2, cardindex2 = None, None
cardindex1 = position
exposed[position] = True
state = 1
# cards are logically 50x100 pixels in size
def draw(canvas):
for num in range(16):
if exposed[num] is True:
canvas.draw_polygon([[num*50, 0],
[(num+1)*50, 0],
[(num+1)*50, 100],
[num*50, 100]], 1,
"White", "White")
canvas.draw_text(str(fulldeck[num]),
(num*50+10, 60),
50, "Black")
else:
canvas.draw_polygon([[num*50, 0],
[(num+1)*50, 0],
[(num+1)*50, 100],
[num*50, 100]], 1,
"White", "Black")
# create frame and add a button and labels
frame = simplegui.create_frame("Memory", 800, 100)
frame.add_button("Reset", new_game)
indicator = frame.add_label("Turns: 0")
# register event handlers
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)
# get things rolling
new_game()
frame.start()