编写实现使用鼠标点击的人机对战和双人对战的五子棋的完整python代码
时间: 2024-05-13 14:18:54 浏览: 124
Python Tkinter 模块编写的五子棋人机对战、双人对弈游戏
以下是实现使用鼠标点击的人机对战和双人对战的五子棋的完整Python代码:
```python
import pygame
# 初始化 Pygame
pygame.init()
# 设置游戏窗口大小
WINDOW_SIZE = (660, 660)
screen = pygame.display.set_mode(WINDOW_SIZE)
# 设置游戏窗口标题
pygame.display.set_caption("五子棋")
# 定义游戏棋盘大小和格子大小
BOARD_SIZE = 15
GRID_SIZE = 44
# 定义黑色和白色的棋子
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# 加载棋盘和棋子图片
board_image = pygame.image.load("board.png")
black_piece_image = pygame.image.load("black.png")
white_piece_image = pygame.image.load("white.png")
# 创建游戏棋盘
board = [[0] * BOARD_SIZE for i in range(BOARD_SIZE)]
# 定义游戏状态
GAME_MODE = "HUMAN_VS_AI" # 可选值为 "HUMAN_VS_HUMAN" 或 "HUMAN_VS_AI"
current_player = 1 # 当前玩家,1 表示黑棋,2 表示白棋
game_over = False # 游戏是否结束
winner = None # 胜者
# 创建 AI
AI_PLAYER = 2 # AI 所使用的棋子颜色
AI_THINKING_TIME = 1000 # AI 思考时间(单位:毫秒)
# TODO: 在这里编写 AI 的代码
def draw_board():
"""
绘制游戏棋盘
"""
screen.blit(board_image, (0, 0))
def draw_piece(row, col, piece_type):
"""
绘制棋子
"""
x = col * GRID_SIZE + GRID_SIZE // 2
y = row * GRID_SIZE + GRID_SIZE // 2
if piece_type == 1:
screen.blit(black_piece_image, (x - black_piece_image.get_width() // 2, y - black_piece_image.get_height() // 2))
elif piece_type == 2:
screen.blit(white_piece_image, (x - white_piece_image.get_width() // 2, y - white_piece_image.get_height() // 2))
def get_clicked_grid(mouse_pos):
"""
根据鼠标点击位置计算出对应的棋盘格子
"""
x, y = mouse_pos
row = y // GRID_SIZE
col = x // GRID_SIZE
if row >= BOARD_SIZE or col >= BOARD_SIZE:
return None
return row, col
def place_piece(row, col, piece_type):
"""
在棋盘上放置棋子
"""
if board[row][col] != 0:
return False
board[row][col] = piece_type
draw_piece(row, col, piece_type)
return True
def check_win():
"""
检查游戏是否结束
"""
global game_over, winner
# 检查横向连续五个棋子
for row in range(BOARD_SIZE):
for col in range(BOARD_SIZE - 4):
if board[row][col] == board[row][col + 1] == board[row][col + 2] == board[row][col + 3] == board[row][col + 4] != 0:
game_over = True
winner = board[row][col]
return
# 检查纵向连续五个棋子
for row in range(BOARD_SIZE - 4):
for col in range(BOARD_SIZE):
if board[row][col] == board[row + 1][col] == board[row + 2][col] == board[row + 3][col] == board[row + 4][col] != 0:
game_over = True
winner = board[row][col]
return
# 检查左上到右下斜线连续五个棋子
for row in range(BOARD_SIZE - 4):
for col in range(BOARD_SIZE - 4):
if board[row][col] == board[row + 1][col + 1] == board[row + 2][col + 2] == board[row + 3][col + 3] == board[row + 4][col + 4] != 0:
game_over = True
winner = board[row][col]
return
# 检查左下到右上斜线连续五个棋子
for row in range(4, BOARD_SIZE):
for col in range(BOARD_SIZE - 4):
if board[row][col] == board[row - 1][col + 1] == board[row - 2][col + 2] == board[row - 3][col + 3] == board[row - 4][col + 4] != 0:
game_over = True
winner = board[row][col]
return
# 检查是否平局
if all(all(row) for row in board):
game_over = True
def human_vs_human():
"""
人类对战模式
"""
global current_player
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
elif event.type == pygame.MOUSEBUTTONDOWN:
if current_player == 1:
piece_type = 1
else:
piece_type = 2
mouse_pos = pygame.mouse.get_pos()
grid_pos = get_clicked_grid(mouse_pos)
if grid_pos is not None:
row, col = grid_pos
if place_piece(row, col, piece_type):
check_win()
if game_over:
break
current_player = 3 - current_player
def human_vs_ai():
"""
人类对战 AI 模式
"""
global current_player
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
elif event.type == pygame.MOUSEBUTTONDOWN:
if current_player == 1:
piece_type = 1
else:
piece_type = 2
mouse_pos = pygame.mouse.get_pos()
grid_pos = get_clicked_grid(mouse_pos)
if grid_pos is not None:
row, col = grid_pos
if place_piece(row, col, piece_type):
check_win()
if game_over:
break
current_player = 3 - current_player
if current_player == AI_PLAYER:
# TODO: 在这里添加 AI 下棋的代码
pygame.time.wait(AI_THINKING_TIME)
# 随机找一个空格子放置棋子
while True:
row = random.randint(0, BOARD_SIZE - 1)
col = random.randint(0, BOARD_SIZE - 1)
if place_piece(row, col, AI_PLAYER):
check_win()
if game_over:
break
current_player = 3 - current_player
break
# 根据游戏模式选择不同的游戏逻辑
if GAME_MODE == "HUMAN_VS_HUMAN":
human_vs_human()
elif GAME_MODE == "HUMAN_VS_AI":
human_vs_ai()
# 游戏结束,显示胜者
if winner is None:
print("平局")
elif winner == 1:
print("黑棋胜利")
else:
print("白棋胜利")
# 退出 Pygame
pygame.quit()
```
注意,以上代码中的 AI 部分是空白的,需要根据具体的实现方式来填写。此外,还需要准备好棋盘和棋子的图片文件(board.png、black.png、white.png),并将它们放在与代码文件相同的目录下。
阅读全文