Skip to content

Commit 47093f4

Browse files
authored
made changes in mouseclick()
1 parent 6f54156 commit 47093f4

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

Memory_game

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,78 @@ def mouseclick(pos):
2828
state += 1
2929
ind = pos[0]//50
3030
card3.append(card1[ind])
31+
po.append(ind)# implementation of card game - Memory
32+
33+
import simplegui
34+
import random
35+
36+
# helper function to initialize globals
37+
def new_game():
38+
global card3, po, state, exposed, card1
39+
def create(card):
40+
while len(card) != 8:
41+
num = random.randrange(0 ,8)
42+
if num not in card:
43+
card.append(num)
44+
return card
45+
card3 = []
46+
card1 = []
47+
card2 = []
48+
po = []
49+
card1 = create(card1)
50+
card2 = create(card2)
51+
card1.extend(card2)
52+
random.shuffle(card1)
53+
state = 0
54+
exposed = []
55+
for i in range(0,16,1):
56+
exposed.insert(i, False)
57+
58+
59+
# define event handlers
60+
def mouseclick(pos):
61+
# add game state logic here
62+
global card3, po, state, exposed, card1
63+
if state == 2:
64+
if card3[0] != card3[1]:
65+
exposed[po[0]] = False
66+
exposed[po[1]] = False
67+
card3 = []
68+
state = 0
69+
po = []
70+
ind = pos[0]//50
71+
card3.append(card1[ind])
3172
po.append(ind)
73+
if exposed[ind] == False and state < 2:
74+
exposed[ind] = True
75+
state += 1
76+
77+
# cards are logically 50x100 pixels in size
78+
def draw(canvas):
79+
global card1
80+
gap = 0
81+
for i in range(0,16,1):
82+
if exposed[i] == False:
83+
canvas.draw_polygon( [ [0 + gap,0], [0 + gap,100] ,[50 + gap,100],[50 + gap,0]],1 ,"Black","Green")
84+
elif exposed[i] == True:
85+
canvas.draw_text( str(card1[i]), [ 15 + gap, 65], 50, 'White')
86+
gap += 50
87+
88+
# create frame and add a button and labels
89+
frame = simplegui.create_frame("Memory", 800, 100)
90+
frame.add_button("Reset", new_game)
91+
label = frame.add_label("Turns = 0")
92+
93+
# register event handlers
94+
frame.set_mouseclick_handler(mouseclick)
95+
frame.set_draw_handler(draw)
96+
97+
# get things rolling
98+
new_game()
99+
frame.start()
100+
101+
102+
# Always remember to review the grading rubric
32103
if exposed[ind] == False and state < 2:
33104
exposed[ind] = True
34105
elif exposed[ind] == False and state == 2:

0 commit comments

Comments
 (0)