python不用pygame制作扫雷,要求有左键点开,右键标记的功能
时间: 2024-02-15 17:01:31 浏览: 68
可以使用Python的tkinter模块来制作扫雷游戏,实现左键点击打开方格,右键标记方格的功能。
以下是一个简单的示例代码:
```python
import tkinter as tk
import random
# 游戏参数
ROWS = 10
COLS = 10
MINES = 10
SIZE = 30
class Cell:
def __init__(self, row, col):
self.row = row
self.col = col
self.is_mine = False
self.is_open = False
self.is_flag = False
self.adjacent_mines = 0
class Minesweeper:
def __init__(self):
self.root = tk.Tk()
self.root.title("Minesweeper")
self.cells = [[Cell(row, col) for col in range(COLS)] for row in range(ROWS)]
self.mines = []
self.game_over = False
self.create_widgets()
self.root.mainloop()
def create_widgets(self):
self.canvas = tk.Canvas(self.root, width=COLS*SIZE, height=ROWS*SIZE)
self.canvas.pack()
self.canvas.bind("<Button-1>", self.on_left_click)
self.canvas.bind("<Button-3>", self.on_right_click)
self.draw_grid()
def draw_grid(self):
self.canvas.delete("all")
for row in range(ROWS):
for col in range(COLS):
x1 = col * SIZE
y1 = row * SIZE
x2 = x1 + SIZE
y2 = y1 + SIZE
fill = "gray"
if self.cells[row][col].is_open:
fill = "white"
if self.cells[row][col].is_mine:
fill = "red"
elif self.cells[row][col].is_flag:
fill = "yellow"
self.canvas.create_rectangle(x1, y1, x2, y2, fill=fill)
if self.cells[row][col].is_open and not self.cells[row][col].is_mine and self.cells[row][col].adjacent_mines > 0:
self.canvas.create_text(x1+SIZE//2, y1+SIZE//2, text=str(self.cells[row][col].adjacent_mines))
def on_left_click(self, event):
if self.game_over:
return
row = event.y // SIZE
col = event.x // SIZE
if not self.cells[row][col].is_open and not self.cells[row][col].is_flag:
self.cells[row][col].is_open = True
if self.cells[row][col].is_mine:
self.game_over = True
else:
self.check_adjacent_cells(row, col)
self.check_win()
self.draw_grid()
def on_right_click(self, event):
if self.game_over:
return
row = event.y // SIZE
col = event.x // SIZE
if not self.cells[row][col].is_open:
self.cells[row][col].is_flag = not self.cells[row][col].is_flag
self.draw_grid()
def check_adjacent_cells(self, row, col):
adjacent_cells = [(r, c) for r in range(max(row-1,0), min(row+2,ROWS)) for c in range(max(col-1,0), min(col+2,COLS)) if not (r==row and c==col)]
for r, c in adjacent_cells:
if not self.cells[r][c].is_open and not self.cells[r][c].is_flag:
self.cells[r][c].is_open = True
if self.cells[r][c].is_mine:
self.game_over = True
return
elif self.cells[r][c].adjacent_mines == 0:
self.check_adjacent_cells(r, c)
def check_win(self):
for row in range(ROWS):
for col in range(COLS):
if not self.cells[row][col].is_open and not self.cells[row][col].is_mine:
return
self.game_over = True
self.draw_grid()
tk.messagebox.showinfo("Minesweeper", "You win!")
def generate_mines(self, row, col):
self.mines = random.sample([(r, c) for r in range(ROWS) for c in range(COLS) if not (r==row and c==col)], MINES)
for row, col in self.mines:
self.cells[row][col].is_mine = True
for row in range(ROWS):
for col in range(COLS):
if not self.cells[row][col].is_mine:
self.cells[row][col].adjacent_mines = sum([1 for r, c in self.mines if abs(row-r)<=1 and abs(col-c)<=1])
def new_game(self):
self.game_over = False
self.mines = []
for row in range(ROWS):
for col in range(COLS):
self.cells[row][col].is_mine = False
self.cells[row][col].is_open = False
self.cells[row][col].is_flag = False
self.cells[row][col].adjacent_mines = 0
self.draw_grid()
self.root.after(1000, self.generate_mines, -1, -1)
if __name__ == "__main__":
game = Minesweeper()
```
这个示例代码实现了基本的扫雷游戏功能,包括左键点击打开方格、右键标记方格,以及游戏结束后弹出提示框等。你可以根据自己的需求来修改代码,例如调整游戏参数、修改画布大小等。
阅读全文