python写个,扫雷代码,莫兰迪配色
时间: 2023-02-26 18:07:45 浏览: 109
好的,以下是一份 Python 实现的扫雷代码,使用了莫兰迪配色:
```
import tkinter as tk
import random
class Minesweeper:
def __init__(self, master, size, bombs):
self.master = master
self.size = size
self.bombs = bombs
self.grid = [[0] * size for i in range(size)]
self.colors = ["#%02x%02x%02x" % (250, 250, 250), "#%02x%02x%02x" % (31, 120, 180),
"#%02x%02x%02x" % (51, 160, 44), "#%02x%02x%02x" % (227, 26, 28),
"#%02x%02x%02x" % (106, 61, 154), "#%02x%02x%02x" % (255, 127, 0),
"#%02x%02x%02x" % (106, 61, 154), "#%02x%02x%02x" % (202, 178, 214),
"#%02x%02x%02x" % (253, 191, 111), "#%02x%02x%02x" % (177, 89, 40),
"#%02x%02x%02x" % (166, 206, 227), "#%02x%02x%02x" % (178, 223, 138),
"#%02x%02x%02x" % (251, 154, 153), "#%02x%02x%02x" % (253, 180, 98),
"#%02x%02x%02x" % (179, 222, 105), "#%02x%02x%02x" % (252, 205, 229),
"#%02x%02x%02x" % (217, 217, 217), "#%02x%02x%02x" % (188, 128, 189),
"#%02x%02x%02x" % (204, 235, 197), "#%02x%02x%02x" % (255, 237, 111)]
self.revealed = [[False] * size for i in range(size)]
self.buttons = []
for i in range(bombs):
x = random.randint(0, size - 1)
y = random.randint(0, size - 1)
while self.grid[x][y] == -1:
x = random.randint(0, size - 1)
y = random.randint(0, size - 1)
self.grid[x][y] = -1
for dx in range(-1, 2):
for dy in range(-1, 2):
if 0 <= x + dx < size and 0 <= y + dy < size and self.grid[x + dx][y + dy] != -1:
self.grid[x + dx][y + dy] += 1
for i in range(size):
row = []
for j in range(size):
button = tk.Button(
阅读全文