下面我详细讲解一下如何使用Python实现简单的五子棋游戏。
1. 准备工作
在开始之前,需要安装Python,并安装pygame
游戏库。可以使用以下命令安装:
pip install pygame
2. 创建游戏窗口
在Python中,使用pygame
库可以快速创建一个窗口。代码如下:
import pygame
# 初始化窗口
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("五子棋")
# 游戏循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
pygame.display.update()
运行这段代码,可以看到一个800x600的窗口被创建了出来,并显示了窗口标题。pygame.display.update()
是用来刷新窗口的方法,在后面的代码中也会使用。
3. 绘制棋盘
五子棋的棋盘是一个15x15的网格,我们可以通过pygame.draw
函数来绘制。代码如下:
# 绘制棋盘
BLACK = (0, 0, 0)
LINE_COLOR = (128, 128, 128)
GRID_SIZE = 40
MARGIN = 30
pygame.draw.rect(screen, (255, 255, 255), (0, 0, 800, 600))
for i in range(15):
pygame.draw.line(screen, LINE_COLOR, (MARGIN, MARGIN + i * GRID_SIZE), (MARGIN + (14 * GRID_SIZE), MARGIN + i * GRID_SIZE), 1)
pygame.draw.line(screen, LINE_COLOR, (MARGIN + i * GRID_SIZE, MARGIN), (MARGIN + i * GRID_SIZE, MARGIN + (14 * GRID_SIZE)), 1)
pygame.draw.circle(screen, BLACK, (MARGIN + 3 * GRID_SIZE, MARGIN + 3 * GRID_SIZE), 5)
pygame.draw.circle(screen, BLACK, (MARGIN + 11 * GRID_SIZE, MARGIN + 3 * GRID_SIZE), 5)
pygame.draw.circle(screen, BLACK, (MARGIN + 7 * GRID_SIZE, MARGIN + 7 * GRID_SIZE), 5)
pygame.draw.circle(screen, BLACK, (MARGIN + 3 * GRID_SIZE, MARGIN + 11 * GRID_SIZE), 5)
pygame.draw.circle(screen, BLACK, (MARGIN + 11 * GRID_SIZE, MARGIN + 11 * GRID_SIZE), 5)
pygame.display.update()
运行这段代码后,就可以看到一个黑白相间的棋盘被绘制出来,并在棋盘的中心和四个角上画了五个黑点。
4. 实现落子功能
接下来,我们需要实现落子功能。在五子棋游戏中,落子是玩家通过点击鼠标来实现的。我们可以使用pygame.event
函数来监听鼠标点击事件。代码如下:
# 落子
player = 0
GRID_COLOR = ((0, 0, 0), (255, 255, 255))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
x, y = event.pos
i = (x - MARGIN + GRID_SIZE // 2) // GRID_SIZE
j = (y - MARGIN + GRID_SIZE // 2) // GRID_SIZE
if 0 <= i < 15 and 0 <= j < 15:
if board[i][j] == -1:
board[i][j] = player
pygame.draw.circle(screen, GRID_COLOR[player], (MARGIN + i * GRID_SIZE, MARGIN + j * GRID_SIZE), GRID_SIZE // 2 - 2)
pygame.display.update()
player = 1 - player
这样,我们就可以通过鼠标点击事件来落子了。其中,player
变量用来记录当前玩家,其取值为0或1。board
变量用来记录棋盘上的落子情况,其初始化为-1,表示该位置没有落子。
5. 判断胜负
落子后,需要判断是否存在胜利者。在五子棋中,胜利的条件为:任意一方在横、竖、斜方向上连续相同颜色的棋子达到5颗。我们可以通过遍历棋盘来实现。代码如下:
# 判断胜负
def check_winner(board):
directions = [(0, 1), (1, 1), (1, 0), (1, -1)]
for i in range(15):
for j in range(15):
if board[i][j] == -1:
continue
for d in directions:
if i + 4 * d[0] >= 15 or j + 4 * d[1] >= 15 or j + 4 * d[1] < 0:
continue
if board[i][j] == board[i + d[0]][j + d[1]] == board[i + 2 * d[0]][j + 2 * d[1]] == board[i + 3 * d[0]][j + 3 * d[1]] == board[i + 4 * d[0]][j + 4 * d[1]]:
return board[i][j]
return -1
winner = check_winner(board)
if winner != -1:
print("Player %d Wins!" % winner)
这样,我们就可以在落子后判断游戏是否结束,并输出胜利信息。
示例说明
以下是两条示例说明:
示例一
在第3行第7列放置一枚黑子:
# 落子
player = 0
i, j = 2, 6
board[i][j] = player
pygame.draw.circle(screen, GRID_COLOR[player], (MARGIN + j * GRID_SIZE, MARGIN + i * GRID_SIZE), GRID_SIZE // 2 - 2)
pygame.display.update()
player = 1 - player
示例二
在第9行第3列放置一枚白子:
# 落子
player = 0
i, j = 8, 2
board[i][j] = player
pygame.draw.circle(screen, GRID_COLOR[player], (MARGIN + j * GRID_SIZE, MARGIN + i * GRID_SIZE), GRID_SIZE // 2 - 2)
pygame.display.update()
player = 1 - player
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python实现简单的五子棋游戏 - Python技术站