while True:
screen.fill(bgcolor) #填充背景色
for event in pygame.event.get(): #获取操作
if event.type == QUIT: #点击右上角的 '×'
sys.exit() #退出程序
elif event.type == MOUSEBUTTONDOWN:# 鼠标按下
mouse_x, mouse_y = event.pos #获取鼠标地位
x = mouse_x // SIZE
y = mouse_y // SIZE - 2
b1, b2, b3 = pygame.mouse.get_pressed()# 鼠标点击的地位,b1 对应左键,b3 对应右键
elif event.type == MOUSEBUTTONUP: #松开鼠标
if y < 0: #点击方块上方
if face_pos_x <= mouse_x <= face_pos_x + face_size and face_pos_y <= mouse_y <= face_pos_y + face_size: #判断是否点击笑脸
game_dict1=game_dict[0] #重置数据,开始新一局游戏
start_time = time.time()
used_time = 0
for i in list_mine:
for j in i:
pos = (j.x * SIZE, (j.y + 2) * SIZE)
j.surround_count = -1 #把周边雷的数目回归到原始状态
j.condition = Block_Condition.unclicked #所有方块回归为未点击状态
screen.blit(png_blank, pos)
pygame.display.flip()
for i in random.sample(range(BLOCK_WIDTH * BLOCK_HEIGHT), BLOCK_WIDTH * BLOCK_HEIGHT):
list_mine[i // BLOCK_WIDTH][i % BLOCK_WIDTH].value = 0 #把上一轮雷的定义全副革除
block=Mine_Block() #从新定义雷
continue
else:
continue
if game_dict1 == game_dict[0]: #状态为准备就绪
game_dict1 = game_dict[1] #开始
start_time = time.time() #开始计时
if game_dict1 == game_dict[1]:
mine = block.getblock(x, y)
if b1 and not b3: #按鼠标左键
if mine.condition == Block_Condition.unclicked: #未点击的会点开
if not block.open_block(x, y): [PerfectMoney 下载](https://www.gendan5.com/wallet/PerfectMoney.html)# 中奖了啊,一颗雷
game_dict1 = game_dict[3] #游戏状态为失败
elif not b1 and b3: #按鼠标右键
if mine.condition == Block_Condition.unclicked: #如果未点击,则标记为旗号 (雷)
mine.condition = Block_Condition.flag
elif mine.condition == Block_Condition.flag: #如果之前的标记是旗号,则改标记为问号
mine.condition = Block_Condition.ask
elif mine.condition == Block_Condition.ask: #如果之前的标记是问号,则改为未点击的状态
mine.condition = Block_Condition.unclicked
flag_count = 0 #旗号为 0
opened_count = 0 #点开的方块为 0
for row in block._block:
for mine in row:
pos = (mine.x * SIZE, (mine.y + 2) * SIZE)
if mine.condition == Block_Condition.clicked: #点开了加载图片 (数字),点开的方块加一
screen.blit(png_dict[mine.surround_count], pos)
opened_count += 1
elif mine.condition == Block_Condition.bomb: #状态是炸弹
screen.blit(png_blood, pos)
elif mine.condition == Block_Condition.flag: #是旗号
screen.blit(png_flag, pos)
flag_count += 1
elif mine.condition == Block_Condition.ask: #是问号
screen.blit(png_ask, pos)
elif game_dict1 == game_dict[3] and mine.value:
screen.blit(png_mine, pos) #失败了显示所有地雷
elif mine.condition == Block_Condition.unclicked: #是未关上
screen.blit(png_blank, pos)
if event.type == MOUSEMOTION:# 用特定的图像显示鼠标的地位
mouse_x, mouse_y = event.pos
x = mouse_x // SIZE
y = mouse_y // SIZE - 2
mine = block.getblock(x, y)
pos = (mine.x * SIZE, (mine.y + 2) * SIZE)
if y < 0:
continue
else:
if mine.condition == Block_Condition.unclicked:
screen.blit(png_dict[0],pos)
font1 = pygame.font.Font(None, SIZE * 2)
width, height = font1.size('100')
text1=font1.render('%d'%(MINE_COUNT-flag_count),True,(255,0,0))
screen.blit(text1,(30,(SIZE*2-height)//2-2)) #打印残余雷的数量
if game_dict1 == game_dict[1]:
used_time = int(time.time() - start_time)
text2=font1.render('%d'%used_time,True,(255,0,0))
screen.blit(text2,(SCREEN_WIDTH-width-30,(SIZE*2-height)//2-2)) #打印已用工夫
if opened_count + MINE_COUNT == BLOCK_WIDTH * BLOCK_HEIGHT: #点开方块的加上地雷数等于总数
game_dict1 = game_dict[2] #则视为胜利
if game_dict1 == game_dict[2]: #游戏的状态为胜利
screen.blit(png_face_win, (face_pos_x, face_pos_y))
elif game_dict1 == game_dict[3]: #游戏的状态为失败
screen.blit(png_face_lose, (face_pos_x, face_pos_y))
else:
screen.blit(png_face_normal, (face_pos_x, face_pos_y))
pygame.display.update()
if name == ‘__main__’:
main()