ftwfwf 2023-10-22 20:13:48

难以描述

# 导入必要的模块
import pygame
import random
import tkinter as tk
import time
import os
import random
import threading  

  

# 设定游戏板大小和地雷数量
BOARD_SIZE = (8, 8)
MINE_COUNT = 10

# 初始化 Pygame
pygame.init()

# 创建游戏窗口
screen_width = BOARD_SIZE[0] * 50
screen_height = BOARD_SIZE[1] * 50
screen = pygame.display.set_mode((screen_width, screen_height))

# 定义颜色
GRAY = (100, 200, 200)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# 创建游戏板
board = []
for x in range(BOARD_SIZE[0]):
    row = []
    for y in range(BOARD_SIZE[1]):
        row.append({'is_mine': False, 'revealed': False, 'flagged': False, 'adjacent_mines': 0})
    board.append(row)

# 添加地雷
mine_count = 0
while mine_count < MINE_COUNT:
    x = random.randint(0, BOARD_SIZE[0] - 1)
    y = random.randint(0, BOARD_SIZE[1] - 1)
    if not board[x][y]['is_mine']:
        board[x][y]['is_mine'] = True
        mine_count += 1

# 计算周围的地雷数量
for x in range(BOARD_SIZE[0]):
    for y in range(BOARD_SIZE[1]):
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                if dx == 0 and dy == 0:
                    continue
                x2 = x + dx
                y2 = y + dy
                if x2 < 0 or x2 >= BOARD_SIZE[0] or y2 < 0 or y2 >= BOARD_SIZE[1]:
                    continue
                if board[x2][y2]['is_mine']:
                    board[x][y]['adjacent_mines'] += 1

# 定义函数来显示方块
def draw_square(x, y):
    square_rect = pygame.Rect(x * 50, y * 50, 50, 50)
    square_color = GRAY
    square_border_color = BLACK
    if board[x][y]['revealed']:
        square_color = WHITE
        if board[x][y]['is_mine']:
            square_color = BLACK
        if board[x][y]['adjacent_mines'] > 0 and not board[x][y]['is_mine']:
            font = pygame.font.Font(None, 40)
            text = font.render(str(board[x][y]['adjacent_mines']), 1, BLACK)
            text_rect = text.get_rect(center=square_rect.center)
            screen.blit(text, text_rect)
    if board[x][y]['flagged']:
        font = pygame.font.Font(None, 40)
        text = font.render("F", 1, BLACK)
        text_rect = text.get_rect(center=square_rect.center)
        screen.blit(text, text_rect)
    pygame.draw.rect(screen, square_color, square_rect)
    pygame.draw.rect(screen, square_border_color, square_rect, 3)

# 定义函数来揭示方块并检查游戏是否结束
def reveal(x, y):
    if board[x][y]['revealed']:
        return
    board[x][y]['revealed'] = True
    if board[x][y]['is_mine']:
        print("游戏结束!你输了!")
        pygame.quit()
        return
    if board[x][y]['adjacent_mines'] == 0:
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                if dx == 0 and dy == 0:
                    continue
                x2 = x + dx
                y2 = y + dy
                if x2 < 0 or x2 >= BOARD_SIZE[0] or y2 < 0 or y2 >= BOARD_SIZE[1]:
                    continue
                reveal(x2, y2)
    if all([all([square['revealed'] or square['is_mine'] for square in row]) for row in board]):
        print("游戏结束!你赢了!")
        pygame.quit()
        return

# 循环游戏
while True:
    # 检查事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            x, y = event.pos[0] // 50, event.pos[1] // 50
            if event.button == 1:
                reveal(x, y)
            elif event.button == 3:
                board[x][y]['flagged'] = not board[x][y]['flagged']

    # 绘制游戏板
    for x in range(BOARD_SIZE[0]):
        for y in range(BOARD_SIZE[1]):
            draw_square(x, y)

    # 刷新屏幕
    pygame.display.flip()

评论

Home - Wiki
Copyright © 2011-2024 iteam. Current version is 2.137.1. UTC+08:00, 2024-11-21 01:44
浙ICP备14020137号-1 $Map of visitor$