用户:
余志翔3查看:0 回复:0 评论:0 创建时间:2023-04-22T14:17:31
【作品展示】

【作品介绍】
用ChatGPT做的
【作品源代码】
import tkinter as tk
import random
class MineSweeper:
def __init__(self, master, rows=10, columns=10, mines=10):
self.master = master
self.rows = rows
self.columns = columns
self.mines = mines
self.board = [[None for c in range(columns)] for r in range(rows)]
self.state = [[None for c in range(columns)] for r in range(rows)]
self.buttons = [[None for c in range(columns)] for r in range(rows)]
self.create_widgets()
self.place_mines()
def create_widgets(self):
frame = tk.Frame(self.master)
frame.pack()
self.status_text = tk.StringVar()
self.status_text.set(
"Number of Mines Remaining: {}".format(self.mines))
status_label = tk.Label(frame, textvariable=self.status_text)
status_label.pack()
self.restart_button = tk.Button(
frame, text="Restart", command=self.restart)
self.restart_button.pack()
self.board_frame = tk.Frame(self.master)
self.board_frame.pack()
for r in range(self.rows):
for c in range(self.columns):
button = tk.Button(
self.board_frame, width=2, height=1, command=lambda r=r, c=c: self.click(r, c))
button.grid(row=r, column=c)
self.buttons[r][c] = button
def place_mines(self):
locations = random.sample(range(self.rows * self.columns), self.mines)
for loc in locations:
r = loc // self.columns
c = loc % self.columns
self.board[r][c] = "mine"
def restart(self):
self.__init__(self.master, self.rows, self.columns, self.mines)
def click(self, r, c):
if not self.board[r][c]:
self.buttons[r][c]["text"] = self.neighbor_mines(r, c)
self.state[r][c] = "revealed"
if self.neighbor_mines(r, c) == 0:
self.reveal_neighbors(r, c)
elif self.board[r][c] == "mine":
self.game_over()
else:
self.buttons[r][c]["text"] = self.board[r][c]
self.state[r][c] = "revealed"
if self.won_game():
self.game_won()
def neighbor_mines(self, r, c):
count = 0
for row in range(max(r-1, 0), min(r+2, self.rows)):
for col in range(max(c-1, 0), min(c+2, self.columns)):
if self.board[row][col] == "mine":
count += 1
return count
def reveal_neighbors(self, r, c):
for row in range(max(r-1, 0), min(r+2, self.rows)):
for col in range(max(c-1, 0), min(c+2, self.columns)):
if not self.state[row][col]:
self.click(row, col)
def game_over(self):
for r in range(self.rows):
for c in range(self.columns):
if self.board[r][c] == "mine":
self.buttons[r][c]["text"] = "*"
def won_game(self):
for r in range(self.rows):
for c in range(self.columns):
if not self.state[r][c] and self.board[r][c] != "mine":
return False
return True
def game_won(self):
self.status_text.set("You won the game!")
for r in range(self.rows):
for c in range(self.columns):
if self.board[r][c] == "mine":
self.buttons[r][c]["text"] = "*"
for r in range(self.rows):
for c in range(self.columns):
self.buttons[r][c]["state"] = "disabled"
def main():
root = tk.Tk()
root.title("Mine Sweeper")
game = MineSweeper(root)
root.mainloop()
if __name__ == "__main__":
main()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn