Python制作扫雷
时间: 2023-11-19 19:57:45 浏览: 35
以下是使用Python和Tkinter模块制作扫雷游戏的步骤:
1.导入Tkinter模块和random模块
```python
import tkinter as tk
import random
```
2.创建主窗口并设置标题
```python
root = tk.Tk()
root.title("扫雷游戏")
```
3.创建游戏区域
```python
frame_game = tk.Frame(root)
frame_game.pack()
```
4.创建游戏地图
```python
map = []
for i in range(10):
row = []
for j in range(10):
row.append(0)
map.append(row)
```
5.随机埋雷
```python
for i in range(10):
x = random.randint(0, 9)
y = random.randint(0, 9)
map[x][y] = -1
```
6.计算雷的数量
```python
for i in range(10):
for j in range(10):
if map[i][j] == -1:
continue
count = 0
if i > 0 and j > 0 and map[i-1][j-1] == -1:
count += 1
if i > 0 and map[i-1][j] == -1:
count += 1
if i > 0 and j < 9 and map[i-1][j+1] == -1:
count += 1
if j > 0 and map[i][j-1] == -1:
count += 1
if j < 9 and map[i][j+1] == -1:
count += 1
if i < 9 and j > 0 and map[i+1][j-1] == -1:
count += 1
if i < 9 and map[i+1][j] == -1:
count += 1
if i < 9 and j < 9 and map[i+1][j+1] == -1:
count += 1
map[i][j] = count
```
7.创建按钮并显示地图
```python
buttons = []
for i in range(10):
row = []
for j in range(10):
button = tk.Button(frame_game, width=2, height=1, command=lambda x=i, y=j: click(x, y))
button.grid(row=i, column=j)
row.append(button)
buttons.append(row)
def show_map():
for i in range(10):
for j in range(10):
if map[i][j] == -1:
buttons[i][j].config(text="*", state="disabled")
else:
buttons[i][j].config(text=str(map[i][j]))
show_map()
```
8.创建点击事件
```python
def click(x, y):
if map[x][y] == -1:
buttons[x][y].config(text="*", state="disabled")
tk.messagebox.showinfo("游戏结束", "你输了!")
else:
buttons[x][y].config(text=str(map[x][y]), state="disabled")
```
完整代码如下:
```python
import tkinter as tk
import random
root = tk.Tk()
root.title("扫雷游戏")
frame_game = tk.Frame(root)
frame_game.pack()
map = []
for i in range(10):
row = []
for j in range(10):
row.append(0)
map.append(row)
for i in range(10):
x = random.randint(0, 9)
y = random.randint(0, 9)
map[x][y] = -1
for i in range(10):
for j in range(10):
if map[i][j] == -1:
continue
count = 0
if i > 0 and j > 0 and map[i-1][j-1] == -1:
count += 1
if i > 0 and map[i-1][j] == -1:
count += 1
if i > 0 and j < 9 and map[i-1][j+1] == -1:
count += 1
if j > 0 and map[i][j-1] == -1:
count += 1
if j < 9 and map[i][j+1] == -1:
count += 1
if i < 9 and j > 0 and map[i+1][j-1] == -1:
count += 1
if i < 9 and map[i+1][j] == -1:
count += 1
if i < 9 and j < 9 and map[i+1][j+1] == -1:
count += 1
map[i][j] = count
buttons = []
for i in range(10):
row = []
for j in range(10):
button = tk.Button(frame_game, width=2, height=1, command=lambda x=i, y=j: click(x, y))
button.grid(row=i, column=j)
row.append(button)
buttons.append(row)
def show_map():
for i in range(10):
for j in range(10):
if map[i][j] == -1:
buttons[i][j].config(text="*", state="disabled")
else:
buttons[i][j].config(text=str(map[i][j]))
show_map()
def click(x, y):
if map[x][y] == -1:
buttons[x][y].config(text="*", state="disabled")
tk.messagebox.showinfo("游戏结束", "你输了!")
else:
buttons[x][y].config(text=str(map[x][y]), state="disabled")
root.mainloop()
```