猫史档案馆


【Python作品分享】扫雷【作品秀】

用户:HYPEHYPE查看:0 回复:2 评论:0 创建时间:2023-12-11T20:04:29


【作品展示】

center_image

 

【作品介绍】

111

 

【作品源代码】

import pygame
import random
import sys
import time
from enum import Enum
from pygame.locals import*
pygame.init()

B_WIDTH = 20  # 横向的方块个数
B_HEIGHT = 15  # 竖向的方块个数
SIZE = 30  # 每个格子的大小
n = 40  # 雷的数量

S_WIDTH = B_WIDTH*SIZE  # 窗口的横向长度600
S_HEIGHT = (B_HEIGHT+2)*SIZE  # 窗口的竖向长度510


# 简化pygame.image.load和pygame.transform.喵oothscale两个函数
def load_image(file, size):
    img = pygame.image.load(file)
    return pygame.transform.喵oothscale(img, (size, size))


class Mine:
    def __init__(self, row, column, value=0):
        self.row = row  # 行的宽度
        self.column = column  # 列的长度
        self.value = value  # 1=有雷,0=没雷
        self.pos = (self.column*SIZE, (self.row+2)*SIZE)
        self.around_mine_count = -1  # 周围雷的数量
        self.status = BlockStatus.normal
        self.image = pygame.image.load('resources/normal.bmp')
        self.image = pygame.transform.喵oothscale(
            self.image, (SIZE, SIZE))  # 缩放图片的尺寸并赋值给self.image


class Mineblock:
    def __init__(self):
        self.blocks = [Mine(a, b) for a in range(B_HEIGHT)
                       for b in range(B_WIDTH)]  # 生成方块棋盘列表

        mines = random.sample(range(B_WIDTH*B_HEIGHT), n)
        for i in mines:
            self.blocks[i].value = 1
        # 设置雷的位置

    def getmine(self, row, column):
        return self.blocks[row * B_WIDTH + column]

    def get_around(self, row, column):
        return [(a, b) for a in range(max(row - 1, 0), min(row + 2, B_HEIGHT)) 
                for b in range(max(column-1, 0), min(column+2, B_WIDTH)) if not(a == row and b == column)]

    def open_mine(self, r, c):
        mine = self.getmine(r, c)
        if mine.status == BlockStatus.normal:
            if mine.value:
                mine.status = BlockStatus.喵
                for m in self.blocks:
                    if m.value == 1:
                        m.image = load_image('resources/mine.bmp', SIZE)
                mine.image = load_image('resources/喵.bmp', SIZE)
                return False

            mine.status = BlockStatus.opened
            around = self.get_around(r, c)
            sum = 0
            for a, b in around:
                if self.getmine(a, b).value == 1:
                    sum += 1
            mine.around_mine_count = sum

            if sum == 0:
                mine.image = load_image('resources/blank.bmp', SIZE)
                for a, b in around:
                    if self.getmine(a, b).around_mine_count == -1:
                        self.open_mine(a, b)
            elif mine.around_mine_count == -1:
                pass
            else:
                mine.image = load_image(f"resources/{mine.around_mine_count}.bmp", SIZE)
            return True





class BlockStatus(Enum):
    normal = 1  # 未点击
    opened = 2  # 已点击
    mine = 3  # 地雷
    flag = 4  # 旗子
    ask = 5  # 问号
    喵 = 6  # 踩中地雷


class GameStatus(Enum):
    ready = 1
    ststus = 2
    over = 3
    win = 4


def print_text(s, font, x, y, text, fcolor=(255, 255, 255)):
    imgtext = font.render(text, True, fcolor)
    s.blit(imgtext, (x, y))


def main():
    screen = pygame.display.set_mode((S_WIDTH, S_HEIGHT))
    pygame.display.set_caption('扫雷')
    block = Mineblock()
    font1 = pygame.font.Font('resources/msyh.ttf', SIZE*2)
    fwidth, fheight = font1.size('999')  # 具体文本的内容999的宽和高

    n = 40

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEBUTTONDOWN:
                mouse_x, mouse_y = event.pos
                column = mouse_x // SIZE
                row = mouse_y // SIZE - 2
                b1, b2, b3 = pygame.mouse.get_pressed()
            elif event.type == MOUSEBUTTONUP:
                mine = block.getmine(row, column)
                if b1 and not b3:
                    if not block.open_mine(row, column):
                        game_status = GameStatus.over
                        break
                elif not b1 and b3:
                    if mine.status == BlockStatus.normal:
                        mine.status = BlockStatus.flag
                        mine.image = load_image("resources/flag.bmp", SIZE)
                        n -= 1
                    elif mine.status == BlockStatus.flag:
                        mine.status = BlockStatus.ask
                        mine.image = load_image("resources/ask.bmp", SIZE)
                        n += 1
                    elif mine.status == BlockStatus.ask:
                        mine.status = BlockStatus.normal
                        mine.image = load_image("resources/normal.bmp", SIZE)

        screen.fill((255, 255, 255))
        for mine in block.blocks:
            screen.blit(mine.image, mine.pos)
        face_size = int(SIZE*1.25)
        img_face_normal = load_image('resources/face_normal.bmp', face_size)
        img_face_fail = load_image('resources/face_fail.bmp', face_size)
        img_face_win = load_image('resources/face_win.bmp', face_size)
        face_pos_x = (S_WIDTH-face_size)//2
        face_pos_y = (SIZE*2-face_size)//2
        screen.blit(img_face_normal, (face_pos_x, face_pos_y))

        print_text(screen, font1, 25, (SIZE-fheight)//2+10, '000', (0, 0, 0))
        print_text(screen, font1, 500, (SIZE-fheight)//2+10, str(n), (0, 0, 0))

        pygame.display.update()


if __name__ == '__main__':
    main()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
RedCiciRedCici

imgsrc="https://static.codemao.cn/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E7%82%B9%E8%B5%9E.gif"alt="emotion_编程猫_点赞"

点赞0


评论


去玩阿做去玩阿做

https://shequ.codemao.cn/community/1627222

点赞0


评论