五子棋,古老而经典,是一种两人对抗的策略棋类游戏。想要体验制作游戏的乐趣吗?本文将详细指导你如何使用Python/ target=_blank class=infotextkey>Python语言和Pygame库,一步步打造自己的五子棋游戏!
首先,确保你已经安装了pygame库。接下来,我们会定义一些必要的常量,这些常量为我们的游戏设定了基础参数,例如棋子的颜色、棋盘的尺寸等:
EMPTY = 0
BLACK = 1
WHITE = 2
BLACK_COLOR = [0, 0, 0]
WHITE_COLOR = [255, 255, 255]
BOARD_SIZE = 15
GRID_SIZE = 40
RenjuBoard类是五子棋的心脏。它代表了棋盘,并且提供了一系列的方法来进行游戏操作:
特别值得注意的是,在draw方法中,我们不仅仅是画出基本的棋盘线条,还特别强调了天元和四个星位,为棋盘增添了传统的触感。
# 五子棋棋盘类
class RenjuBoard:
def __init__(self):
self._board = [[EMPTY] * BOARD_SIZE for _ in range(BOARD_SIZE)]
# 重置棋盘
def reset(self):
for row in range(BOARD_SIZE):
self._board[row] = [EMPTY] * BOARD_SIZE
# 下棋
def move(self, row, col, is_black):
if self._board[row][col] == EMPTY:
self._board[row][col] = BLACK if is_black else WHITE
return True
return False
# 绘制棋盘与棋子
def draw(self, screen):
# 画棋盘线
for h in range(1, BOARD_SIZE + 1):
pygame.draw.line(screen, BLACK_COLOR, [GRID_SIZE, h * GRID_SIZE], [BOARD_SIZE * GRID_SIZE, h * GRID_SIZE], 1)
pygame.draw.line(screen, BLACK_COLOR, [h * GRID_SIZE, GRID_SIZE], [h * GRID_SIZE, BOARD_SIZE * GRID_SIZE], 1)
# 画外框
pygame.draw.rect(screen, BLACK_COLOR, [GRID_SIZE - BORDER_WIDTH, GRID_SIZE - BORDER_WIDTH, (BOARD_SIZE + 1) * GRID_SIZE, (BOARD_SIZE + 1) * GRID_SIZE], BORDER_WIDTH)
# 画棋盘特殊点位
pygame.draw.circle(screen, BLACK_COLOR, [GRID_SIZE * 8, GRID_SIZE * 8], 5, 0) # 天元点
for x in [GRID_SIZE * 4, GRID_SIZE * 12]:
for y in [GRID_SIZE * 4, GRID_SIZE * 12]:
pygame.draw.circle(screen, BLACK_COLOR, [x, y], 3, 0)
# 画棋子
for row in range(BOARD_SIZE):
for col in range(BOARD_SIZE):
if self._board[row][col] != EMPTY:
color = BLACK_COLOR if self._board[row][col] == BLACK else WHITE_COLOR
pos = [GRID_SIZE * (col + 1), GRID_SIZE * (row + 1)]
pygame.draw.circle(screen, color, pos, 18, 0)
在五子棋中,任意五个连续的同色棋子意味着一方的胜利。因此,is_win方法是至关重要的。它通过扫描每一行、每一列和两个斜线方向,检查是否存在五个连续的同色棋子。
# 定义函数,传入当前棋盘上的棋子列表,输出结果,不管黑棋白棋胜,都是传回False,未出结果则为True
def is_win(board):
for n in range(15):
# 判断垂直方向胜利
flag = 0
# flag是一个标签,表示是否有连续以上五个相同颜色的棋子
for b in board._board:
if b[n] == 1:
flag += 1
if flag == 5:
print('黑棋胜')
return False
else:
# else表示此时没有连续相同的棋子,标签flag重置为0
flag = 0
flag = 0
for b in board._board:
if b[n] == 2:
flag += 1
if flag == 5:
print('白棋胜')
return False
else:
flag = 0
# 判断水平方向胜利
flag = 0
for b in board._board[n]:
if b == 1:
flag += 1
if flag == 5:
print('黑棋胜')
return False
else:
flag = 0
flag = 0
for b in board._board[n]:
if b == 2:
flag += 1
if flag == 5:
print('白棋胜')
return False
else:
flag = 0
# 判断正斜方向胜利
for x in range(4, 25):
flag = 0
for i,b in enumerate(board._board):
if 14 >= x - i >= 0 and b[x - i] == 1:
flag += 1
if flag == 5:
print('黑棋胜')
return False
else:
flag = 0
for x in range(4, 25):
flag = 0
for i,b in enumerate(board._board):
if 14 >= x - i >= 0 and b[x - i] == 2:
flag += 1
if flag == 5:
print('白棋胜')
return False
else:
flag = 0
#判断反斜方向胜利
for x in range(11, -11, -1):
flag = 0
for i,b in enumerate(board._board):
if 0 <= x + i <= 14 and b[x + i] == 1:
flag += 1
if flag == 5:
print('黑棋胜')
return False
else:
flag = 0
for x in range(11, -11, -1):
flag = 0
for i,b in enumerate(board._board):
if 0 <= x + i <= 14 and b[x + i] == 2:
flag += 1
if flag == 5:
print('白棋胜')
return False
else:
flag = 0
return True
五子棋的魅力在于玩家的每一次操作。在mAIn函数中,我们创建了一个游戏窗口,并初始化棋盘。主循环监听玩家的操作,每当玩家点击,就会更新棋盘并判断胜负。
现在,你已经有了一个基础的五子棋游戏,但这仅仅是开始。你可以考虑增加更多的功能: