-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.py
More file actions
127 lines (103 loc) · 3.01 KB
/
Memory.py
File metadata and controls
127 lines (103 loc) · 3.01 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# implementation of card game - Memory
import simplegui
import random
#Cards in Play
list1 = range(8)
list2 = range(8)
card_list = []
card_list.extend(list1)
card_list.extend(list2)
#True = open cards, False = closed cards
exposed = range(16)
for i in exposed:
exposed[i] = False
i += 1
#Counter/state/index/flag
counter = -1
state = 0
index_card = []
turn = 0
#Shuffle the cards
random.shuffle(card_list)
#Card Position variable
position = 15
#Back of cards
rect_x1, rect_x2, rect_y1, rect_y2 = 0,50,0,100
# helper function to initialize globals
def new_game():
global card_list, exposed, counter, state, index_card, turn
list1 = range(8)
list2 = range(8)
card_list = []
card_list.extend(list1)
card_list.extend(list2)
exposed = range(16)
for i in exposed:
exposed[i] = False
i += 1
counter = -1
state = 0
index_card = []
turn = 0
label.set_text('Turns: ' + str(turn))
random.shuffle(card_list)
# define event handlers
def mouseclick(pos):
global exposed, state, index_card, turn
# add game state logic here
if len(index_card) == 2:
if (card_list[index_card[0]] == card_list[index_card[1]]) == False:
exposed[index_card[0]] = False
exposed[index_card[1]] = False
index_card = []
index = pos[0]//50
if exposed[index] == True:
return
index_card.append(index)
exposed[index] = True
if state == 0:
state = 1
elif state == 1:
state = 2
#leave exposed if match
if card_list[index_card[0]] == card_list[index_card[1]]:
exposed[index_card[0]] = True
exposed[index_card[1]] = True
turn += 1
label.set_text('Turns: ' + str(turn))
else:
state = 1
# cards are logically 50x100 pixels in size
def draw(canvas):
global position, exposed, rect_x1, rect_x2, rect_y1, rect_y2, counter
for i in exposed:
counter += 1
if i == True:
#draw cards
canvas.draw_text(str(card_list[counter]),
(position,65), 45, 'red')
else:
#draw back cards
canvas.draw_polygon([[rect_x1, rect_y1], [rect_x2,rect_y1],
[rect_x2, rect_y2], [rect_x1,rect_y2]],
1, 'red', 'white')
rect_x1 += 50
rect_x2 += 50
if rect_x2 > 801:
rect_x1, rect_x2 = 0,50
position += 50
if position > 800:
position = 15
if counter == 15:
counter = -1
# 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: 0')
# register event handlers
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)
# get things rolling
new_game()
frame.start()
# Always remember to review the grading rubric