欢迎来到python扫雷

Posted Gendan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了欢迎来到python扫雷相关的知识,希望对你有一定的参考价值。

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()

以上是关于欢迎来到python扫雷的主要内容,如果未能解决你的问题,请参考以下文章

欢迎来到 Python 世界

python第二节

用Python写扫雷游戏实例代码分享还有很多小游戏代码

1000行Python代码实现俄罗斯方块/扫雷/五子棋/贪吃蛇

扫雷小游戏(可选择简单一般困难三种模式可展开,可标记)

Js 网页版扫雷游戏代码实现