-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_game.py
More file actions
executable file
·64 lines (54 loc) · 1.69 KB
/
memory_game.py
File metadata and controls
executable file
·64 lines (54 loc) · 1.69 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
# implementation of card game - Memory
import simplegui
import random
WIDTH=50;
HEIGHT=100;
numlist=[];
exposed=[];
moves=0;
state=0;
choices=[-1,-1];
def init():
global numlist,exposed,moves,state,choices;
numlist=range(0,8);
numlist.extend(range(0,8));
random.shuffle(numlist);
random.shuffle(numlist);
exposed=[0]*16;
moves=0;
state=0;
choices=[-1,-1];
l.set_text("Moves = "+str(moves));
def mouseclick(pos):
global choices,state,moves;
index=int(pos[0]/WIDTH);
if(state==0):
if(exposed[index]==0):
if(numlist[choices[0]]!=numlist[choices[1]]):
exposed[choices[0]]=0;
exposed[choices[1]]=0;
exposed[index]=1;
state=1;
choices[0]=index;
elif state == 1:
if(exposed[index]==0):
state=0;
exposed[index]=1;
choices[1]=index;
moves=moves+1;
l.set_text("Moves = "+str(moves));
if not(0 in exposed):
print "The game ended in "+str(moves)+" moves";
def draw(canvas):
for index in range(0,len(numlist)):
if(exposed[index]==0):
canvas.draw_polygon([(WIDTH*index,0), (WIDTH*(index+1), 0), (WIDTH*(index+1), 100),(WIDTH*index,100)],3,"Yellow","Blue");
else:
canvas.draw_text(str(numlist[index]),[WIDTH*index+5,HEIGHT-25],60,"White");
frame = simplegui.create_frame("Memory", 800, 100)
frame.add_button("Restart", init)
l=frame.add_label("Moves = 0")
init()
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)
frame.start()