-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemory2.pyde
102 lines (77 loc) · 2.37 KB
/
Memory2.pyde
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
from random import shuffle
monsters = []
m2check = set()
warmup = True
gameover = False
hold = millis()
def setup():
size(600, 600)
noStroke()
imageMode(CENTER)
global blank
blank = loadImage('blank.png')
global tile
tile = loadImage('background.jpg')
tile.resize(600, 600)
tile.loadPixels()
populateBoard()
def draw():
global warmup
background(tile)
for monster in monsters:
monster.update()
checkForPair()
if warmup:
rlength = width - frameCount # * 2
if rlength > 0:
stroke(230, 20, 20)
fill(220, 20, 20);
rect(0, 590, rlength, 5)
else:
warmup = False
def checkForPair():
global gameover
if len(m2check) > 1 and (millis() - hold) > 750:
m1, m2 = m2check.pop(), m2check.pop()
if m1.face == m2.face:
m1.found = m2.found = True
if all([m.found for m in monsters]):
gameover = True
def populateBoard():
for n in range(18):
face = loadImage("{}.png".format(n));
monsters.append(Monster(face))
monsters.append(Monster(face))
shuffle(monsters)
mx = (width - 6 * blank.width) / 2
my = (height - 6 * blank.height) / 2
hh = blank.height / 2
hw = blank.width / 2
for p, m in enumerate(monsters):
m.x = (p % 6) * blank.width + mx + hw
m.y = (p // 6) * blank.height + my + hh
def mousePressed():
global m2check, hold
if not gameover and not warmup:
for m in monsters:
if m in m2check or m.found:
continue
if m.isOver(mouseX, mouseY):
if len(m2check) < 2:
m2check.add(m)
hold = millis()
class Monster():
def __init__(self, face):
self.face = face
self.found = False
def update(self):
if self in m2check or gameover:
image(self.face, self.x + random(-1, 1), self.y + random(-1, 1))
elif self.found or warmup:
image(self.face, self.x, self.y)
else:
image(blank, self.x, self.y)
def isOver(self, mx, my):
if dist(self.x, self.y, mx, my) < 40:
return True
return False