import sys
import time
import pygame
from pygame.locals import *
from SaoLei.mineblock import *
# 游戏屏幕的宽
SCREEN_WIDTH = BLOCK_WIDTH * SIZE
# 游戏屏幕的高
SCREEN_HEIGHT = (BLOCK_HEIGHT + 2) * SIZE
class GameStatus(Enum):
readied = 1,
started = 2,
over = 3,
win = 4
def print_text(screen, font, x, y, text, fcolor=(255, 255, 255)):
imgText = font.render(text, True, fcolor)
screen.blit(imgText, (x, y))
def main():
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('扫雷')
font1 = pygame.font.Font("freesansbold.ttf", SIZE * 2) # 得分的字体
fwidth, fheight = font1.size('999')
red = (200, 40, 40)
# 加载资源图片,因为资源文件大小不一,所以做了统一的缩放处理
img0 = pygame.image.load('resources/0.gif').convert()
img0 = pygame.transform.smoothscale(img0, (SIZE, SIZE))
img1 = pygame.image.load('resources/1.gif').convert()
img1 = pygame.transform.smoothscale(img1, (SIZE, SIZE))
img2 = pygame.image.load('resources/2.gif').convert()
img2 = pygame.transform.smoothscale(img2, (SIZE, SIZE))
img3 = pygame.image.load('resources/3.gif').convert()
img3 = pygame.transform.smoothscale(img3, (SIZE, SIZE))
img4 = pygame.image.load('resources/4.gif').convert()
img4 = pygame.transform.smoothscale(img4, (SIZE, SIZE))
img5 = pygame.image.load('resources/5.gif').convert()
img5 = pygame.transform.smoothscale(img5, (SIZE, SIZE))
img6 = pygame.image.load('resources/6.gif').convert()
img6 = pygame.transform.smoothscale(img6, (SIZE, SIZE))
img7 = pygame.image.load('resources/7.gif').convert()
img7 = pygame.transform.smoothscale(img7, (SIZE, SIZE))
img8 = pygame.image.load('resources/8.gif').convert()
img8 = pygame.transform.smoothscale(img8, (SIZE, SIZE))
img_blank = pygame.image.load('resources/blank1.gif').convert()
img_blank = pygame.transform.smoothscale(img_blank, (SIZE, SIZE))
img_flag = pygame.image.load('resources/flag.gif').convert()
img_flag = pygame.transform.smoothscale(img_flag, (SIZE, SIZE))
img_ask = pygame.image.load('resources/ask.gif').convert()
img_ask = pygame.transform.smoothscale(img_ask, (SIZE, SIZE))
img_mine = pygame.image.load('resources/mine.gif').convert()
img_mine = pygame.transform.smoothscale(img_mine, (SIZE, SIZE))
img_blood = pygame.image.load('resources/blood.gif').convert()
img_blood = pygame.transform.smoothscale(img_blood, (SIZE, SIZE))
img_error = pygame.image.load('resources/error.gif').convert()
img_error = pygame.transform.smoothscale(img_error, (SIZE, SIZE))
face_size = int(SIZE * 1.25)
img_face_fail = pygame.image.load('resources/Ooo.gif').convert()
img_face_fail = pygame.transform.smoothscale(img_face_fail, (face_size, face_size))
img_face_normal = pygame.image.load('resources/Ooo.gif').convert()
img_face_normal = pygame.transform.smoothscale(img_face_normal, (face_size, face_size))
img_face_success = pygame.image.load('resources/smile.gif').convert()
img_face_success = pygame.transform.smoothscale(img_face_success, (face_size, face_size))
face_pos_x = (SCREEN_WIDTH - face_size) // 2
face_pos_y = (SIZE * 2 - face_size) // 2
img_dict = {
0: img0,
1: img1,
2: img2,
3: img3,
4: img4,
5: img5,
6: img6,
7: img7,
8: img8
}
bgcolor = (225, 225, 225) # 背景色
block = MineBlock()
game_status = GameStatus.readied
start_time = None # 开始时间
elapsed_time = 0 # 耗时
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()
if game_status == GameStatus.started:
# 鼠标左右键同时按下,如果已经标记了所有雷,则打开周围一圈
# 如果还未标记完所有雷,则有一个周围一圈被同时按下的效果
if b1 and b3:
mine = block.getmine(x, y)
if mine.status == BlockStatus.opened:
if not block.double_mouse_button_down(x, y):
game_status = GameStatus.over
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_status = GameStatus.readied
block = MineBlock()
start_time = time.time()
elapsed_time = 0
continue
if game_status == GameStatus.readied:
game_status = GameStatus.started
start_time = time.time()
elapsed_time = 0
if game_status == GameStatus.started:
mine = block.getmine(x, y)
if b1 and not b3: # 按鼠标左键
if mine.status == BlockStatus.normal:
if not block.open_mine(x, y):
game_status = GameStatus.over
elif not b1 and b3: # 按鼠标右键
if mine.status == BlockStatus.normal:
mine.status = BlockStatus.flag
elif mine.status == BlockStatus.flag:
mine.status = BlockStatus.ask
elif mine.status == BlockStatus.ask:
mine.status = BlockStatus.normal
elif b1 and b3:
if mine.status == BlockStatus.double:
block.double_mouse_button_up(x, y)
flag_count = 0
opened_count = 0
for row in block.block:
for mine in row:
pos = (mine.x * SIZE, (mine.y + 2) * SIZE)
if mine.status == BlockStatus.opened:
screen.blit(img_dict[mine.around_mine_count], pos)
opened_count += 1
elif mine.status == BlockStatus.double:
screen.blit(img_dict[mine.around_mine_count], pos)
elif mine.status == BlockStatus.bomb:
screen.blit(img_blood, pos)
elif mine.status == BlockStatus.flag:
screen.blit(img_flag, pos)
flag_count += 1
elif mine.status == BlockStatus.ask:
screen.blit(img_ask, pos)
elif mine.status == BlockStatus.hint:
screen.blit(img0, pos)
elif game_status == GameStatus.over and mine.value:
screen.blit(img_mine, pos)
elif mine.value == 0 and mine.status == BlockStatus.flag:
screen.blit(img_error, pos)
elif mine.status == BlockStatus.normal:
screen.blit(img_blank, pos)
print_text(screen, font1, 30, (SIZE * 2 - fheight) // 2 - 2, '%02d' % (MINE_COUNT - flag_count), red)
if game_status == GameStatus.started:
elapsed_time = int(time.time() - start_time)
print_text(screen, font1, SCREEN_WIDTH - fwidth - 30, (SIZE * 2 - fheight) // 2 - 2, '%03d' % elapsed_time, red)
if flag_count + opened_count == BLOCK_WIDTH * BLOCK_HEI
![avatar](https://profile-avatar.csdnimg.cn/517d32bea29f46e0ae585631405d377e_weixin_42756970.jpg!1)
Python代码大全
- 粉丝: 2879
- 资源: 686
最新资源
- 探究动态渗透率模型下的天然气水合物降压开采:深度数值模拟分析与研究,探究动态渗透率模型下的天然气水合物降压开采:精细数值模拟与分析研究,基于动态渗透率模型的天然气水合物降压开采数值模拟研究 ,基于动态
- 基于动态渗透率模型下的天然气水合物降压开采过程数值模拟研究:探索开采策略与影响因素分析,基于动态渗透率模型与数值模拟的天然气水合物降压开采技术研究与应用分析,基于动态渗透率模型的天然气水合物降压开采数
- 煤层开挖过程与邻近煤岩层应力变形场变化及保护层效应研究模型,煤层开挖过程应力变形场变化及瓦斯流动弹塑性模型研究,煤层工作开挖过程,会引起邻近煤岩层应力、变形场发生变化,以及引起临近煤层卸压,从而达到保
- 电气安全专业基础-.pdf
- 利用PID控制器模拟智能控温建筑房间温度-Simulink在MATLAB中的实现及详细pdf介绍,基于Simulink的MATLAB环境的PID控制器模拟,实现对房间温度的精准控制附带详细pdf介绍
- 一个用 c 语言编写的学生成绩管理系统源码
- STM32微控制器的入门教程,涵盖基础知识、开发环境搭建、常用功能实现及学习路径
- 直流电机双闭环调速控制系统仿真模型:转速与电流双闭环PI控制研究及Matlab Simulink应用报告,直流电机双闭环调速控制系统仿真模型研究:转速与电流双闭环PI控制算法的探索与实践(基于Matl
- 基于Matlab Simulink的四机两区域与IEEE 39节点系统短路故障分析与潮流计算研究,基于Matlab Simulink的四机两区域与IEEE 39节点系统短路故障分析与潮流计算研究,Ma
- 一个用 c 语言编写的链表实现栈的源码
- VSG并联技术详解:专业讲解视频带你深入了解其运作原理与实际应用,VSG并联技术详解:专业讲解视频带你深入了解并联运行原理与操作应用,VSG并联,有对应讲解视频 ,VSG并联; 对应讲解视频; 电力技
- 健康评分主要受运动频率、胆固醇水平和年龄的影响
- 2000-2019年上市公司银企关系数据.xls
- Matlab Simulink下的双馈风机风电调频技术研究:含虚拟惯性惯量控制与下垂控制,快速仿真达20秒,基于MATLAB Simulink的双馈风机风电调频技术:包含虚拟惯性惯量与下垂控制策略的快
- 基于煤层渗透扩散特性消除的煤与瓦斯气固耦合模型研究:建立修正P-M渗透率模型与多物理场耦合关系分析,煤层瓦斯渗透扩散的深部采煤影响与煤固气耦合模型的构建与应用研究,立足于消除煤层渗透及扩散特性对于煤与
- Matlab Simulink下的风光火储联合调频系统:混合储能优化与等值系统分析,风电三相电压电流离散模型仿真研究,Matlab Simulink下的风光火储联合调频系统:混合储能优化与等值系统分析
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
- 1
- 2
- 3
前往页