python写一个五子棋游戏
时间: 2023-07-18 13:44:46 浏览: 47
下面是一个简单的五子棋游戏,使用 Python 语言实现。该游戏使用了 Pygame 库来创建窗口和处理用户输入。请确保您已经安装了 Pygame 库。
```python
import pygame
# 初始化 Pygame
pygame.init()
# 游戏窗口大小
window_size = (640, 640)
# 游戏棋盘大小
board_size = 600
# 每个格子的大小
cell_size = board_size // 15
# 创建游戏窗口
screen = pygame.display.set_mode(window_size)
# 设置窗口标题
pygame.display.set_caption("五子棋")
# 棋盘颜色
board_color = (186, 126, 82)
# 棋子颜色
player1_color = (0, 0, 0)
player2_color = (255, 255, 255)
# 当前玩家
current_player = 1
# 游戏棋盘状态
board = [[0 for i in range(15)] for j in range(15)]
# 绘制棋盘
def draw_board():
screen.fill(board_color)
for i in range(15):
pygame.draw.line(screen, (0, 0, 0), (cell_size // 2, cell_size // 2 + i * cell_size), (board_size - cell_size // 2, cell_size // 2 + i * cell_size), 2)
pygame.draw.line(screen, (0, 0, 0), (cell_size // 2 + i * cell_size, cell_size // 2), (cell_size // 2 + i * cell_size, board_size - cell_size // 2), 2)
for i in range(3):
for j in range(3):
pygame.draw.circle(screen, (0, 0, 0), ((3 + 4 * i) * cell_size, (3 + 4 * j) * cell_size), 5)
# 绘制棋子
def draw_piece(row, col):
if board[row][col] == 1:
pygame.draw.circle(screen, player1_color, (col * cell_size + cell_size // 2, row * cell_size + cell_size // 2), cell_size // 2 - 5)
elif board[row][col] == 2:
pygame.draw.circle(screen, player2_color, (col * cell_size + cell_size // 2, row * cell_size + cell_size // 2), cell_size // 2 - 5)
# 判断是否胜利
def check_win(row, col):
# 水平方向
count = 1
for i in range(col - 1, -1, -1):
if board[row][i] == current_player:
count += 1
else:
break
for i in range(col + 1, 15):
if board[row][i] == current_player:
count += 1
else:
break
if count >= 5:
return True
# 垂直方向
count = 1
for i in range(row - 1, -1, -1):
if board[i][col] == current_player:
count += 1
else:
break
for i in range(row + 1, 15):
if board[i][col] == current_player:
count += 1
else:
break
if count >= 5:
return True
# 左上到右下方向
count = 1
for i, j in zip(range(row - 1, -1, -1), range(col - 1, -1, -1)):
if board[i][j] == current_player:
count += 1
else:
break
for i, j in zip(range(row + 1, 15), range(col + 1, 15)):
if board[i][j] == current_player:
count += 1
else:
break
if count >= 5:
return True
# 左下到右上方向
count = 1
for i, j in zip(range(row - 1, -1, -1), range(col + 1, 15)):
if board[i][j] == current_player:
count += 1
else:
break
for i, j in zip(range(row + 1, 15), range(col - 1, -1, -1)):
if board[i][j] == current_player:
count += 1
else:
break
if count >= 5:
return True
return False
# 游戏主循环
running = True
while running:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
col = event.pos[0] // cell_size
row = event.pos[1] // cell_size
if board[row][col] == 0:
board[row][col] = current_player
draw_piece(row, col)
if check_win(row, col):
print("玩家 %d 胜利!" % current_player)
running = False
current_player = 3 - current_player
# 绘制棋盘和棋子
draw_board()
for i in range(15):
for j in range(15):
if board[i][j] != 0:
draw_piece(i, j)
# 更新屏幕
pygame.display.flip()
# 退出 Pygame
pygame.quit()
```
运行以上代码,您将看到一个具有图形界面的五子棋游戏。您可以通过单击鼠标来下棋,在游戏中进行互动。