位置: IT常识 - 正文

PyGame做了一个扫雷(pygame编程)

编辑:rootadmin
1 # 这是一个示例 Python 脚本。 2 3 # 按 ⌃R 执行或将其替换为您的代码。 4 # 按 双击 ⇧ 在所有地方搜索类、文件、工具窗口、操作和设置。 5 import sys 6 import pygame 7 import random 8 9 game = None 10 BOMB ... 1 # 这是一个示例 Python 脚本。 2 3 # 按 ⌃R 执行或将其替换为您的代码。 4 # 按 双击 ⇧ 在所有地方搜索类、文件、工具窗口、操作和设置。 5 import sys 6 import pygame 7 import random 8 9 game = None 10 BOMB_COUNT = 1 11 12 # 空间 13 class Button(object): 14 pass 15 16 class Game(object): 17 screen = None 18 row_count = 0 19 bomb_location = [] 20 squares_list = [] 21 squares_val_dict = {} 22 win = False 23 24 def __init__(self, count): 25 pygame.init() 26 total_width = count * 18 + 100 * 2 27 total_height = count * 18 + 100 * 2 28 self.row_count = count 29 self.screen = pygame.display.set_mode((total_width, total_height)) 30 self.win = False 31 self.bomb_location = [] 32 self.squares_list = [] 33 self.squares_val_dict = {} 34 35 def __del__(self): 36 print('del game') 37 pygame.display.quit() 38 39 def is_over(self): 40 return self.win 41 42 def get_screen(self): 43 return self.screen 44 45 def random_bomb(self): 46 # 随机产生炸弹 47 for i in range(BOMB_COUNT): 48 while 1: 49 x = random.randint(0, 29) 50 y = random.randint(0, 29) 51 if (x, y) in self.bomb_location: 52 continue 53 else: 54 self.bomb_location.append((x, y)) 55 break 56 57 # 计算某个位置范围的数字 58 for x in range(self.row_count): 59 for y in range(self.row_count): 60 count = 0 61 if (x - 1, y) in self.bomb_location: 62 count += 1 63 if (x - 1, y - 1) in self.bomb_location: 64 count += 1 65 if (x - 1, y + 1) in self.bomb_location: 66 count += 1 67 68 if (x , y - 1) in self.bomb_location: 69 count += 1 70 if (x , y) in self.bomb_location: 71 count += 1 72 if (x , y + 1) in self.bomb_location: 73 count += 1 74 75 if (x + 1 , y - 1) in self.bomb_location: 76 count += 1 77 if (x + 1 , y) in self.bomb_location: 78 count += 1 79 if (x + 1, y + 1) in self.bomb_location: 80 count += 1 81 82 # print('%s,%s %d' % (x, y, count)) 83 self.squares_val_dict[(x, y)] = count 84 85 def init_square(self): 86 for i in range(self.row_count): 87 self.squares_list.append([]) 88 top = 100 + i * 18 89 for j in range(self.row_count): 90 left = 100 + j * 18 91 width = 18 92 height = 18 93 exist = False 94 if (i, j) in self.bomb_location: 95 # print('%s,%s exists' % (j, i)) 96 exist = True 97 98 # 周围的炸弹数量 99 around_count = self.squares_val_dict.get((i, j), 0)100 # print('init square %s,%s %d' % (i, j, around_count))101 self.squares_list[i].append(Square(exist, around_count, self.screen, left, top, width, height))102 self.squares_list[i][j].draw()103 pygame.display.update()104 105 def start_game(self):106 pass107 108 def display_win(self):109 font = pygame.font.SysFont("Andale Mono", 32)110 txt = font.render("Winner Winner Winner", True, 'Red')111 self.get_screen().blit(txt, (200, 0))112 113 font = pygame.font.SysFont("Andale Mono", 16)114 txt = font.render("uploading to dashboard...", True, 'green')115 self.get_screen().blit(txt, (260, 40))116 117 font = pygame.font.SysFont("Andale Mono", 16)118 txt = font.render("click to continue...", True, 'gray')119 self.get_screen().blit(txt, (280, 60))120 121 self.win = True122 123 def display_flag(self):124 for (x, y) in self.bomb_location:125 square = self.squares_list[x][y]126 square.right_click()127 square.draw()128 129 # 根据所有的旗帜来判断胜利130 def check_win_by_flag(self):131 for (x, y) in self.bomb_location:132 square = self.squares_list[x][y]133 if square.state == 'flag' and square.exist:134 continue135 return False136 self.display_win()137 return True138 139 # 根据已经没有格子点击了来判断胜利140 def check_win_by_click(self):141 # print('check by click')142 for x in range(self.row_count):143 for y in range(self.row_count):144 square = self.squares_list[x][y]145 if square.state == 'blank' or square.exist:146 # print(1)147 continue148 return False149 self.display_flag()150 self.display_win()151 return True152 153 def right_clicked(self, pos):154 left = pos[0]155 top = pos[1]156 x = int((top - 100) / 18)157 y = int((left - 100) / 18)158 159 # print('right clicked %s, %s' % (x, y))160 if x in range(0, self.row_count) and y in range(0, self.row_count):161 square = self.squares_list[x][y]162 if not square.right_click():163 return164 # 表示右键生效165 square.draw()166 167 if square.state == 'flag' and square.exist:168 # 只有当前标记是正确的时候才判断169 # 判断是否已经将所有的炸弹标记出来170 self.check_win_by_flag()171 pygame.display.update()172 173 def clicked(self, pos):174 left = pos[0]175 top = pos[1]176 x = int((top - 100) / 18)177 y = int((left - 100) / 18)178 179 def click_square(self, x, y):180 if x not in range(0, self.row_count) or y not in range(0, self.row_count):181 return False182 183 square = self.squares_list[x][y]184 if square.state != 'new':185 return False186 187 if not square.click():188 return False189 190 square.draw()191 if square.around_count == 0:192 # print('around is 0')193 for (x1, y1) in [194 (x - 1, y), (x - 1, y - 1), (x - 1, y + 1),195 (x, y - 1), (x, y), (x, y + 1),196 (x + 1, y - 1), (x + 1, y), (x + 1, y + 1),197 198 ]:199 click_square(self, x1, y1)200 return True201 202 if x in range(0, self.row_count) and y in range(0, self.row_count):203 if click_square(self, x, y):204 # 判断是否成功205 self.check_win_by_click()206 pygame.display.update()207 208 def refresh(self):209 pygame.display.update()210 211 212 class Square(object):213 exist = False214 surface = None215 rect = None216 state = '' # new, blank, flag, bomed217 face = None218 around_count = 0219 220 def __init__(self, exist, around_count, surface, left, top, width, height):221 self.rect = pygame.Rect(left, top, width, height)222 self.exist = exist223 self.surface = surface224 self.state = 'new'225 self.around_count = around_count226 # print('%s' % (self.around_count))227 228 def exists(self):229 return self.exist230 231 def draw(self):232 global game233 if self.state == 'new':234 self.face = pygame.Surface((self.rect.width, self.rect.height))235 self.face.fill('white')236 game.get_screen().blit(self.face, (self.rect.left, self.rect.top))237 pygame.draw.rect(self.surface, 'gray', self.rect, 1)238 239 elif self.state == 'blank':240 self.face.fill('gray')241 game.get_screen().blit(self.face, (self.rect.left, self.rect.top))242 pygame.draw.rect(self.surface, 'white', self.rect, 1)243 244 # 在格子中间画上数字245 font = pygame.font.SysFont("Andale Mono", 16)246 txt = font.render("%s" % (self.around_count if self.around_count > 0 else ''), True, 'blue')247 # print('%s, %s' % (txt.get_rect(), self.around_count))248 game.get_screen().blit(txt, (self.rect.left + 4, self.rect.top))249 250 pass251 elif self.state == 'flag':252 # 在格子中间画上 F253 font = pygame.font.SysFont("Andale Mono", 16)254 txt = font.render("F", True, 'green')255 # print('%s, %s' % (txt.get_rect(), self.around_count))256 game.get_screen().blit(txt, (self.rect.left + 4, self.rect.top))257 258 elif self.state == 'boom':259 self.face.fill('red')260 game.get_screen().blit(self.face, (self.rect.left, self.rect.top))261 pygame.draw.rect(self.surface, 'white', self.rect, 1)262 pass263 264 def click(self):265 need_update = False266 if self.state == 'new':267 if self.exist:268 self.state = 'boom'269 need_update = True270 else:271 self.state = 'blank'272 need_update = True273 return need_update274 275 def right_click(self):276 need_update = False277 if self.state == 'new':278 self.state = 'flag'279 need_update = True280 elif self.state == 'flag':281 self.state = 'new'282 need_update = True283 return need_update284 285 286 def init_game(count, x=18, y=18):287 global game288 if game:289 del game290 game = Game(count)291 game.random_bomb()292 game.init_square()293 294 295 # 按间距中的绿色按钮以运行脚本。296 if __name__ == '__main__':297 init_game(30)298 299 while True:300 for event in pygame.event.get():301 if event.type == pygame.MOUSEBUTTONUP:302 if game.is_over():303 init_game(30)304 continue305 pos = event.pos306 if event.button == 1:307 game.clicked(pos)308 elif event.button == 3:309 game.right_clicked(pos)310 # 获取当前那个格子被点击了311 if event.type == pygame.QUIT:312 sys.exit(0)313 pygame.display.update()
本文链接地址:https://www.jiuchutong.com/zhishi/313311.html 转载请保留说明!

