1 # 这是一个示例 Python 脚本 。23 # 按 ?R 执行或将其替换为您的代码 。4 # 按 双击 ? 在所有地方搜索类、文件、工具窗口、操作和设置 。5 import sys6 import pygame7 import random89 game = None 10 BOMB_COUNT = 1 11 12 # 空间 13 class Button(object): 14pass 15 16 class Game(object): 17screen = None 18row_count = 0 19bomb_location = [] 20squares_list = [] 21squares_val_dict = {} 22win = False 23 24def __init__(self, count): 25pygame.init() 26total_width = count * 18 + 100 * 2 27total_height = count * 18 + 100 * 2 28self.row_count = count 29self.screen = pygame.display.set_mode((total_width, total_height)) 30self.win = False 31self.bomb_location = [] 32self.squares_list = [] 33self.squares_val_dict = {} 34 35def __del__(self): 36print('del game') 37pygame.display.quit() 38 39def is_over(self): 40return self.win 41 42def get_screen(self): 43return self.screen 44 45def random_bomb(self): 46# 随机产生炸弹 47for i in range(BOMB_COUNT): 48while 1: 49x = random.randint(0, 29) 50y = random.randint(0, 29) 51if (x, y) in self.bomb_location: 52continue 53else: 54self.bomb_location.append((x, y)) 55break 56 57# 计算某个位置范围的数字 58for x in range(self.row_count): 59for y in range(self.row_count): 60count = 0 61if (x - 1, y) in self.bomb_location: 62count += 1 63if (x - 1, y - 1) in self.bomb_location: 64count += 1 65if (x - 1, y + 1) in self.bomb_location: 66count += 1 67 68if (x , y - 1) in self.bomb_location: 69count += 1 70if (x , y) in self.bomb_location: 71count += 1 72if (x , y + 1) in self.bomb_location: 73count += 1 74 75if (x + 1 , y - 1) in self.bomb_location: 76count += 1 77if (x + 1 , y) in self.bomb_location: 78count += 1 79if (x + 1, y + 1) in self.bomb_location: 80count += 1 81 82# print('%s,%s %d' % (x, y, count)) 83self.squares_val_dict[(x, y)] = count 84 85def init_square(self): 86for i in range(self.row_count): 87self.squares_list.append([]) 88top = 100 + i * 18 89for j in range(self.row_count): 90left = 100 + j * 18 91width = 18 92height = 18 93exist = False 94if (i, j) in self.bomb_location: 95# print('%s,%s exists' % (j, i)) 96exist = True 97 98# 周围的炸弹数量 99around_count = self.squares_val_dict.get((i, j), 0)100# print('init square %s,%s %d' % (i, j, around_count))101self.squares_list[i].append(Square(exist, around_count, self.screen, left, top, width, height))102self.squares_list[i][j].draw()103pygame.display.update()104105def start_game(self):106pass107108def display_win(self):109font = pygame.font.SysFont("Andale Mono", 32)110txt = font.render("Winner Winner Winner", True, 'Red')111self.get_screen().blit(txt, (200, 0))112113font = pygame.font.SysFont("Andale Mono", 16)114txt = font.render("uploading to dashboard...", True, 'green')115self.get_screen().blit(txt, (260, 40))116117font = pygame.font.SysFont("Andale Mono", 16)118txt = font.render("click to continue...", True, 'gray')119self.get_screen().blit(txt, (280, 60))120121self.win = True122123def display_flag(self):124for (x, y) in self.bomb_location:125square = self.squares_list[x][y]126square.right_click()127square.draw()128129# 根据所有的旗帜来判断胜利130def check_win_by_flag(self):131for (x, y) in self.bomb_location:132square = self.squares_list[x][y]133if square.state == 'flag' and square.exist:134continue135return False136self.display_win()137return True138139# 根据已经没有格子点击了来判断胜利140def check_win_by_click(self):141# print('check by click')142for x in range(self.row_count):143for y in range(self.row_count):144square = self.squares_list[x][y]145if square.state == 'blank' or square.exist:146# print(1)147continue148return False149self.display_flag()150self.display_win()151return True152153def right_clicked(self, pos):154left = pos[0]155top = pos[1]156x = int((top - 100) / 18)157y = int((left - 100) / 18)158159# print('right clicked %s, %s' % (x, y))160if x in range(0, self.row_count) and y in range(0, self.row_count):161square = self.squares_list[x][y]162if not square.right_click():163return164# 表示右键生效165square.draw()166167if square.state == 'flag' and square.exist:168# 只有当前标记是正确的时候才判断169# 判断是否已经将所有的炸弹标记出来170self.check_win_by_flag()171pygame.display.update()172173def clicked(self, pos):174left = pos[0]175top = pos[1]176x = int((top - 100) / 18)177y = int((left - 100) / 18)178179def click_square(self, x, y):180if x not in range(0, self.row_count) or y not in range(0, self.row_count):181return False182183square = self.squares_list[x][y]184if square.state != 'new':185return False186187if not square.click():188return False189190square.draw()191if square.around_count == 0:192# print('around is 0')193for (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),197198]:199click_square(self, x1, y1)200return True201202if x in range(0, self.row_count) and y in range(0, self.row_count):203if click_square(self, x, y):204# 判断是否成功205self.check_win_by_click()206pygame.display.update()207208def refresh(self):209pygame.display.update()210211212 class Square(object):213exist = False214surface = None215rect = None216state = '' # new, blank, flag, bomed217face = None218around_count = 0219220def __init__(self, exist, around_count, surface, left, top, width, height):221self.rect = pygame.Rect(left, top, width, height)222self.exist = exist223self.surface = surface224self.state = 'new'225self.around_count = around_count226# print('%s' % (self.around_count))227228def exists(self):229return self.exist230231def draw(self):232global game233if self.state == 'new':234self.face = pygame.Surface((self.rect.width, self.rect.height))235self.face.fill('white')236game.get_screen().blit(self.face, (self.rect.left, self.rect.top))237pygame.draw.rect(self.surface, 'gray', self.rect, 1)238239elif self.state == 'blank':240self.face.fill('gray')241game.get_screen().blit(self.face, (self.rect.left, self.rect.top))242pygame.draw.rect(self.surface, 'white', self.rect, 1)243244# 在格子中间画上数字245font = pygame.font.SysFont("Andale Mono", 16)246txt = font.render("%s" % (self.around_count if self.around_count > 0 else ''), True, 'blue')247# print('%s, %s' % (txt.get_rect(), self.around_count))248game.get_screen().blit(txt, (self.rect.left + 4, self.rect.top))249250pass251elif self.state == 'flag':252# 在格子中间画上 F253font = pygame.font.SysFont("Andale Mono", 16)254txt = font.render("F", True, 'green')255# print('%s, %s' % (txt.get_rect(), self.around_count))256game.get_screen().blit(txt, (self.rect.left + 4, self.rect.top))257258elif self.state == 'boom':259self.face.fill('red')260game.get_screen().blit(self.face, (self.rect.left, self.rect.top))261pygame.draw.rect(self.surface, 'white', self.rect, 1)262pass263264def click(self):265need_update = False266if self.state == 'new':267if self.exist:268self.state = 'boom'269need_update = True270else:271self.state = 'blank'272need_update = True273return need_update274275def right_click(self):276need_update = False277if self.state == 'new':278self.state = 'flag'279need_update = True280elif self.state == 'flag':281self.state = 'new'282need_update = True283return need_update284285286 def init_game(count, x=18, y=18):287global game288if game:289del game290game = Game(count)291game.random_bomb()292game.init_square()293294295 # 按间距中的绿色按钮以运行脚本 。296 if __name__ == '__main__':297init_game(30)298299while True:300for event in pygame.event.get():301if event.type == pygame.MOUSEBUTTONUP:302if game.is_over():303init_game(30)304continue305pos = event.pos306if event.button == 1:307game.clicked(pos)308elif event.button == 3:309game.right_clicked(pos)310# 获取当前那个格子被点击了311if event.type == pygame.QUIT:312sys.exit(0)313pygame.display.update()
推荐阅读
- JavaSPI详解
- OpenMP 入门
- 12款4711古龙水测评
- UEC 利用代理/委托写一个生命组件
- 睡前暖心小故事 梦公主的故事 在一个仙国里,有一位专门管小孩子的梦的小仙女
- 从源码入手探究一个因useImperativeHandle引起的Bug
- three.js 如何用webgl搭建一个3D库房,3D仓库3D码头,3D集装箱,车辆定位,叉车定位可视化孪生系统——第十五课
- React实现一个简易版Swiper
- 键盘上双引号怎么打(双引号在键盘的哪一个键)
- 怎么去除电脑上一个mcafee(怎么去除电脑上的小图标)