-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryGame.py
95 lines (80 loc) · 3.13 KB
/
MemoryGame.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
import simplegui
import random
# Load images
image_card_back = simplegui.load_image("https://dl.dropboxusercontent.com/u/40444366/card_back.png?dl=1")
image_cards = simplegui.load_image("https://dl.dropboxusercontent.com/u/40444366/cards.png?dl=1")
# Global variables
CARD_WIDTH = 71
CARD_HEIGHT = 96
CANVAS_WIDTH = CARD_WIDTH * 4 - 1
CANVAS_HEIGHT = CARD_HEIGHT * 4 - 1
# Helper function to initialize globals
def init():
global state, moves, face_down, cards
x_pos = [37, 110, 183, 256, 329, 402, 475, 548, 621, 694, 767, 840, 913]
y_pos = (49, 147, 245, 343)
state = 0
moves = 0
label.set_text("Moves = " + str(moves))
face_down = []
for i in range(16):
face_down.append(True)
cards = []
i = 0
while i < 8:
rnd_x = random.randrange(0, 13)
if x_pos[rnd_x] != 0:
cards.append([x_pos[rnd_x], y_pos[random.randrange(0, 4)]])
x_pos[rnd_x] = 0
i += 1
cards += cards
random.shuffle(cards)
# define event handlers
def mouseclick(pos):
# add game state logic here
global state, moves, first_index, second_index
i = 0
for r in range(4):
for c in range(4):
if CARD_WIDTH * c < pos[0] < CARD_WIDTH * (c + 1) and CARD_HEIGHT * r < pos[1] < CARD_HEIGHT * (r + 1):
if face_down[i]:
face_down[i] = False
if state == 0:
first_index = i
elif state == 1:
second_index = i
moves += 1
label.set_text("Moves = " + str(moves))
elif state == 2:
if cards[first_index][0] != cards[second_index][0]:
face_down[second_index], face_down[first_index] = True, True
first_index = i
state = 0
state += 1
i += 1
# cards 71x96 pixels in size
def draw(canvas):
# Draw cards
for r in range(4):
for c in range(4):
if face_down[r * 4 + c]:
canvas.draw_image(image_card_back,
[CARD_WIDTH // 2, CARD_HEIGHT // 2], [CARD_WIDTH, CARD_HEIGHT],
[CARD_WIDTH * (c + 1) - CARD_WIDTH // 2 - 1, CARD_HEIGHT * (r + 1) - CARD_HEIGHT // 2 - 1],
[CARD_WIDTH, CARD_HEIGHT])
else:
canvas.draw_image(image_cards,
[cards[r * 4 + c][0], cards[r * 4 + c][1]], [CARD_WIDTH, CARD_HEIGHT],
[CARD_WIDTH * (c + 1) - CARD_WIDTH // 2 - 1, CARD_HEIGHT * (r + 1) - CARD_HEIGHT // 2 - 1],
[CARD_WIDTH, CARD_HEIGHT])
# create frame and add a button and labels
frame = simplegui.create_frame("Memory", CANVAS_WIDTH, CANVAS_HEIGHT)
frame.add_button("Restart", init)
label = frame.add_label("Moves = 0")
# initialize global variables
init()
# register event handlers
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)
# get things rolling
frame.start()