上一篇:织梦dedecms友情链接底层模板样式(织梦cms怎么样)

下一篇:python Unittest的组成(pytest unittest)

  • 苹果13防水等级是多少(苹果13防水等级排第几)

    苹果13防水等级是多少(苹果13防水等级排第几)

  • 微信支付宝无感支付是什么 微信支付宝无感支付介绍【详解】(微信支付宝无感支付的好感想)

    微信支付宝无感支付是什么 微信支付宝无感支付介绍【详解】(微信支付宝无感支付的好感想)

  • app商店不提示更新(app商店不提示更新怎么办)

    app商店不提示更新(app商店不提示更新怎么办)

  • 照片后缀名怎么改jpg(照片后缀名怎么改png)

    照片后缀名怎么改jpg(照片后缀名怎么改png)

  • qq更新后怎么回到以前的版本(升级后的qq怎么恢复原状)

    qq更新后怎么回到以前的版本(升级后的qq怎么恢复原状)

  • alt加f4是什么快捷键(alt+ f4)

    alt加f4是什么快捷键(alt+ f4)

  • 流量卡没有网络怎么办(流量卡没有网络咨询谁)

    流量卡没有网络怎么办(流量卡没有网络咨询谁)

  • 路由器无信号闪红是什么原因(路由器无信号闪烁红灯)

    路由器无信号闪红是什么原因(路由器无信号闪烁红灯)

  • 苹果耳机没电了还能定位吗(苹果耳机没电了充不进电)

    苹果耳机没电了还能定位吗(苹果耳机没电了充不进电)

  • 钉钉可以撤销打卡吗(钉钉可以撤销打卡记录吗)

    钉钉可以撤销打卡吗(钉钉可以撤销打卡记录吗)

  • 戴尔g3红色和蓝色区别(戴尔g33590红色和蓝色区别)

    戴尔g3红色和蓝色区别(戴尔g33590红色和蓝色区别)

  • 云服务软件有什么作用(云服务软件有什么好处)

    云服务软件有什么作用(云服务软件有什么好处)

  • bestv当贝影视可以看直播吗

    bestv当贝影视可以看直播吗

  • word中查找的快捷键是(word查找的快捷键)

    word中查找的快捷键是(word查找的快捷键)

  • 拼多多保留15个团什么意思(拼多多保留15个好友)

    拼多多保留15个团什么意思(拼多多保留15个好友)

  • 手机qq怎么查看亲密度排行(手机qq怎么查看别人撤回的消息)

    手机qq怎么查看亲密度排行(手机qq怎么查看别人撤回的消息)

  • 华为mate30的插卡的地方(华为mate30的插卡口在哪)

    华为mate30的插卡的地方(华为mate30的插卡口在哪)

  • 怎样把图片内存调小(怎样把图片内存调大一些)

    怎样把图片内存调小(怎样把图片内存调大一些)

  • ios13查找我的iphone怎么用(ios13查找我的iphone 登录其他账号)

    ios13查找我的iphone怎么用(ios13查找我的iphone 登录其他账号)

  • 小米手机截屏设置在哪里(小米手机截屏设计)

    小米手机截屏设置在哪里(小米手机截屏设计)

  • 如何用excel计算(如何用excel计算数据)

    如何用excel计算(如何用excel计算数据)

  • 小爱音箱必须用wifi吗(小爱音箱必须用无线网吗)

    小爱音箱必须用wifi吗(小爱音箱必须用无线网吗)

  • 快手上已戳怎么取消(快手戳一下怎么取消戳过的人)

    快手上已戳怎么取消(快手戳一下怎么取消戳过的人)

  • 【Vue项目实战】Vue3中动态修改样式和级联样式优先顺序详解(vue 实战)

    【Vue项目实战】Vue3中动态修改样式和级联样式优先顺序详解(vue 实战)

  • JS数组方法中哪些会改变原数组,哪些不会?(js中数组方法有哪些)

    JS数组方法中哪些会改变原数组,哪些不会?(js中数组方法有哪些)

  • 税务专家田野
  • 纳税筹划的方法有哪些
  • 盘盈固定资产明细账怎么填写?
  • 分公司法人必须是总公司法人吗
  • 利息收入为什么不能在贷方
  • 公司税务状态变更怎么办
  • 专票当月认证后什么时候抵扣
  • 属于期间费用抵减项目
  • 残疾人士有哪些
  • 支付金融机构手续费计入什么费用
  • 搬迁到新租赁厂房的费用入什么科目?
  • 银行存款收款凭证是原始凭证吗
  • 金融保险法
  • 房租发票进项税额抵扣
  • 商品流通企业进货费用金额较小的计入什么科目
  • 在会计中加速折旧的方法
  • 通用申报表工会经费可以不申报吗
  • 印花税资金账簿税率
  • 研发用的原材料怎么开领料单
  • 跨年的工会经费怎么做分录
  • ping命令出现unreachable
  • 鸿蒙系统怎么设置导航键
  • 增值税发票的进项和出项要一致吗
  • php安装教程详解
  • win10怎么显示隐藏的硬盘
  • 微软windows11泄露
  • 公司之间转移固定资产
  • 设备租赁费属于劳务吗
  • 生产型企业出口不是自己生产的东西
  • 估价入库金额怎么来
  • php session_start
  • 固定资产清理的累计折旧怎么算
  • 螺旋状芦荟 (© David Madison/Getty Images)
  • 分包工程账务处理
  • php进程数设置
  • import vue from vue报错
  • php获取开始与结束的函数
  • 删除命令rm rf
  • 内部结算属于什么科目类别
  • 企业资产损失税前扣除管理办法最新
  • 补缴去年的税款会计分录
  • 所得税的期初资产怎么算
  • 来料加工企业的税率是多少
  • 年度财务报表分为哪几类
  • 收回投资收到的现金减少
  • 企业适用的增值税税率
  • 纳税人信息变更 需要变更什么
  • 电子商业汇票背书人记载不得转让
  • 住宿发票遗失怎么办
  • 应收管理费,做应收款处理会计分录
  • 对公可以转个人账户货款吗
  • 资金帐薄印花税是指什么
  • 固定资产融资租出计入什么科目
  • 小微企业免征的增值税属于政府补助吗
  • 多计费用以前年度损益调整账务处理
  • 损益类科目如何填写手工总账
  • 主营业务成本的增加在哪一方
  • 应交增值税贷方余额表示什么
  • 汽车固定资产残值率
  • 商业企业进货会计分录
  • sql server 1222解决
  • sqlserver msdb
  • u深度pe装机工具使用教程win7
  • Fedora 9.0 Apache+PHP+MYSQL 环境安装
  • linux的tar
  • 怎么设置开机启动项?
  • win7打开文件夹都是独立的窗口
  • linux中的vi编辑器一般有哪三个模式
  • html5lib
  • 搭建nodejs
  • shell自动化
  • dos命令怎么输入命令
  • 一次性批量随机抽取
  • unity删除对象
  • Easyui form combobox省市区三级联动
  • 一起学下载软件
  • md5加密python
  • python数据通信
  • 安徽税务网上申报领取发票
  • 宝马535车船税
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设