-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
111 lines (97 loc) · 2.77 KB
/
main.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import random
ROWS = 15
COLS = 15
SIZE = 25
BOMBS = 20
WIDTH = SIZE * COLS
HEIGHT = SIZE * ROWS
failed = False
finished = False
# 生成方块阵列
blocks = []
for i in range(ROWS):
for j in range(COLS):
b = Actor("minesweep_block")
b.left = j * SIZE
b.top = i * SIZE
b.isbomb = False
b.isflag = False
b.isopen = False
blocks.append(b)
# 打乱地雷位置
random.shuffle(blocks)
# 埋设地雷
for i in range(BOMBS):
blocks[i].isbomb = True
def draw():
for b in blocks:
b.draw()
if finished:
screen.draw.text("Finished", center=(WIDTH//2,HEIGHT//2), fontsize=100, color="red")
if failed:
screen.draw.text("Failed", center=(WIDTH//2,HEIGHT//2), fontsize=100, color="red")
def update():
global finished
if finished or failed:
return
for b in blocks:
if not b.isbomb and not b.isopen:
return
finished = True
sounds.win.play()
def on_mouse_down(pos, button):
for b in blocks:
if b.collidepoint(pos) and not b.isopen:
if button == mouse.RIGHT:
set_flag(b)
elif button == mouse.LEFT and not b.isflag:
if b.isbomb:
blow_up()
else:
open_block(b)
def blow_up():
global failed
failed = True
sounds.bomb.play()
for i in range(BOMBS):
blocks[i].image = "minesweep_bomb"
# 打开方块,方块没有雷时,调用该函数
def open_block(block):
block.isopen = True
bombnum = get_bomb_number(block)
block.image = "minesweep_number" + str(bombnum)
if bombnum != 0:
return
for b in get_neighbours(block):
if not b.isopen:
open_block(b)
# 获取周围方块
def get_neighbours(block):
nblocks = []
for b in blocks:
if b.isopen:
continue
if b.x == block.x - SIZE and b.y == block.y \
or b.x == block.x + SIZE and b.y == block.y \
or b.x == block.x and b.y == block.y - SIZE \
or b.x == block.x and b.y == block.y + SIZE \
or b.x == block.x - SIZE and b.y == block.y - SIZE \
or b.x == block.x + SIZE and b.y == block.y - SIZE \
or b.x == block.x - SIZE and b.y == block.y + SIZE \
or b.x == block.x + SIZE and b.y == block.y + SIZE:
nblocks.append(b)
return nblocks
# 获取周围方块的地雷数量
def get_bomb_number(block):
num = 0
for b in get_neighbours(block):
if b.isbomb:
num += 1
return num
def set_flag(block):
if block.isflag :
block.image = "minesweep_block"
block.isflag = False
else:
block.image = "minesweep_flag"
block.isflag = True