python写一个鼠标点击操作的五子棋游戏
时间: 2023-07-13 21:38:13 浏览: 95
简易可鼠标点击五子棋(需要easyx)
好的,以下是一个使用 Pygame 库实现鼠标点击操作的五子棋游戏示例代码:
```python
import os
import pygame
# 初始化 Pygame 库
pygame.init()
# 设置窗口大小
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 480
SCREEN_SIZE = (WINDOW_WIDTH, WINDOW_HEIGHT)
# 设置棋盘大小
BOARD_SIZE = 400
START_X = (WINDOW_WIDTH - BOARD_SIZE) // 2
START_Y = (WINDOW_HEIGHT - BOARD_SIZE) // 2
LINE_WIDTH = 2
LINE_COLOR = (0, 0, 0)
CELL_SIZE = BOARD_SIZE // 14
# 设置棋子半径和颜色
STONE_RADIUS = CELL_SIZE // 2 - 2
BLACK_COLOR = (0, 0, 0)
WHITE_COLOR = (255, 255, 255)
# 初始化棋盘
board = [[0 for i in range(15)] for j in range(15)]
# 初始化 Pygame 窗口
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption("五子棋游戏")
# 加载棋子图片
black_stone_image = pygame.image.load('black.png')
white_stone_image = pygame.image.load('white.png')
def draw_board():
# 画棋盘线
for i in range(15):
pygame.draw.line(screen, LINE_COLOR, (START_X, START_Y+i*CELL_SIZE), (START_X+BOARD_SIZE, START_Y+i*CELL_SIZE), LINE_WIDTH)
pygame.draw.line(screen, LINE_COLOR, (START_X+i*CELL_SIZE, START_Y), (START_X+i*CELL_SIZE, START_Y+BOARD_SIZE), LINE_WIDTH)
# 画星位
for i in [3, 7, 11]:
for j in [3, 7, 11]:
x = START_X + i * CELL_SIZE
y = START_Y + j * CELL_SIZE
pygame.draw.circle(screen, LINE_COLOR, (x, y), 5)
def draw_stone(x, y, color):
# 计算棋子中心位置
center_x = START_X + x * CELL_SIZE
center_y = START_Y + y * CELL_SIZE
# 根据颜色选择棋子图片
if color == 1:
stone_image = black_stone_image
else:
stone_image = white_stone_image
# 画棋子
screen.blit(stone_image, (center_x - STONE_RADIUS, center_y - STONE_RADIUS))
def is_win(x, y, color):
count = 1
i, j = x-1, y
while i >= 0 and board[i][j] == color:
count += 1
i -= 1
i, j = x+1, y
while i < 15 and board[i][j] == color:
count += 1
i += 1
if count >= 5:
return True
count = 1
i, j = x, y-1
while j >= 0 and board[i][j] == color:
count += 1
j -= 1
i, j = x, y+1
while j < 15 and board[i][j] == color:
count += 1
j += 1
if count >= 5:
return True
count = 1
i, j = x-1, y-1
while i >= 0 and j >= 0 and board[i][j] == color:
count += 1
i -= 1
j -= 1
i, j = x+1, y+1
while i < 15 and j < 15 and board[i][j] == color:
count += 1
i += 1
j += 1
if count >= 5:
return True
count = 1
i, j = x-1, y+1
while i >= 0 and j < 15 and board[i][j] == color:
count += 1
i -= 1
j += 1
i, j = x+1, y-1
while i < 15 and j >= 0 and board[i][j] == color:
count += 1
i += 1
j -= 1
if count >= 5:
return True
return False
# 游戏主循环
player = 1
game_over = False
clock = pygame.time.Clock()
while not game_over:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
# 获取鼠标点击的位置
x, y = event.pos
if x < START_X or x >= START_X + BOARD_SIZE or y < START_Y or y >= START_Y + BOARD_SIZE:
continue
# 计算落子的位置
i = (x - START_X) // CELL_SIZE
j = (y - START_Y) // CELL_SIZE
# 如果该位置已经有棋子了,则不能落子
if board[i][j] != 0:
continue
# 落子并判断是否获胜
board[i][j] = player
draw_stone(i, j, player)
if is_win(i, j, player):
print("Player {} Win!!".format(player))
game_over = True
# 切换玩家
player = 3 - player
# 画棋盘和棋子
draw_board()
for i in range(15):
for j in range(15):
if board[i][j] == 1:
draw_stone(i, j, 1)
elif board[i][j] == 2:
draw_stone(i, j, 2)
# 刷新屏幕
pygame.display.flip()
# 控制游戏帧率
clock.tick(30)
# 退出 Pygame 库
pygame.quit()
```
这个示例中,我们使用 Pygame 库来实现窗口和鼠标操作。我们首先设置了窗口大小和棋盘大小,然后定义了一些常量和变量,包括棋盘和棋子的尺寸、棋子的颜色、棋盘的二维列表、以及当前玩家等信息。
在游戏主循环中,我们处理了鼠标点击事件,并根据点击位置计算落子的位置。如果该位置已经有棋子了,则不能落子。如果落子后有一方获胜,则输出胜利信息并结束游戏。注意,我们使用了 `pygame.image.load()` 函数来加载棋子图片,并使用 `screen.blit()` 函数来画棋子。
最后,我们使用 Pygame 提供的函数来画棋盘和棋子,并使用 `pygame.display.flip()` 函数来刷新屏幕。我们还使用了 `pygame.time.Clock()` 函数来控制游戏的帧率,以避免过快或过慢的游戏速度。
阅读全文