简单的Python小游戏之扫雷

概要

做了个简单的python小游戏–扫雷
写的比较简陋希望大家多多指教
简单的Python小游戏之扫雷_第1张图片
简单的Python小游戏之扫雷_第2张图片

代码

import tkinter as tk
import random
from tkinter import messagebox

class Minesweeper:
    def __init__(self, root, rows, cols, mines):
        self.root = root
        self.rows = rows
        self.cols = cols
        self.mines = mines
        self.board = [[' ' for _ in range(cols)] for _ in range(rows) ]  # 游戏板,存储地雷和数字
        self.buttons = [[None for _ in range(cols)] for _ in range(rows)]  # 存储按钮对象
        self.mine_coords = []  # 存储地雷坐标
        self.flagged = []  # 存储标记的地雷坐标
        self.revealed = []  # 存储已揭示的坐标
        self.first_click = True  # 标记是否是第一次点击
        self.game_over = False  # 标记游戏是否结束

        self.create_mines()  # 创建地雷分布
        self.create_gui()  # 创建GUI界面

    def create_mines(self):
        # 随机选择地雷坐标
        self.mine_coords = random.sample(
            [(i, j) for i in range(self.rows) for j in range(self.cols)], self.mines)
        for row, col in self.mine_coords:
            self.board[row][col] = 'X'  # 在游戏板上标记地雷

    def create_gui(self):
        for row in range(self.rows):
            for col in range(self.cols):
                button = tk.Button(self.root, text=' ', width=2, height=1, bg='light gray', fg='black')
                button.grid(row=row, column=col)
                # 左键点击绑定到left_click函数,右键点击绑定到right_click函数
                button.bind('', lambda event, r=row, c=col: self.left_click(r, c))
                button.bind('', lambda event, r=row, c=col: self.right_click(r, c))
                self.buttons[row][col] = button  # 存储按钮对象


    def left_click(self, row, col):
        if self.game_over or (row, col) in self.revealed:
            return

        if self.first_click:
            while (row, col) in self.mine_coords:
                self.create_mines()
            self.first_click = False

        if (row, col) in self.mine_coords:
            self.explode_mines()
        else:
            count = self.count_adjacent_mines(row, col)
            if count > 0:
                self.buttons[row][col].config(text=str(count), state=tk.DISABLED, bg='white')
                self.board[row][col] = str(count)
            else:
                self.reveal_empty_cells(row, col)
        self.revealed.append((row, col))
        if self.check_win():
            self.game_win()

    def right_click(self, row, col):
        if self.game_over or (row, col) in self.revealed:
            return

        if (row, col) not in self.flagged:
            self.flagged.append((row, col))
            self.buttons[row][col].config(text='F', state=tk.DISABLED, bg='yellow')
        else:
            self.flagged.remove((row, col))
            self.buttons[row][col].config(text=' ', state=tk.NORMAL, bg='light gray')

    def count_adjacent_mines(self, row, col):
        count = 0
        for i in range(-1, 2):
            for j in range(-1, 2):
                if 0 <= row + i < self.rows and 0 <= col + j < self.cols and self.board[row + i][col + j] == 'X':
                    count += 1
        return count

    def reveal_empty_cells(self, row, col):
        if self.board[row][col] == ' ':
            self.buttons[row][col].config(state=tk.DISABLED, bg='white')
            self.board[row][col] = '0'
            for i in range(-1, 2):
                for j in range(-1, 2):
                    if 0 <= row + i < self.rows and 0 <= col + j < self.cols:
                        if (row + i, col + j) not in self.revealed:
                            self.left_click(row + i, col + j)

    def check_win(self):
        for row in range(self.rows):
            for col in range(self.cols):
                if self.board[row][col] != 'X' and (row, col) not in self.revealed:
                    return False
        return True

    def explode_mines(self):
        for row, col in self.mine_coords:
            self.buttons[row][col].config(text='*', state=tk.DISABLED, bg='red')
        self.game_over = True
        result = messagebox.askquestion("结果", "你输了!再来一把?")
        if result == "yes":
            self.reset_game()
        else:
            self.root.quit()  # 关闭应用程序

    def game_win(self):
        for row in range(self.rows):
            for col in range(self.cols):
                if (row, col) in self.mine_coords:
                    self.buttons[row][col].config(text='X', state=tk.DISABLED, bg='green')
                else:
                    self.buttons[row][col].config(state=tk.DISABLED, bg='white')
        self.game_over = True
        result = messagebox.askquestion("结果", "你赢了!再来一把?")
        if result == "yes":
            self.reset_game()
        else:
            self.root.quit()  # 关闭应用程序

    def reset_game(self):
        self.mine_coords = []
        self.flagged = []
        self.revealed = []
        self.first_click = True
        self.game_over = False
        for row in range(self.rows):
            for col in range(self.cols):
                self.buttons[row][col].config(text=' ', state=tk.NORMAL, bg='light gray')
                self.board[row][col] = ' '
        self.create_mines()
        self.create_gui()

if __name__ == "__main__":
    rows = 10
    cols = 15
    mines = 15

    root = tk.Tk()
    root.title("Minesweeper")
    game = Minesweeper(root, rows, cols, mines)
    root.mainloop()```

## 总结
给博主点个赞吧

你可能感兴趣的:(python,开发语言,游戏)