-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinesweeper.pyde
107 lines (95 loc) · 2.41 KB
/
Minesweeper.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
103
104
105
106
107
from Cell import Cell
import Utils
# final variables
W = 500
H = W
SIZE = 20
# globals
cells = [range(SIZE) for y in range(SIZE)]
bombX = -1
bombY = -1
started = False
# creates a new grid
def setupGrid():
size = W / SIZE
for y in range(SIZE):
for x in range(SIZE):
cells[x][y] = Cell(x, y, size)
Utils.setCells(cells)
# sets bombs in random locations on the grid
def setBombs(percentage, notX, notY):
numBombs = percentage * SIZE*SIZE
while numBombs > 0:
y = int(random(SIZE))
x = int(random(SIZE))
if x == notX and y == notY:
continue
randomCell = cells[x][y]
randomCell.bomb = True
numBombs -= 1
# used to find all adjacent 0's
# and reveal them when one is clicked
def chunk(x, y):
cells[x][y].click()
yy = y-1
if yy < 0:
yy = 0
xx = x-1
if xx < 0:
xx = 0
while yy <= y+1:
if yy >= len(cells):
break
while xx <= x+1:
if xx >= len(cells):
break
cells[xx][yy].data = Utils.countBombs(xx, yy)
if cells[xx][yy].data == 0 and cells[xx][yy].hidden:
chunk(xx, yy)
elif cells[xx][yy].hidden:
cells[xx][yy].click()
xx += 1
yy += 1
xx = x-1
if xx < 0:
xx = 0
# on mouse press down
def mousePressed():
global started
for y in range(len(cells)):
for x in range(len(cells[y])):
cell = cells[x][y]
if cell.isHovering():
if not started:
started = True
setBombs(0.2, x, y)
cell.click()
if not cell.bomb and cell.data == 0:
chunk(x, y)
elif cell.bomb:
global bombX, bombY
bombX = x
bombY = y
thread("lose")
thread("checkWin")
# on key pressed down
def keyPressed():
global started
if keyCode == ord('R'):
setupGrid()
started = False
# check for a win
def checkWin():
Utils.checkWin()
# start lose animation
def lose():
Utils.loseAnimation(bombX, bombY)
def setup():
global cells
size(W, H)
setupGrid()
# setBombs(0.20)
def draw():
for y in range(len(cells)):
for x in range(len(cells[y])):
cells[x][y].update()