-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy path5- Memory.py
104 lines (71 loc) · 2.56 KB
/
5- 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
# implementation of card game - Memory
import simplegui
import random
# --------------------------------------------------------
# --------------------------------------------------------
# global variables
deck = []
exposed = []
card1 = 0
card2 = 0
state = 0
turns = 0
# --------------------------------------------------------
# --------------------------------------------------------
# helper function to initialize globals
def new_game():
global deck, state, exposed, card1, card2, turns
deck = range(8) + range(8)
random.shuffle(deck)
exposed = [False for x in deck]
state = turns = card1 = card2 = 0
# --------------------------------------------------------
# --------------------------------------------------------
# define event handlers
def mouseclick(pos):
card_num = pos[0] // 50
if (exposed[card_num] != True):
exposed[card_num] = True
global card1, card2, state, turns
if state == 0:
state = 1
card1 = card_num
elif state == 1:
state = 2
card2 = card_num
turns += 1
else:
state = 1
if (deck[card1] != deck[card2]):
exposed[card1] = exposed[card2] = False
card1 = card_num
# --------------------------------------------------------
# cards are logically 50x100 pixels in size
def draw(canvas):
x = 0
i = 0
for num in deck:
if exposed[i] == False:
canvas.draw_line((x, 0), (x, 100), 2, 'yellow')
canvas.draw_line((x, 0), (x+50, 0), 200, 'green')
else:
canvas.draw_text(str(num), [x , 90], 115, "red")
x += 50
i += 1
label.set_text("Turns = " + str(turns))
# --------------------------------------------------------
# --------------------------------------------------------
# create frame and add a button and labels
frame = simplegui.create_frame("Memory", 800, 100)
frame.add_button("Reset", new_game)
label = frame.add_label("Turns = " + str(turns))
# --------------------------------------------------------
# register event handlers
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)
# --------------------------------------------------------
# get things rolling
new_game()
frame.start()
# --------------------------------------------------------
# --------------------------------------------------------