素材:链接:https://pan.baidu.com/s/1301Cjza80waeot0M9LmV0Q
提取码:
# -*- coding: utf-8 -*-
import pygame
import sys
import random
import win32api
import win32con
class Game(object):
def __init__(self, x, y, value):
self.x = x
self.y = y
self.value = value
def __init_chess__(self):
self.init_chess_list_down = [] # 后端实际数据
self.init_chess_list_up = [] # 前端布局地图
for i in range(9):
rel_list_1 = []
rel_list_2 = []
for j in range(9):
y = i * 50 + 100
x = j * 50
value_1 = 0
value_2 = -1
sp_1 = Game(x, y, value_1)
sp_2 = Game(x, y, value_2)
rel_list_1.append(sp_1)
rel_list_2.append(sp_2)
self.init_chess_list_down.append(rel_list_1) # value -1 雷 0~8 周围的雷的数量
self.init_chess_list_up.append(rel_list_2) # value -1 未点开 -2 已经点开 -3 标记为地雷 -4 标记为未知
self.sum = 0 # 记录已经翻开数量
self.bu_lei = 0 # 记录埋藏地雷数量
self.result = 0 # 记录游戏状态
self.type = 0 # 记录音乐状态 0 停止 1 播放 2 暂停
self.button = 0 # 记录第一次点击,保证第一次不为雷
def __image_load__(self):
pygame.init()
pygame.display.set_caption("扫雷V2.00")
self.string = ['./images_saolei/flower_sea.mp3', './images_saolei/face_white.mp3', './images_saolei/heard.mp3',
'./images_saolei/if_last.mp3', './images_saolei/listen.mp3', './images_saolei/luzhou_moon.mp3',
'./images_saolei/pugongyin.mp3', './images_saolei/red.mp3', './images_saolei/seventeen.mp3',
'./images_saolei/suyan.mp3', './images_saolei/what_do_not.mp3', './images_saolei/white_moonlight.mp3',
'./images_saolei/white_sheep.mp3']
pygame.mixer.music.stop() # 关闭之前正在播放的音乐,否则无法播放新音乐
self.screen = pygame.display.set_mode((450, 550), 0, 0)
self.zero = pygame.image.load("./images_saolei/0.gif")
self.one = pygame.image.load("./images_saolei/1.gif")
self.two = pygame.image.load("./images_saolei/2.gif")
self.three = pygame.image.load("./images_saolei/3.gif")
self.four = pygame.image.load("./images_saolei/4.gif")
self.five = pygame.image.load("./images_saolei/5.gif")
self.six = pygame.image.load("./images_saolei/6.gif")
self.seven = pygame.image.load("./images_saolei/7.gif")
self.eight = pygame.image.load("./images_saolei/8.gif")
self.lei = pygame.image.load("./images_saolei/blood.gif")
self.flag = pygame.image.load("./images_saolei/flag.gif")
self.ask = pygame.image.load("./images_saolei/ask.gif")
self.blank = pygame.image.load("./images_saolei/blank1.gif")
self.up = pygame.image.load("./images_saolei/up.png")
self.smile = pygame.image.load("./images_saolei/smile.gif")
self.Ooo = pygame.image.load("./images_saolei/Ooo.gif")
self.restart = pygame.image.load("./images_saolei/restart.png")
self.help = pygame.image.load("./images_saolei/help.png")
self.win = pygame.image.load("./images_saolei/win.png")
pygame.display.update()
def __image_print__(self):
if self.type == 0:
num = random.randint(0, 12)
pygame.mixer.music.load(self.string[num])
pygame.mixer.music.play()
pygame.mixer.music.set_volume(0.6)
self.type = 1
self.screen.blit(self.up, (0, 0))
if self.result == 0:
self.screen.blit(self.smile, (200, 25)) # 笑脸
elif self.result == 1:
self.screen.blit(self.Ooo, (200, 25)) # 哭脸
self.screen.blit(self.restart, (310, 10)) # 失败重开
else:
self.screen.blit(self.win, (165, 17)) # 胜利
self.screen.blit(self.restart, (310, 10)) # 胜利重开
self.screen.blit(self.help, (20, 10)) # 帮助
for i in range(9):
for j in range(9):
if self.init_chess_list_up[i][j].value == -1: # 打印空格
self.screen.blit(self.blank, (self.init_chess_list_down[i][j].x, self.init_chess_list_down[i][j].y))
elif self.init_chess_list_up[i][j].value == -2 and self.init_chess_list_down[i][j].value == 0: # 打印已经翻开的格子
self.screen.blit(self.zero, (self.init_chess_list_down[i][j].x, self.init_chess_list_down[i][j].y))
elif self.init_chess_list_up[i][j].value == -2 and self.init_chess_list_down[i][j].value == 1: # 打印已经翻开的格子
self.screen.blit(self.one, (self.init_chess_list_down[i][j].x, self.init_chess_list_down[i][j].y))
elif self.init_chess_list_up[i][j].value == -2 and self.init_chess_list_down[i][j].value == 2: # 打印已经翻开的格子
self.screen.blit(self.two, (self.init_chess_list_down[i][j].x, self.init_chess_list_down[i][j].y))
elif self.init_chess_list_up[i][j].value == -2 and self.init_chess_list_down[i][j].value == 3: # 打印已经翻开的格子
self.screen.blit(self.three, (self.init_chess_list_down[i][j].x, self.init_chess_list_down[i][j].y))
elif self.init_chess_list_up[i][j].value == -2 and self.init_chess_list_down[i][j].value == 4: # 打印已经翻开的格子
self.screen.blit(self.four, (self.init_chess_list_down[i][j].x, self.init_chess_list_down[i][j].y))
elif self.init_chess_list_up[i][j].value == -2 and self.init_chess_list_down[i][j].value == 5: # 打印已经翻开的格子
self.screen.blit(self.five, (self.init_chess_list_down[i][j].x, self.init_chess_list_down[i][j].y))
elif self.init_chess_list_up[i][j].value == -2 and self.init_chess_list_down[i][j].value == 6: # 打印已经翻开的格子
self.screen.blit(self.six, (self.init_chess_list_down[i][j].x, self.init_chess_list_down[i][j].y))
elif self.init_chess_list_up[i][j].value == -2 and self.init_chess_list_down[i][j].value == 7: # 打印已经翻开的格子
self.screen.blit(self.seven, (self.init_chess_list_down[i][j].x, self.init_chess_list_down[i][j].y))
elif self.init_chess_list_up[i][j].value == -2 and self.init_chess_list_down[i][j].value == 8: # 打印已经翻开的格子
self.screen.blit(self.eight, (self.init_chess_list_down[i][j].x, self.init_chess_list_down[i][j].y))
elif self.init_chess_list_up[i][j].value == -2 and self.init_chess_list_down[i][j].value == -1: # 打印已经翻开的格子
self.screen.blit(self.lei, (self.init_chess_list_down[i][j].x, self.init_chess_list_down[i][j].y))
elif self.init_chess_list_up[i][j].value == -3: # 打印标记的棋子
self.screen.blit(self.flag, (self.init_chess_list_down[i][j].x, self.init_chess_list_down[i][j].y))
elif self.init_chess_list_up[i][j].value == -4: # 打印标记的未知
self.screen.blit(self.ask, (self.init_chess_list_down[i][j].x, self.init_chess_list_down[i][j].y))
pygame.display.update()
def __set_lei_map__(self): # 布雷,81个格子10个雷
while self.bu_lei < 10:
if self.init_chess_list_down[random.randint(0, 8)][random.randint(0, 8)].value == 0: # 若随机产生的位置不为雷
self.init_chess_list_down[random.randint(0, 8)][random.randint(0, 8)].value = -1 # 则在该位置布雷
self.bu_lei += 1 # 雷的数量加一
# 统计各个格子附近的地雷数量,分了九种情况,代码冗余严重
for i in range(9):
for j in range(9):
if self.init_chess_list_down[i][j].value != -1:
if i != 0 and i != 8 and j != 0 and j != 8:
sum = 0
for m in range(i - 1, i + 2, 1):
for n in range(j - 1, j + 2, 1):
if self.init_chess_list_down[m][n].value == -1:
sum += 1
self.init_chess_list_down[i][j].value = sum
elif i == j == 0:
sum = 0
for m in range(0, 2, 1):
for n in range(0, 2, 1):
if self.init_chess_list_down[m][n].value == -1:
sum += 1
self.init_chess_list_down[i][j].value = sum
elif i == j == 8:
sum = 0
for m in range(7, 9, 1):
for n in range(7, 9, 1):
if self.init_chess_list_down[m][n].value == -1:
sum += 1
self.init_chess_list_down[i][j].value = sum
elif i == 8 and j == 0:
sum = 0
for m in range(7, 9, 1):
for n in range(0, 2, 1):
if self.init_chess_list_down[m][n].value == -1:
sum += 1
self.init_chess_list_down[i][j].value = sum
elif i == 0 and j == 8:
sum = 0
for m in range(0, 2, 1):
for n in range(7, 9, 1):
if self.init_chess_list_down[m][n].value == -1:
sum += 1
self.init_chess_list_down[i][j].value = sum
elif i == 0 and (j != 0 or j != 8):
sum = 0
for m in range(0, 2, 1):
for n in range(j - 1, j + 2, 1):
if self.init_chess_list_down[m][n].value == -1:
sum += 1
self.init_chess_list_down[i][j].value = sum
elif i == 8 and (j != 0 or j != 8):
sum = 0
for m in range(7, 9, 1):
for n in range(j - 1, j + 2, 1):
if self.init_chess_list_down[m][n].value == -1:
sum += 1
self.init_chess_list_down[i][j].value = sum
elif j == 0 and (i != 0 or i != 8):
sum = 0
for m in range(i - 1, i + 2, 1):
for n in range(0, 2, 1):
if self.init_chess_list_down[m][n].value == -1:
sum += 1
self.init_chess_list_down[i][j].value = sum
elif j == 8 and (i != 0 or i != 8):
sum = 0
for m in range(i - 1, i + 2, 1):
for n in range(7, 9, 1):
if self.init_chess_list_down[m][n].value == -1:
sum += 1
self.init_chess_list_down[i][j].value = sum
def __event_hand__(self): # 事件监听
sum_1 = sum_2 = 0 # 两种胜利方式:1.打开全部非地雷的格子 2.标记全部的地雷格子
for i in range(0, 9, 1): # 检测是否胜利
for j in range(0, 9, 1):
if self.init_chess_list_down[i][j].value != -1 and self.init_chess_list_up[i][j].value == -2: # 格子不是地雷且已经打开
sum_1 += 1
if self.init_chess_list_down[i][j].value == -1 and self.init_chess_list_up[i][j].value == -3:
sum_2 += 1
if pygame.mixer.music.get_busy() != 1: # 判断是否在播放音乐, 返回1为正在播放
self.type = 0
if sum_1 == 71 or sum_2 == 10: # 全部非地雷的格子都打开了或者地雷全部标记了
self.result = 2 # 胜利
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
press_mouse = pygame.mouse.get_pressed(num_buttons=3)
x, y = pygame.mouse.get_pos()
for i in range(9): # 玩家点击打开格子
for j in range(9):
if x >= self.init_chess_list_down[i][j].x and x <= self.init_chess_list_down[i][j].x + 50 \
and y >= self.init_chess_list_down[i][j].y and y <= self.init_chess_list_down[i][j].y + 50 \
and self.init_chess_list_up[i][j].value == -1 and self.result == 0: # 未打开的格子才能打开,已经打开或者已经标记的格子无法打开
print(x, y)
if press_mouse[0]: # 左键按下
self.button += 1
self.init_chess_list_up[i][j].value = -2 # -1 未打开 -2 已经打开 -3 标记为地雷 -4 标记为未知
if self.init_chess_list_down[i][j].value == -1 and self.button != 1: # 如果打开的是地雷则失败 第一次打开不算
self.result = 1
elif self.init_chess_list_down[i][j].value == -1 and self.button == 1: # 如果第一次打开的就是雷则重置地图刷新点击次数
self.__init_chess__()
self.__set_lei_map__()
if press_mouse[2]: # 右键按下
if self.init_chess_list_up[i][j].value == -1: # 未打开
self.init_chess_list_up[i][j].value = -3 # 排雷
elif self.init_chess_list_up[i][j].value == -3: # 已经标记为地雷
self.init_chess_list_up[i][j].value = -4 # 标记为未知
elif self.init_chess_list_up[i][j].value == -4: # 已经标记为未知
self.init_chess_list_up[i][j].value = -1 # 清楚标记
if x >= 20 and x <= 180 and y >= 10 and y <= 90:
"""
这种办法无法关闭对话框,待解决。
"""
win32api.MessageBox(0, '打开本游戏所在文件夹的help.txt文件查看详细信息\n'
'1.游戏胜利方式_打开所有非地雷格_标记所有地雷格\n'
'2.标记方式_右键点击未打开格标记为地雷_右键点击标记为地雷格标记为未知_\n'
' _右键点击标记为未知格清空标记\n'
'3.已经标记的格子无法打开,需要先取消标记\n'
'4.游戏BGM为随机播放,每次播放完或者一把游戏结束将播放新音乐\n'
'5.Key_a or Key_Left 可以减小音量 Key_d or Key_Right 可以增大音量\n'
'6.版权所有,侵权必究!!!\n'
, '游戏须知', win32con.MB_OK)
file = open("./help.txt", 'w')
text = '1.游戏胜利方式_打开所有非地雷格_标记所有地雷格\n' \
'2.标记方式_右键点击未打开格标记为地雷_右键点击标记为地雷格标记为未知_\n' \
' _右键点击标记为未知格清空标记\n' \
'3.已经标记的格子无法打开,需要先取消标记\n' \
'4.游戏BGM为随机播放,每次播放完或者一把游戏结束将播放新音乐\n' \
'5.Key_a or Key_Left 可以减小音量 Key_d or Key_Right 可以增大音量\n' \
'6.版权所有,侵权必究!!!\n' \
'上CSDN查看更多精彩内容 https://blog.csdn.net/may10?spm=1000.2115.3001.5343\n' \
'欢迎关注本博主!!!'
file.write(text)
file.close()
"""
打印布局地图
print("\n\n\n")
for i in range(0, 9, 1):
for j in range(0, 9, 1):
print(self.init_chess_list_down[i][j].value, end="\t")
print()
print("\n\n\n")
for i in range(0, 9, 1):
for j in range(0, 9, 1):
print(self.init_chess_list_up[i][j].value, end="\t")
print()
"""
if x >= 310 and x <= 390 and y >= 10 and y <= 90 and self.result != 0:
self.main() # 点击重新开始
if event.type == pygame.KEYDOWN and (event.key == pygame.K_LEFT or event.key == pygame.K_a):
a = pygame.mixer.music.get_volume()
a -= 0.1
print(a) # 调节音量
pygame.mixer.music.set_volume(a)
if event.type == pygame.KEYDOWN and (event.key == pygame.K_RIGHT or event.key == pygame.K_d):
a = pygame.mixer.music.get_volume()
a += 0.1
print(a) # 调节音量
pygame.mixer.music.set_volume(a)
def main(self):
self.__image_load__() # 加载素材
self.__init_chess__() # 初始化数据
self.__set_lei_map__() # 设置地图
self.__image_print__() # 打印地图
while True:
self.__event_hand__() # 事件监听
self.__image_print__() # 打印地图
if __name__ == '__main__':
game = Game(0, 0, 0)
game.main()
上一版本的扫雷也可以多多关注https://mp.csdn.net/editor/html/115219359