-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniProject_Week5.py
More file actions
71 lines (59 loc) · 1.98 KB
/
MiniProject_Week5.py
File metadata and controls
71 lines (59 loc) · 1.98 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
# implementation of card game - Memory
import simplegui
import random
# helper function to initialize globals
def new_game():
global deck, exposed, state, first_ind, sec_ind, turns
deck =range(8)+range(8)
random.shuffle(deck)
#print deck
exposed = [False for i in range(16)]
turns, state, first_ind, sec_ind=0, 0, None, None
label.set_text("Turns = "+str(turns))
# define event handlers
def mouseclick(pos):
# add game state logic here
global state, first_ind, sec_ind, turns
#print pos
if state==0:
if exposed[pos[0]//50]==False:
exposed[pos[0]//50]=True
state=1
first_ind=pos[0]//50
elif state==1:
if exposed[pos[0]//50]==False:
exposed[pos[0]//50]=True
state=2
sec_ind=pos[0]//50
turns=turns+1
label.set_text("Turns = "+str(turns))
else:
if exposed[pos[0]//50]==False:
exposed[pos[0]//50]=True
state=1
if deck[first_ind]!=deck[sec_ind]:
exposed[first_ind]=False
exposed[sec_ind]=False
first_ind, sec_ind=pos[0]//50,None
# cards are logically 50x100 pixels in size
def draw(canvas):
i=0
for card in deck:
if exposed[i]:
canvas.draw_text(str(card), (50*i+20, 60), 40, 'White')
canvas.draw_line([50*i, 0], [50*i, 100], 1, 'White')
else:
#canvas.draw_line([50*i, 0], [50*i, 100], 1, 'White')
canvas.draw_polygon([[50*i, 0], [50*(i+1), 0], [50*(i+1), 100], [50*i, 100]], 1, 'Black','Green')
i+=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