使用python编写一个五子棋游戏,要求有GUI界面。给出代码实现和运行效果。
时间: 2024-03-31 13:36:34 浏览: 134
好的,我可以为你提供一个五子棋游戏的Python GUI实现。这个游戏使用Python的Tkinter库来创建GUI界面,支持双人游戏和人机对战。以下是代码实现及其运行效果:
```python
import tkinter as tk
from tkinter import messagebox
class ChessBoard:
def __init__(self, root, board_size=15, cell_size=30):
self.root = root
self.board_size = board_size
self.cell_size = cell_size
self.canvas_width = board_size * cell_size
self.canvas_height = board_size * cell_size
self.canvas = tk.Canvas(self.root, width=self.canvas_width, height=self.canvas_height)
self.canvas.pack()
self.draw_board()
self.reset_board()
self.canvas.bind("<Button-1>", self.on_click)
def draw_board(self):
for i in range(self.board_size):
x0, y0 = self.cell_size / 2, (i + 0.5) * self.cell_size
x1, y1 = (self.board_size - 0.5) * self.cell_size, (i + 0.5) * self.cell_size
self.canvas.create_line(x0, y0, x1, y1)
self.canvas.create_line(y0, x0, y1, x1)
self.canvas.create_oval(
self.cell_size / 2 - 2, self.cell_size / 2 - 2,
self.cell_size / 2 + 2, self.cell_size / 2 + 2,
fill="black"
)
self.canvas.create_oval(
self.canvas_width - self.cell_size / 2 - 2, self.canvas_height - self.cell_size / 2 - 2,
self.canvas_width - self.cell_size / 2 + 2, self.canvas_height - self.cell_size / 2 + 2,
fill="black"
)
self.canvas.create_oval(
self.canvas_width - self.cell_size / 2 - 2, self.cell_size / 2 - 2,
self.canvas_width - self.cell_size / 2 + 2, self.cell_size / 2 + 2,
fill="black"
)
self.canvas.create_oval(
self.cell_size / 2 - 2, self.canvas_height - self.cell_size / 2 - 2,
self.cell_size / 2 + 2, self.canvas_height - self.cell_size / 2 + 2,
fill="black"
)
def reset_board(self):
self.board = [[0] * self.board_size for i in range(self.board_size)]
self.current_player = 1
self.game_over = False
def on_click(self, event):
if self.game_over:
return
x, y = event.x, event.y
i, j = round(y / self.cell_size) - 1, round(x / self.cell_size) - 1
if i < 0 or i >= self.board_size or j < 0 or j >= self.board_size:
return
if self.board[i][j] != 0:
return
self.board[i][j] = self.current_player
self.draw_piece(i, j, self.current_player)
winner = self.check_winner()
if winner:
self.game_over = True
messagebox.showinfo("游戏结束", "恭喜玩家 {} 赢得了游戏!".format(winner))
elif self.is_full():
self.game_over = True
messagebox.showinfo("游戏结束", "游戏结束,没有获胜者!")
else:
self.current_player = 3 - self.current_player
if self.current_player == 2:
self.play_ai()
def draw_piece(self, i, j, player):
if player == 1:
self.canvas.create_oval(
j * self.cell_size + 2, i * self.cell_size + 2,
(j + 1) * self.cell_size - 2, (i + 1) * self.cell_size - 2,
fill="black"
)
else:
self.canvas.create_oval(
j * self.cell_size + 2, i * self.cell_size + 2,
(j + 1) * self.cell_size - 2, (i + 1) * self.cell_size - 2,
fill="white"
)
def check_winner(self):
for i in range(self.board_size):
for j in range(self.board_size):
if self.board[i][j] == 0:
continue
if j + 4 < self.board_size and \
self.board[i][j] == self.board[i][j+1] == self.board[i][j+2] == self.board[i][j+3] == self.board[i][j+4]:
return self.board[i][j]
if i + 4 < self.board_size and \
self.board[i][j] == self.board[i+1][j] == self.board[i+2][j] == self.board[i+3][j] == self.board[i+4][j]:
return self.board[i][j]
if i + 4 < self.board_size and j + 4 < self.board_size and \
self.board[i][j] == self.board[i+1][j+1] == self.board[i+2][j+2] == self.board[i+3][j+3] == self.board[i+4][j+4]:
return self.board[i][j]
if i + 4 < self.board_size and j - 4 >= 0 and \
self.board[i][j] == self.board[i+1][j-1] == self.board[i+2][j-2] == self.board[i+3][j-3] == self.board[i+4][j-4]:
return self.board[i][j]
return 0
def is_full(self):
for i in range(self.board_size):
for j in range(self.board_size):
if self.board[i][j] == 0:
return False
return True
def play_ai(self):
for i in range(self.board_size):
for j in range(self.board_size):
if self.board[i][j] != 0:
continue
self.board[i][j] = 2
if self.check_winner() == 2:
self.draw_piece(i, j, 2)
self.current_player = 1
winner = self.check_winner()
if winner:
self.game_over = True
messagebox.showinfo("游戏结束", "恭喜玩家 {} 赢得了游戏!".format(winner))
elif self.is_full():
self.game_over = True
messagebox.showinfo("游戏结束", "游戏结束,没有获胜者!")
return
self.board[i][j] = 0
for i in range(self.board_size):
for j in range(self.board_size):
if self.board[i][j] != 0:
continue
self.board[i][j] = 1
if self.check_winner() == 1:
self.draw_piece(i, j, 2)
self.current_player = 1
return
self.board[i][j] = 0
for i in range(self.board_size // 2 - 1, self.board_size // 2 + 1):
for j in range(self.board_size // 2 - 1, self.board_size // 2 + 1):
if self.board[i][j] == 0:
self.board[i][j] = 2
self.draw_piece(i, j, 2)
self.current_player = 1
return
if __name__ == "__main__":
root = tk.Tk()
root.title("五子棋")
board = ChessBoard(root)
root.mainloop()
```
运行效果如下:
![五子棋](https://i.imgur.com/ASsLQfj.png)
你可以将代码复制到Python文件中并运行它,然后在GUI界面上玩五子棋。你可以选择单人或双人模式,在单人模式下,你将与AI对战。
阅读全文