需要使用tkinter完成五子棋小游戏的源代码
时间: 2024-01-01 14:05:05 浏览: 77
好的,这是一个简单的基于tkinter的五子棋小游戏的源代码,供您参考:
```python
import tkinter as tk
from tkinter import messagebox
class ChessBoard(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.row, self.col = 15, 15 # 棋盘大小
self.color = 'black' # 先手执黑棋
self.is_win = False # 是否获胜
self.is_draw = False # 是否和棋
self.create_widgets()
def create_widgets(self):
self.buttons = []
for i in range(self.row):
row_buttons = []
for j in range(self.col):
button = tk.Button(self, bd=1, width=2, height=1, bg='burlywood', command=lambda x=i, y=j: self.click(x, y))
button.grid(row=i, column=j)
row_buttons.append(button)
self.buttons.append(row_buttons)
self.status_label = tk.Label(self, text="轮到黑棋", font=('Arial', 12), bg='white', fg='black', width=18, height=2)
self.status_label.grid(row=self.row, column=0, columnspan=self.col)
self.reset_button = tk.Button(self, text='重新开始', font=('Arial', 12), bg='white', fg='black', width=10, height=2, command=self.reset)
self.reset_button.grid(row=self.row+1, column=0, columnspan=self.col)
def click(self, row, col):
if self.is_win or self.is_draw:
return
button = self.buttons[row][col]
if button['text'] != '':
return
button['text'] = self.color[0].upper()
button['bg'] = self.color
if self.check_win(row, col):
self.is_win = True
messagebox.showinfo('游戏结束', f'{self.color}方获胜!')
return
if self.check_draw():
self.is_draw = True
messagebox.showinfo('游戏结束', '和棋!')
return
self.color = 'white' if self.color == 'black' else 'black'
self.status_label['text'] = f'轮到{self.color}棋'
def check_win(self, row, col):
count = 0
for i in range(max(row-4, 0), min(row+5, self.row)):
if self.buttons[i][col]['text'] == self.color[0].upper():
count += 1
else:
count = 0
if count == 5:
return True
count = 0
for j in range(max(col-4, 0), min(col+5, self.col)):
if self.buttons[row][j]['text'] == self.color[0].upper():
count += 1
else:
count = 0
if count == 5:
return True
count = 0
for i, j in zip(range(max(row-col, 0), min(row-col+5, self.row)), range(max(col-row, 0), min(col-row+5, self.col))):
if self.buttons[i][j]['text'] == self.color[0].upper():
count += 1
else:
count = 0
if count == 5:
return True
count = 0
for i, j in zip(range(max(row+self.col-col-1, 0), min(row+self.col-col+5, self.row)), range(max(col-self.row+row+1, 0), min(col-self.row+row-5+self.col, self.col))):
if self.buttons[i][j]['text'] == self.color[0].upper():
count += 1
else:
count = 0
if count == 5:
return True
return False
def check_draw(self):
for i in range(self.row):
for j in range(self.col):
if self.buttons[i][j]['text'] == '':
return False
return True
def reset(self):
self.is_win = False
self.is_draw = False
self.color = 'black'
self.status_label['text'] = '轮到黑棋'
for i in range(self.row):
for j in range(self.col):
self.buttons[i][j]['text'] = ''
self.buttons[i][j]['bg'] = 'burlywood'
if __name__ == '__main__':
root = tk.Tk()
root.title('五子棋')
root.geometry('300x350')
ChessBoard(root).pack(side='top', fill='both', expand=True)
root.mainloop()
```
这个程序使用了tkinter库来实现GUI界面,实现了一个简单的五子棋游戏。希望对您有所帮助!
阅读全文